vue3-bem

Simple implementation of BEM in Vue 3.x
11,555 6
Install
npm install vue3-bem
Latest Version:1.0.8
License:ISC
Last Updated:May 19, 2022
Links: GitHub  ·  npm
Maintainer: kesion

vue3-bem

Simple implementation of BEM in Vue 3.x, helps you use the BEM specification more concisely in vue3.

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 {
    }
  }
}

Api

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.

// *.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 {
        }
    }
}

Related Packages

vue-bem-cn

BEM class name generator for Vue.JS

8,290 125
@vesperjs/vue

This is Vesper for Vue.js.

8,552 1
@wzpo/md-ui

Another Momentum UI framework for Vue.js applications

2,054
vuenilla

Unstyled Vue.js component library.

1,552 2

Version History

Version Released License
1.0.8 ISC
1.0.7 ISC
1.0.6 ISC
1.0.5 ISC
1.0.4 ISC