-
Notifications
You must be signed in to change notification settings - Fork 840
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(configprovider): move common logic to common file (#2540)
- Loading branch information
Showing
6 changed files
with
171 additions
and
232 deletions.
There are no files selected for viewing
8 changes: 7 additions & 1 deletion
8
src/packages/__VUE/configprovider/__tests__/__snapshots__/configprovider.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`ConfigProvider: props themeVars 1`] = ` | ||
"<div class=\\"nut-theme-\\" style=\\"--nut-address-region-tab-line: linear-gradient(90deg, #fff 0%, rgba(255,255,255,0.15) 100%) ; --nut-tabs-horizontal-tab-line-color: linear-gradient(90deg, #fff 0%, rgba(255,255,255,0.15)100%); --nut-tabs-vertical-tab-line-color: linear-gradient(180deg, #fff 0%, rgba(255,255,255,0.15) 100%) ; --nut-primary-color: #fff; --nut-title-color: blue; --nut-help-color: #f5f5f5;\\"><svg class=\\"nut-icon nut-icon-close\\" xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 1026 1024\\" role=\\"presentation\\"> | ||
"<div class=\\"nut-theme-\\" style=\\"--nut-address-region-tab-line: linear-gradient(90deg, #fff 0%, rgba(255,255,255, 0.15) 100%); --nut-tabs-horizontal-tab-line-color: linear-gradient(90deg, #fff 0%, rgba(255,255,255, 0.15) 100%); --nut-tabs-vertical-tab-line-color: linear-gradient(180deg, #fff 0%, rgba(255,255,255, 0.15) 100%); --nut-primary-color: #fff; --nut-title-color: blue; --nut-help-color: #f5f5f5;\\"><svg class=\\"nut-icon nut-icon-close\\" xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 1026 1024\\" role=\\"presentation\\"> | ||
<path d=\\"M981.577 1024c-11.703 0-23.406-2.926-32.183-11.703L13.166 76.07c-14.629-17.555-14.629-46.812 0-64.366 17.554-14.629 46.811-14.629 64.365 0L1013.76 947.93c17.554 17.555 17.554 43.886 0 61.44-8.777 11.703-20.48 14.629-32.183 14.629zm-936.228 0c-11.703 0-23.406-2.926-32.183-11.703-17.555-17.554-17.555-43.886 0-61.44L949.394 14.63c17.555-17.555 43.886-17.555 61.44 0 17.555 17.554 17.555 43.885 0 61.44L74.606 1012.297C68.754 1021.074 57.05 1024 45.349 1024z\\" fill=\\"currentColor\\" fill-opacity=\\"0.9\\"></path> | ||
</svg></div>" | ||
`; | ||
exports[`ConfigProvider: props themeVars 2`] = ` | ||
"<div class=\\"nut-theme-\\" style=\\"--nut-address-region-tab-line: linear-gradient(90deg, #ff 0%, rgba(, 0.15) 100%); --nut-tabs-horizontal-tab-line-color: linear-gradient(90deg, #ff 0%, rgba(, 0.15) 100%); --nut-tabs-vertical-tab-line-color: linear-gradient(180deg, #ff 0%, rgba(, 0.15) 100%); --nut-primary-color: #ff;\\"><svg class=\\"nut-icon nut-icon-close\\" xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 1026 1024\\" role=\\"presentation\\"> | ||
<path d=\\"M981.577 1024c-11.703 0-23.406-2.926-32.183-11.703L13.166 76.07c-14.629-17.555-14.629-46.812 0-64.366 17.554-14.629 46.811-14.629 64.365 0L1013.76 947.93c17.554 17.555 17.554 43.886 0 61.44-8.777 11.703-20.48 14.629-32.183 14.629zm-936.228 0c-11.703 0-23.406-2.926-32.183-11.703-17.555-17.554-17.555-43.886 0-61.44L949.394 14.63c17.555-17.555 43.886-17.555 61.44 0 17.555 17.554 17.555 43.885 0 61.44L74.606 1012.297C68.754 1021.074 57.05 1024 45.349 1024z\\" fill=\\"currentColor\\" fill-opacity=\\"0.9\\"></path> | ||
</svg></div>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { h, computed } from 'vue'; | ||
|
||
export const component = (tag: string) => { | ||
return { | ||
props: { | ||
theme: { type: String, default: '' }, | ||
themeVars: { type: Object, default: {} }, | ||
tag: { type: String, default: tag } | ||
}, | ||
|
||
setup(props: any, { slots }: any) { | ||
const hexToRgb = (color: string) => { | ||
//十六进制颜色值的正则表达式 | ||
const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; | ||
if (!reg.test(color)) return ''; | ||
color = color.toLowerCase(); | ||
// 如果是16进制颜色 | ||
if (color.length === 4) { | ||
color = | ||
'#' + | ||
color | ||
.slice(1) | ||
.split('') | ||
.map((char) => char + char) | ||
.join(''); | ||
} | ||
//处理六位的颜色值 | ||
const colors = []; | ||
for (let i = 1; i < 7; i += 2) { | ||
colors.push(parseInt('0x' + color.slice(i, i + 2))); | ||
} | ||
return colors.join(','); | ||
}; | ||
|
||
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()); | ||
}; | ||
|
||
const mapThemeVarsToCSSVars = (themeVars: Record<string, string>) => { | ||
if (!themeVars) return; | ||
const cssVars: Record<string, string> = {}; | ||
const primaryColor = themeVars?.primaryColor; | ||
// 为了处理一些组件的rgba透明颜色 | ||
if (primaryColor) { | ||
const primaryColorRgb = hexToRgb(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; | ||
}; | ||
|
||
const themeStyle = computed(() => mapThemeVarsToCSSVars(props.themeVars)); | ||
const defaultSlots = slots.default?.(); | ||
|
||
return () => { | ||
return h( | ||
props.tag, | ||
{ | ||
class: `nut-theme-${props.theme}`, | ||
style: themeStyle.value | ||
}, | ||
defaultSlots | ||
); | ||
}; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,8 @@ | ||
<script lang="ts"> | ||
import { createComponent } from '@/packages/utils/create'; | ||
import { h } from 'vue'; | ||
import { component } from './common'; | ||
const { create } = createComponent('config-provider'); | ||
export default create({ | ||
props: { | ||
theme: { type: String, default: '' }, | ||
themeVars: { type: Object, default: {} }, | ||
tag: { type: String, default: 'view' } | ||
}, | ||
setup(props: any, { slots }: any) { | ||
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()); | ||
}; | ||
const colorRgb = (str: string) => { | ||
if (!str) return; | ||
var sColor = str.toLowerCase(); | ||
//十六进制颜色值的正则表达式 | ||
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; | ||
// 如果是16进制颜色 | ||
if (sColor && reg.test(sColor)) { | ||
if (sColor.length === 4) { | ||
var sColorNew = '#'; | ||
for (let i = 1; i < 4; i += 1) { | ||
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1)); | ||
} | ||
sColor = sColorNew; | ||
} | ||
//处理六位的颜色值 | ||
var sColorChange = []; | ||
for (let i = 1; i < 7; i += 2) { | ||
sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2))); | ||
} | ||
return sColorChange.join(','); | ||
} | ||
return null; | ||
}; | ||
const mapThemeVarsToCSSVars = (themeVars: Record<string, string>) => { | ||
if (!themeVars) return; | ||
const cssVars: Record<string, string> = {}; | ||
const primaryColor = props?.themeVars?.primaryColor; | ||
// 为了处理一些组件的rgba透明颜色 | ||
if (primaryColor) { | ||
cssVars[`--nut-address-region-tab-line`] = `linear-gradient(90deg, ${primaryColor} 0%, rgba(${colorRgb( | ||
primaryColor | ||
)},0.15) 100%) `; | ||
cssVars[`--nut-tabs-horizontal-tab-line-color`] = `linear-gradient(90deg, ${primaryColor} 0%, rgba(${colorRgb( | ||
primaryColor | ||
)},0.15)100%)`; | ||
cssVars[`--nut-tabs-vertical-tab-line-color`] = `linear-gradient(180deg, ${primaryColor} 0%, rgba(${colorRgb( | ||
primaryColor | ||
)},0.15) 100%) `; | ||
} | ||
Object.keys(themeVars).forEach((key) => { | ||
cssVars[`--nut-${kebabCase(key)}`] = themeVars[key]; | ||
}); | ||
return cssVars; | ||
}; | ||
return () => { | ||
const defaultSlots = slots.default?.(); | ||
return h( | ||
props.tag, | ||
{ | ||
class: `nut-theme-${props.theme}`, | ||
style: mapThemeVarsToCSSVars(props.themeVars) | ||
}, | ||
defaultSlots | ||
); | ||
}; | ||
} | ||
}); | ||
export default create(component('view')); | ||
</script> |
Oops, something went wrong.