element-plus/packages/hooks/use-namespace/index.ts
云游君 3d1b3d4a58
refactor(theme-chalk): scss css var namespace (#7344)
* refactor(theme-chalk): scss css var namespace

* fix(theme-chalk): compile getCssVar

* fix(theme-chalk): more --el namespace replace & getCssVarWithDefault

* fix(theme-chalk): use cssVarBlockName & cssVar rewrite button css vars

* fix(theme-chalk): replace empty svg fill color with ns

* chore: use cssVarName for --el-color-white

* chore: fix namespace reactive

* chore: use defaultNamespace
2022-04-26 17:19:09 +08:00

101 lines
2.7 KiB
TypeScript

import { computed, unref } from 'vue'
import { useGlobalConfig } from '../use-global-config'
export const defaultNamespace = 'el'
const statePrefix = 'is-'
const _bem = (
namespace: string,
block: string,
blockSuffix: string,
element: string,
modifier: string
) => {
let cls = `${namespace}-${block}`
if (blockSuffix) {
cls += `-${blockSuffix}`
}
if (element) {
cls += `__${element}`
}
if (modifier) {
cls += `--${modifier}`
}
return cls
}
export const useNamespace = (block: string) => {
const globalConfig = useGlobalConfig('namespace')
const namespace = computed(() => globalConfig.value || defaultNamespace)
const b = (blockSuffix = '') =>
_bem(unref(namespace), block, blockSuffix, '', '')
const e = (element?: string) =>
element ? _bem(unref(namespace), block, '', element, '') : ''
const m = (modifier?: string) =>
modifier ? _bem(unref(namespace), block, '', '', modifier) : ''
const be = (blockSuffix?: string, element?: string) =>
blockSuffix && element
? _bem(unref(namespace), block, blockSuffix, element, '')
: ''
const em = (element?: string, modifier?: string) =>
element && modifier
? _bem(unref(namespace), block, '', element, modifier)
: ''
const bm = (blockSuffix?: string, modifier?: string) =>
blockSuffix && modifier
? _bem(unref(namespace), block, blockSuffix, '', modifier)
: ''
const bem = (blockSuffix?: string, element?: string, modifier?: string) =>
blockSuffix && element && modifier
? _bem(unref(namespace), block, blockSuffix, element, modifier)
: ''
const is: {
(name: string, state: boolean | undefined): string
(name: string): string
} = (name: string, ...args: [boolean | undefined] | []) => {
const state = args.length >= 1 ? args[0]! : true
return name && state ? `${statePrefix}${name}` : ''
}
// for css var
// --el-xxx: value;
const cssVar = (object: Record<string, string>) => {
const styles: Record<string, string> = {}
for (const key in object) {
styles[`--${namespace.value}-${key}`] = object[key]
}
return styles
}
// with block
const cssVarBlock = (object: Record<string, string>) => {
const styles: Record<string, string> = {}
for (const key in object) {
styles[`--${namespace.value}-${block}-${key}`] = object[key]
}
return styles
}
const cssVarName = (name: string) => `--${namespace.value}-${name}`
const cssVarBlockName = (name: string) =>
`--${namespace.value}-${block}-${name}`
return {
namespace,
b,
e,
m,
be,
em,
bm,
bem,
is,
// css
cssVar,
cssVarName,
cssVarBlock,
cssVarBlockName,
}
}
export type UseNamespaceReturn = ReturnType<typeof useNamespace>