@tamagui/theme-builder 1.88.13 → 1.89.0-1706308641099

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Nate Wienert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,125 @@
1
+ import { applyMask, createMask, createThemeWithPalettes, objectEntries, objectFromEntries } from "@tamagui/create-theme";
2
+ class ThemeBuilder {
3
+ constructor(state) {
4
+ this.state = state;
5
+ }
6
+ addPalettes(palettes) {
7
+ return this.state.palettes = {
8
+ // as {} prevents generic string key merge messing up types
9
+ ...this.state.palettes,
10
+ ...palettes
11
+ }, this;
12
+ }
13
+ addTemplates(templates) {
14
+ return this.state.templates = {
15
+ // as {} prevents generic string key merge messing up types
16
+ ...this.state.templates,
17
+ ...templates
18
+ }, this;
19
+ }
20
+ addMasks(masks) {
21
+ return this.state.masks = {
22
+ // as {} prevents generic string key merge messing up types
23
+ ...this.state.masks,
24
+ ...objectFromEntries(objectEntries(masks).map(([key, val]) => [key, createMask(val)]))
25
+ }, this;
26
+ }
27
+ // for dev mode only really
28
+ _addedThemes = [];
29
+ addThemes(themes) {
30
+ return this._addedThemes.push({
31
+ type: "themes",
32
+ args: [themes]
33
+ }), this.state.themes = {
34
+ // as {} prevents generic string key merge messing up types
35
+ ...this.state.themes,
36
+ ...themes
37
+ }, this;
38
+ }
39
+ addChildThemes(childThemeDefinition, options) {
40
+ const currentThemes = this.state.themes;
41
+ if (!currentThemes) throw new Error("No themes defined yet, use addThemes first to set your base themes");
42
+ this._addedThemes.push({
43
+ type: "childThemes",
44
+ args: [childThemeDefinition, options]
45
+ });
46
+ const currentThemeNames = Object.keys(currentThemes),
47
+ incomingThemeNames = Object.keys(childThemeDefinition),
48
+ namesWithDefinitions = currentThemeNames.flatMap(prefix => {
49
+ const avoidNestingWithin = options?.avoidNestingWithin;
50
+ return avoidNestingWithin && avoidNestingWithin.some(avoidName => prefix.startsWith(avoidName) || prefix.endsWith(avoidName)) ? [] : incomingThemeNames.map(subName => {
51
+ const fullName = `${prefix}_${subName}`,
52
+ definition = childThemeDefinition[subName];
53
+ return "avoidNestingWithin" in definition && definition.avoidNestingWithin.some(name => prefix.startsWith(name) || prefix.endsWith(name)) ? null : [fullName, definition];
54
+ }).filter(Boolean);
55
+ }),
56
+ childThemes = Object.fromEntries(namesWithDefinitions),
57
+ next = {
58
+ // as {} prevents generic string key merge messing up types
59
+ ...this.state.themes,
60
+ ...childThemes
61
+ };
62
+ return this.state.themes = next, this;
63
+ }
64
+ build() {
65
+ if (!this.state.themes) return {};
66
+ const out = {},
67
+ maskedThemes = [];
68
+ for (const themeName in this.state.themes) {
69
+ const nameParts = themeName.split("_"),
70
+ parentName = nameParts.slice(0, nameParts.length - 1).join("_"),
71
+ definitions = this.state.themes[themeName],
72
+ themeDefinition = Array.isArray(definitions) ? (() => {
73
+ const found = definitions.find(
74
+ // endWith match stronger than startsWith
75
+ d => parentName.endsWith(d.parent) || parentName.startsWith(d.parent));
76
+ return found || null;
77
+ })() : definitions;
78
+ if (themeDefinition) if ("theme" in themeDefinition) out[themeName] = themeDefinition.theme;else if ("mask" in themeDefinition) maskedThemes.push({
79
+ parentName,
80
+ themeName,
81
+ mask: themeDefinition
82
+ });else {
83
+ let {
84
+ palette: paletteName,
85
+ template: templateName,
86
+ ...options
87
+ } = themeDefinition;
88
+ if (!this.state.palettes) throw new Error(`No palettes defined for theme with palette expected: ${themeName}`);
89
+ let palette = this.state.palettes[paletteName];
90
+ if (palette || (paletteName = `${parentName}_${paletteName}`, palette = this.state.palettes[paletteName]), !palette) throw new Error(`No palette for theme ${themeName}: ${paletteName} (${Object.keys(this.state.palettes).join(", ")})`);
91
+ const template = this.state.templates?.[templateName];
92
+ if (!template) throw new Error(`No template for theme ${themeName}: ${templateName}`);
93
+ out[themeName] = createThemeWithPalettes(this.state.palettes, paletteName, template, options, themeName, !0);
94
+ }
95
+ }
96
+ for (const {
97
+ mask,
98
+ themeName,
99
+ parentName
100
+ } of maskedThemes) {
101
+ const parent = out[parentName];
102
+ if (!parent) continue;
103
+ const {
104
+ mask: maskName,
105
+ ...options
106
+ } = mask;
107
+ let maskFunction = this.state.masks?.[maskName];
108
+ if (!maskFunction) throw new Error(`No mask ${maskFunction}`);
109
+ const parentTheme = this.state.themes[parentName];
110
+ if (parentTheme && "childOptions" in parentTheme) {
111
+ const {
112
+ mask: mask2,
113
+ ...childOpts
114
+ } = parentTheme.childOptions;
115
+ mask2 && (maskFunction = this.state.masks?.[mask2]), Object.assign(options, childOpts);
116
+ }
117
+ out[themeName] = applyMask(parent, maskFunction, options, parentName, themeName);
118
+ }
119
+ return out;
120
+ }
121
+ }
122
+ function createThemeBuilder() {
123
+ return new ThemeBuilder({});
124
+ }
125
+ export { ThemeBuilder, createThemeBuilder };
@@ -0,0 +1,2 @@
1
+ export * from "./ThemeBuilder.mjs";
2
+ export * from "@tamagui/create-theme";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/theme-builder",
3
- "version": "1.88.13",
3
+ "version": "1.89.0-1706308641099",
4
4
  "source": "src/index.ts",
5
5
  "types": "./types/index.d.ts",
6
6
  "main": "dist/cjs",
@@ -28,11 +28,11 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@tamagui/create-theme": "1.88.13",
31
+ "@tamagui/create-theme": "1.89.0-1706308641099",
32
32
  "color2k": "^2.0.2"
33
33
  },
34
34
  "devDependencies": {
35
- "@tamagui/build": "1.88.13"
35
+ "@tamagui/build": "1.89.0-1706308641099"
36
36
  },
37
37
  "publishConfig": {
38
38
  "access": "public"