bem.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { prefixCls } from '@/settings/designSetting';
  2. type Mod = string | { [key: string]: any };
  3. type Mods = Mod | Mod[];
  4. export type BEM = ReturnType<typeof createBEM>;
  5. function genBem(name: string, mods?: Mods): string {
  6. if (!mods) {
  7. return '';
  8. }
  9. if (typeof mods === 'string') {
  10. return ` ${name}--${mods}`;
  11. }
  12. // ArrayConstructor.isArray(arg: any): arg is any[]
  13. if (Array.isArray(mods)) {
  14. return (mods as Mod[]).reduce<string>((ret, item) => ret + genBem(name, item), '');
  15. }
  16. return Object.keys(mods).reduce((ret, key) => ret + (mods[key] ? genBem(name, key) : ''), '');
  17. }
  18. /**
  19. * bem helper
  20. * b() // 'button'
  21. * b('text') // 'button__text'
  22. * b({ disabled }) // 'button button--disabled'
  23. * b('text', { disabled }) // 'button__text button__text--disabled'
  24. * b(['disabled', 'primary']) // 'button button--disabled button--primary'
  25. */
  26. export function buildBEM(name: string) {
  27. return (el?: Mods, mods?: Mods): Mods => {
  28. if (el && typeof el !== 'string') {
  29. mods = el;
  30. el = '';
  31. }
  32. el = el ? `${name}__${el}` : name;
  33. return `${el}${genBem(el, mods)}`;
  34. };
  35. }
  36. export function createBEM(name: string) {
  37. return [buildBEM(`${prefixCls}-${name}`)];
  38. }
  39. export function createNamespace(name: string) {
  40. const prefixedName = `${prefixCls}-${name}`;
  41. return [prefixedName, buildBEM(prefixedName)] as const;
  42. }