

How to elegantly use BEM in vue3
source link: https://dev.to/kesion/how-to-elegantly-use-bem-in-vue3-91k
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In team development, we need to formulate css naming conventions, and the BEM
specification is one of the css naming conventions. Our project was developed with vue3
, and I didn't find the BEM framework that I wanted on the Internet, so I wrote one myself, then I will introduce vue3-bem
to you.
Installation
npm i vue3-bem
Using
vue3-bem is also very easy to use. As shown below.
// .vue
import useBem from "vue3-bem";
const bem = useBem("block");
<div :class="bem('elem', 'selected')"></div>
.block {
&__elem {
&--selected {
}
}
}
Use useBem
to set the block.
Use bem
to configure elements and modfiers to return classes.
type BemFunction = function (
e?: string | null,
m?: string | string[] | { [key in string]: boolean }
) => string[];
function useBem(block: string) => BemFunction;
bem: BemFunction
Tools
If you think it's too much trouble to write import for each component, you can use the plugin vite-plugin-vue3-bem
so you don't need to write import vue3-bem
.
Use vite-plugin-vue3-bem can help you remove import.
// .vue
<template>
<div :class="bem('elem', 'selected')"></div>
</template>
<script lang="ts" bem-block="tip">
// do some thing
</script>
<style lang="less">
.tip {
&__elem {
&--selected {
}
}
}
</style>
// vite.config.js
import ViteVue3Bem from "vite-plugin-vue3-bem";
plugins:[
ViteVue3Bem()
]
Type Declaration
Type declaration is required in typescript to prevent error reporting.
// env.d.ts
import "vue3-bem";
Example
Custom block name
<div class="tip">
<div :class="bem("wrap")"></div>
</div>
<script setup>
import useBem from "./useBem";
const bem = useBem('tip');
</script>
.tip {
&__wrap {
}
}
Inline modfiers
<div :class="bem('container')">
<div :class="bem("header")"></div>
<div :class="bem('item', ["selected", "highlighted"])"></div>
</div>
<script setup>
import useBem from "./useBem";
const bem = useBem('tip');
</script>
.tip {
&__header {
}
&__item {
&--selected {
}
&--highlighted {
}
}
}
Conditional modfiers
<div :class="bem('container')">
<div :class="bem("header")"></div>
<div :class="bem('item', {"selected": true, "highlighted": highlighted})"></div>
</div>
<script>
import useBem from "./useBem";
const bem = useBem('tip');
const highlighted = ref(false);
</script>
.tip {
&__header {
}
&__item {
&--selected {
}
&--highlighted {
}
}
}
Give a star if you think it will help you.
github: vue3-bem
Recommend
-
60
首先BEM是什么意思? BEM的意思就是块(block)、元素(element)、修饰符(modifier),是由Yandex团队提出的一种前端命名方法论,是一种 CSS命名规范 我们来看一个例子zfbui-tabbar__item 从这个名字我们能够解读出
-
15
BEM modifiers: multiple classes vs @extendThursday, July 24th 2014TL;DR multiple classes is ok, but restrict yourself to a single modifier class and a single state class or state data a...
-
14
Combining BEM and SMACSS June 24, 2016 Code architecture is super important when you want to have clean, readable, and organized code. For small, personal projects, you might be able to get away with jus...
-
21
FakerAndroid (FakerAndroid.jar or FakerAndroid-AS) FakerAndroid (中文文档) A tool transl...
-
8
什么是CSS中的BEM约定?BEM代表 Block-Element-Modifier。这种命名约定非常明确,并增加了前端代码的可读性。它避免了对 HTML 元素命名不明确的情况。该约定由三部分组成:块、元素和修饰符。为了理解这一点,我们需要一个例子。为此,让我们举一...
-
30
Elegantly Design Your Web Application with the Tailwind CSS ThemeThemes are an essential part of an application. They help us design our apps with colors, styles, icons, and more.Right now, the Tailwind C...
-
11
Create an Org Chart to Elegantly Visualize Hierarchical Data in Blazor WebAssemblyAn organizational chart (also known as “org chart”) is a graphic representation of the internal structure of an organization or a...
-
6
-
3
Elegantly Visualize Data with Interactive Features in .NET MAUI ChartsCharts are the perfect tools for visualizing a huge volume of data in a limited space. They help us easily understand trends in data through...
-
7
Generating Random Numbers Elegantly in Swift // Written by Jordan Morgan ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK