搭建vue3组件库(三): CSS架构之BEM

BEM 是由 Yandex 团队提出的一种 CSS 命名方法论,即 Block(块)、Element(元素)和 Modifier(修改器)的简称,是 OOCSS 方法论的一种实现模式,底层仍然是面向对象的思想。

  • BEM 规范下 classname 的命名格式为:
    • 所有实体的命名均使用小写字母,复合词使用连字符 “-” 连接。
    • Block 与 Element 之间使用双下画线 “__” 连接。
    • Mofifier 与 Block/Element 使用双连接符 “–” 连接。
    • modifier-name 和 modifier_value 之间使用单下画线 “_” 连接。

1. 通过 JS 生成 BEM 规范名称

在编写组件的时候如果通过手写 classname 的名称,那么需要经常写 - 、 __ 、 --,那么就会变得非常繁琐,BEM 命名规范是具有一定规律性的,所以可以通过 JavaScript 按照 BEM 命名规范进行动态生成。

1.1 初始化 hooks 目录

packages 目录下创建一个 hooks 目录,进入到 hooks 目录底下初始化一个 package.json 文件,修改内容如下:

{
  "name": "@vision-ui-vue/hooks",
  "version": "0.0.1",
  "license": "MIT",
  "main": "index.ts",
  "module": "index.ts",
  "unpkg": "index.js",
  "jsdelivr": "index.js",
  "types": "index.d.ts",
  "peerDependencies": {
    "vue": "^3.2.0"
  }
}

1.2 创建 BEM 命名空间函数

hooks 目录下再创建一个 use-namespace 目录用于创建 BEM 命名空间函数,再在 hooks 目录下创建一个 index.ts 文件用于模块入口文件。

// index.ts
import * from './use-namespace'
// use-namespace/index.ts
import { computed, unref } from 'vue'

// 默认的命名空间
export const defaultNamespace = 'v'
// 状态前缀
const statePrefix = 'is-'

/**
 * BEM 命名字符拼接函数
 * @param namespace 命名空间
 * @param block 块名
 * @param blockSuffix 块的后缀
 * @param element 元素名
 * @param modifier 修改器名
 * @returns 拼接后的BEM类名字符串
 */
const _bem = (
  namespace: string,
  block: string,
  blockSuffix: string,
  element: string,
  modifier: string
) => {
  // 默认是 Block
  let cls = `${namespace}-${block}`
  // 如果存在 Block 后缀,也就是 Block 里面还有 Block
  if (blockSuffix) {
    cls += `-${blockSuffix}`
  }
  // 如果存在元素
  if (element) {
    cls += `__${element}`
  }
  // 如果存在修改器
  if (modifier) {
    cls += `--${modifier}`
  }
  return cls
}

/**
 * 用于创建和管理BEM类名的工具函数
 * @param block 块名
 * @returns 返回一个对象,包含用于创建BEM类名的各种方法
 */
export const useNamespace = (block: string) => {
  // 基于Vue的computed创建动态命名空间
  const namespace = computed(() => defaultNamespace)
  // 创建块级类名 v-form
  const b = (blockSuffix = '') =>
    _bem(unref(namespace), block, blockSuffix, '', '')

  // 创建元素级类名 v-input__inner
  const e = (element?: string) =>
    element ? _bem(unref(namespace), block, '', element, '') : ''

  // 创建修改器类名 v-form--default
  const m = (modifier?: string) =>
    modifier ? _bem(unref(namespace), block, '', '', modifier) : ''

  // 创建带后缀的块元素类名 v-form-item
  const be = (blockSuffix?: string, element?: string) =>
    blockSuffix && element
      ? _bem(unref(namespace), block, blockSuffix, element, '')
      : ''

  // 创建元素的修改器类名 v-scrollbar__wrap--hidden-default
  const em = (element?: string, modifier?: string) =>
    element && modifier
      ? _bem(unref(namespace), block, '', element, modifier)
      : ''

  // 创建块后缀的修改器类名 v-form-item--default
  const bm = (blockSuffix?: string, modifier?: string) =>
    blockSuffix && modifier
      ? _bem(unref(namespace), block, blockSuffix, '', modifier)
      : ''

  // 创建块元素的修改器类名 v-form-item__content--xxx
  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}` : ''
  }

  return {
    namespace,
    b,
    e,
    m,
    be,
    em,
    bm,
    bem,
    is,
  }
}

1.3 通过 SCSS 生成 BEM 规范样式

packages/theme-chalk 目录下创建一个 src 目录,在 src 目录下创建一个 mixins 目录。在 mixins 目录下新建件:config.scssmixins.scssfunction.scss, config.scss 编写 BEM 的基础配置比如样式名前缀、元素、修饰符、状态前缀:

$namespace: 'v' !default; // 所有的组件以v开头,如 v-input
$common-separator: '-' !default; // 公共的连接符
$element-separator: '__' !default; // 元素以__分割,如 v-input__inner
$modifier-separator: '--' !default; // 修饰符以--分割,如 v-input--mini
$state-prefix: 'is-' !default; // 状态以is-开头,如 is-disabled
// 在 SCSS 中,使用 $+ 变量名:变量 来定义一个变量。在变量后加入 !default 表示默认值。给一个未通过 !default 声明赋值的变量赋值,此时,如果变量已经被赋值,不会再被重新赋值;但是如果变量还没有被赋值,则会被赋予新的值。

mixins.scss 编写 SCSS 的 @mixin 指令定义的 BEM 代码规范:

@use '../common/var' as *;

// 定义 Block
@mixin b($block) {
  $B: $namespace + $common-separator + $block !global;

  .#{$B} {
    @content;
  }
}

// 定义 Element
@mixin e($element) {
  $E: $element !global;
  $selector: &;
  $currentSelector: '';
  @each $unit in $element {
    $currentSelector: #{$currentSelector +
      '.' +
      $B +
      $element-separator +
      $unit +
      ','};
  }

  @if hitAllSpecialNestRule($selector) {
    @at-root {
      #{$selector} {
        #{$currentSelector} {
          @content;
        }
      }
    }
  } @else {
    @at-root {
      #{$currentSelector} {
        @content;
      }
    }
  }
}

// 定义修改器
@mixin m($modifier) {
  $selector: &;
  $currentSelector: '';
  @each $unit in $modifier {
    $currentSelector: #{$currentSelector +
      $selector +
      $modifier-separator +
      $unit +
      ','};
  }

  @at-root {
    #{$currentSelector} {
      @content;
    }
  }
}

// 定义动作状态
@mixin when($state) {
  @at-root {
    &.#{$state-prefix + $state} {
      @content;
    }
  }
}

function.scss 定义一些 SCSS 的 @function 指令定义的函数:

@use 'config';

// 该函数将选择器转化为字符串,并截取指定位置的字符
@function selectorToString($selector) {
  $selector: inspect(
    $selector
  ); // inspect(...) 表达式中的内容如果是正常会返回对应的内容,如果发生错误则会弹出一个错误提示。
  $selector: str-slice($selector, 2, -2); // str-slice 截取指定字符
  @return $selector;
}
// 判断父级选择器是否包含'--'
@function containsModifier($selector) {
  $selector: selectorToString($selector);

  @if str-index($selector, config.$modifier-separator) {
    // str-index 返回字符串的第一个索引
    @return true;
  } @else {
    @return false;
  }
}
// 判断父级选择器是否包含'.is-'
@function containWhenFlag($selector) {
  $selector: selectorToString($selector);

  @if str-index($selector, '.' + config.$state-prefix) {
    @return true;
  } @else {
    @return false;
  }
}
// 判断父级是否包含 ':' (用于判断伪类和伪元素)
@function containPseudoClass($selector) {
  $selector: selectorToString($selector);

  @if str-index($selector, ':') {
    @return true;
  } @else {
    @return false;
  }
}
// 判断父级选择器,是否包含`--` `.is-`  `:`这三种字符
@function hitAllSpecialNestRule($selector) {
  @return containsModifier($selector) or containWhenFlag($selector) or
    containPseudoClass($selector);
}

2. 测试 BEM 规范

  • 在根目录执行 pnpm install sass -D -w
  • 接着执行 pnpm install @vision-ui-vue/hooks -D -w 把 hooks 引入到项目中
  • packages/components 下新建 button 目录,目录结构为
├── packages
│   ├── components
│   │   ├── button
│   │   │   ├── src
│   │   │   │   └── button.vue
│   │   │   └── index.ts
│   │   └── package.json

index.ts 内容:

import Button from './src/button.vue'
export default Button

button.vue 内容:

<template>
  <button :clsaa="bem.b()">测试按钮</button>
</template>

<script lang="ts" setup>
import { useNamespace } from '@vision-ui-vue/hooks'
const bem = useNamespace('button')
</script>
  • 进入 play 项目,修改 App.vue 文件,引入 button 组件:
<template>
  <div>
    <v-button></v-button>
  </div>
</template>

<script setup lang="ts">
import VButton from '@vision-ui-vue/components/button'
</script>

  • 运行 play 项目,查看效果
    打开控制台,可以看到按钮的class为 v-button
    在这里插入图片描述

相关推荐

  1. vue3组件(一):Monorepo架构

    2024-04-29 07:30:04       31 阅读
  2. vue3组件(二): 代码规范自动化

    2024-04-29 07:30:04       40 阅读
  3. vue3组件(一):Monorepo项目

    2024-04-29 07:30:04       37 阅读
  4. 开源Vue3组件

    2024-04-29 07:30:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-29 07:30:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-29 07:30:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-29 07:30:04       82 阅读
  4. Python语言-面向对象

    2024-04-29 07:30:04       91 阅读

热门阅读

  1. 华为OD机试-螺旋数字矩阵

    2024-04-29 07:30:04       38 阅读
  2. Hive EXPLAIN 执行计划解析

    2024-04-29 07:30:04       34 阅读
  3. Hive判空函数 COALESCE 和 NVL 使用示例

    2024-04-29 07:30:04       33 阅读
  4. 在 Ubuntu 下使用 clash-for-linux-backup

    2024-04-29 07:30:04       32 阅读
  5. 如何使用 MySQL Workbench 远程连接到 MySQL 服务器

    2024-04-29 07:30:04       31 阅读
  6. DSP开发实战教程--#pragma CODE_SECTION使用技巧

    2024-04-29 07:30:04       34 阅读
  7. 代谢组数据分析五:溯源分析

    2024-04-29 07:30:04       33 阅读
  8. GitHub 异常——无法连接22端口:Connection timed out

    2024-04-29 07:30:04       32 阅读
  9. 如何在小程序中添加图片和视频

    2024-04-29 07:30:04       46 阅读