vue2、vue3中实现BEM命名规范

182 阅读1分钟

vue2中实现BEM命名规范 用来生成 BEM (Block Element Modifier)

const defaultNameSpace = "sc";
const statePrefix = "is-";
/**
 * 用来生成 BEM (Block Element Modifier) 类名
 *
 * @param {string} namespace - 类名的命名空间
 * @param {string} block - 区块名
 * @param {string} blockSuffix - 区块的可选后缀
 * @param {function} element - 创建元素大小检测器的函数
 * @param {string} modifier - 区块或元素的可选修饰符
 * @returns {string} 所生成的 BEM 类名
 */
function _bem (namespace, block, blockSuffix, element, modifier) {
  let cls = `${namespace}-${block}`;
  if (blockSuffix) {
    cls += `-${blockSuffix}`;
  }
  if (element) {
    cls += `__${element}`;
  }
  if (modifier) {
    cls += `--${modifier}`;
  }
  return cls;
};

export class NameSpace {
  constructor(block) {
    this.block = block;
    this.namespace = defaultNameSpace;
  }
  b (blockSuffix = "") {
    return _bem(this.namespace, this.block, blockSuffix, "", "");
  }
  e (element) {
    return _bem(this.namespace, this.block, "", element, "");
  }
  m (modifier) {
    return _bem(this.namespace, this.block, "", "", modifier);
  }
  be (blockSuffix = "", element = "") {
    return blockSuffix && element ? _bem(this.namespace, this.block, blockSuffix, element, "") : ""
  }
  em (element = "", modifier = "") {
    return element && modifier ? _bem(this.namespace, this.block, "", element, modifier) : ""
  }
  bm (blurSuffix = "", modifier = "") {
    return blurSuffix && modifier ? _bem(this.namespace, this.block, blurSuffix, "", modifier) : ""
  }
  bem (blockSuffix = "", element = "", modifier = "") {
    return blockSuffix && element && modifier ? _bem(this.namespace, this.block, blockSuffix, element, modifier) : ""
  }
  is (name, ...args) {
    const state = args.length >= 1 ? args[0] : true;
    return name && state ? `${statePrefix}${name}` : "";
  }
}

vue3中实现BEM命名规范

import { unref, computed } from "vue";
export const defaultNamespace = "sc";
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;
};
export const useNamespace = (block: string) => {
  const namespace = computed(() => defaultNamespace);
  const b = (blockSuffix = "") =>
    _bem(unref(namespace), block, blockSuffix, "", "");
  const e = (element?: string) =>
    element ? _bem(unref(namespace), block, "", element, "") : "";
  const m = (modifier?: string) =>
    modifier ? _bem(unref(namespace), block, "", "", modifier) : "";
  const be = (blockSuffix?: string, element?: string) =>
    blockSuffix && element
      ? _bem(unref(namespace), block, blockSuffix, element, "")
      : "";
  const em = (element?: string, modifier?: string) =>
    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
      ? _bem(unref(namespace), block, blockSuffix, element, modifier)
      : "";
  const is: {
    (name: string, state: boolean | undefined): string;
    (name: string): string;
  } = (name: string, ...args: [boolean | undefined] | []) => {
    const state = args.length >= 1 ? args[0]! : true;
    return name && state ? `${statePrefix}${name}` : "";
  };

  const cssVar = (object: Record<string, string>) => {
    const styles: Record<string, string> = {};
    for (const key in object) {
      if (object[key]) {
        styles[`--${namespace.value}-${key}`] = object[key];
      }
    }
    return styles;
  };

  const cssVarBlock = (object: Record<string, string>) => {
    const styles: Record<string, string> = {};
    for (const key in object) {
      if (object[key]) {
        styles[`--${namespace.value}-${block}-${key}`] = object[key];
      }
    }
    return styles;
  };

  const cssVarName = (name: string) => `--${namespace.value}-${name}`;
  const cssVarBlockName = (name: string) =>
    `--${namespace.value}-${block}-${name}`;

  return {
    namespace,
    b,
    e,
    m,
    be,
    em,
    bm,
    bem,
    is,
    cssVar,
    cssVarName,
    cssVarBlock,
    cssVarBlockName,
  };
};