@cssninja/bulma-css-vars

中文文档
0.9.3发布于 2025/04/02MITTypeScriptESM + CJS近 30 天下载 2913

Bulma with CSS variables

原始使用文档
文档目录6 节
  1. Usage
  2. Caveats
  3. What is the difficulty? Why this setup?
  4. The solution - how it works
  5. Direct Web Setup
  6. New Features

Bulma CSS Vars

Bulma CSS Vars extends Bulma to use CSS variables, that can be set to arbitrary values at runtime on the website and installs fallbacks for browsers without CSS variables capabilities.

version

This is an extension and a kind of "sass-pre-post-processor" that tries to be as least intrusive as possible to Bulma, while making arbitrary color changes in the bulma color schemes automated, as easy as possible.

There is quite some setup and configuration to be done, but once it is setup, it works like a charm. Read the section What is the difficulty, to learn why this setup is required.

Usage

npm i -S bulma bulma-css-vars
npm i -D sass
node ./node_modules/.bin/bulma-css-vars --init

To use this package, you have to use the dart implementation of sass, the sass package, version 1.23 or higher. If you use webpack and the sass-loader, you have to make sure you do not have node-sass installed, or configure options: { implementation: require('sass') } for the sass-loader.

// bulma-css-vars.config.js
import { hsl, rgb } from 'bulma-css-vars'

// color names have to match bulma's $variable-name, without '$'
// values will be used for initial colors and fallback
const appColors = {
  black: hsl(0, 0, 4),
  'scheme-main': rgb(200, 105, 84),
  red: { r: 255, b: 0, g: 0}
  primary: '#663423',
  blue: 'blue',
}
// reuse variable colors
appColors['text'] = appColors['primary']

export default {
  sassEntryFile: './src/style/main-sass-file.sass',
  jsOutputFile: './src/bulma-generated/bulma-colors.js',
  sassOutputFile: './src/bulma-generated/generated-vars.sass',
  cssFallbackOutputFile: './src/bulma-generated/generated-fallbacks.css',
  colorDefs: appColors,
  globalWebVar: false,
  transition: '0.5s ease',
}

You need to configure bulma-css-vars to tell it about your sass setup, especially your sass entry file, the variables you want to modify and where the generated bulma files should be placed.

Config key
sassEntryFileSass Entry File of you Application - relative path form config file
jsOutputFilefull name of generated js file, can also be <a-typescript-file>.ts
sassOutputFilefull name of generated sass file, should be included in your app styles
cssFallbackOutputFilefull name of generated css file, should be included in your sass app styles (optional)
colorDefscolor definitions, names have to match bulma color names (see examples above)
globalWebVarif you import js files directly in the browser, you need true, see Direct Web Setup, defaults to false
transitionwill create the CSS transition shorthand for all colored CSS variables, should consist of [ <duration> [ <timing-function> [ <time-delay> ] ] ]

Some more files have to be setup, which can be achieved using node ./node_modules/.bin/bulma-css-vars --init.

// main.scss
@import './bulma-generated/generated-fallback.css'; // import fallbacks first, so they are overridden
@import './bulma-generated/generated-vars.sass';
@import '../node_modules/bulma-css-vars/bulma-cv-lib';

Instead of using bulma-cv-lib.sass, you can also just use the bulma packages you like. Look inside the bulma-cv-lib.sass to understand more, and especially import functions.sass right after the original functions.sass is loaded.

// package.json
  scripts: {
    "update-bulma-colors": "bulma-css-vars",
  }

The script ./node_modules/.bin/bulma-css-vars has to be run whenever you modify the colors in bulma-css-vars.config.js and it will update the three output files as well.

// in-the-web-app.js
import { ColorUpdater } from 'bulma-css-vars'
import { bulmaCssVariablesDefs } from './bulma-generated/bulma-colors'

const updater = new ColorUpdater(bulmaCssVariablesDefs)

// do the update manually
const updated = colorUpdater.getUpdatedVars(colorName, value)
updated.forEach(({ name, value }) =>
  document.documentElement.style.setProperty(name, value)
)
// or let it do the updater
colorUpdater.updateVarsInDocument(colorName, value)

You can also use TypeScript

// in-the-web-app.ts
import { ColorUpdater } from 'bulma-css-vars'
import { bulmaCssVariablesDefs } from './bulma-generated/bulma-colors'

// the updater do the style change
const colorUpdater = new ColorUpdater(bulmaCssVariablesDefs)
colorUpdater.updateVarsInDocument(colorName, value)

Annoyingly, the color updater needs knowledge of the current variables, so bulmaCssVariablesDefs from the generated js file has to be included in your app. colorName has to match the name in the bulma-css-vars.config.js.

Caveats

  • Requires Node.js and Dart Sass
  • The complexity of the setup

What is the difficulty? Why this setup?

The problem is, that Bulma relies heavily on sass preprocessing, so if you set a color variable for $primary, this color will be modified for hover / focus shades on buttons, inversions for texts on buttons are calculated, and so on. If we would inject a CSS variable var(--my-dynamic-primary-color) here, not only will sass fail, also if you will change this color on runtime, still all the computed shades will remain calculated from the initial color.

The solution - how it works

First off this library patches original sass functions such as darken, lighten, as well as bulma helper functions like findColorInvert. This is currently only possible thanks to the power of the dart sass package, which has more powerful possibilities than the node-sass package. The patched functions can handle var(--variablename) variables and decide if the original function will be called or if a new, derived variable of the incoming variable gets added.

For example, darken(var(--primary), 10%) will turn into var(--primary--10--darken), and a derived variable gets added to the css stylesheets. This library automates this process, so for each actually used and derived variable a new css variable gets created. To leverage this technique, the library then compiles the css and gets an overview of which derived variables are all in the actually used stylesheets.

The advantage of this technique is that only actually required variables will be generated and added to the stylesheets. The disadvantage is, that you need to run this command whenever you decide to add an additional variable to your app.

From the list of base variables and derived variables, this library then can create an initial sass file, that you should include in your sass compilation. It calculates the style using a js version of darken, lighten, findColorInvert. As an example, --primary--10--darken: #532949; would be generated by bulma-css-vars as well, next to --primary: #694392;.

The class doing this should also be included into your application. Whenever you want to change a bulma color variable, e. g. say $blue, the color updater knows which derived variables are required to be adjusted as well and will let you update all affected variables.

This way you can keep using the full bulma color richness. Try out the demo and see how the font of the buttons changes on dark / bright colors!

Direct Web Setup

If you do not use any bundler or web framework, you can also include bulma-css-vars directly. You will still require Node.js and sass.

// app.sass
@import './bulma-vars.sass'
@import './node_modules/bulma-css-vars/bulma-cv-lib.sass'

Make sure you set globalWebVars to true in your bulma-css-vars.config.js. Then run ./node_modules/.bin/bulma-css-vars and sass app.sass > app.css.

In your html:

<head>
  <link res="stylesheet" src="./app.css">
</head>
<body>
  <!-- [your webpage] -->

  <!-- loads window.BulmaColorUpdater -->
  <script src="./node_modules/bulma-css-vars/dist/bulma-css-vars.web-bundle.js"></script>

  <!-- loads window.bulmaCssVarsDef with your variable definitions -->
  <script src="./bulma-colors.js"></script>
  <script>
    const updater = new BulmaColorUpdater(bulmaCssVarsDef)
    updater.updateVarsInDocument('black', '#553292')
  </script>
</body>

New Features

PRs are welcome! If you want to build this repository and try out the demo, make sure you use yarn and yarn install to install the packages in the demo folder. npm will create symlinks instead of true file copies, which might create problems on referencing relative bulma files.

版本与发布记录

@cssninja/bulma-css-vars 共发布 3 个版本,首次发布于 2023/01/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.9.3latest2025/04/021.6 年前
0.9.22023/01/023 年前
0.9.12023/01/023 年前

关于 @cssninja/bulma-css-vars 的常见问题

@cssninja/bulma-css-vars 是什么?

Bulma with CSS variables

如何安装 @cssninja/bulma-css-vars?

在项目目录运行 npm install @cssninja/bulma-css-vars,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:bulma(^0.9.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 bulma-css-vars,可以用 npx bulma-css-vars 直接运行。

@cssninja/bulma-css-vars 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cssninja/bulma-css-vars 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cssninja/bulma-css-vars 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 10.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cssninja/bulma-css-vars 可以商用吗?

@cssninja/bulma-css-vars 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cssninja/bulma-css-vars 的下载量有多少?

根据 npm 官方下载统计,@cssninja/bulma-css-vars 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,913 次,日均约 97 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cssninja/bulma-css-vars 还在维护吗?

最新版本 0.9.3 发布于 2025/04/02(1.6 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/01/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。