chai-tini 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,16 @@
1
1
  # chai-tini
2
2
 
3
- Lightweight utility-first CSS engine that converts `chai-*` class names into inline styles by scanning the DOM.
3
+ Lightweight utility-first "CSS engine" that converts `chai-*` class names into inline styles by scanning the DOM.
4
+
5
+ ![npm version](https://img.shields.io/npm/v/chai-tini?style=flat&color=blue)
6
+ ![downloads](https://img.shields.io/npm/dm/chai-tini?style=flat)
7
+ ![license](https://img.shields.io/npm/l/chai-tini?style=flat)
8
+
9
+ ## Features
10
+
11
+ - Scans the DOM for elements with `chai-*` utility classes.
12
+ - Applies matching inline styles (the original classes stay intact).
13
+ - Auto-initializes on import, with an optional manual + scoped API.
4
14
 
5
15
  ## Install
6
16
 
@@ -8,15 +18,15 @@ Lightweight utility-first “CSS engine” that converts `chai-*` class names in
8
18
  npm i chai-tini
9
19
  ```
10
20
 
11
- ## Usage
21
+ ## Quick Start
12
22
 
13
- ### Option A: auto-init on import (default)
23
+ ### Auto init (default)
14
24
 
15
25
  ```js
16
26
  import "chai-tini";
17
27
  ```
18
28
 
19
- ### Option B: explicit init
29
+ ### Manual init
20
30
 
21
31
  ```js
22
32
  import { initChaiUtilities } from "chai-tini";
@@ -24,30 +34,70 @@ import { initChaiUtilities } from "chai-tini";
24
34
  initChaiUtilities();
25
35
  ```
26
36
 
27
- ## Supported utilities (v1)
28
-
29
- - Spacing (px): `chai-p-*`, `chai-m-*`, plus `x/y/t/r/b/l` directional variants
30
- - Examples: `chai-p-2`, `chai-px-4`, `chai-mt-6`
31
- - Colors:
32
- - `chai-bg-red` -> `background-color: red`
33
- - `chai-text-red` -> `color: red`
34
- - Alignment:
35
- - `chai-text-center|left|right|justify` -> `text-align: ...`
36
- - Typography:
37
- - `chai-fs-16` -> `font-size: 16px`
38
- - `chai-font-bold|normal|semibold|light` -> `font-weight`
39
- - Borders & radius:
40
- - `chai-rounded-8` and `chai-rounded-t-8|r-8|b-8|l-8`
41
- - `chai-border-w-1` (also sets `border-style: solid`)
42
- - `chai-border-color-red`
43
- - Basic layout:
44
- - `chai-flex|grid|block|inline|inline-block|none` -> `display`
45
- - `chai-w-200`, `chai-h-50` (px)
46
- - Animation:
47
- - `chai-animate-spin` -> `animation: chai-animate-spin 1s linear infinite`
37
+ ### Scope to a subtree (recommended for SPA routes)
38
+
39
+ ```js
40
+ import { initChaiUtilities } from "chai-tini";
41
+
42
+ const root = document.getElementById("app");
43
+ initChaiUtilities({ root: root ?? undefined });
44
+ ```
45
+
46
+ ### Example
47
+
48
+ ```html
49
+ <div class="chai-p-2 chai-bg-red chai-text-center chai-fs-16 chai-font-semibold">
50
+ Hello
51
+ </div>
52
+ ```
53
+
54
+ ## Supported Utilities
55
+
56
+ ### Spacing (padding / margin)
57
+
58
+ - `chai-p-<n>` / `chai-m-<n>` -> `padding` / `margin`
59
+ - Directional variants: `chai-px-<n>`, `chai-py-<n>`, `chai-pt-<n>`, `chai-pr-<n>`, `chai-pb-<n>`, `chai-pl-<n>` (and the same for `chai-m*`)
60
+
61
+ ### Colors / alignment
62
+
63
+ - `chai-bg-<color>` -> `background-color: <color>`
64
+ - `chai-text-<color>` -> `color: <color>`
65
+ - `chai-text-center|left|right|justify` -> `text-align: ...`
66
+
67
+ ### Typography
68
+
69
+ - `chai-fs-<n>` -> `font-size: <n>px`
70
+ - `chai-font-light|normal|semibold|bold` -> `font-weight`
71
+
72
+ ### Borders & radius
73
+
74
+ - `chai-rounded-<n>` -> `border-radius`
75
+ - `chai-rounded-(t|r|b|l)-<n>` -> corner radii
76
+ - `chai-border-w-<n>` -> `border-width: <n>px` and `border-style: solid`
77
+ - `chai-border-color-<color>` -> `border-color`
78
+
79
+ ### Layout / display
80
+
81
+ - `chai-flex` -> `display: flex`
82
+ - `chai-grid` -> `display: grid`
83
+ - `chai-block` -> `display: block`
84
+ - `chai-inline` -> `display: inline`
85
+ - `chai-inline-block` -> `display: inline-block`
86
+ - `chai-none` -> `display: none`
87
+
88
+ ### Sizing
89
+
90
+ - `chai-w-<n>` -> `width: <n>px`
91
+ - `chai-h-<n>` -> `height: <n>px`
92
+
93
+ ### Animation
94
+
95
+ - `chai-animate-spin` -> `animation: chai-animate-spin 1s linear infinite`
96
+ - Also injects the required `@keyframes` into the page.
48
97
 
49
98
  ## Notes
50
99
 
51
100
  - Numeric tokens like `2` are treated as `2px`. Tokens like `10rem` or `5%` are passed through as-is.
52
- - The library keeps the original `chai-*` classes (it only applies inline styles).
101
+ - The library runs in the browser only. If `document` is not available, `initChaiUtilities()` is a no-op.
102
+ - Inline styles are applied; if you update classes after init, call `applyChaiUtilities(root?)` again (or re-run `initChaiUtilities` for your scope).
53
103
 
@@ -4,12 +4,9 @@ export function applyChaiUtilities(root) {
4
4
  const queryRoot = root ?? (typeof document !== "undefined" ? document : null);
5
5
  if (!queryRoot)
6
6
  return;
7
- const doc =
8
- // If `queryRoot` is a document, use it directly.
9
- typeof queryRoot.createElement === "function"
7
+ const doc = typeof queryRoot.createElement === "function"
10
8
  ? queryRoot
11
- : // Otherwise use its owning document.
12
- queryRoot.ownerDocument ?? (typeof document !== "undefined" ? document : null);
9
+ : queryRoot.ownerDocument ?? (typeof document !== "undefined" ? document : null);
13
10
  let needsAnimateSpinKeyframes = false;
14
11
  const selectorRoot = queryRoot;
15
12
  const elements = typeof selectorRoot.querySelectorAll === "function" ? Array.from(selectorRoot.querySelectorAll(`[class*="${prefix}"]`)) : [];
package/dist/index.d.ts CHANGED
@@ -2,5 +2,6 @@ import { applyChaiUtilities } from "./applyChaiUtilities.js";
2
2
  export type ChaiUtilitiesInitOptions = {
3
3
  root?: ParentNode;
4
4
  };
5
+ export declare function stopChaiUtilities(root?: ParentNode): void;
5
6
  export declare function initChaiUtilities(options?: ChaiUtilitiesInitOptions): void;
6
7
  export { applyChaiUtilities };
package/dist/index.js CHANGED
@@ -1,9 +1,57 @@
1
1
  import { applyChaiUtilities } from "./applyChaiUtilities.js";
2
+ const activeObservers = new WeakMap();
3
+ function supportsMutationObserver() {
4
+ return typeof MutationObserver !== "undefined";
5
+ }
6
+ function watchForChaiMutations(root) {
7
+ if (!supportsMutationObserver())
8
+ return;
9
+ if (activeObservers.has(root))
10
+ return;
11
+ let scheduled = false;
12
+ const scheduleApply = () => {
13
+ if (scheduled)
14
+ return;
15
+ scheduled = true;
16
+ queueMicrotask(() => {
17
+ scheduled = false;
18
+ applyChaiUtilities(root);
19
+ });
20
+ };
21
+ const observer = new MutationObserver((mutations) => {
22
+ for (const mutation of mutations) {
23
+ if (mutation.type === "attributes" && mutation.attributeName !== "class")
24
+ continue;
25
+ scheduleApply();
26
+ break;
27
+ }
28
+ });
29
+ observer.observe(root, {
30
+ subtree: true,
31
+ childList: true,
32
+ attributes: true,
33
+ attributeFilter: ["class"],
34
+ });
35
+ activeObservers.set(root, observer);
36
+ }
37
+ export function stopChaiUtilities(root) {
38
+ const target = root ?? (typeof document !== "undefined" ? document : undefined);
39
+ if (!target)
40
+ return;
41
+ const observer = activeObservers.get(target);
42
+ if (!observer)
43
+ return;
44
+ observer.disconnect();
45
+ activeObservers.delete(target);
46
+ }
2
47
  export function initChaiUtilities(options = {}) {
3
48
  if (typeof document === "undefined")
4
49
  return;
5
50
  const root = options.root ?? document;
6
- const run = () => applyChaiUtilities(root);
51
+ const run = () => {
52
+ applyChaiUtilities(root);
53
+ watchForChaiMutations(root);
54
+ };
7
55
  if (document.readyState === "loading") {
8
56
  document.addEventListener("DOMContentLoaded", run, { once: true });
9
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chai-tini",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A lightweight client-side engine that converts `chai-*` utility classes into inline styles by scanning the DOM.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -11,7 +11,7 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "default": "./dist/index.js"
13
13
  }
14
- }
14
+ }
15
15
  },
16
16
  "files": [
17
17
  "dist"