feat(hooks): [use-namespace] add bm (#5475)

This commit is contained in:
三咲智子 2022-01-19 00:06:19 +08:00 committed by GitHub
parent 1a9a609444
commit df3be7c661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,53 +1,54 @@
import { unref, computed } from 'vue'
import curry from 'lodash/curry'
import { useGlobalConfig } from '../use-global-config'
const defaultNamespace = 'el'
const statePrefix = 'is-'
const curryBem = curry(
(
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
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 namespace = computed(
() => useGlobalConfig('namespace').value || defaultNamespace
)
const b = (blockSuffix = '') =>
curryBem(unref(namespace), block, blockSuffix, '', '')
_bem(unref(namespace), block, blockSuffix, '', '')
const e = (element?: string) =>
element ? curryBem(unref(namespace), block, '', element, '') : ''
element ? _bem(unref(namespace), block, '', element, '') : ''
const m = (modifier?: string) =>
modifier ? curryBem(unref(namespace), block, '', '')(modifier) : ''
modifier ? _bem(unref(namespace), block, '', '', modifier) : ''
const be = (blockSuffix?: string, element?: string) =>
blockSuffix && element
? curryBem(unref(namespace), block, blockSuffix, element, '')
? _bem(unref(namespace), block, blockSuffix, element, '')
: ''
const em = (element?: string, modifier?: string) =>
element && modifier
? curryBem(unref(namespace), block, '')(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
? curryBem(unref(namespace), block)(blockSuffix, element, modifier)
? _bem(unref(namespace), block, blockSuffix, element, modifier)
: ''
const is = (name: string, state = true) =>
state ? `${statePrefix}${name}` : ''
@ -58,6 +59,7 @@ export const useNamespace = (block: string) => {
m,
be,
em,
bm,
bem,
is,
}