index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { computed, inject, ref, unref } from 'vue'
  2. import type { InjectionKey, Ref } from 'vue'
  3. export const defaultNamespace = 'el'
  4. const statePrefix = 'is-'
  5. const _bem = (
  6. namespace: string,
  7. block: string,
  8. blockSuffix: string,
  9. element: string,
  10. modifier: string
  11. ) => {
  12. let cls = `${namespace}-${block}`
  13. if (blockSuffix) {
  14. cls += `-${blockSuffix}`
  15. }
  16. if (element) {
  17. cls += `__${element}`
  18. }
  19. if (modifier) {
  20. cls += `--${modifier}`
  21. }
  22. return cls
  23. }
  24. export const namespaceContextKey: InjectionKey<Ref<string | undefined>> =
  25. Symbol('localeContextKey')
  26. export const useGetDerivedNamespace = (namespaceOverrides?: Ref<string>) => {
  27. const derivedNamespace =
  28. namespaceOverrides || inject(namespaceContextKey, ref(defaultNamespace))
  29. const namespace = computed(() => {
  30. return unref(derivedNamespace) || defaultNamespace
  31. })
  32. return namespace
  33. }
  34. export const useNamespace = (
  35. block: string,
  36. namespaceOverrides?: Ref<string>
  37. ) => {
  38. const namespace = useGetDerivedNamespace(namespaceOverrides)
  39. const b = (blockSuffix = '') =>
  40. _bem(namespace.value, block, blockSuffix, '', '')
  41. const e = (element?: string) =>
  42. element ? _bem(namespace.value, block, '', element, '') : ''
  43. const m = (modifier?: string) =>
  44. modifier ? _bem(namespace.value, block, '', '', modifier) : ''
  45. const be = (blockSuffix?: string, element?: string) =>
  46. blockSuffix && element
  47. ? _bem(namespace.value, block, blockSuffix, element, '')
  48. : ''
  49. const em = (element?: string, modifier?: string) =>
  50. element && modifier
  51. ? _bem(namespace.value, block, '', element, modifier)
  52. : ''
  53. const bm = (blockSuffix?: string, modifier?: string) =>
  54. blockSuffix && modifier
  55. ? _bem(namespace.value, block, blockSuffix, '', modifier)
  56. : ''
  57. const bem = (blockSuffix?: string, element?: string, modifier?: string) =>
  58. blockSuffix && element && modifier
  59. ? _bem(namespace.value, block, blockSuffix, element, modifier)
  60. : ''
  61. const is: {
  62. (name: string, state: boolean | undefined): string
  63. (name: string): string
  64. } = (name: string, ...args: [boolean | undefined] | []) => {
  65. const state = args.length >= 1 ? args[0]! : true
  66. return name && state ? `${statePrefix}${name}` : ''
  67. }
  68. // for css var
  69. // --el-xxx: value;
  70. const cssVar = (object: Record<string, string>) => {
  71. const styles: Record<string, string> = {}
  72. for (const key in object) {
  73. if (object[key]) {
  74. styles[`--${namespace.value}-${key}`] = object[key]
  75. }
  76. }
  77. return styles
  78. }
  79. // with block
  80. const cssVarBlock = (object: Record<string, string>) => {
  81. const styles: Record<string, string> = {}
  82. for (const key in object) {
  83. if (object[key]) {
  84. styles[`--${namespace.value}-${block}-${key}`] = object[key]
  85. }
  86. }
  87. return styles
  88. }
  89. const cssVarName = (name: string) => `--${namespace.value}-${name}`
  90. const cssVarBlockName = (name: string) =>
  91. `--${namespace.value}-${block}-${name}`
  92. return {
  93. namespace,
  94. b,
  95. e,
  96. m,
  97. be,
  98. em,
  99. bm,
  100. bem,
  101. is,
  102. // css
  103. cssVar,
  104. cssVarName,
  105. cssVarBlock,
  106. cssVarBlockName,
  107. }
  108. }
  109. export type UseNamespaceReturn = ReturnType<typeof useNamespace>