@homebound/truss 2.21.5 → 2.22.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.js CHANGED
@@ -922,6 +922,18 @@ var reactNativeSections = {
922
922
  spacing: spacing2
923
923
  };
924
924
 
925
+ // src/pseudo-selectors.ts
926
+ var TRUSS_PSEUDO_METHODS = {
927
+ onHover: ":hover",
928
+ onFocus: ":focus",
929
+ onFocusVisible: ":focus-visible",
930
+ onFocusWithin: ":focus-within",
931
+ onActive: ":active",
932
+ onDisabled: ":disabled",
933
+ ifFirstOfType: ":first-of-type",
934
+ ifLastOfType: ":last-of-type"
935
+ };
936
+
925
937
  // src/generate.ts
926
938
  var CssProperties = imp("Properties@csstype");
927
939
  var defaultTypeAliases = {
@@ -1249,6 +1261,12 @@ function generateWebCssBuilder(config, sections) {
1249
1261
  ...Object.entries(genBreakpointsMap).map(([name, value]) => ` ${name} = "${value}",`),
1250
1262
  `}`
1251
1263
  ];
1264
+ const pseudoGetterCode = Object.entries(TRUSS_PSEUDO_METHODS).map(([name, selector]) => {
1265
+ return code`
1266
+ get ${name}() {
1267
+ return this.newCss({ selector: ${quote(selector)} });
1268
+ }`;
1269
+ });
1252
1270
  const typographyType = code`
1253
1271
  export type ${def("Typography")} = ${Object.keys(fonts).map(quote).join(" | ")};
1254
1272
  `;
@@ -1257,7 +1275,7 @@ function generateWebCssBuilder(config, sections) {
1257
1275
  // See your project's \`truss-config.ts\` to make configuration changes (fonts, increments, etc).
1258
1276
  // Target: web (build-time plugin)
1259
1277
 
1260
- import { trussProps, useRuntimeStyle as _useRuntimeStyle } from "@homebound/truss/runtime";
1278
+ import { trussProps, useRuntimeStyle as _useRuntimeStyle, __invertTrussMediaQuery } from "@homebound/truss/runtime";
1261
1279
 
1262
1280
  /** Given a type X, and the user's proposed type T, only allow keys in X and nothing else. */
1263
1281
  export type Only<X, T> = X & Record<Exclude<keyof T, keyof X | "__kind">, never>;
@@ -1335,30 +1353,7 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1335
1353
  return this.rules as any;
1336
1354
  }
1337
1355
 
1338
- get onHover() {
1339
- return this.newCss({ selector: ":hover" });
1340
- }
1341
- get onFocus() {
1342
- return this.newCss({ selector: ":focus" });
1343
- }
1344
- get onFocusVisible() {
1345
- return this.newCss({ selector: ":focus-visible" });
1346
- }
1347
- get onFocusWithin() {
1348
- return this.newCss({ selector: ":focus-within" });
1349
- }
1350
- get onActive() {
1351
- return this.newCss({ selector: ":active" });
1352
- }
1353
- get onDisabled() {
1354
- return this.newCss({ selector: ":disabled" });
1355
- }
1356
- get ifFirstOfType() {
1357
- return this.newCss({ selector: ":first-of-type" });
1358
- }
1359
- get ifLastOfType() {
1360
- return this.newCss({ selector: ":last-of-type" });
1361
- }
1356
+ ${pseudoGetterCode}
1362
1357
 
1363
1358
  /** Marks this element as a default hover marker (for ancestor pseudo selectors). */
1364
1359
  get marker(): CssBuilder<T, S> {
@@ -1384,6 +1379,7 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1384
1379
  *
1385
1380
  * \`when(\":hover\")\` — same semantics as \`onHover\`
1386
1381
  * \`when(\":hover:not(:disabled)\")\` — hover only while enabled
1382
+ * \`when('[data-state="open"]')\` — match an element state attribute
1387
1383
  */
1388
1384
  when(selector: string): CssBuilder<T, S>;
1389
1385
  /**
@@ -1396,7 +1392,7 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1396
1392
  /**
1397
1393
  * Apply different styles for each selector in the object.
1398
1394
  *
1399
- * \`when({ ":hover": Css.blue.$, ":focus": Css.red.$ })\`
1395
+ * \`when({ ":hover": Css.blue.$, '[data-state="open"]': Css.red.$ })\`
1400
1396
  */
1401
1397
  when<W extends Record<string, Properties>>(selectors: W): CssBuilder<T & UnionToIntersection<W[keyof W]>, S>;
1402
1398
  when(
@@ -1434,7 +1430,7 @@ class CssBuilder<T extends Properties, S extends StyleKind = "buildtime"> {
1434
1430
  if (this.opts.elseApplied) {
1435
1431
  throw new Error("else was already called");
1436
1432
  }
1437
- return this.newCss({ selector: invertMediaQuery(this.selector), elseApplied: true });
1433
+ return this.newCss({ selector: __invertTrussMediaQuery(this.selector), elseApplied: true });
1438
1434
  }
1439
1435
  if (this.opts.elseApplied) {
1440
1436
  throw new Error("else was already called");
@@ -1576,29 +1572,6 @@ ${typeAliasCode}
1576
1572
 
1577
1573
  ${breakpointCode}
1578
1574
 
1579
- function invertMediaQuery(query: string): string {
1580
- const screenPrefix = "@media screen and ";
1581
- if (query.startsWith(screenPrefix)) {
1582
- const conditions = query.slice(screenPrefix.length).trim();
1583
- const rangeMatch = conditions.match(/^\(min-width: (\d+)px\) and \(max-width: (\d+)px\)$/);
1584
- if (rangeMatch) {
1585
- const min = Number(rangeMatch[1]);
1586
- const max = Number(rangeMatch[2]);
1587
- return \`@media screen and (max-width: \${min - 1}px), screen and (min-width: \${max + 1}px)\`;
1588
- }
1589
- const minMatch = conditions.match(/^\(min-width: (\d+)px\)$/);
1590
- if (minMatch) {
1591
- return \`@media screen and (max-width: \${Number(minMatch[1]) - 1}px)\`;
1592
- }
1593
- const maxMatch = conditions.match(/^\(max-width: (\d+)px\)$/);
1594
- if (maxMatch) {
1595
- return \`@media screen and (min-width: \${Number(maxMatch[1]) + 1}px)\`;
1596
- }
1597
- }
1598
- return query.replace("@media", "@media not");
1599
- }
1600
-
1601
-
1602
1575
  ${extras || ""}
1603
1576
  `;
1604
1577
  }