@pandacss/token-dictionary 0.33.0 → 0.34.1

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/dist/index.d.mts CHANGED
@@ -130,6 +130,15 @@ declare class TokenDictionary {
130
130
  filter(pattern: Partial<Token> | ((token: Token) => boolean)): Token[];
131
131
  addConditionalTokens(): this;
132
132
  expandTokenReferences(): this;
133
+ colorMix: (value: string, tokenFn: (path: string) => string) => {
134
+ invalid: boolean;
135
+ value: string;
136
+ color?: undefined;
137
+ } | {
138
+ invalid: boolean;
139
+ color: string;
140
+ value: string;
141
+ };
133
142
  /**
134
143
  * Expand token references to their CSS variable
135
144
  */
package/dist/index.d.ts CHANGED
@@ -130,6 +130,15 @@ declare class TokenDictionary {
130
130
  filter(pattern: Partial<Token> | ((token: Token) => boolean)): Token[];
131
131
  addConditionalTokens(): this;
132
132
  expandTokenReferences(): this;
133
+ colorMix: (value: string, tokenFn: (path: string) => string) => {
134
+ invalid: boolean;
135
+ value: string;
136
+ color?: undefined;
137
+ } | {
138
+ invalid: boolean;
139
+ color: string;
140
+ value: string;
141
+ };
133
142
  /**
134
143
  * Expand token references to their CSS variable
135
144
  */
package/dist/index.js CHANGED
@@ -586,6 +586,25 @@ var transformAssets = {
586
586
  return (0, import_ts_pattern2.match)(raw).with(import_ts_pattern2.P.string, (value) => value).with({ type: "url" }, ({ value }) => `url("${value}")`).with({ type: "svg" }, ({ value }) => `url("${svgToDataUri(value)}")`).exhaustive();
587
587
  }
588
588
  };
589
+ var transformColorMix = {
590
+ name: "tokens/color-mix",
591
+ match: (token) => {
592
+ return token.extensions.category === "colors" && token.value.includes("/");
593
+ },
594
+ transform(token, dict) {
595
+ return expandReferences(token.value, (path) => {
596
+ const tokenFn = (tokenPath) => {
597
+ const token2 = dict.getByName(tokenPath);
598
+ return token2?.extensions.varRef;
599
+ };
600
+ const mix = dict.colorMix(path, tokenFn);
601
+ if (mix.invalid) {
602
+ throw new Error("Invalid color mix at " + path + ": " + mix.value);
603
+ }
604
+ return mix.value;
605
+ });
606
+ }
607
+ };
589
608
  var addCssVariables = {
590
609
  type: "extensions",
591
610
  name: "tokens/css-var",
@@ -609,8 +628,23 @@ var addConditionalCssVariables = {
609
628
  if (!refs.length)
610
629
  return token.value;
611
630
  refs.forEach((ref) => {
612
- const variable = dictionary.formatCssVar(ref.split("."), { prefix, hash }).ref;
613
- token.value = token.value.replace(`{${ref}}`, variable);
631
+ if (!ref.includes("/")) {
632
+ const variable = dictionary.formatCssVar(ref.split("."), { prefix, hash }).ref;
633
+ token.value = token.value.replace(`{${ref}}`, variable);
634
+ return;
635
+ }
636
+ const expanded = expandReferences(token.value, (path) => {
637
+ const tokenFn = (tokenPath) => {
638
+ const token2 = dictionary.getByName(tokenPath);
639
+ return token2?.extensions.varRef;
640
+ };
641
+ const mix = dictionary.colorMix(path, tokenFn);
642
+ if (mix.invalid) {
643
+ throw new Error("Invalid color mix at " + path + ": " + mix.value);
644
+ }
645
+ return mix.value;
646
+ });
647
+ token.value = token.value.replace(`{${ref}}`, expanded);
614
648
  });
615
649
  return token.value;
616
650
  }
@@ -668,6 +702,8 @@ var transforms = [
668
702
  transformBorders,
669
703
  transformAssets,
670
704
  addCssVariables,
705
+ transformColorMix,
706
+ // depends on `addCssVariables`
671
707
  addConditionalCssVariables,
672
708
  addColorPalette
673
709
  ];
@@ -896,11 +932,40 @@ var TokenDictionary = class {
896
932
  });
897
933
  return this;
898
934
  }
935
+ colorMix = (value, tokenFn) => {
936
+ if (!value || typeof value !== "string")
937
+ return { invalid: true, value };
938
+ const [colorPath, rawOpacity] = value.split("/");
939
+ if (!colorPath || !rawOpacity) {
940
+ return { invalid: true, value: colorPath };
941
+ }
942
+ const colorToken = tokenFn(colorPath);
943
+ const opacityToken = this.getByName(`opacity.${rawOpacity}`)?.value;
944
+ if (!opacityToken && isNaN(Number(rawOpacity))) {
945
+ return { invalid: true, value: colorPath };
946
+ }
947
+ const percent = opacityToken ? Number(opacityToken) * 100 + "%" : `${rawOpacity}%`;
948
+ const color = colorToken ?? colorPath;
949
+ return {
950
+ invalid: false,
951
+ color,
952
+ value: `color-mix(in srgb, ${color} ${percent}, transparent)`
953
+ };
954
+ };
899
955
  /**
900
956
  * Expand token references to their CSS variable
901
957
  */
902
958
  expandReferenceInValue(value) {
903
- return expandReferences(value, (path) => this.view.get(path));
959
+ return expandReferences(value, (path) => {
960
+ if (path.includes("/")) {
961
+ const mix = this.colorMix(path, this.view.get.bind(this.view));
962
+ if (mix.invalid) {
963
+ throw new Error("Invalid color mix at " + path + ": " + mix.value);
964
+ }
965
+ return mix.value;
966
+ }
967
+ return this.view.get(path);
968
+ });
904
969
  }
905
970
  /**
906
971
  * Resolve token references to their actual raw value
package/dist/index.mjs CHANGED
@@ -566,6 +566,25 @@ var transformAssets = {
566
566
  return match(raw).with(P2.string, (value) => value).with({ type: "url" }, ({ value }) => `url("${value}")`).with({ type: "svg" }, ({ value }) => `url("${svgToDataUri(value)}")`).exhaustive();
567
567
  }
568
568
  };
569
+ var transformColorMix = {
570
+ name: "tokens/color-mix",
571
+ match: (token) => {
572
+ return token.extensions.category === "colors" && token.value.includes("/");
573
+ },
574
+ transform(token, dict) {
575
+ return expandReferences(token.value, (path) => {
576
+ const tokenFn = (tokenPath) => {
577
+ const token2 = dict.getByName(tokenPath);
578
+ return token2?.extensions.varRef;
579
+ };
580
+ const mix = dict.colorMix(path, tokenFn);
581
+ if (mix.invalid) {
582
+ throw new Error("Invalid color mix at " + path + ": " + mix.value);
583
+ }
584
+ return mix.value;
585
+ });
586
+ }
587
+ };
569
588
  var addCssVariables = {
570
589
  type: "extensions",
571
590
  name: "tokens/css-var",
@@ -589,8 +608,23 @@ var addConditionalCssVariables = {
589
608
  if (!refs.length)
590
609
  return token.value;
591
610
  refs.forEach((ref) => {
592
- const variable = dictionary.formatCssVar(ref.split("."), { prefix, hash }).ref;
593
- token.value = token.value.replace(`{${ref}}`, variable);
611
+ if (!ref.includes("/")) {
612
+ const variable = dictionary.formatCssVar(ref.split("."), { prefix, hash }).ref;
613
+ token.value = token.value.replace(`{${ref}}`, variable);
614
+ return;
615
+ }
616
+ const expanded = expandReferences(token.value, (path) => {
617
+ const tokenFn = (tokenPath) => {
618
+ const token2 = dictionary.getByName(tokenPath);
619
+ return token2?.extensions.varRef;
620
+ };
621
+ const mix = dictionary.colorMix(path, tokenFn);
622
+ if (mix.invalid) {
623
+ throw new Error("Invalid color mix at " + path + ": " + mix.value);
624
+ }
625
+ return mix.value;
626
+ });
627
+ token.value = token.value.replace(`{${ref}}`, expanded);
594
628
  });
595
629
  return token.value;
596
630
  }
@@ -648,6 +682,8 @@ var transforms = [
648
682
  transformBorders,
649
683
  transformAssets,
650
684
  addCssVariables,
685
+ transformColorMix,
686
+ // depends on `addCssVariables`
651
687
  addConditionalCssVariables,
652
688
  addColorPalette
653
689
  ];
@@ -876,11 +912,40 @@ var TokenDictionary = class {
876
912
  });
877
913
  return this;
878
914
  }
915
+ colorMix = (value, tokenFn) => {
916
+ if (!value || typeof value !== "string")
917
+ return { invalid: true, value };
918
+ const [colorPath, rawOpacity] = value.split("/");
919
+ if (!colorPath || !rawOpacity) {
920
+ return { invalid: true, value: colorPath };
921
+ }
922
+ const colorToken = tokenFn(colorPath);
923
+ const opacityToken = this.getByName(`opacity.${rawOpacity}`)?.value;
924
+ if (!opacityToken && isNaN(Number(rawOpacity))) {
925
+ return { invalid: true, value: colorPath };
926
+ }
927
+ const percent = opacityToken ? Number(opacityToken) * 100 + "%" : `${rawOpacity}%`;
928
+ const color = colorToken ?? colorPath;
929
+ return {
930
+ invalid: false,
931
+ color,
932
+ value: `color-mix(in srgb, ${color} ${percent}, transparent)`
933
+ };
934
+ };
879
935
  /**
880
936
  * Expand token references to their CSS variable
881
937
  */
882
938
  expandReferenceInValue(value) {
883
- return expandReferences(value, (path) => this.view.get(path));
939
+ return expandReferences(value, (path) => {
940
+ if (path.includes("/")) {
941
+ const mix = this.colorMix(path, this.view.get.bind(this.view));
942
+ if (mix.invalid) {
943
+ throw new Error("Invalid color mix at " + path + ": " + mix.value);
944
+ }
945
+ return mix.value;
946
+ }
947
+ return this.view.get(path);
948
+ });
884
949
  }
885
950
  /**
886
951
  * Resolve token references to their actual raw value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pandacss/token-dictionary",
3
- "version": "0.33.0",
3
+ "version": "0.34.1",
4
4
  "description": "Common error messages for css panda",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -33,9 +33,9 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "ts-pattern": "5.0.8",
36
- "@pandacss/logger": "^0.33.0",
37
- "@pandacss/shared": "0.33.0",
38
- "@pandacss/types": "0.33.0"
36
+ "@pandacss/logger": "^0.34.1",
37
+ "@pandacss/shared": "0.34.1",
38
+ "@pandacss/types": "0.34.1"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsup src/index.ts --format=esm,cjs --dts",