From df5cc33c90ca9e31266d5d1bb32dcabe23297f6c Mon Sep 17 00:00:00 2001 From: bordon <1078464063@qq.com> Date: Fri, 1 Sep 2023 18:35:58 +0800 Subject: [PATCH 1/4] refactor: replace var with const --- .../__snapshots__/configprovider.spec.ts.snap | 8 ++- .../__tests__/configprovider.spec.ts | 7 +++ src/packages/__VUE/configprovider/common.ts | 55 ++++++++++++++++++ .../__VUE/configprovider/index.taro.vue | 55 ++---------------- src/packages/__VUE/configprovider/index.vue | 57 ++----------------- 5 files changed, 79 insertions(+), 103 deletions(-) create mode 100644 src/packages/__VUE/configprovider/common.ts diff --git a/src/packages/__VUE/configprovider/__tests__/__snapshots__/configprovider.spec.ts.snap b/src/packages/__VUE/configprovider/__tests__/__snapshots__/configprovider.spec.ts.snap index 9842cbf7dd..3c63022fd3 100644 --- a/src/packages/__VUE/configprovider/__tests__/__snapshots__/configprovider.spec.ts.snap +++ b/src/packages/__VUE/configprovider/__tests__/__snapshots__/configprovider.spec.ts.snap @@ -1,7 +1,13 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`ConfigProvider: props themeVars 1`] = ` -"
+"
+ +
" +`; + +exports[`ConfigProvider: props themeVars 2`] = ` +"
" `; diff --git a/src/packages/__VUE/configprovider/__tests__/configprovider.spec.ts b/src/packages/__VUE/configprovider/__tests__/configprovider.spec.ts index a21200bbfe..b57fa0baa0 100644 --- a/src/packages/__VUE/configprovider/__tests__/configprovider.spec.ts +++ b/src/packages/__VUE/configprovider/__tests__/configprovider.spec.ts @@ -27,4 +27,11 @@ test('ConfigProvider: props themeVars', async () => { } }); expect(wrapper.html()).toMatchSnapshot(); + + await wrapper.setProps({ + themeVars: { + primaryColor: '#ff' + } + }); + expect(wrapper.html()).toMatchSnapshot(); }); diff --git a/src/packages/__VUE/configprovider/common.ts b/src/packages/__VUE/configprovider/common.ts new file mode 100644 index 0000000000..c5145be18c --- /dev/null +++ b/src/packages/__VUE/configprovider/common.ts @@ -0,0 +1,55 @@ +export const colorRgb = (str: string) => { + if (!str) return; + let sColor = str.toLowerCase(); + //十六进制颜色值的正则表达式 + const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; + // 如果是16进制颜色 + if (reg.test(sColor)) { + if (sColor.length === 4) { + sColor = + '#' + + str + .slice(1) + .split('') + .map((char) => char + char) + .join(''); + } + //处理六位的颜色值 + const sColorChange = []; + for (let i = 1; i < 7; i += 2) { + sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2))); + } + return sColorChange.join(','); + } + return ''; +}; + +export const kebabCase = (str: string): string => { + str = str.replace(str.charAt(0), str.charAt(0).toLocaleLowerCase()); + return str.replace(/([a-z])([A-Z])/g, (_, p1, p2) => p1 + '-' + p2.toLowerCase()); +}; + +export const mapThemeVarsToCSSVars = (themeVars: Record) => { + if (!themeVars) return; + const cssVars: Record = {}; + const primaryColor = themeVars?.primaryColor; + // 为了处理一些组件的rgba透明颜色 + if (primaryColor) { + const primaryColorRgb = colorRgb(primaryColor); + cssVars[ + '--nut-address-region-tab-line' + ] = `linear-gradient(90deg, ${primaryColor} 0%, rgba(${primaryColorRgb}, 0.15) 100%)`; + cssVars[ + '--nut-tabs-horizontal-tab-line-color' + ] = `linear-gradient(90deg, ${primaryColor} 0%, rgba(${primaryColorRgb}, 0.15) 100%)`; + cssVars[ + '--nut-tabs-vertical-tab-line-color' + ] = `linear-gradient(180deg, ${primaryColor} 0%, rgba(${primaryColorRgb}, 0.15) 100%)`; + } + + Object.keys(themeVars).forEach((key) => { + cssVars[`--nut-${kebabCase(key)}`] = themeVars[key]; + }); + + return cssVars; +}; diff --git a/src/packages/__VUE/configprovider/index.taro.vue b/src/packages/__VUE/configprovider/index.taro.vue index 1db530b2b3..816f5f4420 100644 --- a/src/packages/__VUE/configprovider/index.taro.vue +++ b/src/packages/__VUE/configprovider/index.taro.vue @@ -1,63 +1,18 @@ diff --git a/src/packages/__VUE/configprovider/index.vue b/src/packages/__VUE/configprovider/index.vue index 41d70912ad..65317bee01 100644 --- a/src/packages/__VUE/configprovider/index.vue +++ b/src/packages/__VUE/configprovider/index.vue @@ -1,29 +1,8 @@ From dcb567a78afbe83769730a17e2aa2f05f39e381e Mon Sep 17 00:00:00 2001 From: bordon <1078464063@qq.com> Date: Mon, 4 Sep 2023 10:49:00 +0800 Subject: [PATCH 4/4] feat: demo using script setup --- src/packages/__VUE/configprovider/demo.vue | 162 +++++++++------------ 1 file changed, 71 insertions(+), 91 deletions(-) diff --git a/src/packages/__VUE/configprovider/demo.vue b/src/packages/__VUE/configprovider/demo.vue index e0171f9c7c..91cb36054b 100644 --- a/src/packages/__VUE/configprovider/demo.vue +++ b/src/packages/__VUE/configprovider/demo.vue @@ -68,11 +68,12 @@
- -