@styleframe/runtime 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Alex Grozav
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.
@@ -0,0 +1,93 @@
1
+ import { PrimitiveTokenValue } from '@styleframe/core';
2
+ import { RecipeRuntime } from '@styleframe/core';
3
+ import { RuntimeModifierDeclarationsBlock } from '@styleframe/core';
4
+ import { RuntimeVariantDeclarationsBlock } from '@styleframe/core';
5
+ import { RuntimeVariantDeclarationsValue } from '@styleframe/core';
6
+ import { TokenValue } from '@styleframe/core';
7
+
8
+ /**
9
+ * Creates a runtime recipe function that generates utility class strings based on variant props.
10
+ *
11
+ * The function:
12
+ * 1. Applies the recipe name as the base class
13
+ * 2. Applies all base declarations
14
+ * 3. Applies variant declarations based on props (with defaultVariants as fallback)
15
+ * 4. Applies compound variants if all conditions match
16
+ * 5. Later declarations override earlier ones
17
+ *
18
+ * @param name - The recipe name (used as the base class)
19
+ * @param runtime - The pre-computed RecipeRuntime object with resolved values
20
+ * @returns A function that accepts variant props and returns a className string
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const buttonRuntime: RecipeRuntime = {
25
+ * base: {
26
+ * borderWidth: "thin",
27
+ * borderStyle: "[solid]",
28
+ * },
29
+ * variants: {
30
+ * color: {
31
+ * primary: { background: "primary", color: "white" },
32
+ * secondary: { background: "secondary", color: "white" },
33
+ * },
34
+ * size: {
35
+ * sm: { padding: "[10px]" },
36
+ * md: { padding: "2" },
37
+ * },
38
+ * },
39
+ * defaultVariants: {
40
+ * color: "primary",
41
+ * size: "md",
42
+ * },
43
+ * };
44
+ *
45
+ * const button = createRecipe("button", buttonRuntime);
46
+ * button({}); // "button _border-width:thin _border-style:[solid] _background:primary _color:white _padding:2"
47
+ * button({ color: "secondary" }); // "button _border-width:thin _border-style:[solid] _background:secondary _color:white _padding:2"
48
+ * ```
49
+ */
50
+ export declare function createRecipe<R extends RecipeRuntime>(name: string, runtime: R): (props?: RecipeVariantProps<R>) => string;
51
+
52
+ export { PrimitiveTokenValue }
53
+
54
+ export { RecipeRuntime }
55
+
56
+ /**
57
+ * Extracts the variant props type from a RecipeRuntime.
58
+ * Maps each variant key to its possible option values.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const runtime = {
63
+ * variants: {
64
+ * color: { primary: {...}, secondary: {...} },
65
+ * size: { sm: {...}, md: {...} },
66
+ * },
67
+ * } as const;
68
+ *
69
+ * type Props = RecipeVariantProps<typeof runtime>;
70
+ * // { color?: 'primary' | 'secondary', size?: 'sm' | 'md' }
71
+ * ```
72
+ */
73
+ export declare type RecipeVariantProps<R> = R extends {
74
+ variants?: infer V;
75
+ } ? V extends Record<string, Record<string, unknown> | undefined> ? string extends keyof V ? Record<string, string> : {
76
+ [K in keyof V]?: V[K] extends Record<string, unknown> ? keyof V[K] & string : never;
77
+ } : Record<string, string> : Record<string, string>;
78
+
79
+ export { RuntimeModifierDeclarationsBlock }
80
+
81
+ export { RuntimeVariantDeclarationsBlock }
82
+
83
+ export { RuntimeVariantDeclarationsValue }
84
+
85
+ /**
86
+ * Represents a mapping of variant option names to their runtime declarations.
87
+ * Used for cleaner type assertions when iterating over variant options.
88
+ */
89
+ export declare type RuntimeVariantOptions = Record<string, RuntimeVariantDeclarationsBlock | undefined>;
90
+
91
+ export { TokenValue }
92
+
93
+ export { }
@@ -0,0 +1,57 @@
1
+ function u(s, t, i = []) {
2
+ const o = s.replace(
3
+ /[A-Z]/g,
4
+ (e) => `-${e.toLowerCase()}`
5
+ ), a = i.length > 0 ? `${i.join(":")}:` : "";
6
+ return t === !0 ? `_${a}${o}` : `_${a}${o}:${t}`;
7
+ }
8
+ function d(s) {
9
+ return typeof s == "object" && s !== null;
10
+ }
11
+ function l(s, t) {
12
+ for (const [i, o] of Object.entries(s))
13
+ if (d(o)) {
14
+ const a = i.split(":");
15
+ for (const [e, c] of Object.entries(o)) {
16
+ const n = `${a.join(":")}:${e}`;
17
+ t.set(n, {
18
+ value: c,
19
+ modifiers: a
20
+ });
21
+ }
22
+ } else
23
+ t.set(i, { value: o, modifiers: [] });
24
+ }
25
+ function p(s, t) {
26
+ return (i = {}) => {
27
+ const o = /* @__PURE__ */ new Map();
28
+ if (t.base && l(t.base, o), t.variants)
29
+ for (const [e, c] of Object.entries(
30
+ t.variants
31
+ )) {
32
+ const n = i[e] ?? t.defaultVariants?.[e], f = n ? c[n] : void 0;
33
+ f && l(f, o);
34
+ }
35
+ if (t.compoundVariants)
36
+ for (const e of t.compoundVariants) {
37
+ let c = !0;
38
+ for (const [n, r] of Object.entries(
39
+ e.match
40
+ ))
41
+ if ((i[n] ?? t.defaultVariants?.[n]) !== r) {
42
+ c = !1;
43
+ break;
44
+ }
45
+ c && e.css && l(e.css, o);
46
+ }
47
+ const a = [s];
48
+ for (const [e, { value: c, modifiers: n }] of o.entries()) {
49
+ const r = n.length > 0 ? e.slice(n.join(":").length + 1) : e;
50
+ a.push(u(r, c, n));
51
+ }
52
+ return a.join(" ");
53
+ };
54
+ }
55
+ export {
56
+ p as createRecipe
57
+ };
@@ -0,0 +1 @@
1
+ (function(r,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(r=typeof globalThis<"u"?globalThis:r||self,f(r.runtime={}))})(this,(function(r){"use strict";function f(i,e,s=[]){const o=i.replace(/[A-Z]/g,t=>`-${t.toLowerCase()}`),a=s.length>0?`${s.join(":")}:`:"";return e===!0?`_${a}${o}`:`_${a}${o}:${e}`}function p(i){return typeof i=="object"&&i!==null}function d(i,e){for(const[s,o]of Object.entries(i))if(p(o)){const a=s.split(":");for(const[t,c]of Object.entries(o)){const n=`${a.join(":")}:${t}`;e.set(n,{value:c,modifiers:a})}}else e.set(s,{value:o,modifiers:[]})}function y(i,e){return(s={})=>{const o=new Map;if(e.base&&d(e.base,o),e.variants)for(const[t,c]of Object.entries(e.variants)){const n=s[t]??e.defaultVariants?.[t],u=n?c[n]:void 0;u&&d(u,o)}if(e.compoundVariants)for(const t of e.compoundVariants){let c=!0;for(const[n,l]of Object.entries(t.match))if((s[n]??e.defaultVariants?.[n])!==l){c=!1;break}c&&t.css&&d(t.css,o)}const a=[i];for(const[t,{value:c,modifiers:n}]of o.entries()){const l=n.length>0?t.slice(n.join(":").length+1):t;a.push(f(l,c,n))}return a.join(" ")}}r.createRecipe=y,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@styleframe/runtime",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "types": "./dist/runtime.d.ts",
6
+ "module": "./dist/runtime.js",
7
+ "main": "./dist/runtime.umd.cjs",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/runtime.d.ts",
11
+ "import": "./dist/runtime.js",
12
+ "require": "./dist/runtime.umd.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "LICENSE",
18
+ "CHANGELOG.md",
19
+ "README.md"
20
+ ],
21
+ "devDependencies": {
22
+ "@styleframe/config-typescript": "^2",
23
+ "@styleframe/config-vite": "^2",
24
+ "@styleframe/core": "^2",
25
+ "@vitest/coverage-v8": "^3.2.4",
26
+ "typescript": "^5.8.3",
27
+ "vite": "^7.0.6",
28
+ "vitest": "^3.2.4",
29
+ "vite-plugin-dts": "^4.5.4"
30
+ },
31
+ "peerDependencies": {
32
+ "@styleframe/core": "^2"
33
+ },
34
+ "homepage": "https://github.com/styleframe-dev/styleframe#readme",
35
+ "bugs": {
36
+ "url": "https://github.com/styleframe-dev/styleframe/issues"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/styleframe-dev/styleframe.git"
41
+ },
42
+ "author": "Alex Grozav <alex@styleframe.dev>",
43
+ "overrides": {
44
+ "vite": "npm:rolldown-vite@latest"
45
+ },
46
+ "scripts": {
47
+ "dev": "vite build --watch",
48
+ "build": "pnpm typecheck && vite build",
49
+ "typecheck": "tsc --noEmit",
50
+ "test": "vitest run",
51
+ "test:dev": "vitest --watch"
52
+ }
53
+ }