@tamagui/create-theme 1.14.8 → 1.15.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/cjs/index.js +17 -13
- package/dist/cjs/index.js.map +2 -2
- package/dist/esm/index.js +15 -12
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/index.mjs +15 -12
- package/dist/esm/index.mjs.map +2 -2
- package/package.json +4 -4
- package/src/index.tsx +15 -16
- package/types/index.d.ts +2 -0
- package/types/index.d.ts.map +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -23,7 +23,8 @@ __export(src_exports, {
|
|
|
23
23
|
createShiftMask: () => createShiftMask,
|
|
24
24
|
createStrengthenMask: () => createStrengthenMask,
|
|
25
25
|
createTheme: () => createTheme,
|
|
26
|
-
createWeakenMask: () => createWeakenMask
|
|
26
|
+
createWeakenMask: () => createWeakenMask,
|
|
27
|
+
skipMask: () => skipMask
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(src_exports);
|
|
29
30
|
const THEME_INFO = /* @__PURE__ */ new WeakMap();
|
|
@@ -58,15 +59,25 @@ function addChildren(themes, getChildren) {
|
|
|
58
59
|
}
|
|
59
60
|
return out;
|
|
60
61
|
}
|
|
62
|
+
const skipMask = (template, { skip }) => {
|
|
63
|
+
if (!skip)
|
|
64
|
+
return template;
|
|
65
|
+
return Object.fromEntries(
|
|
66
|
+
Object.entries(template).filter(([k]) => !(k in skip))
|
|
67
|
+
);
|
|
68
|
+
};
|
|
61
69
|
const createShiftMask = ({ inverse } = {}) => {
|
|
62
|
-
return (template,
|
|
70
|
+
return (template, opts) => {
|
|
71
|
+
const { override, max: maxIn, palette, min = 0, strength = 1 } = opts;
|
|
63
72
|
const values = Object.entries(template);
|
|
64
73
|
const max = maxIn ?? (palette ? Object.values(palette).length - 1 : Infinity);
|
|
65
74
|
const out = {};
|
|
66
75
|
for (const [key, value] of values) {
|
|
67
76
|
if (typeof value === "string")
|
|
68
77
|
continue;
|
|
69
|
-
if (
|
|
78
|
+
if (typeof (override == null ? void 0 : override[key]) === "number") {
|
|
79
|
+
const overrideShift = override[key];
|
|
80
|
+
out[key] = value + overrideShift;
|
|
70
81
|
continue;
|
|
71
82
|
}
|
|
72
83
|
const isPositive = value === 0 ? !isMinusZero(value) : value >= 0;
|
|
@@ -76,7 +87,7 @@ const createShiftMask = ({ inverse } = {}) => {
|
|
|
76
87
|
const clamped = isPositive ? Math.max(min, Math.min(max, next)) : Math.min(-min, Math.max(-max, next));
|
|
77
88
|
out[key] = clamped;
|
|
78
89
|
}
|
|
79
|
-
return out;
|
|
90
|
+
return skipMask(out, opts);
|
|
80
91
|
};
|
|
81
92
|
};
|
|
82
93
|
const createWeakenMask = () => createShiftMask();
|
|
@@ -84,7 +95,6 @@ const createStrengthenMask = () => createShiftMask({ inverse: true });
|
|
|
84
95
|
function isMinusZero(value) {
|
|
85
96
|
return 1 / value === -Infinity;
|
|
86
97
|
}
|
|
87
|
-
const MaskKeyCache = /* @__PURE__ */ new WeakMap();
|
|
88
98
|
function applyMask(theme, mask, options = {}) {
|
|
89
99
|
const info = THEME_INFO.get(theme);
|
|
90
100
|
if (!info) {
|
|
@@ -92,18 +102,11 @@ function applyMask(theme, mask, options = {}) {
|
|
|
92
102
|
process.env.NODE_ENV !== "production" ? `No info found for theme, you must pass the theme created by createThemeFromPalette directly to extendTheme` : `\u274C Err2`
|
|
93
103
|
);
|
|
94
104
|
}
|
|
95
|
-
const maskKey = MaskKeyCache.get(mask) ?? `${Math.random()}`;
|
|
96
|
-
MaskKeyCache.set(mask, maskKey);
|
|
97
|
-
const key = `${maskKey}${JSON.stringify(options)}`;
|
|
98
|
-
if (info.cache.has(key)) {
|
|
99
|
-
return info.cache.get(key);
|
|
100
|
-
}
|
|
101
105
|
const template = mask(info.definition, {
|
|
102
106
|
palette: info.palette,
|
|
103
107
|
...options
|
|
104
108
|
});
|
|
105
109
|
const next = createTheme(info.palette, template);
|
|
106
|
-
info.cache.set(key, next);
|
|
107
110
|
return next;
|
|
108
111
|
}
|
|
109
112
|
if (process.env.NODE_ENV === "development") {
|
|
@@ -139,6 +142,7 @@ if (process.env.NODE_ENV === "development") {
|
|
|
139
142
|
createShiftMask,
|
|
140
143
|
createStrengthenMask,
|
|
141
144
|
createTheme,
|
|
142
|
-
createWeakenMask
|
|
145
|
+
createWeakenMask,
|
|
146
|
+
skipMask
|
|
143
147
|
});
|
|
144
148
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.tsx"],
|
|
4
|
-
"sourcesContent": ["import type { Variable } from '@tamagui/web'\n\nexport type ThemeMask = Record<string, string | number>\nexport type Palette = string[]\n\nexport type MaskOptions = {\n palette?: Palette\n skip?: Partial<ThemeMask>\n strength?: number\n max?: number\n min?: number\n}\n\ntype GenericTheme = { [key: string]: string | Variable }\ntype CreateMask = <A extends ThemeMask>(template: A, options: MaskOptions) => A\n\nconst THEME_INFO = new WeakMap<\n any,\n { palette: Palette; definition: ThemeMask; cache: Map<any, any> }\n>()\n\nexport function createTheme<\n Definition extends ThemeMask,\n Extras extends Record<string, string> = {}\n>(\n palette: Palette,\n definition: Definition,\n options?: {\n nonInheritedValues?: Extras\n }\n): {\n [key in keyof Definition | keyof Extras]: string\n} {\n const theme = {\n ...(Object.fromEntries(\n Object.entries(definition).map(([key, offset]) => {\n return [key, getValue(palette, offset)]\n })\n ) as any),\n ...options?.nonInheritedValues,\n }\n THEME_INFO.set(theme, { palette, definition, cache: new Map() })\n return theme\n}\n\nconst getValue = (palette: Palette, value: string | number) => {\n if (typeof value === 'string') return value\n const max = palette.length - 1\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const next = isPositive ? value : max + value\n const index = Math.min(Math.max(0, next), max)\n return palette[index]\n}\n\ntype SubThemeKeys<ParentKeys, ChildKeys> = `${ParentKeys extends string\n ? ParentKeys\n : never}_${ChildKeys extends string ? ChildKeys : never}`\n\ntype ChildGetter<Name extends string | number | symbol, Theme extends GenericTheme> = (\n name: Name,\n theme: Theme\n) => { [key: string]: Theme }\n\nexport function addChildren<\n Themes extends { [key: string]: GenericTheme },\n GetChildren extends ChildGetter<keyof Themes, Themes[keyof Themes]>\n>(\n themes: Themes,\n getChildren: GetChildren\n): Themes & {\n [key in SubThemeKeys<keyof Themes, keyof ReturnType<GetChildren>>]: Themes[keyof Themes]\n} {\n const out = { ...themes }\n for (const key in themes) {\n const subThemes = getChildren(key, themes[key])\n for (const sKey in subThemes) {\n out[`${key}_${sKey}`] = subThemes[sKey] as any\n }\n }\n return out as any\n}\n\nexport const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {\n return ((template, {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["import type { Variable } from '@tamagui/web'\n\nexport type ThemeMask = Record<string, string | number>\nexport type Palette = string[]\n\nexport type MaskOptions = {\n palette?: Palette\n override?: Partial<ThemeMask>\n skip?: Partial<ThemeMask>\n strength?: number\n max?: number\n min?: number\n}\n\ntype GenericTheme = { [key: string]: string | Variable }\ntype CreateMask = <A extends ThemeMask>(template: A, options: MaskOptions) => A\n\nconst THEME_INFO = new WeakMap<\n any,\n { palette: Palette; definition: ThemeMask; cache: Map<any, any> }\n>()\n\nexport function createTheme<\n Definition extends ThemeMask,\n Extras extends Record<string, string> = {}\n>(\n palette: Palette,\n definition: Definition,\n options?: {\n nonInheritedValues?: Extras\n }\n): {\n [key in keyof Definition | keyof Extras]: string\n} {\n const theme = {\n ...(Object.fromEntries(\n Object.entries(definition).map(([key, offset]) => {\n return [key, getValue(palette, offset)]\n })\n ) as any),\n ...options?.nonInheritedValues,\n }\n THEME_INFO.set(theme, { palette, definition, cache: new Map() })\n return theme\n}\n\nconst getValue = (palette: Palette, value: string | number) => {\n if (typeof value === 'string') return value\n const max = palette.length - 1\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const next = isPositive ? value : max + value\n const index = Math.min(Math.max(0, next), max)\n return palette[index]\n}\n\ntype SubThemeKeys<ParentKeys, ChildKeys> = `${ParentKeys extends string\n ? ParentKeys\n : never}_${ChildKeys extends string ? ChildKeys : never}`\n\ntype ChildGetter<Name extends string | number | symbol, Theme extends GenericTheme> = (\n name: Name,\n theme: Theme\n) => { [key: string]: Theme }\n\nexport function addChildren<\n Themes extends { [key: string]: GenericTheme },\n GetChildren extends ChildGetter<keyof Themes, Themes[keyof Themes]>\n>(\n themes: Themes,\n getChildren: GetChildren\n): Themes & {\n [key in SubThemeKeys<keyof Themes, keyof ReturnType<GetChildren>>]: Themes[keyof Themes]\n} {\n const out = { ...themes }\n for (const key in themes) {\n const subThemes = getChildren(key, themes[key])\n for (const sKey in subThemes) {\n out[`${key}_${sKey}`] = subThemes[sKey] as any\n }\n }\n return out as any\n}\n\nexport const skipMask: CreateMask = (template, { skip }) => {\n if (!skip) return template\n return Object.fromEntries(\n Object.entries(template).filter(([k]) => !(k in skip))\n ) as typeof template\n}\n\nexport const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {\n return ((template, opts) => {\n const { override, max: maxIn, palette, min = 0, strength = 1 } = opts\n const values = Object.entries(template)\n const max = maxIn ?? (palette ? Object.values(palette).length - 1 : Infinity)\n const out = {}\n for (const [key, value] of values) {\n if (typeof value === 'string') continue\n if (typeof override?.[key] === 'number') {\n const overrideShift = override[key] as number\n out[key] = value + overrideShift\n continue\n }\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const direction = isPositive ? 1 : -1\n const invert = inverse ? -1 : 1\n const next = value + strength * direction * invert\n const clamped = isPositive\n ? Math.max(min, Math.min(max, next))\n : Math.min(-min, Math.max(-max, next))\n\n out[key] = clamped\n }\n\n return skipMask(out, opts) as typeof template\n }) as CreateMask\n}\n\nexport const createWeakenMask = () => createShiftMask()\nexport const createStrengthenMask = () => createShiftMask({ inverse: true })\n\nfunction isMinusZero(value) {\n return 1 / value === -Infinity\n}\n\nexport function applyMask<Theme extends GenericTheme>(\n theme: Theme,\n mask: CreateMask,\n options: MaskOptions = {}\n): Theme {\n const info = THEME_INFO.get(theme)\n if (!info) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No info found for theme, you must pass the theme created by createThemeFromPalette directly to extendTheme`\n : `\u274C Err2`\n )\n }\n\n const template = mask(info.definition, {\n palette: info.palette,\n ...options,\n })\n const next = createTheme(info.palette, template) as Theme\n\n return next\n}\n\n// --- tests ---\n\nif (process.env.NODE_ENV === 'development') {\n const palette = ['0', '1', '2', '3', '-3', '-2', '-1', '-0']\n const template = { bg: 1, fg: -1 }\n\n const stongerMask = createStrengthenMask()\n const weakerMask = createWeakenMask()\n\n const theme = createTheme(palette, template)\n if (theme.bg !== '1') throw `\u274C`\n if (theme.fg !== '-1') throw `\u274C`\n\n const str = applyMask(theme, stongerMask)\n if (str.bg !== '0') throw `\u274C`\n if (str.fg !== '-0') throw `\u274C`\n\n const weak = applyMask(theme, weakerMask)\n if (weak.bg !== '2') throw `\u274C`\n if (weak.fg !== '-2') throw `\u274C`\n\n const weak2 = applyMask(theme, weakerMask, { strength: 2 })\n if (weak2.bg !== '3') throw `\u274C`\n if (weak2.fg !== '-3') throw `\u274C`\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBA,MAAM,aAAa,oBAAI,QAGrB;AAEK,SAAS,YAId,SACA,YACA,SAKA;AACA,QAAM,QAAQ;AAAA,IACZ,GAAI,OAAO;AAAA,MACT,OAAO,QAAQ,UAAU,EAAE,IAAI,CAAC,CAAC,KAAK,MAAM,MAAM;AAChD,eAAO,CAAC,KAAK,SAAS,SAAS,MAAM,CAAC;AAAA,MACxC,CAAC;AAAA,IACH;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AM,QAAQ,YAAY,SAAS,QAAQ;AAC3C,MAAI,MAAM,OAAO;AAAK,UAAM;AAC5B,MAAI,MAAM,OAAO;AAAM,UAAM;AAE7B,QAAM,MAAM,UAAU,OAAO,WAAW;AACxC,MAAI,IAAI,OAAO;AAAK,UAAM;AAC1B,MAAI,IAAI,OAAO;AAAM,UAAM;AAE3B,QAAM,OAAO,UAAU,OAAO,UAAU;AACxC,MAAI,KAAK,OAAO;AAAK,UAAM;AAC3B,MAAI,KAAK,OAAO;AAAM,UAAM;AAE5B,QAAM,QAAQ,UAAU,OAAO,YAAY,EAAE,UAAU,EAAE,CAAC;AAC1D,MAAI,MAAM,OAAO;AAAK,UAAM;AAC5B,MAAI,MAAM,OAAO;AAAM,UAAM;AAC/B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -30,15 +30,25 @@ function addChildren(themes, getChildren) {
|
|
|
30
30
|
}
|
|
31
31
|
return out;
|
|
32
32
|
}
|
|
33
|
+
const skipMask = (template, { skip }) => {
|
|
34
|
+
if (!skip)
|
|
35
|
+
return template;
|
|
36
|
+
return Object.fromEntries(
|
|
37
|
+
Object.entries(template).filter(([k]) => !(k in skip))
|
|
38
|
+
);
|
|
39
|
+
};
|
|
33
40
|
const createShiftMask = ({ inverse } = {}) => {
|
|
34
|
-
return (template,
|
|
41
|
+
return (template, opts) => {
|
|
42
|
+
const { override, max: maxIn, palette, min = 0, strength = 1 } = opts;
|
|
35
43
|
const values = Object.entries(template);
|
|
36
44
|
const max = maxIn ?? (palette ? Object.values(palette).length - 1 : Infinity);
|
|
37
45
|
const out = {};
|
|
38
46
|
for (const [key, value] of values) {
|
|
39
47
|
if (typeof value === "string")
|
|
40
48
|
continue;
|
|
41
|
-
if (
|
|
49
|
+
if (typeof (override == null ? void 0 : override[key]) === "number") {
|
|
50
|
+
const overrideShift = override[key];
|
|
51
|
+
out[key] = value + overrideShift;
|
|
42
52
|
continue;
|
|
43
53
|
}
|
|
44
54
|
const isPositive = value === 0 ? !isMinusZero(value) : value >= 0;
|
|
@@ -48,7 +58,7 @@ const createShiftMask = ({ inverse } = {}) => {
|
|
|
48
58
|
const clamped = isPositive ? Math.max(min, Math.min(max, next)) : Math.min(-min, Math.max(-max, next));
|
|
49
59
|
out[key] = clamped;
|
|
50
60
|
}
|
|
51
|
-
return out;
|
|
61
|
+
return skipMask(out, opts);
|
|
52
62
|
};
|
|
53
63
|
};
|
|
54
64
|
const createWeakenMask = () => createShiftMask();
|
|
@@ -56,7 +66,6 @@ const createStrengthenMask = () => createShiftMask({ inverse: true });
|
|
|
56
66
|
function isMinusZero(value) {
|
|
57
67
|
return 1 / value === -Infinity;
|
|
58
68
|
}
|
|
59
|
-
const MaskKeyCache = /* @__PURE__ */ new WeakMap();
|
|
60
69
|
function applyMask(theme, mask, options = {}) {
|
|
61
70
|
const info = THEME_INFO.get(theme);
|
|
62
71
|
if (!info) {
|
|
@@ -64,18 +73,11 @@ function applyMask(theme, mask, options = {}) {
|
|
|
64
73
|
process.env.NODE_ENV !== "production" ? `No info found for theme, you must pass the theme created by createThemeFromPalette directly to extendTheme` : `\u274C Err2`
|
|
65
74
|
);
|
|
66
75
|
}
|
|
67
|
-
const maskKey = MaskKeyCache.get(mask) ?? `${Math.random()}`;
|
|
68
|
-
MaskKeyCache.set(mask, maskKey);
|
|
69
|
-
const key = `${maskKey}${JSON.stringify(options)}`;
|
|
70
|
-
if (info.cache.has(key)) {
|
|
71
|
-
return info.cache.get(key);
|
|
72
|
-
}
|
|
73
76
|
const template = mask(info.definition, {
|
|
74
77
|
palette: info.palette,
|
|
75
78
|
...options
|
|
76
79
|
});
|
|
77
80
|
const next = createTheme(info.palette, template);
|
|
78
|
-
info.cache.set(key, next);
|
|
79
81
|
return next;
|
|
80
82
|
}
|
|
81
83
|
if (process.env.NODE_ENV === "development") {
|
|
@@ -110,6 +112,7 @@ export {
|
|
|
110
112
|
createShiftMask,
|
|
111
113
|
createStrengthenMask,
|
|
112
114
|
createTheme,
|
|
113
|
-
createWeakenMask
|
|
115
|
+
createWeakenMask,
|
|
116
|
+
skipMask
|
|
114
117
|
};
|
|
115
118
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.tsx"],
|
|
4
|
-
"sourcesContent": ["import type { Variable } from '@tamagui/web'\n\nexport type ThemeMask = Record<string, string | number>\nexport type Palette = string[]\n\nexport type MaskOptions = {\n palette?: Palette\n skip?: Partial<ThemeMask>\n strength?: number\n max?: number\n min?: number\n}\n\ntype GenericTheme = { [key: string]: string | Variable }\ntype CreateMask = <A extends ThemeMask>(template: A, options: MaskOptions) => A\n\nconst THEME_INFO = new WeakMap<\n any,\n { palette: Palette; definition: ThemeMask; cache: Map<any, any> }\n>()\n\nexport function createTheme<\n Definition extends ThemeMask,\n Extras extends Record<string, string> = {}\n>(\n palette: Palette,\n definition: Definition,\n options?: {\n nonInheritedValues?: Extras\n }\n): {\n [key in keyof Definition | keyof Extras]: string\n} {\n const theme = {\n ...(Object.fromEntries(\n Object.entries(definition).map(([key, offset]) => {\n return [key, getValue(palette, offset)]\n })\n ) as any),\n ...options?.nonInheritedValues,\n }\n THEME_INFO.set(theme, { palette, definition, cache: new Map() })\n return theme\n}\n\nconst getValue = (palette: Palette, value: string | number) => {\n if (typeof value === 'string') return value\n const max = palette.length - 1\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const next = isPositive ? value : max + value\n const index = Math.min(Math.max(0, next), max)\n return palette[index]\n}\n\ntype SubThemeKeys<ParentKeys, ChildKeys> = `${ParentKeys extends string\n ? ParentKeys\n : never}_${ChildKeys extends string ? ChildKeys : never}`\n\ntype ChildGetter<Name extends string | number | symbol, Theme extends GenericTheme> = (\n name: Name,\n theme: Theme\n) => { [key: string]: Theme }\n\nexport function addChildren<\n Themes extends { [key: string]: GenericTheme },\n GetChildren extends ChildGetter<keyof Themes, Themes[keyof Themes]>\n>(\n themes: Themes,\n getChildren: GetChildren\n): Themes & {\n [key in SubThemeKeys<keyof Themes, keyof ReturnType<GetChildren>>]: Themes[keyof Themes]\n} {\n const out = { ...themes }\n for (const key in themes) {\n const subThemes = getChildren(key, themes[key])\n for (const sKey in subThemes) {\n out[`${key}_${sKey}`] = subThemes[sKey] as any\n }\n }\n return out as any\n}\n\nexport const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {\n return ((template, {
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { Variable } from '@tamagui/web'\n\nexport type ThemeMask = Record<string, string | number>\nexport type Palette = string[]\n\nexport type MaskOptions = {\n palette?: Palette\n override?: Partial<ThemeMask>\n skip?: Partial<ThemeMask>\n strength?: number\n max?: number\n min?: number\n}\n\ntype GenericTheme = { [key: string]: string | Variable }\ntype CreateMask = <A extends ThemeMask>(template: A, options: MaskOptions) => A\n\nconst THEME_INFO = new WeakMap<\n any,\n { palette: Palette; definition: ThemeMask; cache: Map<any, any> }\n>()\n\nexport function createTheme<\n Definition extends ThemeMask,\n Extras extends Record<string, string> = {}\n>(\n palette: Palette,\n definition: Definition,\n options?: {\n nonInheritedValues?: Extras\n }\n): {\n [key in keyof Definition | keyof Extras]: string\n} {\n const theme = {\n ...(Object.fromEntries(\n Object.entries(definition).map(([key, offset]) => {\n return [key, getValue(palette, offset)]\n })\n ) as any),\n ...options?.nonInheritedValues,\n }\n THEME_INFO.set(theme, { palette, definition, cache: new Map() })\n return theme\n}\n\nconst getValue = (palette: Palette, value: string | number) => {\n if (typeof value === 'string') return value\n const max = palette.length - 1\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const next = isPositive ? value : max + value\n const index = Math.min(Math.max(0, next), max)\n return palette[index]\n}\n\ntype SubThemeKeys<ParentKeys, ChildKeys> = `${ParentKeys extends string\n ? ParentKeys\n : never}_${ChildKeys extends string ? ChildKeys : never}`\n\ntype ChildGetter<Name extends string | number | symbol, Theme extends GenericTheme> = (\n name: Name,\n theme: Theme\n) => { [key: string]: Theme }\n\nexport function addChildren<\n Themes extends { [key: string]: GenericTheme },\n GetChildren extends ChildGetter<keyof Themes, Themes[keyof Themes]>\n>(\n themes: Themes,\n getChildren: GetChildren\n): Themes & {\n [key in SubThemeKeys<keyof Themes, keyof ReturnType<GetChildren>>]: Themes[keyof Themes]\n} {\n const out = { ...themes }\n for (const key in themes) {\n const subThemes = getChildren(key, themes[key])\n for (const sKey in subThemes) {\n out[`${key}_${sKey}`] = subThemes[sKey] as any\n }\n }\n return out as any\n}\n\nexport const skipMask: CreateMask = (template, { skip }) => {\n if (!skip) return template\n return Object.fromEntries(\n Object.entries(template).filter(([k]) => !(k in skip))\n ) as typeof template\n}\n\nexport const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {\n return ((template, opts) => {\n const { override, max: maxIn, palette, min = 0, strength = 1 } = opts\n const values = Object.entries(template)\n const max = maxIn ?? (palette ? Object.values(palette).length - 1 : Infinity)\n const out = {}\n for (const [key, value] of values) {\n if (typeof value === 'string') continue\n if (typeof override?.[key] === 'number') {\n const overrideShift = override[key] as number\n out[key] = value + overrideShift\n continue\n }\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const direction = isPositive ? 1 : -1\n const invert = inverse ? -1 : 1\n const next = value + strength * direction * invert\n const clamped = isPositive\n ? Math.max(min, Math.min(max, next))\n : Math.min(-min, Math.max(-max, next))\n\n out[key] = clamped\n }\n\n return skipMask(out, opts) as typeof template\n }) as CreateMask\n}\n\nexport const createWeakenMask = () => createShiftMask()\nexport const createStrengthenMask = () => createShiftMask({ inverse: true })\n\nfunction isMinusZero(value) {\n return 1 / value === -Infinity\n}\n\nexport function applyMask<Theme extends GenericTheme>(\n theme: Theme,\n mask: CreateMask,\n options: MaskOptions = {}\n): Theme {\n const info = THEME_INFO.get(theme)\n if (!info) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No info found for theme, you must pass the theme created by createThemeFromPalette directly to extendTheme`\n : `\u274C Err2`\n )\n }\n\n const template = mask(info.definition, {\n palette: info.palette,\n ...options,\n })\n const next = createTheme(info.palette, template) as Theme\n\n return next\n}\n\n// --- tests ---\n\nif (process.env.NODE_ENV === 'development') {\n const palette = ['0', '1', '2', '3', '-3', '-2', '-1', '-0']\n const template = { bg: 1, fg: -1 }\n\n const stongerMask = createStrengthenMask()\n const weakerMask = createWeakenMask()\n\n const theme = createTheme(palette, template)\n if (theme.bg !== '1') throw `\u274C`\n if (theme.fg !== '-1') throw `\u274C`\n\n const str = applyMask(theme, stongerMask)\n if (str.bg !== '0') throw `\u274C`\n if (str.fg !== '-0') throw `\u274C`\n\n const weak = applyMask(theme, weakerMask)\n if (weak.bg !== '2') throw `\u274C`\n if (weak.fg !== '-2') throw `\u274C`\n\n const weak2 = applyMask(theme, weakerMask, { strength: 2 })\n if (weak2.bg !== '3') throw `\u274C`\n if (weak2.fg !== '-3') throw `\u274C`\n}\n"],
|
|
5
|
+
"mappings": "AAiBA,MAAM,aAAa,oBAAI,QAGrB;AAEK,SAAS,YAId,SACA,YACA,SAKA;AACA,QAAM,QAAQ;AAAA,IACZ,GAAI,OAAO;AAAA,MACT,OAAO,QAAQ,UAAU,EAAE,IAAI,CAAC,CAAC,KAAK,MAAM,MAAM;AAChD,eAAO,CAAC,KAAK,SAAS,SAAS,MAAM,CAAC;AAAA,MACxC,CAAC;AAAA,IACH;AAAA,IACA,GAAG,mCAAS;AAAA,EACd;AACA,aAAW,IAAI,OAAO,EAAE,SAAS,YAAY,OAAO,oBAAI,IAAI,EAAE,CAAC;AAC/D,SAAO;AACT;AAEA,MAAM,WAAW,CAAC,SAAkB,UAA2B;AAC7D,MAAI,OAAO,UAAU;AAAU,WAAO;AACtC,QAAM,MAAM,QAAQ,SAAS;AAC7B,QAAM,aAAa,UAAU,IAAI,CAAC,YAAY,KAAK,IAAI,SAAS;AAChE,QAAM,OAAO,aAAa,QAAQ,MAAM;AACxC,QAAM,Q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,UAAU,OAAO,YAAY,EAAE,UAAU,EAAE,CAAC;AAC1D,MAAI,MAAM,OAAO;AAAK,UAAM;AAC5B,MAAI,MAAM,OAAO;AAAM,UAAM;AAC/B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -30,15 +30,25 @@ function addChildren(themes, getChildren) {
|
|
|
30
30
|
}
|
|
31
31
|
return out;
|
|
32
32
|
}
|
|
33
|
+
const skipMask = (template, { skip }) => {
|
|
34
|
+
if (!skip)
|
|
35
|
+
return template;
|
|
36
|
+
return Object.fromEntries(
|
|
37
|
+
Object.entries(template).filter(([k]) => !(k in skip))
|
|
38
|
+
);
|
|
39
|
+
};
|
|
33
40
|
const createShiftMask = ({ inverse } = {}) => {
|
|
34
|
-
return (template,
|
|
41
|
+
return (template, opts) => {
|
|
42
|
+
const { override, max: maxIn, palette, min = 0, strength = 1 } = opts;
|
|
35
43
|
const values = Object.entries(template);
|
|
36
44
|
const max = maxIn ?? (palette ? Object.values(palette).length - 1 : Infinity);
|
|
37
45
|
const out = {};
|
|
38
46
|
for (const [key, value] of values) {
|
|
39
47
|
if (typeof value === "string")
|
|
40
48
|
continue;
|
|
41
|
-
if (
|
|
49
|
+
if (typeof (override == null ? void 0 : override[key]) === "number") {
|
|
50
|
+
const overrideShift = override[key];
|
|
51
|
+
out[key] = value + overrideShift;
|
|
42
52
|
continue;
|
|
43
53
|
}
|
|
44
54
|
const isPositive = value === 0 ? !isMinusZero(value) : value >= 0;
|
|
@@ -48,7 +58,7 @@ const createShiftMask = ({ inverse } = {}) => {
|
|
|
48
58
|
const clamped = isPositive ? Math.max(min, Math.min(max, next)) : Math.min(-min, Math.max(-max, next));
|
|
49
59
|
out[key] = clamped;
|
|
50
60
|
}
|
|
51
|
-
return out;
|
|
61
|
+
return skipMask(out, opts);
|
|
52
62
|
};
|
|
53
63
|
};
|
|
54
64
|
const createWeakenMask = () => createShiftMask();
|
|
@@ -56,7 +66,6 @@ const createStrengthenMask = () => createShiftMask({ inverse: true });
|
|
|
56
66
|
function isMinusZero(value) {
|
|
57
67
|
return 1 / value === -Infinity;
|
|
58
68
|
}
|
|
59
|
-
const MaskKeyCache = /* @__PURE__ */ new WeakMap();
|
|
60
69
|
function applyMask(theme, mask, options = {}) {
|
|
61
70
|
const info = THEME_INFO.get(theme);
|
|
62
71
|
if (!info) {
|
|
@@ -64,18 +73,11 @@ function applyMask(theme, mask, options = {}) {
|
|
|
64
73
|
process.env.NODE_ENV !== "production" ? `No info found for theme, you must pass the theme created by createThemeFromPalette directly to extendTheme` : `\u274C Err2`
|
|
65
74
|
);
|
|
66
75
|
}
|
|
67
|
-
const maskKey = MaskKeyCache.get(mask) ?? `${Math.random()}`;
|
|
68
|
-
MaskKeyCache.set(mask, maskKey);
|
|
69
|
-
const key = `${maskKey}${JSON.stringify(options)}`;
|
|
70
|
-
if (info.cache.has(key)) {
|
|
71
|
-
return info.cache.get(key);
|
|
72
|
-
}
|
|
73
76
|
const template = mask(info.definition, {
|
|
74
77
|
palette: info.palette,
|
|
75
78
|
...options
|
|
76
79
|
});
|
|
77
80
|
const next = createTheme(info.palette, template);
|
|
78
|
-
info.cache.set(key, next);
|
|
79
81
|
return next;
|
|
80
82
|
}
|
|
81
83
|
if (process.env.NODE_ENV === "development") {
|
|
@@ -110,6 +112,7 @@ export {
|
|
|
110
112
|
createShiftMask,
|
|
111
113
|
createStrengthenMask,
|
|
112
114
|
createTheme,
|
|
113
|
-
createWeakenMask
|
|
115
|
+
createWeakenMask,
|
|
116
|
+
skipMask
|
|
114
117
|
};
|
|
115
118
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.tsx"],
|
|
4
|
-
"sourcesContent": ["import type { Variable } from '@tamagui/web'\n\nexport type ThemeMask = Record<string, string | number>\nexport type Palette = string[]\n\nexport type MaskOptions = {\n palette?: Palette\n skip?: Partial<ThemeMask>\n strength?: number\n max?: number\n min?: number\n}\n\ntype GenericTheme = { [key: string]: string | Variable }\ntype CreateMask = <A extends ThemeMask>(template: A, options: MaskOptions) => A\n\nconst THEME_INFO = new WeakMap<\n any,\n { palette: Palette; definition: ThemeMask; cache: Map<any, any> }\n>()\n\nexport function createTheme<\n Definition extends ThemeMask,\n Extras extends Record<string, string> = {}\n>(\n palette: Palette,\n definition: Definition,\n options?: {\n nonInheritedValues?: Extras\n }\n): {\n [key in keyof Definition | keyof Extras]: string\n} {\n const theme = {\n ...(Object.fromEntries(\n Object.entries(definition).map(([key, offset]) => {\n return [key, getValue(palette, offset)]\n })\n ) as any),\n ...options?.nonInheritedValues,\n }\n THEME_INFO.set(theme, { palette, definition, cache: new Map() })\n return theme\n}\n\nconst getValue = (palette: Palette, value: string | number) => {\n if (typeof value === 'string') return value\n const max = palette.length - 1\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const next = isPositive ? value : max + value\n const index = Math.min(Math.max(0, next), max)\n return palette[index]\n}\n\ntype SubThemeKeys<ParentKeys, ChildKeys> = `${ParentKeys extends string\n ? ParentKeys\n : never}_${ChildKeys extends string ? ChildKeys : never}`\n\ntype ChildGetter<Name extends string | number | symbol, Theme extends GenericTheme> = (\n name: Name,\n theme: Theme\n) => { [key: string]: Theme }\n\nexport function addChildren<\n Themes extends { [key: string]: GenericTheme },\n GetChildren extends ChildGetter<keyof Themes, Themes[keyof Themes]>\n>(\n themes: Themes,\n getChildren: GetChildren\n): Themes & {\n [key in SubThemeKeys<keyof Themes, keyof ReturnType<GetChildren>>]: Themes[keyof Themes]\n} {\n const out = { ...themes }\n for (const key in themes) {\n const subThemes = getChildren(key, themes[key])\n for (const sKey in subThemes) {\n out[`${key}_${sKey}`] = subThemes[sKey] as any\n }\n }\n return out as any\n}\n\nexport const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {\n return ((template, {
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { Variable } from '@tamagui/web'\n\nexport type ThemeMask = Record<string, string | number>\nexport type Palette = string[]\n\nexport type MaskOptions = {\n palette?: Palette\n override?: Partial<ThemeMask>\n skip?: Partial<ThemeMask>\n strength?: number\n max?: number\n min?: number\n}\n\ntype GenericTheme = { [key: string]: string | Variable }\ntype CreateMask = <A extends ThemeMask>(template: A, options: MaskOptions) => A\n\nconst THEME_INFO = new WeakMap<\n any,\n { palette: Palette; definition: ThemeMask; cache: Map<any, any> }\n>()\n\nexport function createTheme<\n Definition extends ThemeMask,\n Extras extends Record<string, string> = {}\n>(\n palette: Palette,\n definition: Definition,\n options?: {\n nonInheritedValues?: Extras\n }\n): {\n [key in keyof Definition | keyof Extras]: string\n} {\n const theme = {\n ...(Object.fromEntries(\n Object.entries(definition).map(([key, offset]) => {\n return [key, getValue(palette, offset)]\n })\n ) as any),\n ...options?.nonInheritedValues,\n }\n THEME_INFO.set(theme, { palette, definition, cache: new Map() })\n return theme\n}\n\nconst getValue = (palette: Palette, value: string | number) => {\n if (typeof value === 'string') return value\n const max = palette.length - 1\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const next = isPositive ? value : max + value\n const index = Math.min(Math.max(0, next), max)\n return palette[index]\n}\n\ntype SubThemeKeys<ParentKeys, ChildKeys> = `${ParentKeys extends string\n ? ParentKeys\n : never}_${ChildKeys extends string ? ChildKeys : never}`\n\ntype ChildGetter<Name extends string | number | symbol, Theme extends GenericTheme> = (\n name: Name,\n theme: Theme\n) => { [key: string]: Theme }\n\nexport function addChildren<\n Themes extends { [key: string]: GenericTheme },\n GetChildren extends ChildGetter<keyof Themes, Themes[keyof Themes]>\n>(\n themes: Themes,\n getChildren: GetChildren\n): Themes & {\n [key in SubThemeKeys<keyof Themes, keyof ReturnType<GetChildren>>]: Themes[keyof Themes]\n} {\n const out = { ...themes }\n for (const key in themes) {\n const subThemes = getChildren(key, themes[key])\n for (const sKey in subThemes) {\n out[`${key}_${sKey}`] = subThemes[sKey] as any\n }\n }\n return out as any\n}\n\nexport const skipMask: CreateMask = (template, { skip }) => {\n if (!skip) return template\n return Object.fromEntries(\n Object.entries(template).filter(([k]) => !(k in skip))\n ) as typeof template\n}\n\nexport const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {\n return ((template, opts) => {\n const { override, max: maxIn, palette, min = 0, strength = 1 } = opts\n const values = Object.entries(template)\n const max = maxIn ?? (palette ? Object.values(palette).length - 1 : Infinity)\n const out = {}\n for (const [key, value] of values) {\n if (typeof value === 'string') continue\n if (typeof override?.[key] === 'number') {\n const overrideShift = override[key] as number\n out[key] = value + overrideShift\n continue\n }\n const isPositive = value === 0 ? !isMinusZero(value) : value >= 0\n const direction = isPositive ? 1 : -1\n const invert = inverse ? -1 : 1\n const next = value + strength * direction * invert\n const clamped = isPositive\n ? Math.max(min, Math.min(max, next))\n : Math.min(-min, Math.max(-max, next))\n\n out[key] = clamped\n }\n\n return skipMask(out, opts) as typeof template\n }) as CreateMask\n}\n\nexport const createWeakenMask = () => createShiftMask()\nexport const createStrengthenMask = () => createShiftMask({ inverse: true })\n\nfunction isMinusZero(value) {\n return 1 / value === -Infinity\n}\n\nexport function applyMask<Theme extends GenericTheme>(\n theme: Theme,\n mask: CreateMask,\n options: MaskOptions = {}\n): Theme {\n const info = THEME_INFO.get(theme)\n if (!info) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No info found for theme, you must pass the theme created by createThemeFromPalette directly to extendTheme`\n : `\u274C Err2`\n )\n }\n\n const template = mask(info.definition, {\n palette: info.palette,\n ...options,\n })\n const next = createTheme(info.palette, template) as Theme\n\n return next\n}\n\n// --- tests ---\n\nif (process.env.NODE_ENV === 'development') {\n const palette = ['0', '1', '2', '3', '-3', '-2', '-1', '-0']\n const template = { bg: 1, fg: -1 }\n\n const stongerMask = createStrengthenMask()\n const weakerMask = createWeakenMask()\n\n const theme = createTheme(palette, template)\n if (theme.bg !== '1') throw `\u274C`\n if (theme.fg !== '-1') throw `\u274C`\n\n const str = applyMask(theme, stongerMask)\n if (str.bg !== '0') throw `\u274C`\n if (str.fg !== '-0') throw `\u274C`\n\n const weak = applyMask(theme, weakerMask)\n if (weak.bg !== '2') throw `\u274C`\n if (weak.fg !== '-2') throw `\u274C`\n\n const weak2 = applyMask(theme, weakerMask, { strength: 2 })\n if (weak2.bg !== '3') throw `\u274C`\n if (weak2.fg !== '-3') throw `\u274C`\n}\n"],
|
|
5
|
+
"mappings": "AAiBA,MAAM,aAAa,oBAAI,QAGrB;AAEK,SAAS,YAId,SACA,YACA,SAKA;AACA,QAAM,QAAQ;AAAA,IACZ,GAAI,OAAO;AAAA,MACT,OAAO,QAAQ,UAAU,EAAE,IAAI,CAAC,CAAC,KAAK,MAAM,MAAM;AAChD,eAAO,CAAC,KAAK,SAAS,SAAS,MAAM,CAAC;AAAA,MACxC,CAAC;AAAA,IACH;AAAA,IACA,GAAG,mCAAS;AAAA,EACd;AACA,aAAW,IAAI,OAAO,EAAE,SAAS,YAAY,OAAO,oBAAI,IAAI,EAAE,CAAC;AAC/D,SAAO;AACT;AAEA,MAAM,WAAW,CAAC,SAAkB,UAA2B;AAC7D,MAAI,OAAO,UAAU;AAAU,WAAO;AACtC,QAAM,MAAM,QAAQ,SAAS;AAC7B,QAAM,aAAa,UAAU,IAAI,CAAC,YAAY,KAAK,IAAI,SAAS;AAChE,QAAM,OAAO,aAAa,QAAQ,MAAM;AACxC,QAAM,QAAQ,KAAK,IAAI,KAAK,IAAI,GAAG,IAAI,GAAG,GAAG;AAC7C,SAAO,QAAQ,KAAK;AACtB;AAWO,SAAS,YAId,QACA,aAGA;AACA,QAAM,MAAM,EAAE,GAAG,OAAO;AACxB,aAAW,OAAO,QAAQ;AACxB,UAAM,YAAY,YAAY,KAAK,OAAO,GAAG,CAAC;AAC9C,eAAW,QAAQ,WAAW;AAC5B,UAAI,GAAG,OAAO,MAAM,IAAI,UAAU,IAAI;AAAA,IACxC;AAAA,EACF;AACA,SAAO;AACT;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main": "dist/cjs",
|
|
6
6
|
"module": "dist/esm",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"build": "tamagui-build",
|
|
15
15
|
"watch": "tamagui-build --watch",
|
|
16
16
|
"lint": "../../node_modules/.bin/rome check src",
|
|
17
|
-
"lint:fix": "../../node_modules/.bin/rome check --apply
|
|
17
|
+
"lint:fix": "../../node_modules/.bin/rome check --apply src",
|
|
18
18
|
"clean": "tamagui-build clean",
|
|
19
19
|
"clean:build": "tamagui-build clean:build"
|
|
20
20
|
},
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@tamagui/web": "1.
|
|
30
|
+
"@tamagui/web": "1.15.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@tamagui/build": "1.
|
|
33
|
+
"@tamagui/build": "1.15.0"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
package/src/index.tsx
CHANGED
|
@@ -5,6 +5,7 @@ export type Palette = string[]
|
|
|
5
5
|
|
|
6
6
|
export type MaskOptions = {
|
|
7
7
|
palette?: Palette
|
|
8
|
+
override?: Partial<ThemeMask>
|
|
8
9
|
skip?: Partial<ThemeMask>
|
|
9
10
|
strength?: number
|
|
10
11
|
max?: number
|
|
@@ -80,14 +81,24 @@ export function addChildren<
|
|
|
80
81
|
return out as any
|
|
81
82
|
}
|
|
82
83
|
|
|
84
|
+
export const skipMask: CreateMask = (template, { skip }) => {
|
|
85
|
+
if (!skip) return template
|
|
86
|
+
return Object.fromEntries(
|
|
87
|
+
Object.entries(template).filter(([k]) => !(k in skip))
|
|
88
|
+
) as typeof template
|
|
89
|
+
}
|
|
90
|
+
|
|
83
91
|
export const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {
|
|
84
|
-
return ((template,
|
|
92
|
+
return ((template, opts) => {
|
|
93
|
+
const { override, max: maxIn, palette, min = 0, strength = 1 } = opts
|
|
85
94
|
const values = Object.entries(template)
|
|
86
95
|
const max = maxIn ?? (palette ? Object.values(palette).length - 1 : Infinity)
|
|
87
96
|
const out = {}
|
|
88
97
|
for (const [key, value] of values) {
|
|
89
98
|
if (typeof value === 'string') continue
|
|
90
|
-
if (
|
|
99
|
+
if (typeof override?.[key] === 'number') {
|
|
100
|
+
const overrideShift = override[key] as number
|
|
101
|
+
out[key] = value + overrideShift
|
|
91
102
|
continue
|
|
92
103
|
}
|
|
93
104
|
const isPositive = value === 0 ? !isMinusZero(value) : value >= 0
|
|
@@ -100,7 +111,8 @@ export const createShiftMask = ({ inverse }: { inverse?: boolean } = {}) => {
|
|
|
100
111
|
|
|
101
112
|
out[key] = clamped
|
|
102
113
|
}
|
|
103
|
-
|
|
114
|
+
|
|
115
|
+
return skipMask(out, opts) as typeof template
|
|
104
116
|
}) as CreateMask
|
|
105
117
|
}
|
|
106
118
|
|
|
@@ -111,8 +123,6 @@ function isMinusZero(value) {
|
|
|
111
123
|
return 1 / value === -Infinity
|
|
112
124
|
}
|
|
113
125
|
|
|
114
|
-
const MaskKeyCache = new WeakMap<any, string>()
|
|
115
|
-
|
|
116
126
|
export function applyMask<Theme extends GenericTheme>(
|
|
117
127
|
theme: Theme,
|
|
118
128
|
mask: CreateMask,
|
|
@@ -127,23 +137,12 @@ export function applyMask<Theme extends GenericTheme>(
|
|
|
127
137
|
)
|
|
128
138
|
}
|
|
129
139
|
|
|
130
|
-
const maskKey = MaskKeyCache.get(mask) ?? `${Math.random()}`
|
|
131
|
-
MaskKeyCache.set(mask, maskKey)
|
|
132
|
-
|
|
133
|
-
const key = `${maskKey}${JSON.stringify(options)}`
|
|
134
|
-
|
|
135
|
-
if (info.cache.has(key)) {
|
|
136
|
-
return info.cache.get(key)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
140
|
const template = mask(info.definition, {
|
|
140
141
|
palette: info.palette,
|
|
141
142
|
...options,
|
|
142
143
|
})
|
|
143
144
|
const next = createTheme(info.palette, template) as Theme
|
|
144
145
|
|
|
145
|
-
info.cache.set(key, next)
|
|
146
|
-
|
|
147
146
|
return next
|
|
148
147
|
}
|
|
149
148
|
|
package/types/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type ThemeMask = Record<string, string | number>;
|
|
|
3
3
|
export type Palette = string[];
|
|
4
4
|
export type MaskOptions = {
|
|
5
5
|
palette?: Palette;
|
|
6
|
+
override?: Partial<ThemeMask>;
|
|
6
7
|
skip?: Partial<ThemeMask>;
|
|
7
8
|
strength?: number;
|
|
8
9
|
max?: number;
|
|
@@ -26,6 +27,7 @@ export declare function addChildren<Themes extends {
|
|
|
26
27
|
}, GetChildren extends ChildGetter<keyof Themes, Themes[keyof Themes]>>(themes: Themes, getChildren: GetChildren): Themes & {
|
|
27
28
|
[key in SubThemeKeys<keyof Themes, keyof ReturnType<GetChildren>>]: Themes[keyof Themes];
|
|
28
29
|
};
|
|
30
|
+
export declare const skipMask: CreateMask;
|
|
29
31
|
export declare const createShiftMask: ({ inverse }?: {
|
|
30
32
|
inverse?: boolean | undefined;
|
|
31
33
|
}) => CreateMask;
|
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAA;AACvD,MAAM,MAAM,OAAO,GAAG,MAAM,EAAE,CAAA;AAE9B,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,CAAA;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;CAC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