How it works
-
Scan the DOM
On init,
document.querySelectorAll('*')walks every element. Each element'sclassListis checked againstcustomStyles. -
Match and parse
On a hit, the CSS string like
"padding: 8px; margin: 0"is split on;and each declaration is set withel.style.setProperty(). Safer than writing tocssText. -
Watch for changes
A
MutationObserverwatcheschildList,subtree, andclassattribute changes. Dynamically added elements get styled too.
Typography
Heading 1 - chai-text-4xl
Heading 2 - chai-text-3xl
Heading 3 - chai-text-2xl
This is body text. Class: chai-text-base chai-text-gray.
Scanned at runtime, injected inline. No stylesheet touched.
Small italic text - chai-text-sm chai-italic
Colors
The original example
This is the exact use case from the spec. Inspect this heading - it has no stylesheet, just a class and a data attribute.
hello world
<h1 class="chai-p-2 chai-text-2xl chai-font-bold ...">
hello world
</h1>
↓ After chai-wind runs:
<h1 class="chai-p-2 ..."
style="padding: 8px; font-size: 24px; font-weight: 700; ..."
data-chai-applied="chai-p-2 chai-text-2xl chai-font-bold ...">
hello world
</h1>
MutationObserver - dynamic injection
Click the button. A new element is created and injected into the DOM after the initial scan. The MutationObserver catches it and styles it automatically.
Runtime register()
You can register new classes at runtime via
ChaiWind.register(). The element below has class
chai-custom-card which doesn't exist yet - click to
define and apply it.
I have class chai-custom-card. Right now it does
nothing. Register it below.
Live class inspector
Type any chai class name and see what style it maps to.
→ result appears here