@pandacss/token-dictionary 0.39.1 → 0.40.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/dist/index.js CHANGED
@@ -32,7 +32,7 @@ __export(src_exports, {
32
32
  module.exports = __toCommonJS(src_exports);
33
33
 
34
34
  // src/dictionary.ts
35
- var import_shared5 = require("@pandacss/shared");
35
+ var import_shared6 = require("@pandacss/shared");
36
36
  var import_ts_pattern3 = require("ts-pattern");
37
37
 
38
38
  // src/is-composite.ts
@@ -718,12 +718,144 @@ var transforms = [
718
718
  addColorPalette
719
719
  ];
720
720
 
721
+ // src/expand-token-references.ts
722
+ var import_shared5 = require("@pandacss/shared");
723
+ var expandTokenReferences = (str, resolve) => {
724
+ let expanded = "";
725
+ let index = 0;
726
+ let state = "char";
727
+ let tokenPath = "";
728
+ let fallback = "";
729
+ const currentStates = [];
730
+ while (index < str.length) {
731
+ const char = str[index];
732
+ if (char === "{") {
733
+ const endIndex = str.indexOf("}", index);
734
+ if (endIndex === -1) {
735
+ break;
736
+ }
737
+ const path = str.slice(index + 1, endIndex);
738
+ const resolved = resolve(path);
739
+ expanded += resolved ?? path;
740
+ index = endIndex + 1;
741
+ continue;
742
+ }
743
+ if (state === "token") {
744
+ if (char === ",") {
745
+ if (str[index] === "") {
746
+ index++;
747
+ }
748
+ state = "fallback";
749
+ currentStates.push(state);
750
+ const resolved = resolve(tokenPath);
751
+ if (resolved?.endsWith(")")) {
752
+ expanded += resolved.slice(0, -1);
753
+ }
754
+ tokenPath = "";
755
+ fallback = "";
756
+ continue;
757
+ }
758
+ }
759
+ if (state === "fallback") {
760
+ const nextFallback = fallback + char;
761
+ if (nextFallback === ", var(") {
762
+ const innerEndIndex = cssVarParser(str.slice(index + 1));
763
+ const endIndex = innerEndIndex + index + 1;
764
+ const cssVar2 = str.slice(index + 1, endIndex);
765
+ if (endIndex === -1) {
766
+ break;
767
+ }
768
+ expanded += ", var(" + cssVar2 + ")";
769
+ index = endIndex + 1;
770
+ state = currentStates.pop() ?? state;
771
+ fallback = "";
772
+ continue;
773
+ }
774
+ }
775
+ if (state === "token" || state === "fallback") {
776
+ index++;
777
+ if (char === ")") {
778
+ state = currentStates.pop() ?? state ?? "char";
779
+ fallback += char;
780
+ const resolved = tokenPath ? resolve(tokenPath) ?? (0, import_shared5.esc)(tokenPath) : tokenPath;
781
+ if (fallback) {
782
+ fallback = fallback.slice(1).trim();
783
+ if (!fallback.startsWith("token(") && fallback.endsWith(")")) {
784
+ fallback = fallback.slice(0, -1);
785
+ }
786
+ if (fallback.includes("token(")) {
787
+ const parsed = expandTokenReferences(fallback, resolve);
788
+ if (parsed) {
789
+ fallback = parsed.slice(0, -1);
790
+ }
791
+ } else if (fallback) {
792
+ const resolvedFallback = resolve(fallback);
793
+ if (resolvedFallback) {
794
+ fallback = resolvedFallback;
795
+ }
796
+ }
797
+ }
798
+ const lastChar = expanded.at(-1);
799
+ if (fallback) {
800
+ if (lastChar?.trim()) {
801
+ expanded += resolved.slice(0, -1) + (", " + fallback + ")");
802
+ } else {
803
+ expanded += fallback;
804
+ }
805
+ } else {
806
+ expanded += resolved || ")";
807
+ }
808
+ tokenPath = "";
809
+ fallback = "";
810
+ state = "char";
811
+ continue;
812
+ }
813
+ if (state === "token") {
814
+ tokenPath += char;
815
+ }
816
+ if (state === "fallback") {
817
+ fallback += char;
818
+ }
819
+ continue;
820
+ }
821
+ const tokenIndex = str.indexOf("token(", index);
822
+ if (tokenIndex !== -1) {
823
+ const innerTokenIndex = tokenIndex + "token(".length;
824
+ expanded += str.slice(index, tokenIndex);
825
+ index = innerTokenIndex;
826
+ state = "token";
827
+ currentStates.push(state);
828
+ continue;
829
+ }
830
+ expanded += char;
831
+ index++;
832
+ }
833
+ return expanded;
834
+ };
835
+ var cssVarParser = (str) => {
836
+ let index = 0;
837
+ const openedParenthesises = ["("];
838
+ while (index < str.length) {
839
+ const char = str[index];
840
+ if (char === "(") {
841
+ openedParenthesises.push(char);
842
+ } else if (char === ")") {
843
+ openedParenthesises.pop();
844
+ if (openedParenthesises.length === 0) {
845
+ return index;
846
+ }
847
+ }
848
+ index++;
849
+ }
850
+ return index;
851
+ };
852
+
721
853
  // src/dictionary.ts
722
854
  function expandBreakpoints(breakpoints) {
723
855
  if (!breakpoints)
724
856
  return { breakpoints: {}, sizes: {} };
725
857
  return {
726
- breakpoints: (0, import_shared5.mapObject)(breakpoints, (value) => ({ value })),
858
+ breakpoints: (0, import_shared6.mapObject)(breakpoints, (value) => ({ value })),
727
859
  sizes: Object.fromEntries(Object.entries(breakpoints).map(([key, value]) => [`breakpoint-${key}`, { value }]))
728
860
  };
729
861
  }
@@ -756,11 +888,11 @@ var TokenDictionary = class {
756
888
  return this.byName.get(path);
757
889
  };
758
890
  formatTokenName = (path) => path.join(".");
759
- formatCssVar = (path, options) => (0, import_shared5.cssVar)(path.join("-"), options);
891
+ formatCssVar = (path, options) => (0, import_shared6.cssVar)(path.join("-"), options);
760
892
  registerTokens() {
761
893
  const { tokens = {}, semanticTokens = {}, breakpoints, themes = {} } = this.options;
762
894
  const breakpointTokens = expandBreakpoints(breakpoints);
763
- const computedTokens = (0, import_shared5.compact)({
895
+ const computedTokens = (0, import_shared6.compact)({
764
896
  ...tokens,
765
897
  breakpoints: breakpointTokens.breakpoints,
766
898
  sizes: {
@@ -790,7 +922,7 @@ var TokenDictionary = class {
790
922
  assertTokenFormat(token);
791
923
  const category = path[0];
792
924
  const name = this.formatTokenName(path);
793
- const normalizedToken = (0, import_shared5.isString)(token.value) || isCompositeTokenValue(token.value) ? { value: { base: token.value } } : token;
925
+ const normalizedToken = (0, import_shared6.isString)(token.value) || isCompositeTokenValue(token.value) ? { value: { base: token.value } } : token;
794
926
  const { value, ...restData } = normalizedToken;
795
927
  const node = new Token({
796
928
  ...restData,
@@ -808,7 +940,7 @@ var TokenDictionary = class {
808
940
  }
809
941
  return node;
810
942
  };
811
- (0, import_shared5.walkObject)(
943
+ (0, import_shared6.walkObject)(
812
944
  computedTokens,
813
945
  (token, path) => {
814
946
  const node = processToken(token, path);
@@ -816,7 +948,7 @@ var TokenDictionary = class {
816
948
  },
817
949
  { stop: isToken }
818
950
  );
819
- (0, import_shared5.walkObject)(
951
+ (0, import_shared6.walkObject)(
820
952
  semanticTokens,
821
953
  (token, path) => {
822
954
  const node = processSemantic(token, path);
@@ -825,8 +957,8 @@ var TokenDictionary = class {
825
957
  { stop: isToken }
826
958
  );
827
959
  Object.entries(themes).forEach(([theme, themeVariant]) => {
828
- const condName = "_theme" + (0, import_shared5.capitalize)(theme);
829
- (0, import_shared5.walkObject)(
960
+ const condName = "_theme" + (0, import_shared6.capitalize)(theme);
961
+ (0, import_shared6.walkObject)(
830
962
  themeVariant.tokens ?? {},
831
963
  (token, path) => {
832
964
  const themeToken = { value: { [condName]: token.value } };
@@ -836,7 +968,7 @@ var TokenDictionary = class {
836
968
  },
837
969
  { stop: isToken }
838
970
  );
839
- (0, import_shared5.walkObject)(
971
+ (0, import_shared6.walkObject)(
840
972
  themeVariant.semanticTokens ?? {},
841
973
  (token, path) => {
842
974
  const themeToken = { value: { [condName]: token.value } };
@@ -896,7 +1028,7 @@ var TokenDictionary = class {
896
1028
  }
897
1029
  if (token.extensions.conditions) {
898
1030
  const conditions = token.extensions.conditions;
899
- const transformedConditions = (0, import_shared5.walkObject)(conditions, (value) => exec({ value }), {
1031
+ const transformedConditions = (0, import_shared6.walkObject)(conditions, (value) => exec({ value }), {
900
1032
  stop: isCompositeTokenValue
901
1033
  });
902
1034
  token.setExtensions({
@@ -935,7 +1067,7 @@ var TokenDictionary = class {
935
1067
  return refs.map((ref) => this.getByName(ref)).filter(Boolean);
936
1068
  }
937
1069
  usesReference(value) {
938
- if (!(0, import_shared5.isString)(value))
1070
+ if (!(0, import_shared6.isString)(value))
939
1071
  return false;
940
1072
  return this.getReferences(value).length > 0;
941
1073
  }
@@ -1001,7 +1133,9 @@ var TokenDictionary = class {
1001
1133
  * Expand token references to their CSS variable
1002
1134
  */
1003
1135
  expandReferenceInValue(value) {
1004
- return expandReferences(value, (path) => {
1136
+ return expandTokenReferences(value, (path) => {
1137
+ if (!path)
1138
+ return;
1005
1139
  if (path.includes("/")) {
1006
1140
  const mix = this.colorMix(path, this.view.get.bind(this.view));
1007
1141
  if (mix.invalid) {
@@ -1009,7 +1143,10 @@ var TokenDictionary = class {
1009
1143
  }
1010
1144
  return mix.value;
1011
1145
  }
1012
- return this.view.get(path);
1146
+ const resolved = this.view.get(path);
1147
+ if (resolved)
1148
+ return resolved;
1149
+ return tokenPathRegex.test(path) ? (0, import_shared6.esc)(path) : path;
1013
1150
  });
1014
1151
  }
1015
1152
  /**
@@ -1092,10 +1229,10 @@ var TokenDictionaryView = class {
1092
1229
  nameByVar,
1093
1230
  json,
1094
1231
  valuesByCategory,
1095
- get: (0, import_shared5.memo)((path, fallback) => {
1232
+ get: (0, import_shared6.memo)((path, fallback) => {
1096
1233
  return flatValues.get(path) ?? fallback;
1097
1234
  }),
1098
- getCategoryValues: (0, import_shared5.memo)((category) => {
1235
+ getCategoryValues: (0, import_shared6.memo)((category) => {
1099
1236
  const result = json[category];
1100
1237
  if (result != null) {
1101
1238
  return result;
@@ -1187,6 +1324,7 @@ function replaceRootWithColorPalette(path, colorPaletteRoot) {
1187
1324
  path.splice(startIndex, 0, "colorPalette");
1188
1325
  return path;
1189
1326
  }
1327
+ var tokenPathRegex = /\w+\.\w+/;
1190
1328
  // Annotate the CommonJS export names for ESM import in node:
1191
1329
  0 && (module.exports = {
1192
1330
  Token,
package/dist/index.mjs CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  capitalize,
4
4
  compact,
5
5
  cssVar,
6
+ esc as esc3,
6
7
  isString as isString2,
7
8
  mapObject,
8
9
  memo,
@@ -693,6 +694,138 @@ var transforms = [
693
694
  addColorPalette
694
695
  ];
695
696
 
697
+ // src/expand-token-references.ts
698
+ import { esc as esc2 } from "@pandacss/shared";
699
+ var expandTokenReferences = (str, resolve) => {
700
+ let expanded = "";
701
+ let index = 0;
702
+ let state = "char";
703
+ let tokenPath = "";
704
+ let fallback = "";
705
+ const currentStates = [];
706
+ while (index < str.length) {
707
+ const char = str[index];
708
+ if (char === "{") {
709
+ const endIndex = str.indexOf("}", index);
710
+ if (endIndex === -1) {
711
+ break;
712
+ }
713
+ const path = str.slice(index + 1, endIndex);
714
+ const resolved = resolve(path);
715
+ expanded += resolved ?? path;
716
+ index = endIndex + 1;
717
+ continue;
718
+ }
719
+ if (state === "token") {
720
+ if (char === ",") {
721
+ if (str[index] === "") {
722
+ index++;
723
+ }
724
+ state = "fallback";
725
+ currentStates.push(state);
726
+ const resolved = resolve(tokenPath);
727
+ if (resolved?.endsWith(")")) {
728
+ expanded += resolved.slice(0, -1);
729
+ }
730
+ tokenPath = "";
731
+ fallback = "";
732
+ continue;
733
+ }
734
+ }
735
+ if (state === "fallback") {
736
+ const nextFallback = fallback + char;
737
+ if (nextFallback === ", var(") {
738
+ const innerEndIndex = cssVarParser(str.slice(index + 1));
739
+ const endIndex = innerEndIndex + index + 1;
740
+ const cssVar2 = str.slice(index + 1, endIndex);
741
+ if (endIndex === -1) {
742
+ break;
743
+ }
744
+ expanded += ", var(" + cssVar2 + ")";
745
+ index = endIndex + 1;
746
+ state = currentStates.pop() ?? state;
747
+ fallback = "";
748
+ continue;
749
+ }
750
+ }
751
+ if (state === "token" || state === "fallback") {
752
+ index++;
753
+ if (char === ")") {
754
+ state = currentStates.pop() ?? state ?? "char";
755
+ fallback += char;
756
+ const resolved = tokenPath ? resolve(tokenPath) ?? esc2(tokenPath) : tokenPath;
757
+ if (fallback) {
758
+ fallback = fallback.slice(1).trim();
759
+ if (!fallback.startsWith("token(") && fallback.endsWith(")")) {
760
+ fallback = fallback.slice(0, -1);
761
+ }
762
+ if (fallback.includes("token(")) {
763
+ const parsed = expandTokenReferences(fallback, resolve);
764
+ if (parsed) {
765
+ fallback = parsed.slice(0, -1);
766
+ }
767
+ } else if (fallback) {
768
+ const resolvedFallback = resolve(fallback);
769
+ if (resolvedFallback) {
770
+ fallback = resolvedFallback;
771
+ }
772
+ }
773
+ }
774
+ const lastChar = expanded.at(-1);
775
+ if (fallback) {
776
+ if (lastChar?.trim()) {
777
+ expanded += resolved.slice(0, -1) + (", " + fallback + ")");
778
+ } else {
779
+ expanded += fallback;
780
+ }
781
+ } else {
782
+ expanded += resolved || ")";
783
+ }
784
+ tokenPath = "";
785
+ fallback = "";
786
+ state = "char";
787
+ continue;
788
+ }
789
+ if (state === "token") {
790
+ tokenPath += char;
791
+ }
792
+ if (state === "fallback") {
793
+ fallback += char;
794
+ }
795
+ continue;
796
+ }
797
+ const tokenIndex = str.indexOf("token(", index);
798
+ if (tokenIndex !== -1) {
799
+ const innerTokenIndex = tokenIndex + "token(".length;
800
+ expanded += str.slice(index, tokenIndex);
801
+ index = innerTokenIndex;
802
+ state = "token";
803
+ currentStates.push(state);
804
+ continue;
805
+ }
806
+ expanded += char;
807
+ index++;
808
+ }
809
+ return expanded;
810
+ };
811
+ var cssVarParser = (str) => {
812
+ let index = 0;
813
+ const openedParenthesises = ["("];
814
+ while (index < str.length) {
815
+ const char = str[index];
816
+ if (char === "(") {
817
+ openedParenthesises.push(char);
818
+ } else if (char === ")") {
819
+ openedParenthesises.pop();
820
+ if (openedParenthesises.length === 0) {
821
+ return index;
822
+ }
823
+ }
824
+ index++;
825
+ }
826
+ return index;
827
+ };
828
+
696
829
  // src/dictionary.ts
697
830
  function expandBreakpoints(breakpoints) {
698
831
  if (!breakpoints)
@@ -976,7 +1109,9 @@ var TokenDictionary = class {
976
1109
  * Expand token references to their CSS variable
977
1110
  */
978
1111
  expandReferenceInValue(value) {
979
- return expandReferences(value, (path) => {
1112
+ return expandTokenReferences(value, (path) => {
1113
+ if (!path)
1114
+ return;
980
1115
  if (path.includes("/")) {
981
1116
  const mix = this.colorMix(path, this.view.get.bind(this.view));
982
1117
  if (mix.invalid) {
@@ -984,7 +1119,10 @@ var TokenDictionary = class {
984
1119
  }
985
1120
  return mix.value;
986
1121
  }
987
- return this.view.get(path);
1122
+ const resolved = this.view.get(path);
1123
+ if (resolved)
1124
+ return resolved;
1125
+ return tokenPathRegex.test(path) ? esc3(path) : path;
988
1126
  });
989
1127
  }
990
1128
  /**
@@ -1162,6 +1300,7 @@ function replaceRootWithColorPalette(path, colorPaletteRoot) {
1162
1300
  path.splice(startIndex, 0, "colorPalette");
1163
1301
  return path;
1164
1302
  }
1303
+ var tokenPathRegex = /\w+\.\w+/;
1165
1304
  export {
1166
1305
  Token,
1167
1306
  TokenDictionary,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pandacss/token-dictionary",
3
- "version": "0.39.1",
3
+ "version": "0.40.0",
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.39.1",
37
- "@pandacss/shared": "0.39.1",
38
- "@pandacss/types": "0.39.1"
36
+ "@pandacss/logger": "^0.40.0",
37
+ "@pandacss/shared": "0.40.0",
38
+ "@pandacss/types": "0.40.0"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsup src/index.ts --format=esm,cjs --dts",