@tamagui/create-theme 3.0.0-beta.807.1 → 3.0.0-beta.881.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/cjs/createThemes.cjs +2 -2
- package/dist/cjs/createThemes.native.cjs +2 -2
- package/dist/cjs/createThemes.native.js +2 -2
- package/dist/cjs/createThemes.native.js.map +1 -1
- package/dist/esm/createThemes.mjs +2 -2
- package/dist/esm/createThemes.mjs.map +1 -1
- package/dist/esm/createThemes.native.js +2 -2
- package/dist/esm/createThemes.native.js.map +1 -1
- package/package.json +2 -2
- package/src/createThemes.tsx +2 -6
- package/types/createThemes.d.ts.map +1 -1
|
@@ -23,7 +23,7 @@ __export(createThemes_exports, { createThemes: () => createThemes });
|
|
|
23
23
|
module.exports = __toCommonJS(createThemes_exports);
|
|
24
24
|
function createThemes(tokens, tree, options) {
|
|
25
25
|
const rootKeys = Object.keys(tree).filter((key) => key !== "children");
|
|
26
|
-
if (rootKeys.length !== 2 || !
|
|
26
|
+
if (rootKeys.length !== 2 || !("light" in tree) || !("dark" in tree)) {
|
|
27
27
|
throw new Error("createThemes tree must have exactly the roots \"light\" and \"dark\"");
|
|
28
28
|
}
|
|
29
29
|
const themes = {};
|
|
@@ -86,7 +86,7 @@ function createThemes(tokens, tree, options) {
|
|
|
86
86
|
if (!childSet) continue;
|
|
87
87
|
for (const childName in childSet) {
|
|
88
88
|
const fullName = `${name}_${childName}`;
|
|
89
|
-
if (
|
|
89
|
+
if (fullName in themes) {
|
|
90
90
|
throw new Error(`Duplicate generated theme name "${fullName}"`);
|
|
91
91
|
}
|
|
92
92
|
add(fullName, childSet[childName], recipe);
|
|
@@ -27,7 +27,7 @@ function createThemes(tokens, tree, options) {
|
|
|
27
27
|
var rootKeys = Object.keys(tree).filter(function(key) {
|
|
28
28
|
return key !== "children";
|
|
29
29
|
});
|
|
30
|
-
if (rootKeys.length !== 2 || !
|
|
30
|
+
if (rootKeys.length !== 2 || !("light" in tree) || !("dark" in tree)) {
|
|
31
31
|
throw new Error("createThemes tree must have exactly the roots \"light\" and \"dark\"");
|
|
32
32
|
}
|
|
33
33
|
var themes = {};
|
|
@@ -98,7 +98,7 @@ function createThemes(tokens, tree, options) {
|
|
|
98
98
|
if (!childSet) continue;
|
|
99
99
|
for (var childName in childSet) {
|
|
100
100
|
var fullName = `${name}_${childName}`;
|
|
101
|
-
if (
|
|
101
|
+
if (fullName in themes) {
|
|
102
102
|
throw new Error(`Duplicate generated theme name "${fullName}"`);
|
|
103
103
|
}
|
|
104
104
|
add(fullName, childSet[childName], recipe);
|
|
@@ -27,7 +27,7 @@ function createThemes(tokens, tree, options) {
|
|
|
27
27
|
var rootKeys = Object.keys(tree).filter(function(key) {
|
|
28
28
|
return key !== "children";
|
|
29
29
|
});
|
|
30
|
-
if (rootKeys.length !== 2 || !
|
|
30
|
+
if (rootKeys.length !== 2 || !("light" in tree) || !("dark" in tree)) {
|
|
31
31
|
throw new Error("createThemes tree must have exactly the roots \"light\" and \"dark\"");
|
|
32
32
|
}
|
|
33
33
|
var themes = {};
|
|
@@ -98,7 +98,7 @@ function createThemes(tokens, tree, options) {
|
|
|
98
98
|
if (!childSet) continue;
|
|
99
99
|
for (var childName in childSet) {
|
|
100
100
|
var fullName = `${name}_${childName}`;
|
|
101
|
-
if (
|
|
101
|
+
if (fullName in themes) {
|
|
102
102
|
throw new Error(`Duplicate generated theme name "${fullName}"`);
|
|
103
103
|
}
|
|
104
104
|
add(fullName, childSet[childName], recipe);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThemes.native.js","names":[],"sources":["cjs/createThemes.native.js"],"sourcesContent":["\"use strict\";\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\nvar createThemes_exports = {};\n__export(createThemes_exports, {\n createThemes: () => createThemes\n});\nmodule.exports = __toCommonJS(createThemes_exports);\nfunction createThemes(tokens, tree, options) {\n var rootKeys = Object.keys(tree).filter(function(key) {\n return key !== \"children\";\n });\n if (rootKeys.length !== 2 || !
|
|
1
|
+
{"version":3,"file":"createThemes.native.js","names":[],"sources":["cjs/createThemes.native.js"],"sourcesContent":["\"use strict\";\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\nvar createThemes_exports = {};\n__export(createThemes_exports, {\n createThemes: () => createThemes\n});\nmodule.exports = __toCommonJS(createThemes_exports);\nfunction createThemes(tokens, tree, options) {\n var rootKeys = Object.keys(tree).filter(function(key) {\n return key !== \"children\";\n });\n if (rootKeys.length !== 2 || !(\"light\" in tree) || !(\"dark\" in tree)) {\n throw new Error('createThemes tree must have exactly the roots \"light\" and \"dark\"');\n }\n var themes = {};\n var deduped = /* @__PURE__ */ new Map();\n var add = function(name, definition, parent, inheritedChildren) {\n var _options_getTheme;\n var resolvedDefinition = typeof definition === \"function\" ? definition({\n parent\n }) : definition;\n if (resolvedDefinition === null) return;\n var { values, children, ...recipeFields } = resolvedDefinition;\n var recipe = {\n ...parent,\n ...recipeFields\n };\n var unresolved = {\n ...options === null || options === void 0 ? void 0 : (_options_getTheme = options.getTheme) === null || _options_getTheme === void 0 ? void 0 : _options_getTheme.call(options, {\n recipe,\n name,\n tokens\n }),\n ...values\n };\n var resolved = {};\n for (var key in unresolved) {\n var value = unresolved[key];\n var token = tokens.color[value];\n if (token !== void 0) {\n resolved[key] = token;\n continue;\n }\n var isColor = value === \"transparent\" || /^#(?:[\\da-f]{3,4}|[\\da-f]{6}|[\\da-f]{8})$/i.test(value) || /^(?:rgb|rgba|hsl|hsla)\\(\\s*[^)]+\\)$/i.test(value);\n if (isColor) {\n resolved[key] = value;\n continue;\n }\n var nearest = \"\";\n var nearestDistance = Number.POSITIVE_INFINITY;\n for (var tokenName in tokens.color) {\n var previous = Array.from({\n length: tokenName.length + 1\n }, function(_, index) {\n return index;\n });\n for (var valueIndex = 0; valueIndex < value.length; valueIndex++) {\n var diagonal = previous[0];\n previous[0] = valueIndex + 1;\n for (var tokenIndex = 0; tokenIndex < tokenName.length; tokenIndex++) {\n var above = previous[tokenIndex + 1];\n previous[tokenIndex + 1] = Math.min(previous[tokenIndex] + 1, above + 1, diagonal + (value[valueIndex] === tokenName[tokenIndex] ? 0 : 1));\n diagonal = above;\n }\n }\n var distance = previous[tokenName.length];\n if (distance < nearestDistance) {\n nearest = tokenName;\n nearestDistance = distance;\n }\n }\n throw new Error(`Invalid color value \"${value}\" for theme \"${name}\" key \"${key}\".${nearest ? ` Did you mean token \"${nearest}\"?` : \"\"}`);\n }\n var identity = JSON.stringify(Object.keys(resolved).sort().map(function(key2) {\n return [\n key2,\n resolved[key2]\n ];\n }));\n themes[name] = deduped.get(identity) || resolved;\n deduped.set(identity, themes[name]);\n var childSets = [\n inheritedChildren,\n children\n ];\n var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;\n try {\n for (var _iterator = childSets[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {\n var childSet = _step.value;\n if (!childSet) continue;\n for (var childName in childSet) {\n var fullName = `${name}_${childName}`;\n if (fullName in themes) {\n throw new Error(`Duplicate generated theme name \"${fullName}\"`);\n }\n add(fullName, childSet[childName], recipe);\n }\n }\n } catch (err) {\n _didIteratorError = true;\n _iteratorError = err;\n } finally {\n try {\n if (!_iteratorNormalCompletion && _iterator.return != null) {\n _iterator.return();\n }\n } finally {\n if (_didIteratorError) {\n throw _iteratorError;\n }\n }\n }\n };\n add(\"light\", tree.light, {}, tree.children);\n add(\"dark\", tree.dark, {}, tree.children);\n return themes;\n}\n//# sourceMappingURL=createThemes.js.map\n"],"mappings":";;;AACA,IAAI,YAAY,OAAO;AACvB,IAAI,mBAAmB,OAAO;AAC9B,IAAI,oBAAoB,OAAO;AAC/B,IAAI,eAAe,OAAO,UAAU;AACpC,IAAI,YAAY,QAAQ,QAAQ;CAC9B,KAAK,IAAI,QAAQ,KACf,UAAU,QAAQ,MAAM;EAAE,KAAK,IAAI;EAAO,YAAY;CAAK,CAAC;AAChE;AACA,IAAI,eAAe,IAAI,MAAM,QAAQ,SAAS;CAC5C,IAAI,QAAQ,OAAO,SAAS,YAAY,OAAO,SAAS,YAAY;EAClE,KAAK,IAAI,OAAO,kBAAkB,IAAI,GACpC,IAAI,CAAC,aAAa,KAAK,IAAI,GAAG,KAAK,QAAQ,QACzC,UAAU,IAAI,KAAK;GAAE,WAAW,KAAK;GAAM,YAAY,EAAE,OAAO,iBAAiB,MAAM,GAAG,MAAM,KAAK;EAAW,CAAC;CACvH;CACA,OAAO;AACT;AACA,IAAI,gBAAgB,QAAQ,YAAY,UAAU,CAAC,GAAG,cAAc,EAAE,OAAO,KAAK,CAAC,GAAG,GAAG;AACzF,I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,mCAAmC,SAAS,EAAE;KAChE;KACA,IAAI,UAAU,SAAS,YAAY,MAAM;IAC3C;GACF;EACF,SAAS,KAAK;GACZ,oBAAoB;GACpB,iBAAiB;EACnB,UAAU;GACR,IAAI;IACF,IAAI,CAAC,6BAA6B,UAAU,UAAU,MAAM;KAC1D,UAAU,OAAO;IACnB;GACF,UAAU;IACR,IAAI,mBAAmB;KACrB,MAAM;IACR;GACF;EACF;CACF;CACA,IAAI,SAAS,KAAK,OAAO,CAAC,GAAG,KAAK,QAAQ;CAC1C,IAAI,QAAQ,KAAK,MAAM,CAAC,GAAG,KAAK,QAAQ;CACxC,OAAO;AACT"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
function createThemes(tokens, tree, options) {
|
|
2
2
|
const rootKeys = Object.keys(tree).filter((key) => key !== "children");
|
|
3
|
-
if (rootKeys.length !== 2 || !
|
|
3
|
+
if (rootKeys.length !== 2 || !("light" in tree) || !("dark" in tree)) {
|
|
4
4
|
throw new Error("createThemes tree must have exactly the roots \"light\" and \"dark\"");
|
|
5
5
|
}
|
|
6
6
|
const themes = {};
|
|
@@ -63,7 +63,7 @@ function createThemes(tokens, tree, options) {
|
|
|
63
63
|
if (!childSet) continue;
|
|
64
64
|
for (const childName in childSet) {
|
|
65
65
|
const fullName = `${name}_${childName}`;
|
|
66
|
-
if (
|
|
66
|
+
if (fullName in themes) {
|
|
67
67
|
throw new Error(`Duplicate generated theme name "${fullName}"`);
|
|
68
68
|
}
|
|
69
69
|
add(fullName, childSet[childName], recipe);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThemes.js","names":[],"sources":["esm/createThemes.js"],"sourcesContent":["function createThemes(tokens, tree, options) {\n const rootKeys = Object.keys(tree).filter((key) => key !== \"children\");\n if (rootKeys.length !== 2 || !
|
|
1
|
+
{"version":3,"file":"createThemes.js","names":[],"sources":["esm/createThemes.js"],"sourcesContent":["function createThemes(tokens, tree, options) {\n const rootKeys = Object.keys(tree).filter((key) => key !== \"children\");\n if (rootKeys.length !== 2 || !(\"light\" in tree) || !(\"dark\" in tree)) {\n throw new Error('createThemes tree must have exactly the roots \"light\" and \"dark\"');\n }\n const themes = {};\n const deduped = /* @__PURE__ */ new Map();\n const add = (name, definition, parent, inheritedChildren) => {\n const resolvedDefinition = typeof definition === \"function\" ? definition({ parent }) : definition;\n if (resolvedDefinition === null) return;\n const { values, children, ...recipeFields } = resolvedDefinition;\n const recipe = { ...parent, ...recipeFields };\n const unresolved = {\n ...options?.getTheme?.({ recipe, name, tokens }),\n ...values\n };\n const resolved = {};\n for (const key in unresolved) {\n const value = unresolved[key];\n const token = tokens.color[value];\n if (token !== void 0) {\n resolved[key] = token;\n continue;\n }\n const isColor = value === \"transparent\" || /^#(?:[\\da-f]{3,4}|[\\da-f]{6}|[\\da-f]{8})$/i.test(value) || /^(?:rgb|rgba|hsl|hsla)\\(\\s*[^)]+\\)$/i.test(value);\n if (isColor) {\n resolved[key] = value;\n continue;\n }\n let nearest = \"\";\n let nearestDistance = Number.POSITIVE_INFINITY;\n for (const tokenName in tokens.color) {\n const previous = Array.from({ length: tokenName.length + 1 }, (_, index) => index);\n for (let valueIndex = 0; valueIndex < value.length; valueIndex++) {\n let diagonal = previous[0];\n previous[0] = valueIndex + 1;\n for (let tokenIndex = 0; tokenIndex < tokenName.length; tokenIndex++) {\n const above = previous[tokenIndex + 1];\n previous[tokenIndex + 1] = Math.min(\n previous[tokenIndex] + 1,\n above + 1,\n diagonal + (value[valueIndex] === tokenName[tokenIndex] ? 0 : 1)\n );\n diagonal = above;\n }\n }\n const distance = previous[tokenName.length];\n if (distance < nearestDistance) {\n nearest = tokenName;\n nearestDistance = distance;\n }\n }\n throw new Error(\n `Invalid color value \"${value}\" for theme \"${name}\" key \"${key}\".${nearest ? ` Did you mean token \"${nearest}\"?` : \"\"}`\n );\n }\n const identity = JSON.stringify(\n Object.keys(resolved).sort().map((key) => [key, resolved[key]])\n );\n themes[name] = deduped.get(identity) || resolved;\n deduped.set(identity, themes[name]);\n const childSets = [inheritedChildren, children];\n for (const childSet of childSets) {\n if (!childSet) continue;\n for (const childName in childSet) {\n const fullName = `${name}_${childName}`;\n if (fullName in themes) {\n throw new Error(`Duplicate generated theme name \"${fullName}\"`);\n }\n add(fullName, childSet[childName], recipe);\n }\n }\n };\n add(\"light\", tree.light, {}, tree.children);\n add(\"dark\", tree.dark, {}, tree.children);\n return themes;\n}\nexport {\n createThemes\n};\n//# sourceMappingURL=createThemes.js.map\n"],"mappings":";AAAA,SAAS,aAAa,QAAQ,MAAM,SAAS;CAC3C,MAAM,WAAW,OAAO,KAAK,IAAI,CAAC,CAAC,QAAQ,QAAQ,QAAQ,UAAU;CACrE,IAAI,SAAS,WAAW,KAAK,EAAE,WAAW,SAAS,EAAE,UAAU,OAAO;EACpE,MAAM,IAAI,MAAM,sEAAkE;CACpF;CACA,MAAM,SAAS,CAAC;CAChB,MAAM,0BAA0B,IAAI,IAAI;CACxC,MAAM,OAAO,MAAM,YAAY,QAAQ,sBAAsB;EAC3D,MAAM,qBAAqB,OAAO,eAAe,aAAa,WAAW,EAAE,OAAO,CAAC,IAAI;EACvF,IAAI,uBAAuB,MAAM;EACjC,MAAM,EAAE,QAAQ,UAAU,GAAG,iBAAiB;EAC9C,MAAM,SAAS;GAAE,GAAG;GAAQ,GAAG;EAAa;EAC5C,MAAM,aAAa;GACjB,GAAG,SAAS,WAAW;IAAE;IAAQ;IAAM;GAAO,CAAC;GAC/C,GAAG;EACL;EACA,MAAM,WAAW,CAAC;EAClB,KAAK,MAAM,OAAO,YAAY;GAC5B,MAAM,QAAQ,WAAW;GACzB,MAAM,QAAQ,OAAO,MAAM;GAC3B,IAAI,UAAU,KAAK,GAAG;IACpB,SAAS,OAAO;IAChB;GACF;GACA,MAAM,UAAU,UAAU,iBAAiB,6CAA6C,KAAK,KAAK,KAAK,uCAAuC,KAAK,KAAK;GACxJ,IAAI,SAAS;IACX,SAAS,OAAO;IAChB;GACF;GACA,IAAI,UAAU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AI,QAAQ,KAAK,MAAM,CAAC,GAAG,KAAK,QAAQ;CACxC,OAAO;AACT"}
|
|
@@ -2,7 +2,7 @@ function createThemes(tokens, tree, options) {
|
|
|
2
2
|
var rootKeys = Object.keys(tree).filter(function(key) {
|
|
3
3
|
return key !== "children";
|
|
4
4
|
});
|
|
5
|
-
if (rootKeys.length !== 2 || !
|
|
5
|
+
if (rootKeys.length !== 2 || !("light" in tree) || !("dark" in tree)) {
|
|
6
6
|
throw new Error("createThemes tree must have exactly the roots \"light\" and \"dark\"");
|
|
7
7
|
}
|
|
8
8
|
var themes = {};
|
|
@@ -73,7 +73,7 @@ function createThemes(tokens, tree, options) {
|
|
|
73
73
|
if (!childSet) continue;
|
|
74
74
|
for (var childName in childSet) {
|
|
75
75
|
var fullName = `${name}_${childName}`;
|
|
76
|
-
if (
|
|
76
|
+
if (fullName in themes) {
|
|
77
77
|
throw new Error(`Duplicate generated theme name "${fullName}"`);
|
|
78
78
|
}
|
|
79
79
|
add(fullName, childSet[childName], recipe);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThemes.native.js","names":[],"sources":["esm/createThemes.native.js"],"sourcesContent":["function createThemes(tokens, tree, options) {\n var rootKeys = Object.keys(tree).filter(function(key) {\n return key !== \"children\";\n });\n if (rootKeys.length !== 2 || !
|
|
1
|
+
{"version":3,"file":"createThemes.native.js","names":[],"sources":["esm/createThemes.native.js"],"sourcesContent":["function createThemes(tokens, tree, options) {\n var rootKeys = Object.keys(tree).filter(function(key) {\n return key !== \"children\";\n });\n if (rootKeys.length !== 2 || !(\"light\" in tree) || !(\"dark\" in tree)) {\n throw new Error('createThemes tree must have exactly the roots \"light\" and \"dark\"');\n }\n var themes = {};\n var deduped = /* @__PURE__ */ new Map();\n var add = function(name, definition, parent, inheritedChildren) {\n var _options_getTheme;\n var resolvedDefinition = typeof definition === \"function\" ? definition({\n parent\n }) : definition;\n if (resolvedDefinition === null) return;\n var { values, children, ...recipeFields } = resolvedDefinition;\n var recipe = {\n ...parent,\n ...recipeFields\n };\n var unresolved = {\n ...options === null || options === void 0 ? void 0 : (_options_getTheme = options.getTheme) === null || _options_getTheme === void 0 ? void 0 : _options_getTheme.call(options, {\n recipe,\n name,\n tokens\n }),\n ...values\n };\n var resolved = {};\n for (var key in unresolved) {\n var value = unresolved[key];\n var token = tokens.color[value];\n if (token !== void 0) {\n resolved[key] = token;\n continue;\n }\n var isColor = value === \"transparent\" || /^#(?:[\\da-f]{3,4}|[\\da-f]{6}|[\\da-f]{8})$/i.test(value) || /^(?:rgb|rgba|hsl|hsla)\\(\\s*[^)]+\\)$/i.test(value);\n if (isColor) {\n resolved[key] = value;\n continue;\n }\n var nearest = \"\";\n var nearestDistance = Number.POSITIVE_INFINITY;\n for (var tokenName in tokens.color) {\n var previous = Array.from({\n length: tokenName.length + 1\n }, function(_, index) {\n return index;\n });\n for (var valueIndex = 0; valueIndex < value.length; valueIndex++) {\n var diagonal = previous[0];\n previous[0] = valueIndex + 1;\n for (var tokenIndex = 0; tokenIndex < tokenName.length; tokenIndex++) {\n var above = previous[tokenIndex + 1];\n previous[tokenIndex + 1] = Math.min(previous[tokenIndex] + 1, above + 1, diagonal + (value[valueIndex] === tokenName[tokenIndex] ? 0 : 1));\n diagonal = above;\n }\n }\n var distance = previous[tokenName.length];\n if (distance < nearestDistance) {\n nearest = tokenName;\n nearestDistance = distance;\n }\n }\n throw new Error(`Invalid color value \"${value}\" for theme \"${name}\" key \"${key}\".${nearest ? ` Did you mean token \"${nearest}\"?` : \"\"}`);\n }\n var identity = JSON.stringify(Object.keys(resolved).sort().map(function(key2) {\n return [\n key2,\n resolved[key2]\n ];\n }));\n themes[name] = deduped.get(identity) || resolved;\n deduped.set(identity, themes[name]);\n var childSets = [\n inheritedChildren,\n children\n ];\n var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;\n try {\n for (var _iterator = childSets[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {\n var childSet = _step.value;\n if (!childSet) continue;\n for (var childName in childSet) {\n var fullName = `${name}_${childName}`;\n if (fullName in themes) {\n throw new Error(`Duplicate generated theme name \"${fullName}\"`);\n }\n add(fullName, childSet[childName], recipe);\n }\n }\n } catch (err) {\n _didIteratorError = true;\n _iteratorError = err;\n } finally {\n try {\n if (!_iteratorNormalCompletion && _iterator.return != null) {\n _iterator.return();\n }\n } finally {\n if (_didIteratorError) {\n throw _iteratorError;\n }\n }\n }\n };\n add(\"light\", tree.light, {}, tree.children);\n add(\"dark\", tree.dark, {}, tree.children);\n return themes;\n}\nexport {\n createThemes\n};\n//# sourceMappingURL=createThemes.js.map\n"],"mappings":";AAAA,SAAS,aAAa,QAAQ,MAAM,SAAS;CAC3C,IAAI,WAAW,OAAO,KAAK,IAAI,CAAC,CAAC,OAAO,SAAS,KAAK;EACpD,OAAO,QAAQ;CACjB,CAAC;CACD,IAAI,SAAS,WAAW,KAAK,EAAE,WAAW,SAAS,EAAE,UAAU,OAAO;EACpE,MAAM,IAAI,MAAM,sEAAkE;CACpF;CACA,IAAI,SAAS,CAAC;CACd,IAAI,0BAA0B,IAAI,IAAI;CACtC,IAAI,MAAM,SAAS,MAAM,YAAY,QAAQ,mBAAmB;EAC9D,IAAI;EACJ,IAAI,qBAAqB,OAAO,eAAe,aAAa,WAAW,EACrE,OACF,CAAC,IAAI;EACL,IAAI,uBAAuB,MAAM;EACjC,IAAI,EAAE,QAAQ,UAAU,GAAG,iBAAiB;EAC5C,IAAI,SAAS;GACX,GAAG;GACH,GAAG;EACL;EACA,IAAI,aAAa;GACf,GAAG,YAAY,QAAQ,YAAY,KAAK,IAAI,KAAK,KAAK,oBAAoB,QAAQ,cAAc,QAAQ,sBAAsB,KAAK,IAAI,KAAK,IAAI,kBAAkB,KAAK,SAAS;IAC9K;IACA;IACA;GACF,CAAC;GACD,GAAG;EACL;EACA,IAAI,WAAW,CAAC;EAChB,KAAK,IAAI,OAAO,YAAY;GAC1B,IA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oBAAoB,OAAO,iBAAiB,KAAK;EACvF,IAAI;GACF,KAAK,IAAI,YAAY,UAAU,OAAO,SAAS,CAAC,GAAG,OAAO,EAAE,6BAA6B,QAAQ,UAAU,KAAK,EAAC,CAAE,OAAO,4BAA4B,MAAM;IAC1J,IAAI,WAAW,MAAM;IACrB,IAAI,CAAC,UAAU;IACf,KAAK,IAAI,aAAa,UAAU;KAC9B,IAAI,WAAW,GAAG,KAAK,GAAG;KAC1B,IAAI,YAAY,QAAQ;MACtB,MAAM,IAAI,MAAM,mCAAmC,SAAS,EAAE;KAChE;KACA,IAAI,UAAU,SAAS,YAAY,MAAM;IAC3C;GACF;EACF,SAAS,KAAK;GACZ,oBAAoB;GACpB,iBAAiB;EACnB,UAAU;GACR,IAAI;IACF,IAAI,CAAC,6BAA6B,UAAU,UAAU,MAAM;KAC1D,UAAU,OAAO;IACnB;GACF,UAAU;IACR,IAAI,mBAAmB;KACrB,MAAM;IACR;GACF;EACF;CACF;CACA,IAAI,SAAS,KAAK,OAAO,CAAC,GAAG,KAAK,QAAQ;CAC1C,IAAI,QAAQ,KAAK,MAAM,CAAC,GAAG,KAAK,QAAQ;CACxC,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/create-theme",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.881.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"src",
|
|
6
6
|
"types",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"clean:build": "tamagui-build clean:build"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@tamagui/build": "3.0.0-beta.
|
|
38
|
+
"@tamagui/build": "3.0.0-beta.881.1",
|
|
39
39
|
"vitest": "4.1.0"
|
|
40
40
|
},
|
|
41
41
|
"repository": {
|
package/src/createThemes.tsx
CHANGED
|
@@ -225,11 +225,7 @@ export function createThemes(
|
|
|
225
225
|
}
|
|
226
226
|
): Record<string, Record<string, string>> {
|
|
227
227
|
const rootKeys = Object.keys(tree).filter((key) => key !== 'children')
|
|
228
|
-
if (
|
|
229
|
-
rootKeys.length !== 2 ||
|
|
230
|
-
!Object.prototype.hasOwnProperty.call(tree, 'light') ||
|
|
231
|
-
!Object.prototype.hasOwnProperty.call(tree, 'dark')
|
|
232
|
-
) {
|
|
228
|
+
if (rootKeys.length !== 2 || !('light' in tree) || !('dark' in tree)) {
|
|
233
229
|
throw new Error('createThemes tree must have exactly the roots "light" and "dark"')
|
|
234
230
|
}
|
|
235
231
|
|
|
@@ -313,7 +309,7 @@ export function createThemes(
|
|
|
313
309
|
if (!childSet) continue
|
|
314
310
|
for (const childName in childSet) {
|
|
315
311
|
const fullName = `${name}_${childName}`
|
|
316
|
-
if (
|
|
312
|
+
if (fullName in themes) {
|
|
317
313
|
throw new Error(`Duplicate generated theme name "${fullName}"`)
|
|
318
314
|
}
|
|
319
315
|
add(fullName, childSet[childName]!, recipe)
|
|
@@ -6,6 +6,6 @@
|
|
|
6
6
|
],
|
|
7
7
|
"version": 3,
|
|
8
8
|
"sourcesContent": [
|
|
9
|
-
"export type ThemeValue = string\n\nexport type ThemeTokens = {\n color: Record<string, ThemeValue>\n}\n\nexport type ThemeRecipe = Record<string, unknown>\n\nexport type ThemeDefinitionContext<Parent extends ThemeRecipe = ThemeRecipe> = {\n parent: Parent\n}\n\nexport type ThemeDefinitionObject = {\n values?: Record<string, ThemeValue>\n children?: ThemeChildren\n [key: string]: unknown\n}\n\nexport type ThemeTreeDefinition<Parent extends ThemeRecipe = ThemeRecipe> =\n | ThemeDefinitionObject\n | ((context: ThemeDefinitionContext<Parent>) => ThemeDefinitionObject | null)\n\nexport type ThemeChildren = Record<string, ThemeTreeDefinition>\n\nexport type ThemeTree = {\n light: ThemeTreeDefinition\n dark: ThemeTreeDefinition\n children?: ThemeChildren\n}\n\ntype ColorLiteral = `#${string}` | `rgb${string}` | `hsl${string}` | 'transparent'\n\ntype ThemeInputValue<Tokens extends ThemeTokens> =\n | Extract<keyof Tokens['color'], string>\n | ColorLiteral\n\ntype ResolvedDefinition<Definition> = Definition extends (...args: any[]) => infer Result\n ? Exclude<Result, null>\n : Definition\n\ntype DefinitionChildren<Definition> =\n ResolvedDefinition<Definition> extends {\n children?: infer Children\n }\n ? Children\n : never\n\ntype ChildThemeNames<Parent extends string, Children> =\n Children extends Record<string, unknown>\n ? {\n [Name in Extract<keyof Children, string>]:\n | `${Parent}_${Name}`\n | ChildThemeNames<`${Parent}_${Name}`, DefinitionChildren<Children[Name]>>\n }[Extract<keyof Children, string>]\n : never\n\nexport type ThemeNames<Tree extends ThemeTree> =\n | 'light'\n | 'dark'\n | ChildThemeNames<'light', Tree['children']>\n | ChildThemeNames<'dark', Tree['children']>\n | ChildThemeNames<'light', DefinitionChildren<Tree['light']>>\n | ChildThemeNames<'dark', DefinitionChildren<Tree['dark']>>\n\ntype RecipeFields<Definition> = Omit<\n ResolvedDefinition<Definition>,\n 'children' | 'values'\n>\n\ntype ChildDefinitions<\n Children,\n Depth extends readonly unknown[],\n> = Depth extends readonly [unknown, ...infer Rest]\n ? Children extends Record<string, unknown>\n ? Children[keyof Children] | DefinitionsWithin<Children[keyof Children], Rest>\n : never\n : never\n\ntype DefinitionsWithin<Definition, Depth extends readonly unknown[]> =\n | Definition\n | ChildDefinitions<DefinitionChildren<Definition>, Depth>\n\ntype DefinitionsInTree<Tree extends ThemeTree> =\n | DefinitionsWithin<Tree['light'], [1, 1, 1, 1, 1, 1]>\n | DefinitionsWithin<Tree['dark'], [1, 1, 1, 1, 1, 1]>\n | ChildDefinitions<Tree['children'], [1, 1, 1, 1, 1, 1]>\n\ntype KeysOfUnion<Union> = Union extends Union ? keyof Union : never\n\ntype ValueOfUnion<Union, Key extends PropertyKey> = Union extends Union\n ? Key extends keyof Union\n ? Union[Key]\n : never\n : never\n\ntype MergeRecipeUnion<Union> = {\n [Key in KeysOfUnion<Union>]: ValueOfUnion<Union, Key>\n}\n\ntype RootRecipe<Tree extends ThemeTree> =\n | RecipeFields<Tree['light']>\n | RecipeFields<Tree['dark']>\n\ntype RecipeForTree<Tree extends ThemeTree> = {\n [Key in keyof RootRecipe<Tree>]: ValueOfUnion<RootRecipe<Tree>, Key>\n} & Partial<MergeRecipeUnion<RecipeFields<DefinitionsInTree<Tree>>>>\n\nexport type GetThemeContext<\n Tokens extends ThemeTokens = ThemeTokens,\n Recipe extends ThemeRecipe = ThemeRecipe,\n> = {\n recipe: Recipe\n name: string\n tokens: Tokens\n}\n\ntype InvalidValueKeys<Values, Theme> = Exclude<keyof Values, keyof Theme>\n\ntype ValidatedValues<Values, Theme, Tokens extends ThemeTokens> =\n Values extends Record<string, unknown>\n ? Values & {\n [Key in InvalidValueKeys<Values, Theme>]: never\n } & {\n [Key in keyof Values]: Values[Key] extends ThemeInputValue<Tokens>\n ? Values[Key]\n : never\n }\n : never\n\ntype ValidatedChildren<Children, Theme, Tokens extends ThemeTokens> =\n Children extends Record<string, unknown>\n ? {\n [Name in keyof Children]: ValidatedDefinition<Children[Name], Theme, Tokens>\n }\n : never\n\ntype ValidatedDefinition<\n Definition,\n Theme,\n Tokens extends ThemeTokens,\n> = Definition extends (...args: infer Args) => infer Result\n ? (\n ...args: Args\n ) => Result extends null ? null : ValidatedDefinitionObject<Result, Theme, Tokens>\n : ValidatedDefinitionObject<Definition, Theme, Tokens>\n\ntype ValidatedDefinitionObject<Definition, Theme, Tokens extends ThemeTokens> =\n Definition extends Record<string, unknown>\n ? Definition &\n ('values' extends keyof Definition\n ? {\n values: ValidatedValues<Definition['values'], Theme, Tokens>\n }\n : unknown) &\n ('children' extends keyof Definition\n ? {\n children: ValidatedChildren<Definition['children'], Theme, Tokens>\n }\n : unknown)\n : never\n\ntype ValidatedTree<Tree extends ThemeTree, Theme, Tokens extends ThemeTokens> = Tree & {\n light: ValidatedDefinition<Tree['light'], Theme, Tokens>\n dark: ValidatedDefinition<Tree['dark'], Theme, Tokens>\n children?: ValidatedChildren<Tree['children'], Theme, Tokens>\n}\n\ntype ValuesInDefinition<Definition> =\n ResolvedDefinition<Definition> extends {\n values?: infer Values\n }\n ? Values\n : never\n\ntype ValueDefinitionsWithin<Definition, Depth extends readonly unknown[]> =\n | ValuesInDefinition<Definition>\n | (Depth extends readonly [unknown, ...infer Rest]\n ? DefinitionChildren<Definition> extends infer Children\n ? Children extends Record<string, unknown>\n ? ValueDefinitionsWithin<Children[keyof Children], Rest>\n : never\n : never\n : never)\n\ntype ExplicitTheme<Tree extends ThemeTree> = MergeRecipeUnion<\n | ValueDefinitionsWithin<Tree['light'], [1, 1, 1, 1, 1, 1]>\n | ValueDefinitionsWithin<Tree['dark'], [1, 1, 1, 1, 1, 1]>\n | (Tree['children'] extends infer Children\n ? Children extends Record<string, unknown>\n ? ValueDefinitionsWithin<Children[keyof Children], [1, 1, 1, 1, 1, 1]>\n : never\n : never)\n>\n\nexport type CreatedThemes<Names extends string, Theme extends Record<string, string>> = {\n [Name in Names]: { [Key in keyof Theme]: string }\n}\n\nexport function createThemes<\n const Tokens extends ThemeTokens,\n const Tree extends ThemeTree,\n const Theme extends Record<string, ThemeInputValue<Tokens>>,\n>(\n tokens: Tokens,\n tree: ValidatedTree<Tree, Theme, Tokens>,\n options: {\n getTheme: (context: GetThemeContext<Tokens, RecipeForTree<Tree>>) => Theme\n }\n): CreatedThemes<ThemeNames<Tree>, Theme>\n\nexport function createThemes<\n const Tokens extends ThemeTokens,\n const Tree extends ThemeTree,\n>(\n tokens: Tokens,\n tree: ValidatedTree<Tree, Record<string, string>, Tokens>,\n options?: { getTheme?: undefined }\n): CreatedThemes<ThemeNames<Tree>, ExplicitTheme<Tree>>\n\nexport function createThemes(\n tokens: ThemeTokens,\n tree: ThemeTree,\n options?: {\n getTheme?: (context: GetThemeContext) => Record<string, string>\n }\n): Record<string, Record<string, string>> {\n const rootKeys = Object.keys(tree).filter((key) => key !== 'children')\n if (\n rootKeys.length !== 2 ||\n !Object.prototype.hasOwnProperty.call(tree, 'light') ||\n !Object.prototype.hasOwnProperty.call(tree, 'dark')\n ) {\n throw new Error('createThemes tree must have exactly the roots \"light\" and \"dark\"')\n }\n\n const themes: Record<string, Record<string, string>> = {}\n const deduped = new Map<string, Record<string, string>>()\n\n const add = (\n name: string,\n definition: ThemeTreeDefinition,\n parent: ThemeRecipe,\n inheritedChildren?: ThemeChildren\n ): void => {\n const resolvedDefinition =\n typeof definition === 'function' ? definition({ parent }) : definition\n if (resolvedDefinition === null) return\n\n const { values, children, ...recipeFields } = resolvedDefinition\n const recipe = { ...parent, ...recipeFields }\n const unresolved = {\n ...options?.getTheme?.({ recipe, name, tokens }),\n ...values,\n }\n const resolved: Record<string, string> = {}\n\n for (const key in unresolved) {\n const value = unresolved[key]\n const token = tokens.color[value]\n if (token !== undefined) {\n resolved[key] = token\n continue\n }\n\n const isColor =\n value === 'transparent' ||\n /^#(?:[\\da-f]{3,4}|[\\da-f]{6}|[\\da-f]{8})$/i.test(value) ||\n /^(?:rgb|rgba|hsl|hsla)\\(\\s*[^)]+\\)$/i.test(value)\n if (isColor) {\n resolved[key] = value\n continue\n }\n\n let nearest = ''\n let nearestDistance = Number.POSITIVE_INFINITY\n for (const tokenName in tokens.color) {\n const previous = Array.from({ length: tokenName.length + 1 }, (_, index) => index)\n for (let valueIndex = 0; valueIndex < value.length; valueIndex++) {\n let diagonal = previous[0]!\n previous[0] = valueIndex + 1\n for (let tokenIndex = 0; tokenIndex < tokenName.length; tokenIndex++) {\n const above = previous[tokenIndex + 1]!\n previous[tokenIndex + 1] = Math.min(\n previous[tokenIndex]! + 1,\n above + 1,\n diagonal + (value[valueIndex] === tokenName[tokenIndex] ? 0 : 1)\n )\n diagonal = above\n }\n }\n const distance = previous[tokenName.length]!\n if (distance < nearestDistance) {\n nearest = tokenName\n nearestDistance = distance\n }\n }\n\n throw new Error(\n `Invalid color value \"${value}\" for theme \"${name}\" key \"${key}\".${nearest ? ` Did you mean token \"${nearest}\"?` : ''}`\n )\n }\n\n const identity = JSON.stringify(\n Object.keys(resolved)\n .sort()\n .map((key) => [key, resolved[key]])\n )\n themes[name] = deduped.get(identity) || resolved\n deduped.set(identity, themes[name]!)\n\n const childSets = [inheritedChildren, children]\n for (const childSet of childSets) {\n if (!childSet) continue\n for (const childName in childSet) {\n const fullName = `${name}_${childName}`\n if (Object.prototype.hasOwnProperty.call(themes, fullName)) {\n throw new Error(`Duplicate generated theme name \"${fullName}\"`)\n }\n add(fullName, childSet[childName]!, recipe)\n }\n }\n }\n\n add('light', tree.light, {}, tree.children)\n add('dark', tree.dark, {}, tree.children)\n\n return themes\n}\n"
|
|
9
|
+
"export type ThemeValue = string\n\nexport type ThemeTokens = {\n color: Record<string, ThemeValue>\n}\n\nexport type ThemeRecipe = Record<string, unknown>\n\nexport type ThemeDefinitionContext<Parent extends ThemeRecipe = ThemeRecipe> = {\n parent: Parent\n}\n\nexport type ThemeDefinitionObject = {\n values?: Record<string, ThemeValue>\n children?: ThemeChildren\n [key: string]: unknown\n}\n\nexport type ThemeTreeDefinition<Parent extends ThemeRecipe = ThemeRecipe> =\n | ThemeDefinitionObject\n | ((context: ThemeDefinitionContext<Parent>) => ThemeDefinitionObject | null)\n\nexport type ThemeChildren = Record<string, ThemeTreeDefinition>\n\nexport type ThemeTree = {\n light: ThemeTreeDefinition\n dark: ThemeTreeDefinition\n children?: ThemeChildren\n}\n\ntype ColorLiteral = `#${string}` | `rgb${string}` | `hsl${string}` | 'transparent'\n\ntype ThemeInputValue<Tokens extends ThemeTokens> =\n | Extract<keyof Tokens['color'], string>\n | ColorLiteral\n\ntype ResolvedDefinition<Definition> = Definition extends (...args: any[]) => infer Result\n ? Exclude<Result, null>\n : Definition\n\ntype DefinitionChildren<Definition> =\n ResolvedDefinition<Definition> extends {\n children?: infer Children\n }\n ? Children\n : never\n\ntype ChildThemeNames<Parent extends string, Children> =\n Children extends Record<string, unknown>\n ? {\n [Name in Extract<keyof Children, string>]:\n | `${Parent}_${Name}`\n | ChildThemeNames<`${Parent}_${Name}`, DefinitionChildren<Children[Name]>>\n }[Extract<keyof Children, string>]\n : never\n\nexport type ThemeNames<Tree extends ThemeTree> =\n | 'light'\n | 'dark'\n | ChildThemeNames<'light', Tree['children']>\n | ChildThemeNames<'dark', Tree['children']>\n | ChildThemeNames<'light', DefinitionChildren<Tree['light']>>\n | ChildThemeNames<'dark', DefinitionChildren<Tree['dark']>>\n\ntype RecipeFields<Definition> = Omit<\n ResolvedDefinition<Definition>,\n 'children' | 'values'\n>\n\ntype ChildDefinitions<\n Children,\n Depth extends readonly unknown[],\n> = Depth extends readonly [unknown, ...infer Rest]\n ? Children extends Record<string, unknown>\n ? Children[keyof Children] | DefinitionsWithin<Children[keyof Children], Rest>\n : never\n : never\n\ntype DefinitionsWithin<Definition, Depth extends readonly unknown[]> =\n | Definition\n | ChildDefinitions<DefinitionChildren<Definition>, Depth>\n\ntype DefinitionsInTree<Tree extends ThemeTree> =\n | DefinitionsWithin<Tree['light'], [1, 1, 1, 1, 1, 1]>\n | DefinitionsWithin<Tree['dark'], [1, 1, 1, 1, 1, 1]>\n | ChildDefinitions<Tree['children'], [1, 1, 1, 1, 1, 1]>\n\ntype KeysOfUnion<Union> = Union extends Union ? keyof Union : never\n\ntype ValueOfUnion<Union, Key extends PropertyKey> = Union extends Union\n ? Key extends keyof Union\n ? Union[Key]\n : never\n : never\n\ntype MergeRecipeUnion<Union> = {\n [Key in KeysOfUnion<Union>]: ValueOfUnion<Union, Key>\n}\n\ntype RootRecipe<Tree extends ThemeTree> =\n | RecipeFields<Tree['light']>\n | RecipeFields<Tree['dark']>\n\ntype RecipeForTree<Tree extends ThemeTree> = {\n [Key in keyof RootRecipe<Tree>]: ValueOfUnion<RootRecipe<Tree>, Key>\n} & Partial<MergeRecipeUnion<RecipeFields<DefinitionsInTree<Tree>>>>\n\nexport type GetThemeContext<\n Tokens extends ThemeTokens = ThemeTokens,\n Recipe extends ThemeRecipe = ThemeRecipe,\n> = {\n recipe: Recipe\n name: string\n tokens: Tokens\n}\n\ntype InvalidValueKeys<Values, Theme> = Exclude<keyof Values, keyof Theme>\n\ntype ValidatedValues<Values, Theme, Tokens extends ThemeTokens> =\n Values extends Record<string, unknown>\n ? Values & {\n [Key in InvalidValueKeys<Values, Theme>]: never\n } & {\n [Key in keyof Values]: Values[Key] extends ThemeInputValue<Tokens>\n ? Values[Key]\n : never\n }\n : never\n\ntype ValidatedChildren<Children, Theme, Tokens extends ThemeTokens> =\n Children extends Record<string, unknown>\n ? {\n [Name in keyof Children]: ValidatedDefinition<Children[Name], Theme, Tokens>\n }\n : never\n\ntype ValidatedDefinition<\n Definition,\n Theme,\n Tokens extends ThemeTokens,\n> = Definition extends (...args: infer Args) => infer Result\n ? (\n ...args: Args\n ) => Result extends null ? null : ValidatedDefinitionObject<Result, Theme, Tokens>\n : ValidatedDefinitionObject<Definition, Theme, Tokens>\n\ntype ValidatedDefinitionObject<Definition, Theme, Tokens extends ThemeTokens> =\n Definition extends Record<string, unknown>\n ? Definition &\n ('values' extends keyof Definition\n ? {\n values: ValidatedValues<Definition['values'], Theme, Tokens>\n }\n : unknown) &\n ('children' extends keyof Definition\n ? {\n children: ValidatedChildren<Definition['children'], Theme, Tokens>\n }\n : unknown)\n : never\n\ntype ValidatedTree<Tree extends ThemeTree, Theme, Tokens extends ThemeTokens> = Tree & {\n light: ValidatedDefinition<Tree['light'], Theme, Tokens>\n dark: ValidatedDefinition<Tree['dark'], Theme, Tokens>\n children?: ValidatedChildren<Tree['children'], Theme, Tokens>\n}\n\ntype ValuesInDefinition<Definition> =\n ResolvedDefinition<Definition> extends {\n values?: infer Values\n }\n ? Values\n : never\n\ntype ValueDefinitionsWithin<Definition, Depth extends readonly unknown[]> =\n | ValuesInDefinition<Definition>\n | (Depth extends readonly [unknown, ...infer Rest]\n ? DefinitionChildren<Definition> extends infer Children\n ? Children extends Record<string, unknown>\n ? ValueDefinitionsWithin<Children[keyof Children], Rest>\n : never\n : never\n : never)\n\ntype ExplicitTheme<Tree extends ThemeTree> = MergeRecipeUnion<\n | ValueDefinitionsWithin<Tree['light'], [1, 1, 1, 1, 1, 1]>\n | ValueDefinitionsWithin<Tree['dark'], [1, 1, 1, 1, 1, 1]>\n | (Tree['children'] extends infer Children\n ? Children extends Record<string, unknown>\n ? ValueDefinitionsWithin<Children[keyof Children], [1, 1, 1, 1, 1, 1]>\n : never\n : never)\n>\n\nexport type CreatedThemes<Names extends string, Theme extends Record<string, string>> = {\n [Name in Names]: { [Key in keyof Theme]: string }\n}\n\nexport function createThemes<\n const Tokens extends ThemeTokens,\n const Tree extends ThemeTree,\n const Theme extends Record<string, ThemeInputValue<Tokens>>,\n>(\n tokens: Tokens,\n tree: ValidatedTree<Tree, Theme, Tokens>,\n options: {\n getTheme: (context: GetThemeContext<Tokens, RecipeForTree<Tree>>) => Theme\n }\n): CreatedThemes<ThemeNames<Tree>, Theme>\n\nexport function createThemes<\n const Tokens extends ThemeTokens,\n const Tree extends ThemeTree,\n>(\n tokens: Tokens,\n tree: ValidatedTree<Tree, Record<string, string>, Tokens>,\n options?: { getTheme?: undefined }\n): CreatedThemes<ThemeNames<Tree>, ExplicitTheme<Tree>>\n\nexport function createThemes(\n tokens: ThemeTokens,\n tree: ThemeTree,\n options?: {\n getTheme?: (context: GetThemeContext) => Record<string, string>\n }\n): Record<string, Record<string, string>> {\n const rootKeys = Object.keys(tree).filter((key) => key !== 'children')\n if (rootKeys.length !== 2 || !('light' in tree) || !('dark' in tree)) {\n throw new Error('createThemes tree must have exactly the roots \"light\" and \"dark\"')\n }\n\n const themes: Record<string, Record<string, string>> = {}\n const deduped = new Map<string, Record<string, string>>()\n\n const add = (\n name: string,\n definition: ThemeTreeDefinition,\n parent: ThemeRecipe,\n inheritedChildren?: ThemeChildren\n ): void => {\n const resolvedDefinition =\n typeof definition === 'function' ? definition({ parent }) : definition\n if (resolvedDefinition === null) return\n\n const { values, children, ...recipeFields } = resolvedDefinition\n const recipe = { ...parent, ...recipeFields }\n const unresolved = {\n ...options?.getTheme?.({ recipe, name, tokens }),\n ...values,\n }\n const resolved: Record<string, string> = {}\n\n for (const key in unresolved) {\n const value = unresolved[key]\n const token = tokens.color[value]\n if (token !== undefined) {\n resolved[key] = token\n continue\n }\n\n const isColor =\n value === 'transparent' ||\n /^#(?:[\\da-f]{3,4}|[\\da-f]{6}|[\\da-f]{8})$/i.test(value) ||\n /^(?:rgb|rgba|hsl|hsla)\\(\\s*[^)]+\\)$/i.test(value)\n if (isColor) {\n resolved[key] = value\n continue\n }\n\n let nearest = ''\n let nearestDistance = Number.POSITIVE_INFINITY\n for (const tokenName in tokens.color) {\n const previous = Array.from({ length: tokenName.length + 1 }, (_, index) => index)\n for (let valueIndex = 0; valueIndex < value.length; valueIndex++) {\n let diagonal = previous[0]!\n previous[0] = valueIndex + 1\n for (let tokenIndex = 0; tokenIndex < tokenName.length; tokenIndex++) {\n const above = previous[tokenIndex + 1]!\n previous[tokenIndex + 1] = Math.min(\n previous[tokenIndex]! + 1,\n above + 1,\n diagonal + (value[valueIndex] === tokenName[tokenIndex] ? 0 : 1)\n )\n diagonal = above\n }\n }\n const distance = previous[tokenName.length]!\n if (distance < nearestDistance) {\n nearest = tokenName\n nearestDistance = distance\n }\n }\n\n throw new Error(\n `Invalid color value \"${value}\" for theme \"${name}\" key \"${key}\".${nearest ? ` Did you mean token \"${nearest}\"?` : ''}`\n )\n }\n\n const identity = JSON.stringify(\n Object.keys(resolved)\n .sort()\n .map((key) => [key, resolved[key]])\n )\n themes[name] = deduped.get(identity) || resolved\n deduped.set(identity, themes[name]!)\n\n const childSets = [inheritedChildren, children]\n for (const childSet of childSets) {\n if (!childSet) continue\n for (const childName in childSet) {\n const fullName = `${name}_${childName}`\n if (fullName in themes) {\n throw new Error(`Duplicate generated theme name \"${fullName}\"`)\n }\n add(fullName, childSet[childName]!, recipe)\n }\n }\n }\n\n add('light', tree.light, {}, tree.children)\n add('dark', tree.dark, {}, tree.children)\n\n return themes\n}\n"
|
|
10
10
|
]
|
|
11
11
|
}
|