@styleframe/core 1.0.1 → 1.0.2

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/LICENSE +21 -0
  3. package/dist/styleframe.d.ts +301 -0
  4. package/dist/styleframe.js +528 -0
  5. package/dist/styleframe.umd.cjs +1 -0
  6. package/package.json +13 -3
  7. package/.tsbuildinfo +0 -1
  8. package/src/index.ts +0 -5
  9. package/src/styleframe.ts +0 -64
  10. package/src/tokens/atRule.test.ts +0 -1013
  11. package/src/tokens/atRule.ts +0 -67
  12. package/src/tokens/css.test.ts +0 -404
  13. package/src/tokens/css.ts +0 -23
  14. package/src/tokens/declarations.test.ts +0 -584
  15. package/src/tokens/declarations.ts +0 -71
  16. package/src/tokens/index.ts +0 -11
  17. package/src/tokens/modifier.test.ts +0 -90
  18. package/src/tokens/modifier.ts +0 -86
  19. package/src/tokens/recipe.test.ts +0 -105
  20. package/src/tokens/recipe.ts +0 -32
  21. package/src/tokens/ref.test.ts +0 -430
  22. package/src/tokens/ref.ts +0 -24
  23. package/src/tokens/root.test.ts +0 -70
  24. package/src/tokens/root.ts +0 -14
  25. package/src/tokens/selector.test.ts +0 -440
  26. package/src/tokens/selector.ts +0 -47
  27. package/src/tokens/theme.test.ts +0 -338
  28. package/src/tokens/theme.ts +0 -26
  29. package/src/tokens/utility.test.ts +0 -1456
  30. package/src/tokens/utility.ts +0 -92
  31. package/src/tokens/variable.test.ts +0 -235
  32. package/src/tokens/variable.ts +0 -42
  33. package/src/typeGuards.test.ts +0 -33
  34. package/src/typeGuards.ts +0 -98
  35. package/src/types/declarations.ts +0 -42
  36. package/src/types/index.ts +0 -3
  37. package/src/types/options.ts +0 -22
  38. package/src/types/tokens.ts +0 -149
  39. package/src/utils/capitalizeFirst.ts +0 -9
  40. package/src/utils/deepClone.ts +0 -317
  41. package/src/utils/getters.test.ts +0 -399
  42. package/src/utils/getters.ts +0 -36
  43. package/src/utils/index.ts +0 -4
  44. package/src/utils/merge.test.ts +0 -978
  45. package/src/utils/merge.ts +0 -73
  46. package/src/vite-env.d.ts +0 -1
  47. package/tsconfig.json +0 -7
  48. package/vite.config.ts +0 -5
@@ -1,338 +0,0 @@
1
- import type {
2
- DeclarationsCallback,
3
- DeclarationsCallbackContext,
4
- Root,
5
- Selector,
6
- Theme,
7
- } from "../types";
8
- import { createRoot } from "./root";
9
- import { createThemeFunction } from "./theme";
10
-
11
- describe("createThemeFunction", () => {
12
- let parent: Root;
13
- let root: Root;
14
- let theme: ReturnType<typeof createThemeFunction>;
15
-
16
- beforeEach(() => {
17
- root = createRoot();
18
- parent = root;
19
-
20
- theme = createThemeFunction(parent, root);
21
- });
22
-
23
- describe("when creating a new theme", () => {
24
- it("should create a new theme with the given name", () => {
25
- const callback: DeclarationsCallback = () => {};
26
-
27
- const result = theme("dark", callback);
28
-
29
- expect(result).toEqual({
30
- type: "theme",
31
- name: "dark",
32
- declarations: {},
33
- variables: [],
34
- children: [],
35
- });
36
- });
37
-
38
- it("should add the new theme to root.themes array", () => {
39
- const callback: DeclarationsCallback = () => {};
40
-
41
- const result = theme("dark", callback);
42
-
43
- expect(root.themes).toHaveLength(1);
44
- expect(root.themes[0]).toBe(result);
45
- expect(root.themes[0]).toEqual({
46
- type: "theme",
47
- name: "dark",
48
- declarations: {},
49
- variables: [],
50
- children: [],
51
- });
52
- });
53
-
54
- it("should call the callback with declarations context", () => {
55
- let callbackWasCalled = false;
56
- let receivedContext: DeclarationsCallbackContext | null = null;
57
-
58
- const callback: DeclarationsCallback = (context) => {
59
- callbackWasCalled = true;
60
- receivedContext = context;
61
- };
62
-
63
- theme("dark", callback);
64
-
65
- expect(callbackWasCalled).toBe(true);
66
- expect(receivedContext).toBeDefined();
67
- if (!receivedContext) {
68
- throw new Error("Callback context should not be null");
69
- }
70
-
71
- expect(receivedContext).toHaveProperty("variable");
72
- expect(receivedContext).toHaveProperty("selector");
73
- expect(receivedContext).toHaveProperty("keyframes");
74
- expect(receivedContext).toHaveProperty("media");
75
-
76
- expect(
77
- (receivedContext as DeclarationsCallbackContext).variable,
78
- ).toBeTypeOf("function");
79
- expect(
80
- (receivedContext as DeclarationsCallbackContext).selector,
81
- ).toBeTypeOf("function");
82
- expect(
83
- (receivedContext as DeclarationsCallbackContext).keyframes,
84
- ).toBeTypeOf("function");
85
- expect((receivedContext as DeclarationsCallbackContext).media).toBeTypeOf(
86
- "function",
87
- );
88
- });
89
-
90
- it("should allow callback to add children to the theme", () => {
91
- const callback: DeclarationsCallback = (context) => {
92
- context.variable("primary-color", "#000000");
93
- context.selector("body", { color: "#ffffff" });
94
- };
95
-
96
- const result = theme("dark", callback);
97
-
98
- expect(result.children.length).toBeGreaterThan(0);
99
- });
100
- });
101
-
102
- describe("when theme already exists", () => {
103
- it("should return the existing theme instead of creating a new one", () => {
104
- const existingTheme: Theme = {
105
- type: "theme",
106
- name: "dark",
107
- declarations: {},
108
- variables: [],
109
- children: [],
110
- };
111
- root.themes.push(existingTheme);
112
-
113
- const callback: DeclarationsCallback = () => {};
114
-
115
- const result = theme("dark", callback);
116
-
117
- expect(result).toBe(existingTheme);
118
- expect(root.themes).toHaveLength(1);
119
- });
120
-
121
- it("should not add duplicate theme to root.themes array", () => {
122
- const existingTheme: Theme = {
123
- type: "theme",
124
- name: "dark",
125
- declarations: {},
126
- variables: [],
127
- children: [],
128
- };
129
- root.themes.push(existingTheme);
130
-
131
- const callback: DeclarationsCallback = () => {};
132
-
133
- theme("dark", callback);
134
-
135
- expect(root.themes).toHaveLength(1);
136
- expect(root.themes[0]).toBe(existingTheme);
137
- });
138
-
139
- it("should still call the callback with existing theme", () => {
140
- const existingTheme: Theme = {
141
- type: "theme",
142
- name: "dark",
143
- declarations: {},
144
- variables: [],
145
- children: [],
146
- };
147
- root.themes.push(existingTheme);
148
-
149
- let callbackWasCalled = false;
150
- const callback: DeclarationsCallback = (context) => {
151
- callbackWasCalled = true;
152
- context.variable("test-var", "test-value");
153
- };
154
-
155
- const result = theme("dark", callback);
156
-
157
- expect(callbackWasCalled).toBe(true);
158
- expect(result).toBe(existingTheme);
159
- expect(result.variables.length).toBeGreaterThan(0);
160
- });
161
- });
162
-
163
- describe("when callback is not provided", () => {
164
- it("should create theme without calling any callback", () => {
165
- // biome-ignore lint/suspicious/noExplicitAny: Required for test
166
- const result = theme("light", undefined as any);
167
-
168
- expect(result).toEqual({
169
- type: "theme",
170
- name: "light",
171
- declarations: {},
172
- variables: [],
173
- children: [],
174
- });
175
- expect(root.themes).toHaveLength(1);
176
- expect(result.children).toHaveLength(0);
177
- });
178
-
179
- it("should handle null callback gracefully", () => {
180
- // biome-ignore lint/suspicious/noExplicitAny: Required for test
181
- const result = theme("light", null as any);
182
-
183
- expect(result).toEqual({
184
- type: "theme",
185
- name: "light",
186
- declarations: {},
187
- variables: [],
188
- children: [],
189
- });
190
- expect(root.themes).toHaveLength(1);
191
- });
192
- });
193
-
194
- describe("multiple theme creation", () => {
195
- it("should handle creating multiple different themes", () => {
196
- const callback1: DeclarationsCallback = (context) => {
197
- context.variable("dark-bg", "#000000");
198
- };
199
-
200
- const callback2: DeclarationsCallback = (context) => {
201
- context.variable("light-bg", "#ffffff");
202
- };
203
-
204
- const darkTheme = theme("dark", callback1);
205
- const lightTheme = theme("light", callback2);
206
-
207
- expect(root.themes).toHaveLength(2);
208
- expect(darkTheme.name).toBe("dark");
209
- expect(lightTheme.name).toBe("light");
210
- expect(darkTheme.variables.length).toBeGreaterThan(0);
211
- expect(lightTheme.variables.length).toBeGreaterThan(0);
212
- });
213
-
214
- it("should handle mixed new and existing themes", () => {
215
- const existingTheme: Theme = {
216
- type: "theme",
217
- name: "existing",
218
- declarations: {},
219
- variables: [],
220
- children: [],
221
- };
222
- root.themes.push(existingTheme);
223
-
224
- const callback: DeclarationsCallback = (context) => {
225
- context.variable("test-var", "test-value");
226
- };
227
-
228
- const retrievedTheme = theme("existing", callback);
229
- const newTheme = theme("new", callback);
230
-
231
- expect(root.themes).toHaveLength(2);
232
- expect(retrievedTheme).toBe(existingTheme);
233
- expect(newTheme.name).toBe("new");
234
- expect(retrievedTheme.variables.length).toBeGreaterThan(0);
235
- expect(newTheme.variables.length).toBeGreaterThan(0);
236
- });
237
- });
238
-
239
- describe("callback functionality", () => {
240
- it("should provide working variable function in callback context", () => {
241
- const callback: DeclarationsCallback = (context) => {
242
- const variable = context.variable("test-color", "#ff0000");
243
- expect(variable).toBeDefined();
244
- expect(variable.name).toBe("test-color");
245
- expect(variable.value).toBe("#ff0000");
246
- };
247
-
248
- const result = theme("test", callback);
249
- expect(result.variables.length).toBe(1);
250
- });
251
-
252
- it("should provide working selector function in callback context", () => {
253
- let selectorResult: Selector | undefined;
254
-
255
- const callback: DeclarationsCallback = (context) => {
256
- selectorResult = context.selector(".test", { color: "red" });
257
- };
258
-
259
- const result = theme("test", callback);
260
-
261
- expect(selectorResult).toBeDefined();
262
- expect(result.children.length).toBe(1);
263
- });
264
-
265
- it("should allow complex theme definitions with multiple elements", () => {
266
- const callback: DeclarationsCallback = (context) => {
267
- context.variable("primary", "#007bff");
268
- context.variable("secondary", "#6c757d");
269
- context.selector(".btn-primary", {
270
- backgroundColor: "var(--primary)",
271
- color: "#fff",
272
- });
273
- context.selector(".btn-secondary", {
274
- backgroundColor: "var(--secondary)",
275
- color: "#fff",
276
- });
277
- };
278
-
279
- const result = theme("bootstrap-theme", callback);
280
-
281
- expect(result.children).toHaveLength(2);
282
- expect(result.variables).toHaveLength(2);
283
- expect(root.themes).toHaveLength(1);
284
- });
285
- });
286
-
287
- describe("edge cases", () => {
288
- it("should handle empty theme name", () => {
289
- const callback: DeclarationsCallback = () => {};
290
-
291
- const result = theme("", callback);
292
-
293
- expect(result.name).toBe("");
294
- expect(result.type).toBe("theme");
295
- expect(root.themes).toHaveLength(1);
296
- });
297
-
298
- it("should handle theme names with special characters", () => {
299
- const callback: DeclarationsCallback = () => {};
300
- const specialName = "theme-with_special.chars@123";
301
-
302
- const result = theme(specialName, callback);
303
-
304
- expect(result.name).toBe(specialName);
305
- expect(root.themes[0]?.name).toBe(specialName);
306
- });
307
-
308
- it("should preserve existing children when theme already exists", () => {
309
- const existingTheme: Theme = {
310
- type: "theme",
311
- name: "existing",
312
- declarations: {},
313
- variables: [
314
- {
315
- type: "variable",
316
- name: "existing-var",
317
- value: "existing-value",
318
- },
319
- ],
320
- children: [],
321
- };
322
- root.themes.push(existingTheme);
323
-
324
- const callback: DeclarationsCallback = (context) => {
325
- context.variable("new-var", "new-value");
326
- };
327
-
328
- const result = theme("existing", callback);
329
-
330
- expect(result.variables).toHaveLength(2);
331
- expect(result.variables[0]).toEqual({
332
- type: "variable",
333
- name: "existing-var",
334
- value: "existing-value",
335
- });
336
- });
337
- });
338
- });
@@ -1,26 +0,0 @@
1
- import type { Container, DeclarationsCallback, Root, Theme } from "../types";
2
- import { createDeclarationsCallbackContext } from "./declarations";
3
-
4
- export function createThemeFunction(_parent: Container, root: Root) {
5
- return function theme(name: string, callback: DeclarationsCallback): Theme {
6
- const existingTheme = root.themes.find((t) => t.name === name);
7
- const instance: Theme = existingTheme ?? {
8
- type: "theme",
9
- name,
10
- declarations: {},
11
- variables: [],
12
- children: [],
13
- };
14
-
15
- if (!existingTheme) {
16
- root.themes.push(instance);
17
- }
18
-
19
- const callbackContext = createDeclarationsCallbackContext(instance, root);
20
- if (callback) {
21
- callback(callbackContext);
22
- }
23
-
24
- return instance;
25
- };
26
- }