wot-design-uni
Version:
一个基于Vue3+TS开发的uni-app组件库,提供70+高质量组件,支持暗黑模式、国际化和自定义主题。
32 lines (25 loc) • 1.06 kB
text/typescript
import { computed, provide, unref, type Ref } from 'vue'
import { type ConfigProviderThemeVars } from '../wd-config-provider/types'
import { objToStyle } from '../common/util'
export const USE_CONFIG_PROVIDER_KEY = '__CONFIG_PROVIDER__'
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<string, string>) => {
if (!themeVars) return
const cssVars: Record<string, string> = {}
Object.keys(themeVars).forEach((key) => {
cssVars[`--wot-${kebabCase(key)}`] = themeVars[key]
})
return cssVars
}
export function useConfigProvider({ themeVars }: { themeVars: ConfigProviderThemeVars | Ref<ConfigProviderThemeVars> }) {
const themeStyle = computed(() => {
const styleObj = mapThemeVarsToCSSVars(unref(themeVars))
return styleObj ? `${objToStyle(styleObj)}` : ''
})
provide(USE_CONFIG_PROVIDER_KEY, {
themeStyle
})
}