2022-03-25 15:35:56 +08:00
|
|
|
import { computed, unref } from 'vue'
|
2022-01-15 18:12:48 +08:00
|
|
|
import { useGlobalConfig } from '../use-global-config'
|
|
|
|
|
2022-01-18 21:04:56 +08:00
|
|
|
const defaultNamespace = 'el'
|
|
|
|
const statePrefix = 'is-'
|
|
|
|
|
2022-01-19 00:06:19 +08:00
|
|
|
const _bem = (
|
|
|
|
namespace: string,
|
|
|
|
block: string,
|
|
|
|
blockSuffix: string,
|
|
|
|
element: string,
|
|
|
|
modifier: string
|
|
|
|
) => {
|
|
|
|
let cls = `${namespace}-${block}`
|
|
|
|
if (blockSuffix) {
|
|
|
|
cls += `-${blockSuffix}`
|
2022-01-15 18:12:48 +08:00
|
|
|
}
|
2022-01-19 00:06:19 +08:00
|
|
|
if (element) {
|
|
|
|
cls += `__${element}`
|
|
|
|
}
|
|
|
|
if (modifier) {
|
|
|
|
cls += `--${modifier}`
|
|
|
|
}
|
|
|
|
return cls
|
|
|
|
}
|
2022-01-15 18:12:48 +08:00
|
|
|
|
|
|
|
export const useNamespace = (block: string) => {
|
2022-02-09 00:35:29 +08:00
|
|
|
const globalConfig = useGlobalConfig('namespace')
|
|
|
|
const namespace = computed(() => globalConfig.value || defaultNamespace)
|
2022-01-15 18:12:48 +08:00
|
|
|
const b = (blockSuffix = '') =>
|
2022-01-19 00:06:19 +08:00
|
|
|
_bem(unref(namespace), block, blockSuffix, '', '')
|
2022-01-15 18:12:48 +08:00
|
|
|
const e = (element?: string) =>
|
2022-01-19 00:06:19 +08:00
|
|
|
element ? _bem(unref(namespace), block, '', element, '') : ''
|
2022-01-15 18:12:48 +08:00
|
|
|
const m = (modifier?: string) =>
|
2022-01-19 00:06:19 +08:00
|
|
|
modifier ? _bem(unref(namespace), block, '', '', modifier) : ''
|
2022-01-15 18:12:48 +08:00
|
|
|
const be = (blockSuffix?: string, element?: string) =>
|
|
|
|
blockSuffix && element
|
2022-01-19 00:06:19 +08:00
|
|
|
? _bem(unref(namespace), block, blockSuffix, element, '')
|
2022-01-15 18:12:48 +08:00
|
|
|
: ''
|
|
|
|
const em = (element?: string, modifier?: string) =>
|
|
|
|
element && modifier
|
2022-01-19 00:06:19 +08:00
|
|
|
? _bem(unref(namespace), block, '', element, modifier)
|
|
|
|
: ''
|
|
|
|
const bm = (blockSuffix?: string, modifier?: string) =>
|
|
|
|
blockSuffix && modifier
|
|
|
|
? _bem(unref(namespace), block, blockSuffix, '', modifier)
|
2022-01-15 18:12:48 +08:00
|
|
|
: ''
|
|
|
|
const bem = (blockSuffix?: string, element?: string, modifier?: string) =>
|
|
|
|
blockSuffix && element && modifier
|
2022-01-19 00:06:19 +08:00
|
|
|
? _bem(unref(namespace), block, blockSuffix, element, modifier)
|
2022-01-15 18:12:48 +08:00
|
|
|
: ''
|
2022-02-11 09:09:21 +08:00
|
|
|
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}` : ''
|
|
|
|
}
|
2022-01-15 18:12:48 +08:00
|
|
|
return {
|
|
|
|
namespace,
|
|
|
|
b,
|
|
|
|
e,
|
|
|
|
m,
|
|
|
|
be,
|
|
|
|
em,
|
2022-01-19 00:06:19 +08:00
|
|
|
bm,
|
2022-01-15 18:12:48 +08:00
|
|
|
bem,
|
2022-01-18 21:04:56 +08:00
|
|
|
is,
|
2022-01-15 18:12:48 +08:00
|
|
|
}
|
|
|
|
}
|