@homebound/truss 2.23.0 → 2.24.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/build/index.d.ts CHANGED
@@ -263,12 +263,24 @@ type IncConfig = [string, Prop | Prop[]];
263
263
  declare function newIncrementMethods(config: Config, abbr: UtilityName, prop: Prop | Prop[], opts?: {
264
264
  auto?: boolean;
265
265
  }): 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
266
  declare function newCoreIncrementMethods(config: Config, abbr: UtilityName, props: Prop[]): UtilityMethod[];
272
267
  declare function newPxMethod(abbr: UtilityName, prop: Prop): UtilityMethod;
273
268
 
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 };
269
+ /**
270
+ * Web increment utilities use `--t-spacing` with `calc` (see generated `Css.ts` and the Vite plugin).
271
+ * Keep literals in one place so codegen, emitted CSS, and the transform stay aligned.
272
+ * `--t-spacing` must be set (e.g. `:root` prelude from `collectCss()` / mapping `increment`).
273
+ */
274
+ /** Custom property for increment-based spacing (web). */
275
+ declare const SPACING_CUSTOM_PROPERTY = "--t-spacing";
276
+ /** I.e. `calc(var(--t-spacing) * 3)` — requires prelude defining `--t-spacing`. */
277
+ declare function incrementCssValue(multiplier: number): string;
278
+ /**
279
+ * If `cssValue` is exactly `calc(var(--t-spacing) * k)` for this package's spacing property,
280
+ * returns the multiplier substring `k` (e.g. `"2"`, `"-1"`, `"2.5"`). Otherwise null.
281
+ */
282
+ declare function tryParseIncrementCalcMultiplier(cssValue: string): string | null;
283
+ /** Prepended to emitted Truss CSS; `incrementPx` comes from `truss-config` / `Css.json`. */
284
+ declare function rootSpacingPreludeCss(incrementPx: number): string;
285
+
286
+ 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
  }
@@ -1554,9 +1572,9 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1554
1572
  }
1555
1573
  }
1556
1574
 
1557
- /** Converts \`inc\` into pixels value with a \`px\` suffix. */
1575
+ /** Converts \`inc\` into a spacing length using \`--t-spacing\` (build-time atomic CSS). */
1558
1576
  export function maybeInc(inc: number | string): string {
1559
- return typeof inc === "string" ? inc : \`\${inc * ${increment}}px\`;
1577
+ return typeof inc === "string" ? inc : \`calc(var(${SPACING_CUSTOM_PROPERTY}) * \${inc})\`;
1560
1578
  }
1561
1579
 
1562
1580
  /** Converts \`inc\` into pixels. */
@@ -1696,9 +1714,11 @@ function condensedJson(mapping) {
1696
1714
  return lines.join("\n");
1697
1715
  }
1698
1716
  export {
1717
+ SPACING_CUSTOM_PROPERTY,
1699
1718
  defaultSections,
1700
1719
  defineConfig,
1701
1720
  generate,
1721
+ incrementCssValue,
1702
1722
  newAliasesMethods,
1703
1723
  newCoreIncrementMethods,
1704
1724
  newIncrementMethods,
@@ -1707,7 +1727,9 @@ export {
1707
1727
  newParamMethod,
1708
1728
  newPxMethod,
1709
1729
  newSetCssVariablesMethod,
1730
+ rootSpacingPreludeCss,
1710
1731
  startWebCollection,
1711
- stopWebCollection
1732
+ stopWebCollection,
1733
+ tryParseIncrementCalcMultiplier
1712
1734
  };
1713
1735
  //# sourceMappingURL=index.js.map