mintcss 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nexryai
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.
package/README.md CHANGED
@@ -1,3 +1,98 @@
1
- # Temporary Holding Version
1
+ # mintcss
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Small, dependency-free CSS-in-JS for browser applications.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install mintcss
9
+ ```
10
+
11
+ mintcss is an ESM package with bundled TypeScript declarations. It targets modern
12
+ browsers with `String.replaceAll` support. Importing it does not touch the DOM;
13
+ calling its styling functions requires a browser `document` with a `head`.
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { css, globalCss, keyframes } from "mintcss";
19
+
20
+ globalCss({
21
+ ":root": { "--accent": "#f4bd36" },
22
+ body: { margin: 0, fontFamily: "system-ui, sans-serif" },
23
+ });
24
+
25
+ const fade = keyframes({
26
+ from: { opacity: 0 },
27
+ to: { opacity: 1 },
28
+ });
29
+
30
+ const button = css({
31
+ backgroundColor: "var(--accent)",
32
+ borderRadius: 8,
33
+ padding: "8px 16px",
34
+ animation: `${fade} 150ms ease-out`,
35
+ "&:hover": { opacity: 0.8 },
36
+ "& > svg": { width: 16 },
37
+ "@media (width >= 40rem)": { padding: "12px 24px" },
38
+ "@supports (display: grid)": { display: "grid" },
39
+ });
40
+
41
+ document.querySelector("button")?.classList.add(button);
42
+ ```
43
+
44
+ In React, pass the result to `className`:
45
+
46
+ ```tsx
47
+ import { css } from "mintcss";
48
+
49
+ const button = css({ backgroundColor: "#f4bd36", borderRadius: 8 });
50
+
51
+ export function Button() {
52
+ return <button className={button}>Hello</button>;
53
+ }
54
+ ```
55
+
56
+ ## API
57
+
58
+ - `css(styles: CSSObject): string` inserts rules and returns a deterministic
59
+ `mint-…` class name. Object key order does not change that name.
60
+ - `keyframes(frames: Record<string, CSSDeclarations>): string` inserts an
61
+ animation and returns its `mint-kf-…` name. Steps are `from`, `to`, or integer
62
+ percentages from `0%` through `100%`, emitted in numeric order.
63
+ - `globalCss(rules: Record<string, CSSObject>): void` inserts global selector
64
+ rules. Top-level `@media` and `@supports` blocks contain selector objects.
65
+ - Types `CSSObject`, `CSSDeclarations`, and `CSSValue` are exported. Properties
66
+ accept strings, numbers, and `undefined`; nested objects represent selectors
67
+ or supported conditional rules. These types do not validate property names
68
+ against a complete CSS specification.
69
+
70
+ Properties use camelCase (`backgroundColor`, `WebkitAppearance`, `msOverflowStyle`)
71
+ except CSS custom properties (`--accent`). Nonzero numeric values receive `px`
72
+ unless they are custom properties or supported unitless properties, such as
73
+ `opacity`, `lineHeight`, `fontWeight`, `flexGrow`, or `zIndex`. Zero stays `0`.
74
+ Use strings for other units and numeric values that must remain unitless.
75
+ `undefined` declarations are omitted.
76
+
77
+ Nested selectors must include `&`, which is replaced by the parent selector.
78
+ Only `@media` and `@supports` conditional rules are supported; use `keyframes`
79
+ for animations. Declarations and nested keys are emitted in sorted order, so
80
+ avoid mixing shorthand and longhand declarations that depend on insertion order.
81
+
82
+ Rules are appended to a shared `<style data-mintcss>` element. Its DOM registry
83
+ avoids duplicate insertion, including across module reloads. Global rules keep
84
+ selector insertion order so their cascade is preserved. Generated identifiers
85
+ are implementation details and should not be persisted or hard-coded.
86
+
87
+ ## Limits
88
+
89
+ This is a browser runtime library: there is no SSR stylesheet collection,
90
+ hydration API, build-time extraction, automatic vendor prefixing, or CSP nonce
91
+ configuration. Styles accumulate for the lifetime of the document; prefer
92
+ reusing stable style objects and CSS variables for continuously changing values.
93
+ Strict CSP deployments must permit the generated inline stylesheet.
94
+
95
+ The serializer rejects malformed properties and rules, non-finite numeric
96
+ values, and certain injection-like strings. It is **not an untrusted-input
97
+ sanitizer**: pass application-authored styles and validate external data before
98
+ using it, particularly URLs. It does not enforce an external URL policy.
package/dist/core.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type CSSValue = string | number;
2
+ export type CSSObject = {
3
+ [key: string]: CSSValue | CSSObject | undefined;
4
+ };
5
+ export type CSSDeclarations = Record<string, CSSValue | undefined>;
6
+ export declare const css: (object: CSSObject) => string;
7
+ export declare const keyframes: (frames: Record<string, CSSDeclarations>) => string;
8
+ export declare const globalCss: (rules: Record<string, CSSObject>) => void;
9
+ export declare const __resetCssForTests: () => void;
package/dist/core.js ADDED
@@ -0,0 +1,158 @@
1
+ const STYLE_ATTRIBUTE = "data-mintcss";
2
+ let styleElement;
3
+ const unitlessProperties = new Set([
4
+ "animationIterationCount",
5
+ "aspectRatio",
6
+ "columnCount",
7
+ "flex",
8
+ "flexGrow",
9
+ "flexShrink",
10
+ "fontWeight",
11
+ "gridArea",
12
+ "gridColumn",
13
+ "gridColumnEnd",
14
+ "gridColumnStart",
15
+ "gridRow",
16
+ "gridRowEnd",
17
+ "gridRowStart",
18
+ "lineHeight",
19
+ "opacity",
20
+ "order",
21
+ "orphans",
22
+ "scale",
23
+ "tabSize",
24
+ "widows",
25
+ "zIndex",
26
+ "zoom",
27
+ ]);
28
+ const hyphenate = (property) => {
29
+ if (property.startsWith("--"))
30
+ return property;
31
+ return property.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`).replace(/^ms-/, "-ms-");
32
+ };
33
+ const assertSafe = (value, kind) => {
34
+ if (/[;{}]/.test(value) || /<\/style|\/\*|javascript:|expression\s*\(/i.test(value))
35
+ throw new Error(`Unsafe CSS ${kind}`);
36
+ };
37
+ const assertConditionalRule = (rule) => {
38
+ if (!/^@(media|supports)\s+[^;{}]+$/.test(rule))
39
+ throw new Error(`Invalid CSS at-rule: ${rule}`);
40
+ assertSafe(rule, "at-rule");
41
+ };
42
+ const declaration = (property, value) => {
43
+ if (!/^(--[a-zA-Z0-9_-]+|[a-zA-Z][a-zA-Z0-9]*)$/.test(property))
44
+ throw new Error(`Invalid CSS property: ${property}`);
45
+ const serialized = typeof value === "number" && value !== 0 && !property.startsWith("--") && !unitlessProperties.has(property) ? `${value}px` : String(value);
46
+ if (typeof value === "number" && !Number.isFinite(value))
47
+ throw new Error(`Invalid CSS number: ${value}`);
48
+ assertSafe(serialized, "value");
49
+ return `${hyphenate(property)}:${serialized};`;
50
+ };
51
+ const serializeRule = (selector, object) => {
52
+ let declarations = "";
53
+ let nested = "";
54
+ for (const key of Object.keys(object).sort()) {
55
+ const value = object[key];
56
+ if (value === undefined)
57
+ continue;
58
+ if (value === null)
59
+ throw new Error(`Invalid CSS object: ${key}`);
60
+ if (typeof value !== "object") {
61
+ declarations += declaration(key, value);
62
+ continue;
63
+ }
64
+ if (key.startsWith("@media") || key.startsWith("@supports")) {
65
+ assertConditionalRule(key);
66
+ nested += `${key}{${serializeRule(selector, value)}}`;
67
+ continue;
68
+ }
69
+ if (!key.includes("&"))
70
+ throw new Error(`Nested selector must contain &: ${key}`);
71
+ assertSafe(key, "selector");
72
+ nested += serializeRule(key.replaceAll("&", selector), value);
73
+ }
74
+ return `${declarations ? `${selector}{${declarations}}` : ""}${nested}`;
75
+ };
76
+ const canonicalize = (object) => Object.keys(object)
77
+ .sort()
78
+ .map((key) => {
79
+ const value = object[key];
80
+ return `${JSON.stringify(key)}:${typeof value === "object" && value !== null ? `{${canonicalize(value)}}` : JSON.stringify(value)}`;
81
+ })
82
+ .join(",");
83
+ const hash = (value) => {
84
+ let result = 2166136261;
85
+ for (let index = 0; index < value.length; index += 1) {
86
+ result ^= value.charCodeAt(index);
87
+ result = Math.imul(result, 16777619);
88
+ }
89
+ return (result >>> 0).toString(36);
90
+ };
91
+ const registry = () => {
92
+ if (typeof document === "undefined")
93
+ throw new Error("mintcss requires a browser document");
94
+ if (styleElement?.isConnected && styleElement.ownerDocument === document)
95
+ return styleElement;
96
+ const existing = document.head.querySelector(`style[${STYLE_ATTRIBUTE}]`);
97
+ styleElement = existing ?? document.createElement("style");
98
+ if (!existing) {
99
+ styleElement.setAttribute(STYLE_ATTRIBUTE, "");
100
+ document.head.append(styleElement);
101
+ }
102
+ return styleElement;
103
+ };
104
+ const insert = (id, rule) => {
105
+ const target = registry();
106
+ // The DOM is authoritative across module reloads and stylesheet replacement.
107
+ const registeredInDOM = new Set((target.dataset.mintcss ?? "").split(" ").filter(Boolean));
108
+ if (registeredInDOM.has(id))
109
+ return;
110
+ registeredInDOM.add(id);
111
+ target.dataset.mintcss = [...registeredInDOM].join(" ");
112
+ target.append(document.createTextNode(rule));
113
+ };
114
+ export const css = (object) => {
115
+ const id = `mint-${hash(canonicalize(object))}`;
116
+ insert(id, serializeRule(`.${id}`, object));
117
+ return id;
118
+ };
119
+ export const keyframes = (frames) => {
120
+ const object = frames;
121
+ const name = `mint-kf-${hash(canonicalize(object))}`;
122
+ const body = Object.entries(object)
123
+ .sort(([left], [right]) => {
124
+ const position = (step) => (step === "from" ? 0 : step === "to" ? 100 : Number.parseInt(step, 10));
125
+ return position(left) - position(right);
126
+ })
127
+ .map(([step, properties]) => {
128
+ if (!/^(from|to|(?:\d|[1-9]\d|100)%)$/.test(step))
129
+ throw new Error(`Invalid keyframe step: ${step}`);
130
+ return serializeRule(step, properties);
131
+ })
132
+ .join("");
133
+ insert(name, `@keyframes ${name}{${body}}`);
134
+ return name;
135
+ };
136
+ export const globalCss = (rules) => {
137
+ const output = Object.entries(rules)
138
+ .map(([selector, object]) => {
139
+ if (selector.startsWith("@media") || selector.startsWith("@supports")) {
140
+ assertConditionalRule(selector);
141
+ return `${selector}{${Object.entries(object)
142
+ .map(([nestedSelector, nestedObject]) => {
143
+ assertSafe(nestedSelector, "selector");
144
+ return serializeRule(nestedSelector, nestedObject);
145
+ })
146
+ .join("")}}`;
147
+ }
148
+ assertSafe(selector, "selector");
149
+ return serializeRule(selector, object);
150
+ })
151
+ .join("");
152
+ const id = `mint-global-${hash(output)}`;
153
+ insert(id, output);
154
+ };
155
+ export const __resetCssForTests = () => {
156
+ document.head.querySelector(`style[${STYLE_ATTRIBUTE}]`)?.remove();
157
+ styleElement = undefined;
158
+ };
@@ -0,0 +1,2 @@
1
+ export type { CSSDeclarations, CSSObject, CSSValue } from "#core";
2
+ export { css, globalCss, keyframes } from "#core";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { css, globalCss, keyframes } from "#core";
@@ -0,0 +1 @@
1
+ export { __resetCssForTests } from "#core";
@@ -0,0 +1 @@
1
+ export { __resetCssForTests } from "#core";
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
- "name": "mintcss",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "mintcss",
3
+ "version": "0.1.1",
4
+ "description": "Small, dependency-free CSS-in-JS for browser applications.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "nexryai",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/nexryai/mintcss.git"
11
+ },
12
+ "homepage": "https://github.com/nexryai/mintcss#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/nexryai/mintcss/issues"
15
+ },
16
+ "keywords": ["css", "css-in-js", "styles", "typescript", "browser"],
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ },
24
+ "./testing": {
25
+ "types": "./dist/testing.d.ts",
26
+ "import": "./dist/testing.js"
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "files": ["dist", "README.md", "LICENSE"],
31
+ "sideEffects": false,
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "registry": "https://registry.npmjs.org/"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc -p tsconfig.json",
38
+ "check": "tsc -p tsconfig.check.json",
39
+ "lint": "biome check .",
40
+ "format": "biome check --write .",
41
+ "test": "vitest run",
42
+ "test:package": "node scripts/test-package.mjs",
43
+ "prepack": "npm run build",
44
+ "prepublishOnly": "npm run lint && npm run check && npm test && npm run test:package"
45
+ },
46
+ "devDependencies": {
47
+ "@biomejs/biome": "2.5.11",
48
+ "@types/node": "^24.13.3",
49
+ "jsdom": "30.1.2",
50
+ "typescript": "~6.0.2",
51
+ "vitest": "^4.1.11"
52
+ },
53
+ "imports": {
54
+ "#core": "./dist/core.js",
55
+ "#mintcss": "./dist/index.js",
56
+ "#testing": "./dist/testing.js"
57
+ }
58
+ }