use-namespace.ts 2.8 KB

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