@homebound/truss 2.24.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`.
package/build/index.js CHANGED
@@ -1078,6 +1078,17 @@ type Opts<T> = {
1078
1078
  elseApplied: boolean,
1079
1079
  };
1080
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
+
1081
1092
  function invertMediaQuery(query: string): string {
1082
1093
  const screenPrefix = "@media screen and ";
1083
1094
  if (query.startsWith(screenPrefix)) {
@@ -1174,7 +1185,10 @@ class CssBuilder<T extends Properties> {
1174
1185
  add<K extends keyof Properties>(prop: K, value: Properties[K]): CssBuilder<T & { [U in K]: Properties[K] }>;
1175
1186
  add<K extends keyof Properties>(propOrProperties: K | Properties, value?: Properties[K]): CssBuilder<any> {
1176
1187
  if (!this.enabled) return this;
1177
- 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>);
1178
1192
  if (typeof propOrProperties !== "string" && (newRules as any).$css) {
1179
1193
  throw new Error("add() received a Css expression — use with() to compose Css expressions");
1180
1194
  }
@@ -1189,7 +1203,7 @@ class CssBuilder<T extends Properties> {
1189
1203
  with(cssProp: Properties): CssBuilder<any> {
1190
1204
  if (!this.enabled) return this;
1191
1205
  const { $css, ...rest } = cssProp as any;
1192
- const filtered = omitUndefinedValues(rest);
1206
+ const filtered = maybeCssVarValues(omitUndefinedValues(rest));
1193
1207
  const rules = this.selector
1194
1208
  ? { ...this.rules, [this.selector]: { ...(this.rules as any)[this.selector], ...filtered } }
1195
1209
  : { ...this.rules, ...filtered };
@@ -1504,7 +1518,10 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1504
1518
  add<K extends keyof Properties>(propOrStyles: K | Properties, value?: Properties[K]): CssBuilder<any, S> {
1505
1519
  if (!this.enabled) return this;
1506
1520
 
1507
- 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>);
1508
1525
  if (typeof propOrStyles !== "string" && (newRules as any).$css) {
1509
1526
  throw new Error("add() received a Css expression — use with() to compose Css expressions");
1510
1527
  }
@@ -1519,7 +1536,7 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1519
1536
  with(cssProp: Properties): CssBuilder<any, S> {
1520
1537
  if (!this.enabled) return this;
1521
1538
  const { $css, ...rest } = cssProp as any;
1522
- const filtered = omitUndefinedValues(rest);
1539
+ const filtered = maybeCssVarValues(omitUndefinedValues(rest));
1523
1540
  const rules = this.selector
1524
1541
  ? { ...this.rules, [this.selector]: { ...(this.rules as any)[this.selector], ...filtered } }
1525
1542
  : { ...this.rules, ...filtered };
@@ -1577,6 +1594,17 @@ export function maybeInc(inc: number | string): string {
1577
1594
  return typeof inc === "string" ? inc : \`calc(var(${SPACING_CUSTOM_PROPERTY}) * \${inc})\`;
1578
1595
  }
1579
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;
1606
+ }
1607
+
1580
1608
  /** Converts \`inc\` into pixels. */
1581
1609
  export function increment(inc: number): number {
1582
1610
  return inc * ${increment};
@@ -1713,12 +1741,20 @@ function condensedJson(mapping) {
1713
1741
  lines.push("}");
1714
1742
  return lines.join("\n");
1715
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
+ }
1716
1751
  export {
1717
1752
  SPACING_CUSTOM_PROPERTY,
1718
1753
  defaultSections,
1719
1754
  defineConfig,
1720
1755
  generate,
1721
1756
  incrementCssValue,
1757
+ maybeCssVar,
1722
1758
  newAliasesMethods,
1723
1759
  newCoreIncrementMethods,
1724
1760
  newIncrementMethods,