Chai-Wind
chai-wind is a utility-first CSS-in-JS library designed exclusively for browser environments, focusing on a zero-dependency, no-build-step approach to styling. It allows developers to apply inline CSS styles to HTML elements by using `chai-` prefixed class names, such as `chai-bg-22c55e` or `chai-f-20`. The library operates by scanning the DOM for these special class names upon `DOMContentLoaded` and converting them directly into `element.style` properties. Currently at version `1.0.9`, it appears to be in an active development phase, though specific release cadence isn't detailed. Its primary differentiator is the complete eschewal of traditional CSS files, build tools, and bundlers, making it highly suitable for quick prototyping or projects where a minimal setup is paramount.
Common errors
-
Uncaught TypeError: require is not a function
cause Attempting to use CommonJS `require()` syntax (`const chai = require('chai-wind');`) in an ES Module context or directly in a browser without a bundler.fixUse ES Module `import` syntax: `import { applyChaiStyles } from 'chai-wind';` and ensure your script tag has `type="module"` or your build setup supports ESM. -
Styles are not applied to elements with `chai-` classes.
cause `applyChaiStyles()` was not called, or there might be an issue with script loading order preventing its execution.fixEnsure `applyChaiStyles()` is called once in your script. Although it uses `DOMContentLoaded`, verify your script loads correctly and executes. Place the `<script type="module">` tag at the end of the `<body>`. -
My custom CSS is not working; `chai-wind` styles are always winning.
cause `chai-wind` applies styles as inline `element.style` properties, which have the highest specificity in CSS and will override almost any other style rule.fixUnderstand that inline styles take precedence. If you absolutely need to override `chai-wind` with external CSS, you might have to use `!important` in your external stylesheet. Alternatively, re-evaluate if `chai-wind` is the most appropriate styling solution for parts of your UI requiring complex cascading or fine-grained style control.
Warnings
- gotcha chai-wind applies styles directly to `element.style`, which has the highest CSS specificity. This means traditional CSS stylesheets will be overridden by `chai-wind` unless `!important` is used in external CSS, which is generally discouraged.
- gotcha As a client-side JavaScript utility that manipulates the DOM, chai-wind does not support Server-Side Rendering (SSR). Initial server-rendered content will appear unstyled until the JavaScript executes in the browser.
- gotcha For pages with a very large number of elements utilizing `chai-` classes, querying and manipulating the DOM for each element during `DOMContentLoaded` might introduce a measurable performance overhead, causing a brief flash of unstyled content.
- breaking The library is designed for ES Modules. Attempting to use CommonJS `require()` syntax will result in runtime errors as the package does not provide a CommonJS entry point.
Install
-
npm install chai-winder -
yarn add chai-winder -
pnpm add chai-winder
Imports
- applyChaiStyles
const { applyChaiStyles } = require('chai-wind')import { applyChaiStyles } from 'chai-wind'
Quickstart
<!DOCTYPE html>
<html>
<head>
<title>Chai-Wind Quickstart</title>
<style>
body { font-family: sans-serif; margin: 2rem; }
</style>
</head>
<body>
<div class="chai-bg-22c55e chai-text-white chai-p-24 chai-rounded-8 chai-fw-700 chai-f-20">
Hello, chai-wind 🍵
</div>
<p class="chai-text-orange chai-f-16 chai-dp-flex chai-mt-16">
This paragraph has some inline styles applied via chai-wind classes.
</p>
<script type="module">
import { applyChaiStyles } from "https://esm.sh/chai-winder@1.0.9";
// For local development, you'd use: import { applyChaiStyles } from './node_modules/chai-wind/index.js';
// Call applyChaiStyles to convert `chai-` prefixed classes to inline styles.
// This function automatically waits for the DOMContentLoaded event.
applyChaiStyles();
</script>
</body>
</html>