@homebound/truss 2.23.0 → 2.25.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.
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Utilities for CSS custom properties (`--token`) in Truss style values.
3
+ */
4
+ /**
5
+ * If `value` is a custom property name (`--token`), wrap as `var(--token)` for use as a property value.
6
+ * Passes through values that are not custom-property names (including existing `var(...)`).
7
+ */
8
+ declare function maybeCssVar<T>(value: T): T;
9
+
10
+ export { maybeCssVar as m };
package/build/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Properties } from 'csstype';
2
2
  import { Code } from 'ts-poet';
3
+ export { m as maybeCssVar } from './css-custom-property-B-Ncwzgt.js';
3
4
 
4
5
  /**
5
6
  * A map from human name to font size, i.e. `f12` -> `12px`.
@@ -263,12 +264,24 @@ type IncConfig = [string, Prop | Prop[]];
263
264
  declare function newIncrementMethods(config: Config, abbr: UtilityName, prop: Prop | Prop[], opts?: {
264
265
  auto?: boolean;
265
266
  }): UtilityMethod[];
266
- /**
267
- * Creates just the core `<abbr>X` utility methods that set `props` with each increment value.
268
- *
269
- * See `newIncrementMethods` for handling the `<abbr>Px`, `<abbr>a`, and `<attr>(value)` methods.
270
- */
271
267
  declare function newCoreIncrementMethods(config: Config, abbr: UtilityName, props: Prop[]): UtilityMethod[];
272
268
  declare function newPxMethod(abbr: UtilityName, prop: Prop): UtilityMethod;
273
269
 
274
- export { type Aliases, type Config, type CreateMethodsFn, type FontConfig, type IncConfig, type SectionName, type Sections, type TokenRegistry, type UtilityMethod, type UtilityName, type WebEntry, defaultSections, defineConfig, generate, newAliasesMethods, newCoreIncrementMethods, newIncrementMethods, newMethod, newMethodsForProp, newParamMethod, newPxMethod, newSetCssVariablesMethod, startWebCollection, stopWebCollection };
270
+ /**
271
+ * Web increment utilities use `--t-spacing` with `calc` (see generated `Css.ts` and the Vite plugin).
272
+ * Keep literals in one place so codegen, emitted CSS, and the transform stay aligned.
273
+ * `--t-spacing` must be set (e.g. `:root` prelude from `collectCss()` / mapping `increment`).
274
+ */
275
+ /** Custom property for increment-based spacing (web). */
276
+ declare const SPACING_CUSTOM_PROPERTY = "--t-spacing";
277
+ /** I.e. `calc(var(--t-spacing) * 3)` — requires prelude defining `--t-spacing`. */
278
+ declare function incrementCssValue(multiplier: number): string;
279
+ /**
280
+ * If `cssValue` is exactly `calc(var(--t-spacing) * k)` for this package's spacing property,
281
+ * returns the multiplier substring `k` (e.g. `"2"`, `"-1"`, `"2.5"`). Otherwise null.
282
+ */
283
+ declare function tryParseIncrementCalcMultiplier(cssValue: string): string | null;
284
+ /** Prepended to emitted Truss CSS; `incrementPx` comes from `truss-config` / `Css.json`. */
285
+ declare function rootSpacingPreludeCss(incrementPx: number): string;
286
+
287
+ export { type Aliases, type Config, type CreateMethodsFn, type FontConfig, type IncConfig, SPACING_CUSTOM_PROPERTY, type SectionName, type Sections, type TokenRegistry, type UtilityMethod, type UtilityName, type WebEntry, defaultSections, defineConfig, generate, incrementCssValue, newAliasesMethods, newCoreIncrementMethods, newIncrementMethods, newMethod, newMethodsForProp, newParamMethod, newPxMethod, newSetCssVariablesMethod, rootSpacingPreludeCss, startWebCollection, stopWebCollection, tryParseIncrementCalcMultiplier };
package/build/index.js CHANGED
@@ -3,6 +3,21 @@ function defineConfig(config) {
3
3
  return config;
4
4
  }
5
5
 
6
+ // src/spacing-css-var.ts
7
+ var SPACING_CUSTOM_PROPERTY = "--t-spacing";
8
+ function incrementCssValue(multiplier) {
9
+ return `calc(var(${SPACING_CUSTOM_PROPERTY}) * ${multiplier})`;
10
+ }
11
+ function tryParseIncrementCalcMultiplier(cssValue) {
12
+ const prop = SPACING_CUSTOM_PROPERTY.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
13
+ const re = new RegExp(`^calc\\(var\\(${prop}\\) \\* (-?\\d+(?:\\.\\d+)?)\\)$`);
14
+ const m = cssValue.match(re);
15
+ return m ? m[1] : null;
16
+ }
17
+ function rootSpacingPreludeCss(incrementPx) {
18
+ return `:root { ${SPACING_CUSTOM_PROPERTY}: ${incrementPx}px; }`;
19
+ }
20
+
6
21
  // src/methods.ts
7
22
  var _webCollector = null;
8
23
  function startWebCollection() {
@@ -74,12 +89,15 @@ function newIncrementMethods(config, abbr, prop, opts = {}) {
74
89
  `${pxComment} ${abbr}Px(px: number) { return this.${props.map((p) => `add("${p}", \`\${px}px\`)`).join(".")}; }`
75
90
  ];
76
91
  }
92
+ function isWebIncrementTarget(config) {
93
+ return config.target !== "react-native";
94
+ }
77
95
  function newCoreIncrementMethods(config, abbr, props) {
78
96
  return zeroTo(config.numberOfIncrements).map((i) => {
79
- const px = `${i * config.increment}px`;
80
- const defs = Object.fromEntries(props.map((p) => [p, px]));
97
+ const value = isWebIncrementTarget(config) ? incrementCssValue(i) : `${i * config.increment}px`;
98
+ const defs = Object.fromEntries(props.map((p) => [p, value]));
81
99
  collect({ kind: "static", abbr: `${abbr}${i}`, defs });
82
- const sets = props.map((p) => `add("${p}", "${px}")`).join(".");
100
+ const sets = props.map((p) => `add("${p}", "${value}")`).join(".");
83
101
  return `${comment(defs)} get ${abbr}${i}() { return this.${sets}; }`;
84
102
  });
85
103
  }
@@ -1060,6 +1078,17 @@ type Opts<T> = {
1060
1078
  elseApplied: boolean,
1061
1079
  };
1062
1080
 
1081
+ /** Wraps \`--token\` custom property names as \`var(--token)\` for CSS property values. */
1082
+ function maybeCssVar<T>(value: T): T {
1083
+ if (typeof value !== "string") return value;
1084
+ if (value.startsWith("--")) return \`var(\${value})\` as T;
1085
+ return value;
1086
+ }
1087
+
1088
+ function maybeCssVarValues<O extends Record<string, unknown>>(obj: O): O {
1089
+ return Object.fromEntries(Object.entries(obj).map(([key, val]) => [key, maybeCssVar(val)])) as O;
1090
+ }
1091
+
1063
1092
  function invertMediaQuery(query: string): string {
1064
1093
  const screenPrefix = "@media screen and ";
1065
1094
  if (query.startsWith(screenPrefix)) {
@@ -1156,7 +1185,10 @@ class CssBuilder<T extends Properties> {
1156
1185
  add<K extends keyof Properties>(prop: K, value: Properties[K]): CssBuilder<T & { [U in K]: Properties[K] }>;
1157
1186
  add<K extends keyof Properties>(propOrProperties: K | Properties, value?: Properties[K]): CssBuilder<any> {
1158
1187
  if (!this.enabled) return this;
1159
- const newRules = typeof propOrProperties === "string" ? { [propOrProperties]: value } : propOrProperties;
1188
+ const newRules =
1189
+ typeof propOrProperties === "string"
1190
+ ? { [propOrProperties]: maybeCssVar(value) }
1191
+ : maybeCssVarValues(propOrProperties as Record<string, unknown>);
1160
1192
  if (typeof propOrProperties !== "string" && (newRules as any).$css) {
1161
1193
  throw new Error("add() received a Css expression — use with() to compose Css expressions");
1162
1194
  }
@@ -1171,7 +1203,7 @@ class CssBuilder<T extends Properties> {
1171
1203
  with(cssProp: Properties): CssBuilder<any> {
1172
1204
  if (!this.enabled) return this;
1173
1205
  const { $css, ...rest } = cssProp as any;
1174
- const filtered = omitUndefinedValues(rest);
1206
+ const filtered = maybeCssVarValues(omitUndefinedValues(rest));
1175
1207
  const rules = this.selector
1176
1208
  ? { ...this.rules, [this.selector]: { ...(this.rules as any)[this.selector], ...filtered } }
1177
1209
  : { ...this.rules, ...filtered };
@@ -1486,7 +1518,10 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1486
1518
  add<K extends keyof Properties>(propOrStyles: K | Properties, value?: Properties[K]): CssBuilder<any, S> {
1487
1519
  if (!this.enabled) return this;
1488
1520
 
1489
- const newRules = typeof propOrStyles === "string" ? { [propOrStyles]: value } : propOrStyles;
1521
+ const newRules =
1522
+ typeof propOrStyles === "string"
1523
+ ? { [propOrStyles]: maybeCssVar(value) }
1524
+ : maybeCssVarValues(propOrStyles as Record<string, unknown>);
1490
1525
  if (typeof propOrStyles !== "string" && (newRules as any).$css) {
1491
1526
  throw new Error("add() received a Css expression — use with() to compose Css expressions");
1492
1527
  }
@@ -1501,7 +1536,7 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1501
1536
  with(cssProp: Properties): CssBuilder<any, S> {
1502
1537
  if (!this.enabled) return this;
1503
1538
  const { $css, ...rest } = cssProp as any;
1504
- const filtered = omitUndefinedValues(rest);
1539
+ const filtered = maybeCssVarValues(omitUndefinedValues(rest));
1505
1540
  const rules = this.selector
1506
1541
  ? { ...this.rules, [this.selector]: { ...(this.rules as any)[this.selector], ...filtered } }
1507
1542
  : { ...this.rules, ...filtered };
@@ -1554,9 +1589,20 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1554
1589
  }
1555
1590
  }
1556
1591
 
1557
- /** Converts \`inc\` into pixels value with a \`px\` suffix. */
1592
+ /** Converts \`inc\` into a spacing length using \`--t-spacing\` (build-time atomic CSS). */
1558
1593
  export function maybeInc(inc: number | string): string {
1559
- return typeof inc === "string" ? inc : \`\${inc * ${increment}}px\`;
1594
+ return typeof inc === "string" ? inc : \`calc(var(${SPACING_CUSTOM_PROPERTY}) * \${inc})\`;
1595
+ }
1596
+
1597
+ /** Wraps \`--token\` custom property names as \`var(--token)\` for CSS property values. */
1598
+ export function maybeCssVar<T>(value: T): T {
1599
+ if (typeof value !== "string") return value;
1600
+ if (value.startsWith("--")) return \`var(\${value})\` as T;
1601
+ return value;
1602
+ }
1603
+
1604
+ function maybeCssVarValues<O extends Record<string, unknown>>(obj: O): O {
1605
+ return Object.fromEntries(Object.entries(obj).map(([key, val]) => [key, maybeCssVar(val)])) as O;
1560
1606
  }
1561
1607
 
1562
1608
  /** Converts \`inc\` into pixels. */
@@ -1695,10 +1741,20 @@ function condensedJson(mapping) {
1695
1741
  lines.push("}");
1696
1742
  return lines.join("\n");
1697
1743
  }
1744
+
1745
+ // src/css-custom-property.ts
1746
+ function maybeCssVar(value) {
1747
+ if (typeof value !== "string") return value;
1748
+ if (value.startsWith("--")) return `var(${value})`;
1749
+ return value;
1750
+ }
1698
1751
  export {
1752
+ SPACING_CUSTOM_PROPERTY,
1699
1753
  defaultSections,
1700
1754
  defineConfig,
1701
1755
  generate,
1756
+ incrementCssValue,
1757
+ maybeCssVar,
1702
1758
  newAliasesMethods,
1703
1759
  newCoreIncrementMethods,
1704
1760
  newIncrementMethods,
@@ -1707,7 +1763,9 @@ export {
1707
1763
  newParamMethod,
1708
1764
  newPxMethod,
1709
1765
  newSetCssVariablesMethod,
1766
+ rootSpacingPreludeCss,
1710
1767
  startWebCollection,
1711
- stopWebCollection
1768
+ stopWebCollection,
1769
+ tryParseIncrementCalcMultiplier
1712
1770
  };
1713
1771
  //# sourceMappingURL=index.js.map