@homebound/truss 2.12.0 → 2.13.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.
@@ -15,6 +15,9 @@ type TrussStyleValue = string | [classNames: string, vars: Record<string, string
15
15
  /** A property-keyed style hash where each key owns one logical CSS property. */
16
16
  type TrussCustomClassNameValue = string | ReadonlyArray<string | false | null | undefined>;
17
17
  type TrussStyleHash = Record<string, TrussStyleValue | TrussCustomClassNameValue>;
18
+ type RuntimeStyleDeclarationValue = string | number | null | undefined;
19
+ type RuntimeStyleDeclarations = Record<string, RuntimeStyleDeclarationValue | Record<string, unknown>>;
20
+ type RuntimeStyleCss = Record<string, RuntimeStyleDeclarations | string>;
18
21
  /** Merge one or more Truss style hashes into `{ className, style?, data-truss-src? }`. */
19
22
  declare function trussProps(...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>): Record<string, unknown>;
20
23
  /** Merge explicit className/style with Truss style hashes. */
@@ -25,5 +28,43 @@ declare function mergeProps(explicitClassName: string | undefined, explicitStyle
25
28
  * In browser dev mode, CSS is served via the Vite virtual endpoint instead.
26
29
  */
27
30
  declare function __injectTrussCSS(cssText: string): void;
31
+ interface RuntimeStyleProps {
32
+ css: RuntimeStyleCss;
33
+ }
34
+ /**
35
+ * Inject dynamic or selector-based CSS at runtime into a transient `<style>` tag.
36
+ *
37
+ * This is the runtime counterpart to `.css.ts` files:
38
+ * - use `.css.ts` for static/global arbitrary selectors that should be baked into the build output
39
+ * - use `RuntimeStyle` for selectors that depend on runtime values or should only exist while a component is mounted
40
+ *
41
+ * Example with a flat `Css` expression:
42
+ * ```tsx
43
+ * <RuntimeStyle
44
+ * css={{
45
+ * ".preview a": Css.blue.$,
46
+ * }}
47
+ * />
48
+ * ```
49
+ *
50
+ * Example with raw CSS via `Css.raw`:
51
+ * ```tsx
52
+ * <RuntimeStyle
53
+ * css={{
54
+ * ".preview code": Css.raw`
55
+ * font-variant-ligatures: none;
56
+ * text-decoration: underline;
57
+ * `,
58
+ * }}
59
+ * />
60
+ * ```
61
+ *
62
+ * The injected `<style>` element is appended on mount and removed on unmount.
63
+ *
64
+ * Note: Only flat `Css.*.$` expressions are supported here; selector/marker helpers like
65
+ * `onHover`, `when`, `ifSm`, `ifContainer`, `element`, and `className()` are rejected
66
+ * at runtime.
67
+ */
68
+ declare function RuntimeStyle(props: RuntimeStyleProps): null;
28
69
 
29
- export { type TrussCustomClassNameValue, TrussDebugInfo, type TrussStyleHash, type TrussStyleValue, __injectTrussCSS, mergeProps, trussProps };
70
+ export { RuntimeStyle, type RuntimeStyleCss, type RuntimeStyleDeclarationValue, type RuntimeStyleDeclarations, type RuntimeStyleProps, type TrussCustomClassNameValue, TrussDebugInfo, type TrussStyleHash, type TrussStyleValue, __injectTrussCSS, mergeProps, trussProps };
package/build/runtime.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/runtime.ts
2
+ import { useInsertionEffect } from "react";
2
3
  var TrussDebugInfo = class {
3
4
  /** I.e. `"FileName.tsx:line"` */
4
5
  src;
@@ -88,6 +89,61 @@ function __injectTrussCSS(cssText) {
88
89
  injectedChunks.add(cssText);
89
90
  style.textContent = (style.textContent ?? "") + cssText;
90
91
  }
92
+ function RuntimeStyle(props) {
93
+ const cssText = buildRuntimeStyleCssText(props.css);
94
+ useInsertionEffect(() => {
95
+ if (typeof document === "undefined" || cssText.length === 0) return;
96
+ const style = document.createElement("style");
97
+ style.setAttribute("data-truss-runtime-style", "");
98
+ style.textContent = cssText;
99
+ document.head.appendChild(style);
100
+ return () => style.remove();
101
+ }, [cssText]);
102
+ return null;
103
+ }
104
+ function buildRuntimeStyleCssText(css) {
105
+ const rules = [];
106
+ for (const [selector, value] of Object.entries(css)) {
107
+ if (typeof value === "string") {
108
+ rules.push(formatRawRuntimeStyleRule(selector, value));
109
+ } else {
110
+ rules.push(formatRuntimeStyleRule(selector, value));
111
+ }
112
+ }
113
+ return rules.join("\n\n");
114
+ }
115
+ function formatRawRuntimeStyleRule(selector, raw) {
116
+ const trimmed = raw.trim();
117
+ if (!trimmed) return `${selector} {}`;
118
+ const body = trimmed.split("\n").map((line) => ` ${line.trim()}`).filter((line) => line.trim().length > 0).join("\n");
119
+ return `${selector} {
120
+ ${body}
121
+ }`;
122
+ }
123
+ function formatRuntimeStyleRule(selector, declarations) {
124
+ const lines = [];
125
+ for (const [property, value] of Object.entries(declarations)) {
126
+ if (value === void 0 || value === null) continue;
127
+ if (typeof value !== "string" && typeof value !== "number") {
128
+ throw new Error(runtimeStyleUnsupportedValueMessage(selector, property));
129
+ }
130
+ lines.push(` ${camelToKebabRuntime(property)}: ${String(value)};`);
131
+ }
132
+ if (lines.length === 0) return `${selector} {}`;
133
+ return `${selector} {
134
+ ${lines.join("\n")}
135
+ }`;
136
+ }
137
+ function runtimeStyleUnsupportedValueMessage(selector, property) {
138
+ return `RuntimeStyle selector \`${selector}\` has an unsupported nested value for \`${property}\`. Only flat Css expressions can be used here; selector/marker/className helpers like onHover, when, ifSm, ifContainer, element, and className() are not supported.`;
139
+ }
140
+ function camelToKebabRuntime(property) {
141
+ return property.replace(/^(Webkit|Moz|Ms|O)/, function prefixToCss(prefix) {
142
+ return `-${prefix.toLowerCase()}`;
143
+ }).replace(/[A-Z]/g, function upperToCss(letter) {
144
+ return `-${letter.toLowerCase()}`;
145
+ });
146
+ }
91
147
  function assertValidTrussStyleValue(key, value) {
92
148
  if (typeof value === "string") return;
93
149
  if (Array.isArray(value) && typeof value[0] === "string") {
@@ -131,6 +187,7 @@ function getOrCreateTrussStyleElement() {
131
187
  return trussStyleElement;
132
188
  }
133
189
  export {
190
+ RuntimeStyle,
134
191
  TrussDebugInfo,
135
192
  __injectTrussCSS,
136
193
  mergeProps,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/runtime.ts"],"sourcesContent":["/** A compact source label for a Truss CSS expression, used in debug mode. */\nexport class TrussDebugInfo {\n /** I.e. `\"FileName.tsx:line\"` */\n readonly src: string;\n\n constructor(src: string) {\n this.src = src;\n }\n}\n\n/**\n * Space-separated atomic class names, or a variable tuple with class names + CSS variable map.\n *\n * In debug mode, the transform appends a TrussDebugInfo as an extra tuple element:\n * - static with debug: `[classNames, debugInfo]`\n * - variable with debug: `[classNames, vars, debugInfo]`\n */\nexport type TrussStyleValue =\n | string\n | [classNames: string, vars: Record<string, string>]\n | [classNames: string, debugInfo: TrussDebugInfo]\n | [classNames: string, vars: Record<string, string>, debugInfo: TrussDebugInfo];\n\n/** A property-keyed style hash where each key owns one logical CSS property. */\nexport type TrussCustomClassNameValue = string | ReadonlyArray<string | false | null | undefined>;\nexport type TrussStyleHash = Record<string, TrussStyleValue | TrussCustomClassNameValue>;\n\nconst shouldValidateTrussStyleValues = resolveShouldValidateTrussStyleValues();\nconst TRUSS_CSS_CHUNKS = \"__trussCssChunks__\";\nlet trussStyleElement: TrussStyleElement | null = null;\n\n/** Merge one or more Truss style hashes into `{ className, style?, data-truss-src? }`. */\nexport function trussProps(\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const merged: Record<string, TrussStyleValue> = {};\n\n for (const hash of hashes) {\n if (!hash || typeof hash !== \"object\") continue;\n Object.assign(merged, hash);\n }\n\n const classNames: string[] = [];\n const inlineStyle: Record<string, string> = {};\n const debugSources: string[] = [];\n\n for (const [key, value] of Object.entries(merged)) {\n // __marker is a special key — its value is a marker class name, not a CSS property\n if (key === \"__marker\") {\n if (typeof value === \"string\") {\n classNames.push(value);\n }\n continue;\n }\n\n if (key.startsWith(\"className_\")) {\n // I.e. plugin-emitted raw class names that should flow straight into the final prop.\n appendCustomClassNames(classNames, value);\n continue;\n }\n\n if (shouldValidateTrussStyleValues) assertValidTrussStyleValue(key, value);\n\n if (typeof value === \"string\") {\n // I.e. \"df\" or \"black blue_h\"\n classNames.push(value);\n continue;\n }\n\n // Tuple: [classNames, varsOrDebug?, maybeDebug?]\n classNames.push(value[0]);\n\n for (let i = 1; i < value.length; i++) {\n const el = value[i];\n if (el instanceof TrussDebugInfo) {\n debugSources.push(el.src);\n } else if (typeof el === \"object\" && el !== null) {\n Object.assign(inlineStyle, el);\n }\n }\n }\n\n const props: Record<string, unknown> = {\n className: classNames.join(\" \"),\n };\n\n if (Object.keys(inlineStyle).length > 0) {\n props.style = inlineStyle;\n }\n\n if (debugSources.length > 0) {\n props[\"data-truss-src\"] = [...new Set(debugSources)].join(\"; \");\n }\n\n return props;\n}\n\nfunction appendCustomClassNames(classNames: string[], value: unknown): void {\n if (typeof value === \"string\") {\n // I.e. `className_button: \"button\"`\n classNames.push(value);\n return;\n }\n\n if (!Array.isArray(value)) return;\n for (const entry of value) {\n if (typeof entry === \"string\") {\n // I.e. `className_button: [\"button\", cond && \"selected\"]`\n classNames.push(entry);\n }\n }\n}\n\n/** Merge explicit className/style with Truss style hashes. */\nexport function mergeProps(\n explicitClassName: string | undefined,\n explicitStyle: Record<string, unknown> | undefined,\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const result = trussProps(...hashes);\n\n if (explicitClassName) {\n result.className = `${explicitClassName} ${result.className ?? \"\"}`.trim();\n }\n\n if (explicitStyle) {\n result.style = { ...explicitStyle, ...(result.style as Record<string, unknown> | undefined) };\n }\n\n return result;\n}\n\n/**\n * Inject CSS text into the document for jsdom/test environments.\n *\n * In browser dev mode, CSS is served via the Vite virtual endpoint instead.\n */\nexport function __injectTrussCSS(cssText: string): void {\n if (typeof document === \"undefined\" || cssText.length === 0) return;\n\n const style = getOrCreateTrussStyleElement();\n\n // Track exact injected chunks on the style node so repeated execution of the\n // test bootstrap or transformed modules does not append duplicate CSS text.\n const injectedChunks = (style[TRUSS_CSS_CHUNKS] ??= new Set<string>());\n if (injectedChunks.has(cssText) || style.textContent?.includes(cssText)) {\n injectedChunks.add(cssText);\n return;\n }\n\n injectedChunks.add(cssText);\n style.textContent = (style.textContent ?? \"\") + cssText;\n}\n\n/** Fail fast when `trussProps` receives a non-Truss style value. */\nfunction assertValidTrussStyleValue(key: string, value: unknown): asserts value is TrussStyleValue {\n if (typeof value === \"string\") return;\n if (Array.isArray(value) && typeof value[0] === \"string\") {\n for (let i = 1; i < value.length; i++) {\n const el = value[i];\n if (el instanceof TrussDebugInfo) continue;\n if (typeof el === \"object\" && el !== null && !Array.isArray(el)) continue;\n throw new TypeError(invalidTrussStyleValueMessage(key));\n }\n return;\n }\n throw new TypeError(invalidTrussStyleValueMessage(key));\n}\n\nfunction invalidTrussStyleValueMessage(key: string): string {\n return `Invalid Truss style value for \\`${key}\\`. trussProps only accepts generated Truss style hashes; use mergeProps for explicit className/style merging.`;\n}\n\n/** Enable validation in dev/test environments, but skip it in production. */\nfunction resolveShouldValidateTrussStyleValues(): boolean {\n if (typeof process !== \"undefined\" && typeof process.env.NODE_ENV === \"string\") {\n return process.env.NODE_ENV !== \"production\";\n }\n const viteEnv = (import.meta as ImportMeta & { env?: { DEV?: boolean; PROD?: boolean } }).env;\n if (typeof viteEnv?.DEV === \"boolean\") {\n return viteEnv.DEV;\n }\n if (typeof viteEnv?.PROD === \"boolean\") {\n return !viteEnv.PROD;\n }\n return false;\n}\n\nfunction getOrCreateTrussStyleElement(): TrussStyleElement {\n const id = \"data-truss\";\n if (trussStyleElement?.ownerDocument === document && trussStyleElement.isConnected) {\n return trussStyleElement;\n }\n\n const style = (document.querySelector(`style[${id}]`) as TrussStyleElement | null) ?? document.createElement(\"style\");\n if (!style.isConnected) {\n style.setAttribute(id, \"\");\n document.head.appendChild(style);\n }\n trussStyleElement = style;\n return trussStyleElement;\n}\n\ntype TrussStyleElement = HTMLStyleElement & {\n [TRUSS_CSS_CHUNKS]?: Set<string>;\n};\n"],"mappings":";AACO,IAAM,iBAAN,MAAqB;AAAA;AAAA,EAEjB;AAAA,EAET,YAAY,KAAa;AACvB,SAAK,MAAM;AAAA,EACb;AACF;AAmBA,IAAM,iCAAiC,sCAAsC;AAC7E,IAAM,mBAAmB;AACzB,IAAI,oBAA8C;AAG3C,SAAS,cACX,QACsB;AACzB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;AACxD;AAEA,SAAS,8BAA8B,KAAqB;AAC1D,SAAO,mCAAmC,GAAG;AAC/C;AAGA,SAAS,wCAAiD;AACxD,MAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,IAAI,aAAa,UAAU;AAC9E,WAAO,QAAQ,IAAI,aAAa;AAAA,EAClC;AACA,QAAM,UAAW,YAAyE;AAC1F,MAAI,OAAO,SAAS,QAAQ,WAAW;AACrC,WAAO,QAAQ;AAAA,EACjB;AACA,MAAI,OAAO,SAAS,SAAS,WAAW;AACtC,WAAO,CAAC,QAAQ;AAAA,EAClB;AACA,SAAO;AACT;AAEA,SAAS,+BAAkD;AACzD,QAAM,KAAK;AACX,MAAI,mBAAmB,kBAAkB,YAAY,kBAAkB,aAAa;AAClF,WAAO;AAAA,EACT;AAEA,QAAM,QAAS,SAAS,cAAc,SAAS,EAAE,GAAG,KAAkC,SAAS,cAAc,OAAO;AACpH,MAAI,CAAC,MAAM,aAAa;AACtB,UAAM,aAAa,IAAI,EAAE;AACzB,aAAS,KAAK,YAAY,KAAK;AAAA,EACjC;AACA,sBAAoB;AACpB,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/runtime.ts"],"sourcesContent":["import { useInsertionEffect } from \"react\";\n\n/** A compact source label for a Truss CSS expression, used in debug mode. */\nexport class TrussDebugInfo {\n /** I.e. `\"FileName.tsx:line\"` */\n readonly src: string;\n\n constructor(src: string) {\n this.src = src;\n }\n}\n\n/**\n * Space-separated atomic class names, or a variable tuple with class names + CSS variable map.\n *\n * In debug mode, the transform appends a TrussDebugInfo as an extra tuple element:\n * - static with debug: `[classNames, debugInfo]`\n * - variable with debug: `[classNames, vars, debugInfo]`\n */\nexport type TrussStyleValue =\n | string\n | [classNames: string, vars: Record<string, string>]\n | [classNames: string, debugInfo: TrussDebugInfo]\n | [classNames: string, vars: Record<string, string>, debugInfo: TrussDebugInfo];\n\n/** A property-keyed style hash where each key owns one logical CSS property. */\nexport type TrussCustomClassNameValue = string | ReadonlyArray<string | false | null | undefined>;\nexport type TrussStyleHash = Record<string, TrussStyleValue | TrussCustomClassNameValue>;\nexport type RuntimeStyleDeclarationValue = string | number | null | undefined;\nexport type RuntimeStyleDeclarations = Record<string, RuntimeStyleDeclarationValue | Record<string, unknown>>;\nexport type RuntimeStyleCss = Record<string, RuntimeStyleDeclarations | string>;\n\nconst shouldValidateTrussStyleValues = resolveShouldValidateTrussStyleValues();\nconst TRUSS_CSS_CHUNKS = \"__trussCssChunks__\";\nlet trussStyleElement: TrussStyleElement | null = null;\n\n/** Merge one or more Truss style hashes into `{ className, style?, data-truss-src? }`. */\nexport function trussProps(\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const merged: Record<string, TrussStyleValue> = {};\n\n for (const hash of hashes) {\n if (!hash || typeof hash !== \"object\") continue;\n Object.assign(merged, hash);\n }\n\n const classNames: string[] = [];\n const inlineStyle: Record<string, string> = {};\n const debugSources: string[] = [];\n\n for (const [key, value] of Object.entries(merged)) {\n // __marker is a special key — its value is a marker class name, not a CSS property\n if (key === \"__marker\") {\n if (typeof value === \"string\") {\n classNames.push(value);\n }\n continue;\n }\n\n if (key.startsWith(\"className_\")) {\n // I.e. plugin-emitted raw class names that should flow straight into the final prop.\n appendCustomClassNames(classNames, value);\n continue;\n }\n\n if (shouldValidateTrussStyleValues) assertValidTrussStyleValue(key, value);\n\n if (typeof value === \"string\") {\n // I.e. \"df\" or \"black blue_h\"\n classNames.push(value);\n continue;\n }\n\n // Tuple: [classNames, varsOrDebug?, maybeDebug?]\n classNames.push(value[0]);\n\n for (let i = 1; i < value.length; i++) {\n const el = value[i];\n if (el instanceof TrussDebugInfo) {\n debugSources.push(el.src);\n } else if (typeof el === \"object\" && el !== null) {\n Object.assign(inlineStyle, el);\n }\n }\n }\n\n const props: Record<string, unknown> = {\n className: classNames.join(\" \"),\n };\n\n if (Object.keys(inlineStyle).length > 0) {\n props.style = inlineStyle;\n }\n\n if (debugSources.length > 0) {\n props[\"data-truss-src\"] = [...new Set(debugSources)].join(\"; \");\n }\n\n return props;\n}\n\nfunction appendCustomClassNames(classNames: string[], value: unknown): void {\n if (typeof value === \"string\") {\n // I.e. `className_button: \"button\"`\n classNames.push(value);\n return;\n }\n\n if (!Array.isArray(value)) return;\n for (const entry of value) {\n if (typeof entry === \"string\") {\n // I.e. `className_button: [\"button\", cond && \"selected\"]`\n classNames.push(entry);\n }\n }\n}\n\n/** Merge explicit className/style with Truss style hashes. */\nexport function mergeProps(\n explicitClassName: string | undefined,\n explicitStyle: Record<string, unknown> | undefined,\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const result = trussProps(...hashes);\n\n if (explicitClassName) {\n result.className = `${explicitClassName} ${result.className ?? \"\"}`.trim();\n }\n\n if (explicitStyle) {\n result.style = { ...explicitStyle, ...(result.style as Record<string, unknown> | undefined) };\n }\n\n return result;\n}\n\n/**\n * Inject CSS text into the document for jsdom/test environments.\n *\n * In browser dev mode, CSS is served via the Vite virtual endpoint instead.\n */\nexport function __injectTrussCSS(cssText: string): void {\n if (typeof document === \"undefined\" || cssText.length === 0) return;\n\n const style = getOrCreateTrussStyleElement();\n\n // Track exact injected chunks on the style node so repeated execution of the\n // test bootstrap or transformed modules does not append duplicate CSS text.\n const injectedChunks = (style[TRUSS_CSS_CHUNKS] ??= new Set<string>());\n if (injectedChunks.has(cssText) || style.textContent?.includes(cssText)) {\n injectedChunks.add(cssText);\n return;\n }\n\n injectedChunks.add(cssText);\n style.textContent = (style.textContent ?? \"\") + cssText;\n}\n\nexport interface RuntimeStyleProps {\n css: RuntimeStyleCss;\n}\n\n/**\n * Inject dynamic or selector-based CSS at runtime into a transient `<style>` tag.\n *\n * This is the runtime counterpart to `.css.ts` files:\n * - use `.css.ts` for static/global arbitrary selectors that should be baked into the build output\n * - use `RuntimeStyle` for selectors that depend on runtime values or should only exist while a component is mounted\n *\n * Example with a flat `Css` expression:\n * ```tsx\n * <RuntimeStyle\n * css={{\n * \".preview a\": Css.blue.$,\n * }}\n * />\n * ```\n *\n * Example with raw CSS via `Css.raw`:\n * ```tsx\n * <RuntimeStyle\n * css={{\n * \".preview code\": Css.raw`\n * font-variant-ligatures: none;\n * text-decoration: underline;\n * `,\n * }}\n * />\n * ```\n *\n * The injected `<style>` element is appended on mount and removed on unmount.\n *\n * Note: Only flat `Css.*.$` expressions are supported here; selector/marker helpers like\n * `onHover`, `when`, `ifSm`, `ifContainer`, `element`, and `className()` are rejected\n * at runtime.\n */\nexport function RuntimeStyle(props: RuntimeStyleProps): null {\n const cssText = buildRuntimeStyleCssText(props.css);\n useInsertionEffect(() => {\n if (typeof document === \"undefined\" || cssText.length === 0) return;\n const style = document.createElement(\"style\");\n style.setAttribute(\"data-truss-runtime-style\", \"\");\n style.textContent = cssText;\n document.head.appendChild(style);\n return () => style.remove();\n }, [cssText]);\n return null;\n}\n\n/** Serialize RuntimeStyle rules into CSS text for a transient `<style>` tag. */\nfunction buildRuntimeStyleCssText(css: RuntimeStyleCss): string {\n const rules: string[] = [];\n for (const [selector, value] of Object.entries(css)) {\n if (typeof value === \"string\") {\n rules.push(formatRawRuntimeStyleRule(selector, value));\n } else {\n rules.push(formatRuntimeStyleRule(selector, value));\n }\n }\n return rules.join(\"\\n\\n\");\n}\n\nfunction formatRawRuntimeStyleRule(selector: string, raw: string): string {\n const trimmed = raw.trim();\n if (!trimmed) return `${selector} {}`;\n const body = trimmed\n .split(\"\\n\")\n .map((line) => ` ${line.trim()}`)\n .filter((line) => line.trim().length > 0)\n .join(\"\\n\");\n return `${selector} {\\n${body}\\n}`;\n}\n\nfunction formatRuntimeStyleRule(selector: string, declarations: RuntimeStyleDeclarations): string {\n const lines: string[] = [];\n for (const [property, value] of Object.entries(declarations)) {\n if (value === undefined || value === null) continue;\n if (typeof value !== \"string\" && typeof value !== \"number\") {\n throw new Error(runtimeStyleUnsupportedValueMessage(selector, property));\n }\n lines.push(` ${camelToKebabRuntime(property)}: ${String(value)};`);\n }\n if (lines.length === 0) return `${selector} {}`;\n return `${selector} {\\n${lines.join(\"\\n\")}\\n}`;\n}\n\nfunction runtimeStyleUnsupportedValueMessage(selector: string, property: string): string {\n return `RuntimeStyle selector \\`${selector}\\` has an unsupported nested value for \\`${property}\\`. Only flat Css expressions can be used here; selector/marker/className helpers like onHover, when, ifSm, ifContainer, element, and className() are not supported.`;\n}\n\nfunction camelToKebabRuntime(property: string): string {\n return property\n .replace(/^(Webkit|Moz|Ms|O)/, function prefixToCss(prefix) {\n return `-${prefix.toLowerCase()}`;\n })\n .replace(/[A-Z]/g, function upperToCss(letter) {\n return `-${letter.toLowerCase()}`;\n });\n}\n\n/** Fail fast when `trussProps` receives a non-Truss style value. */\nfunction assertValidTrussStyleValue(key: string, value: unknown): asserts value is TrussStyleValue {\n if (typeof value === \"string\") return;\n if (Array.isArray(value) && typeof value[0] === \"string\") {\n for (let i = 1; i < value.length; i++) {\n const el = value[i];\n if (el instanceof TrussDebugInfo) continue;\n if (typeof el === \"object\" && el !== null && !Array.isArray(el)) continue;\n throw new TypeError(invalidTrussStyleValueMessage(key));\n }\n return;\n }\n throw new TypeError(invalidTrussStyleValueMessage(key));\n}\n\nfunction invalidTrussStyleValueMessage(key: string): string {\n return `Invalid Truss style value for \\`${key}\\`. trussProps only accepts generated Truss style hashes; use mergeProps for explicit className/style merging.`;\n}\n\n/** Enable validation in dev/test environments, but skip it in production. */\nfunction resolveShouldValidateTrussStyleValues(): boolean {\n if (typeof process !== \"undefined\" && typeof process.env.NODE_ENV === \"string\") {\n return process.env.NODE_ENV !== \"production\";\n }\n const viteEnv = (import.meta as ImportMeta & { env?: { DEV?: boolean; PROD?: boolean } }).env;\n if (typeof viteEnv?.DEV === \"boolean\") {\n return viteEnv.DEV;\n }\n if (typeof viteEnv?.PROD === \"boolean\") {\n return !viteEnv.PROD;\n }\n return false;\n}\n\nfunction getOrCreateTrussStyleElement(): TrussStyleElement {\n const id = \"data-truss\";\n if (trussStyleElement?.ownerDocument === document && trussStyleElement.isConnected) {\n return trussStyleElement;\n }\n\n const style = (document.querySelector(`style[${id}]`) as TrussStyleElement | null) ?? document.createElement(\"style\");\n if (!style.isConnected) {\n style.setAttribute(id, \"\");\n document.head.appendChild(style);\n }\n trussStyleElement = style;\n return trussStyleElement;\n}\n\ntype TrussStyleElement = HTMLStyleElement & {\n [TRUSS_CSS_CHUNKS]?: Set<string>;\n};\n"],"mappings":";AAAA,SAAS,0BAA0B;AAG5B,IAAM,iBAAN,MAAqB;AAAA;AAAA,EAEjB;AAAA,EAET,YAAY,KAAa;AACvB,SAAK,MAAM;AAAA,EACb;AACF;AAsBA,IAAM,iCAAiC,sCAAsC;AAC7E,IAAM,mBAAmB;AACzB,IAAI,oBAA8C;AAG3C,SAAS,cACX,QACsB;AACzB,QAAM,SAA0C,CAAC;AAEjD,aAAW,QAAQ,QAAQ;AACzB,QAAI,CAAC,QAAQ,OAAO,SAAS,SAAU;AACvC,WAAO,OAAO,QAAQ,IAAI;AAAA,EAC5B;AAEA,QAAM,aAAuB,CAAC;AAC9B,QAAM,cAAsC,CAAC;AAC7C,QAAM,eAAyB,CAAC;AAEhC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,GAAG;AAEjD,QAAI,QAAQ,YAAY;AACtB,UAAI,OAAO,UAAU,UAAU;AAC7B,mBAAW,KAAK,KAAK;AAAA,MACvB;AACA;AAAA,IACF;AAEA,QAAI,IAAI,WAAW,YAAY,GAAG;AAEhC,6BAAuB,YAAY,KAAK;AACxC;AAAA,IACF;AAEA,QAAI,+BAAgC,4BAA2B,KAAK,KAAK;AAEzE,QAAI,OAAO,UAAU,UAAU;AAE7B,iBAAW,KAAK,KAAK;AACrB;AAAA,IACF;AAGA,eAAW,KAAK,MAAM,CAAC,CAAC;AAExB,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,YAAM,KAAK,MAAM,CAAC;AAClB,UAAI,cAAc,gBAAgB;AAChC,qBAAa,KAAK,GAAG,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AM,UAAW,YAAyE;AAC1F,MAAI,OAAO,SAAS,QAAQ,WAAW;AACrC,WAAO,QAAQ;AAAA,EACjB;AACA,MAAI,OAAO,SAAS,SAAS,WAAW;AACtC,WAAO,CAAC,QAAQ;AAAA,EAClB;AACA,SAAO;AACT;AAEA,SAAS,+BAAkD;AACzD,QAAM,KAAK;AACX,MAAI,mBAAmB,kBAAkB,YAAY,kBAAkB,aAAa;AAClF,WAAO;AAAA,EACT;AAEA,QAAM,QAAS,SAAS,cAAc,SAAS,EAAE,GAAG,KAAkC,SAAS,cAAc,OAAO;AACpH,MAAI,CAAC,MAAM,aAAa;AACtB,UAAM,aAAa,IAAI,EAAE;AACzB,aAAS,KAAK,YAAY,KAAK;AAAA,EACjC;AACA,sBAAoB;AACpB,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/truss",
3
- "version": "2.12.0",
3
+ "version": "2.13.0",
4
4
  "type": "module",
5
5
  "main": "build/index.js",
6
6
  "bin": "cli.js",
@@ -41,11 +41,16 @@
41
41
  "jiti": "^2.6.1",
42
42
  "ts-poet": "^6.12.0"
43
43
  },
44
+ "peerDependencies": {
45
+ "react": ">=18"
46
+ },
44
47
  "devDependencies": {
45
48
  "@homebound/tsconfig": "^1.1.1",
46
49
  "@types/babel__generator": "^7.27.0",
47
50
  "@types/babel__traverse": "^7.28.0",
48
51
  "@types/node": "^25.5.0",
52
+ "@types/react": "^19.2.14",
53
+ "react": "^19.2.4",
49
54
  "tsup": "^8.5.1",
50
55
  "typescript": "^5.9.3",
51
56
  "vitest": "^4.1.0"