Blame view

Yi.Vben5.Vue3/packages/@core/composables/src/use-namespace.ts 2.8 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  import { DEFAULT_NAMESPACE } from '@vben-core/shared/constants';
  
  /**
   * @see copy https://github.com/element-plus/element-plus/blob/dev/packages/hooks/use-namespace/index.ts
   */
  
  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;
  };
  
  const is: {
    (name: string): string;
    // eslint-disable-next-line @typescript-eslint/unified-signatures
    (name: string, state: boolean | undefined): string;
  } = (name: string, ...args: [] | [boolean | undefined]) => {
    const state = args.length > 0 ? args[0] : true;
    return name && state ? `${statePrefix}${name}` : '';
  };
  
  const useNamespace = (block: string) => {
    const namespace = DEFAULT_NAMESPACE;
    const b = (blockSuffix = '') => _bem(namespace, block, blockSuffix, '', '');
    const e = (element?: string) =>
      element ? _bem(namespace, block, '', element, '') : '';
    const m = (modifier?: string) =>
      modifier ? _bem(namespace, block, '', '', modifier) : '';
    const be = (blockSuffix?: string, element?: string) =>
      blockSuffix && element
        ? _bem(namespace, block, blockSuffix, element, '')
        : '';
    const em = (element?: string, modifier?: string) =>
      element && modifier ? _bem(namespace, block, '', element, modifier) : '';
    const bm = (blockSuffix?: string, modifier?: string) =>
      blockSuffix && modifier
        ? _bem(namespace, block, blockSuffix, '', modifier)
        : '';
    const bem = (blockSuffix?: string, element?: string, modifier?: string) =>
      blockSuffix && element && modifier
        ? _bem(namespace, block, blockSuffix, element, modifier)
        : '';
  
    // for css var
    // --el-xxx: value;
    const cssVar = (object: Record<string, string>) => {
      const styles: Record<string, string> = {};
      for (const key in object) {
        if (object[key]) {
          styles[`--${namespace}-${key}`] = object[key];
        }
      }
      return styles;
    };
    // with block
    const cssVarBlock = (object: Record<string, string>) => {
      const styles: Record<string, string> = {};
      for (const key in object) {
        if (object[key]) {
          styles[`--${namespace}-${block}-${key}`] = object[key];
        }
      }
      return styles;
    };
  
    const cssVarName = (name: string) => `--${namespace}-${name}`;
    const cssVarBlockName = (name: string) => `--${namespace}-${block}-${name}`;
  
    return {
      b,
      be,
      bem,
      bm,
      // css
      cssVar,
      cssVarBlock,
      cssVarBlockName,
      cssVarName,
      e,
      em,
      is,
      m,
      namespace,
    };
  };
  
  type UseNamespaceReturn = ReturnType<typeof useNamespace>;
  
  export type { UseNamespaceReturn };
  export { useNamespace };