chai-tini 0.1.0
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/LICENSE +22 -0
- package/README.md +53 -0
- package/dist/applyChaiUtilities.d.ts +1 -0
- package/dist/applyChaiUtilities.js +42 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +16 -0
- package/dist/parseChaiClass.d.ts +6 -0
- package/dist/parseChaiClass.js +149 -0
- package/package.json +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# chai-tini
|
|
2
|
+
|
|
3
|
+
Lightweight utility-first “CSS engine” that converts `chai-*` class names into inline styles by scanning the DOM.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i chai-tini
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Option A: auto-init on import (default)
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import "chai-tini";
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Option B: explicit init
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { initChaiUtilities } from "chai-tini";
|
|
23
|
+
|
|
24
|
+
initChaiUtilities();
|
|
25
|
+
```
|
|
26
|
+
|
|
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`
|
|
48
|
+
|
|
49
|
+
## Notes
|
|
50
|
+
|
|
51
|
+
- 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).
|
|
53
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function applyChaiUtilities(root: ParentNode | undefined): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { parseChaiClass } from "./parseChaiClass.js";
|
|
2
|
+
export function applyChaiUtilities(root) {
|
|
3
|
+
const prefix = "chai-";
|
|
4
|
+
const queryRoot = root ?? (typeof document !== "undefined" ? document : null);
|
|
5
|
+
if (!queryRoot)
|
|
6
|
+
return;
|
|
7
|
+
const doc =
|
|
8
|
+
// If `queryRoot` is a document, use it directly.
|
|
9
|
+
typeof queryRoot.createElement === "function"
|
|
10
|
+
? queryRoot
|
|
11
|
+
: // Otherwise use its owning document.
|
|
12
|
+
queryRoot.ownerDocument ?? (typeof document !== "undefined" ? document : null);
|
|
13
|
+
let needsAnimateSpinKeyframes = false;
|
|
14
|
+
const selectorRoot = queryRoot;
|
|
15
|
+
const elements = typeof selectorRoot.querySelectorAll === "function" ? Array.from(selectorRoot.querySelectorAll(`[class*="${prefix}"]`)) : [];
|
|
16
|
+
for (const el of elements) {
|
|
17
|
+
const classAttr = el.getAttribute("class") ?? "";
|
|
18
|
+
const tokens = classAttr.split(/\s+/).filter(Boolean);
|
|
19
|
+
const patches = [];
|
|
20
|
+
for (const token of tokens) {
|
|
21
|
+
if (!token.startsWith(prefix))
|
|
22
|
+
continue;
|
|
23
|
+
if (token === "chai-animate-spin")
|
|
24
|
+
needsAnimateSpinKeyframes = true;
|
|
25
|
+
patches.push(...parseChaiClass(token));
|
|
26
|
+
}
|
|
27
|
+
for (const patch of patches) {
|
|
28
|
+
el.style.setProperty(patch.prop, patch.value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (needsAnimateSpinKeyframes && doc) {
|
|
32
|
+
const styleId = "chai-animate-spin-keyframes";
|
|
33
|
+
if (!doc.getElementById(styleId)) {
|
|
34
|
+
const styleEl = doc.createElement("style");
|
|
35
|
+
styleEl.id = styleId;
|
|
36
|
+
styleEl.textContent =
|
|
37
|
+
"@keyframes chai-animate-spin{from{transform:rotate(0deg);}to{transform:rotate(360deg);}}";
|
|
38
|
+
const head = doc.head ?? doc.getElementsByTagName("head")[0];
|
|
39
|
+
(head ?? doc.documentElement).appendChild(styleEl);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { applyChaiUtilities } from "./applyChaiUtilities.js";
|
|
2
|
+
export function initChaiUtilities(options = {}) {
|
|
3
|
+
if (typeof document === "undefined")
|
|
4
|
+
return;
|
|
5
|
+
const root = options.root ?? document;
|
|
6
|
+
const run = () => applyChaiUtilities(root);
|
|
7
|
+
if (document.readyState === "loading") {
|
|
8
|
+
document.addEventListener("DOMContentLoaded", run, { once: true });
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
run();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if (typeof document !== "undefined")
|
|
15
|
+
initChaiUtilities();
|
|
16
|
+
export { applyChaiUtilities };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
export function parseNumericOrPx(raw) {
|
|
2
|
+
if (/^-?\d+(\.\d+)?$/.test(raw))
|
|
3
|
+
return `${raw}px`;
|
|
4
|
+
return raw;
|
|
5
|
+
}
|
|
6
|
+
const fontWeightMap = {
|
|
7
|
+
light: "300",
|
|
8
|
+
normal: "400",
|
|
9
|
+
semibold: "600",
|
|
10
|
+
bold: "700",
|
|
11
|
+
};
|
|
12
|
+
function patchesForSpacing(kind, dir, valueRaw) {
|
|
13
|
+
const value = parseNumericOrPx(valueRaw);
|
|
14
|
+
const propBase = kind === "p" ? "padding" : "margin";
|
|
15
|
+
switch (dir) {
|
|
16
|
+
case undefined:
|
|
17
|
+
return [{ prop: propBase, value }];
|
|
18
|
+
case "x":
|
|
19
|
+
return [
|
|
20
|
+
{ prop: `${propBase}-left`, value },
|
|
21
|
+
{ prop: `${propBase}-right`, value },
|
|
22
|
+
];
|
|
23
|
+
case "y":
|
|
24
|
+
return [
|
|
25
|
+
{ prop: `${propBase}-top`, value },
|
|
26
|
+
{ prop: `${propBase}-bottom`, value },
|
|
27
|
+
];
|
|
28
|
+
case "t":
|
|
29
|
+
return [{ prop: `${propBase}-top`, value }];
|
|
30
|
+
case "r":
|
|
31
|
+
return [{ prop: `${propBase}-right`, value }];
|
|
32
|
+
case "b":
|
|
33
|
+
return [{ prop: `${propBase}-bottom`, value }];
|
|
34
|
+
case "l":
|
|
35
|
+
return [{ prop: `${propBase}-left`, value }];
|
|
36
|
+
default:
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function patchesForRounded(className) {
|
|
41
|
+
const dirMatch = /^chai-rounded-([trbl])-(.+)$/.exec(className);
|
|
42
|
+
if (dirMatch) {
|
|
43
|
+
const dir = dirMatch[1];
|
|
44
|
+
const value = parseNumericOrPx(dirMatch[2]);
|
|
45
|
+
switch (dir) {
|
|
46
|
+
case "t":
|
|
47
|
+
return [
|
|
48
|
+
{ prop: "border-top-left-radius", value },
|
|
49
|
+
{ prop: "border-top-right-radius", value },
|
|
50
|
+
];
|
|
51
|
+
case "r":
|
|
52
|
+
return [
|
|
53
|
+
{ prop: "border-top-right-radius", value },
|
|
54
|
+
{ prop: "border-bottom-right-radius", value },
|
|
55
|
+
];
|
|
56
|
+
case "b":
|
|
57
|
+
return [
|
|
58
|
+
{ prop: "border-bottom-left-radius", value },
|
|
59
|
+
{ prop: "border-bottom-right-radius", value },
|
|
60
|
+
];
|
|
61
|
+
case "l":
|
|
62
|
+
return [
|
|
63
|
+
{ prop: "border-top-left-radius", value },
|
|
64
|
+
{ prop: "border-bottom-left-radius", value },
|
|
65
|
+
];
|
|
66
|
+
default:
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const plain = /^chai-rounded-(.+)$/.exec(className);
|
|
71
|
+
if (plain)
|
|
72
|
+
return [{ prop: "border-radius", value: parseNumericOrPx(plain[1]) }];
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
function tryPatches(className) {
|
|
76
|
+
// Colors
|
|
77
|
+
const bg = /^chai-bg-(.+)$/.exec(className);
|
|
78
|
+
if (bg)
|
|
79
|
+
return [{ prop: "background-color", value: bg[1] }];
|
|
80
|
+
const text = /^chai-text-(.+)$/.exec(className);
|
|
81
|
+
if (text) {
|
|
82
|
+
const tail = text[1];
|
|
83
|
+
const align = tail === "center" ? "center" : tail === "left" ? "left" : tail === "right" ? "right" : tail === "justify" ? "justify" : null;
|
|
84
|
+
if (align)
|
|
85
|
+
return [{ prop: "text-align", value: align }];
|
|
86
|
+
return [{ prop: "color", value: tail }];
|
|
87
|
+
}
|
|
88
|
+
// Typography - Font Size
|
|
89
|
+
const fs = /^chai-fs-(.+)$/.exec(className);
|
|
90
|
+
if (fs)
|
|
91
|
+
return [{ prop: "font-size", value: parseNumericOrPx(fs[1]) }];
|
|
92
|
+
const fontWeight = /^chai-font-(.+)$/.exec(className);
|
|
93
|
+
if (fontWeight) {
|
|
94
|
+
const mapped = fontWeightMap[fontWeight[1]];
|
|
95
|
+
if (mapped)
|
|
96
|
+
return [{ prop: "font-weight", value: mapped }];
|
|
97
|
+
}
|
|
98
|
+
// BR's and Radius
|
|
99
|
+
const rounded = className.startsWith("chai-rounded-") ? patchesForRounded(className) : [];
|
|
100
|
+
if (rounded.length)
|
|
101
|
+
return rounded;
|
|
102
|
+
const borderW = /^chai-border-w-(.+)$/.exec(className);
|
|
103
|
+
if (borderW) {
|
|
104
|
+
const value = parseNumericOrPx(borderW[1]);
|
|
105
|
+
return [
|
|
106
|
+
{ prop: "border-width", value },
|
|
107
|
+
{ prop: "border-style", value: "solid" },
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
const borderColor = /^chai-border-color-(.+)$/.exec(className);
|
|
111
|
+
if (borderColor)
|
|
112
|
+
return [{ prop: "border-color", value: borderColor[1] }];
|
|
113
|
+
// Spacing
|
|
114
|
+
const spacing = /^chai-(p|m)(x|y|t|r|b|l)?-(.+)$/.exec(className);
|
|
115
|
+
if (spacing)
|
|
116
|
+
return patchesForSpacing(spacing[1], spacing[2], spacing[3]);
|
|
117
|
+
const compactSpacing = /^chai-(p|m)(x|y|t|r|b|l)-(.+)$/.exec(className);
|
|
118
|
+
if (compactSpacing) {
|
|
119
|
+
return patchesForSpacing(compactSpacing[1], compactSpacing[2], compactSpacing[3]);
|
|
120
|
+
}
|
|
121
|
+
if (className === "chai-flex")
|
|
122
|
+
return [{ prop: "display", value: "flex" }];
|
|
123
|
+
if (className === "chai-grid")
|
|
124
|
+
return [{ prop: "display", value: "grid" }];
|
|
125
|
+
if (className === "chai-block")
|
|
126
|
+
return [{ prop: "display", value: "block" }];
|
|
127
|
+
if (className === "chai-inline")
|
|
128
|
+
return [{ prop: "display", value: "inline" }];
|
|
129
|
+
if (className === "chai-inline-block")
|
|
130
|
+
return [{ prop: "display", value: "inline-block" }];
|
|
131
|
+
if (className === "chai-none")
|
|
132
|
+
return [{ prop: "display", value: "none" }];
|
|
133
|
+
const w = /^chai-w-(.+)$/.exec(className);
|
|
134
|
+
if (w)
|
|
135
|
+
return [{ prop: "width", value: parseNumericOrPx(w[1]) }];
|
|
136
|
+
const h = /^chai-h-(.+)$/.exec(className);
|
|
137
|
+
if (h)
|
|
138
|
+
return [{ prop: "height", value: parseNumericOrPx(h[1]) }];
|
|
139
|
+
if (className === "chai-animate-spin") {
|
|
140
|
+
return [{ prop: "animation", value: "chai-animate-spin 1s linear infinite" }];
|
|
141
|
+
}
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
export function parseChaiClass(className) {
|
|
145
|
+
const normalized = className.trim();
|
|
146
|
+
if (!normalized.startsWith("chai-"))
|
|
147
|
+
return [];
|
|
148
|
+
return tryPatches(normalized);
|
|
149
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chai-tini",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A lightweight client-side engine that converts `chai-*` utility classes into inline styles by scanning the DOM.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc -p tsconfig.json",
|
|
21
|
+
"prepublishOnly": "npm run build",
|
|
22
|
+
"test": "npm run build && node test/test.mjs"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"jsdom": "^29.0.1",
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
}
|
|
28
|
+
}
|