@g4rcez/components 0.0.67 → 0.0.69

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 (66) hide show
  1. package/dist/index.css +1 -1
  2. package/dist/index.js +50 -50
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +17470 -14017
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/index.umd.js +50 -50
  7. package/dist/index.umd.js.map +1 -1
  8. package/dist/preset/src/styles/dark.d.ts.map +1 -1
  9. package/dist/preset/src/styles/dark.js +3 -1
  10. package/dist/preset/src/styles/design-tokens.d.ts +9 -0
  11. package/dist/preset/src/styles/design-tokens.d.ts.map +1 -1
  12. package/dist/preset/src/styles/design-tokens.js +27 -9
  13. package/dist/preset/src/styles/light.d.ts.map +1 -1
  14. package/dist/preset/src/styles/light.js +3 -1
  15. package/dist/preset/src/styles/theme.types.d.ts +44 -70
  16. package/dist/preset/src/styles/theme.types.d.ts.map +1 -1
  17. package/dist/src/components/core/resizable.js +1 -1
  18. package/dist/src/components/display/alert.d.ts +1 -1
  19. package/dist/src/components/display/alert.d.ts.map +1 -1
  20. package/dist/src/components/display/alert.js +1 -1
  21. package/dist/src/components/display/calendar.js +1 -1
  22. package/dist/src/components/display/list.d.ts.map +1 -1
  23. package/dist/src/components/display/list.js +2 -1
  24. package/dist/src/components/display/notifications.js +1 -1
  25. package/dist/src/components/display/step.js +1 -1
  26. package/dist/src/components/display/tabs.js +1 -1
  27. package/dist/src/components/floating/expand.d.ts +1 -1
  28. package/dist/src/components/floating/expand.d.ts.map +1 -1
  29. package/dist/src/components/floating/expand.js +1 -1
  30. package/dist/src/components/floating/modal.js +1 -1
  31. package/dist/src/components/floating/toolbar.js +1 -1
  32. package/dist/src/components/form/autocomplete.d.ts +3 -1
  33. package/dist/src/components/form/autocomplete.d.ts.map +1 -1
  34. package/dist/src/components/form/autocomplete.js +28 -32
  35. package/dist/src/components/form/select.d.ts.map +1 -1
  36. package/dist/src/components/form/select.js +8 -4
  37. package/dist/src/components/form/switch.d.ts.map +1 -1
  38. package/dist/src/components/form/switch.js +1 -1
  39. package/dist/src/components/form/task-list.d.ts.map +1 -1
  40. package/dist/src/components/form/task-list.js +2 -1
  41. package/dist/src/components/form/virtual-autocomplete.d.ts +15 -0
  42. package/dist/src/components/form/virtual-autocomplete.d.ts.map +1 -0
  43. package/dist/src/components/form/virtual-autocomplete.js +222 -0
  44. package/dist/src/components/index.d.ts +1 -0
  45. package/dist/src/components/index.d.ts.map +1 -1
  46. package/dist/src/components/index.js +1 -0
  47. package/dist/src/components/table/group.js +1 -1
  48. package/dist/src/components/table/index.js +1 -1
  49. package/dist/src/components/table/thead.d.ts.map +1 -1
  50. package/dist/src/components/table/thead.js +2 -1
  51. package/dist/src/hooks/use-form.d.ts +309 -18
  52. package/dist/src/hooks/use-form.d.ts.map +1 -1
  53. package/dist/src/hooks/use-form.js +327 -90
  54. package/dist/src/index.d.ts +6 -5
  55. package/dist/src/index.d.ts.map +1 -1
  56. package/dist/src/index.js +5 -4
  57. package/dist/src/styles/dark.d.ts.map +1 -1
  58. package/dist/src/styles/dark.js +3 -1
  59. package/dist/src/styles/design-tokens.d.ts +9 -0
  60. package/dist/src/styles/design-tokens.d.ts.map +1 -1
  61. package/dist/src/styles/design-tokens.js +22 -13
  62. package/dist/src/styles/light.d.ts.map +1 -1
  63. package/dist/src/styles/light.js +3 -1
  64. package/dist/src/styles/theme.types.d.ts +44 -70
  65. package/dist/src/styles/theme.types.d.ts.map +1 -1
  66. package/package.json +2 -3
@@ -1,9 +1,92 @@
1
1
  import { parse } from "qs";
2
2
  import { useCallback, useEffect, useRef, useState } from "react";
3
- import { Is, setPath } from "sidekicker";
4
- import { formReset } from "../components/form/form";
3
+ import { getPath, Is } from "sidekicker";
4
+ import { LocalStorage } from "storage-manager-js";
5
+ import { ZodNumber } from "zod";
6
+ import { formReset } from "../components";
5
7
  import { path } from "../lib/fns";
8
+ const isValidJSON = (value) => {
9
+ if (typeof value !== "string") {
10
+ try {
11
+ value = JSON.stringify(value);
12
+ }
13
+ catch (error) {
14
+ return false;
15
+ }
16
+ }
17
+ try {
18
+ JSON.parse(value);
19
+ return true;
20
+ }
21
+ catch (error) {
22
+ return false;
23
+ }
24
+ };
25
+ const setHelper = (obj, path, value) => {
26
+ const lastIndex = path.length - 1;
27
+ for (let i = 0; i < path.length; i++) {
28
+ const key = path[i];
29
+ if (i === lastIndex) {
30
+ obj[key] = value;
31
+ }
32
+ else {
33
+ if (!(key in obj) || typeof obj[key] !== "object") {
34
+ const nextKey = path[i + 1];
35
+ obj[key] = isNaN(Number(nextKey)) ? {} : [];
36
+ }
37
+ obj = obj[key];
38
+ }
39
+ }
40
+ };
41
+ const convertPath = (path) => path.replace(/\[(\d+)]/g, ".$1").split(".");
42
+ const setPath = (o, path, value) => {
43
+ const pathArr = Array.isArray(path) ? path.map(String) : convertPath(path);
44
+ const obj = structuredClone(o);
45
+ setHelper(obj, pathArr, value);
46
+ return obj;
47
+ };
6
48
  const sort = (a, b) => a.localeCompare(b);
49
+ const noop = {};
50
+ const getDefaultValue = (inner) => {
51
+ const instanceName = inner._def.typeName;
52
+ if (instanceName === "ZodDefault")
53
+ return inner._def.defaultValue();
54
+ if (instanceName === "ZodObject")
55
+ return getDefaults(inner);
56
+ if (instanceName === "ZodArray")
57
+ return [];
58
+ if ("innerType" in inner._def) {
59
+ const defaults = getDefaultValue(inner._def.innerType);
60
+ if (instanceName === "ZodArray")
61
+ return defaults ?? [];
62
+ return defaults;
63
+ }
64
+ return undefined;
65
+ };
66
+ const getDefaults = (schema) => Object.fromEntries(Object.entries(schema.shape).map(([key, value]) => {
67
+ return [key, getDefaultValue(value)];
68
+ }));
69
+ const deepMerge = (a, b) => {
70
+ const result = structuredClone(a);
71
+ for (const key in b) {
72
+ const bValue = b[key];
73
+ const aValue = a[key];
74
+ if (bValue !== undefined) {
75
+ if (typeof bValue === "object" &&
76
+ bValue !== null &&
77
+ !Array.isArray(bValue) &&
78
+ typeof aValue === "object" &&
79
+ aValue !== null &&
80
+ !Array.isArray(aValue)) {
81
+ result[key] = deepMerge(aValue, bValue);
82
+ }
83
+ else {
84
+ result[key] = bValue ? bValue : aValue;
85
+ }
86
+ }
87
+ }
88
+ return result;
89
+ };
7
90
  const options = {
8
91
  sort,
9
92
  allowDots: true,
@@ -21,13 +104,14 @@ export const formToJson = (form) => {
21
104
  const urlSearchParams = new URLSearchParams(formData);
22
105
  return parse(urlSearchParams.toString(), options);
23
106
  };
24
- export const convertPath = (path) => path.replace("[", ".").replace("]", "").split(".");
25
- export const getSchemaShape = (name, schema) => convertPath(name).reduce((acc, el) => {
26
- if (el === "")
27
- return acc;
28
- const shape = acc.shape?.[el] || acc;
29
- return shape._def.typeName === "ZodArray" ? shape.element : shape;
30
- }, schema);
107
+ export const getSchemaShape = (name, schema) => {
108
+ return convertPath(name).reduce((acc, el) => {
109
+ if (el === "")
110
+ return acc;
111
+ const shape = acc.shape?.[el] || acc;
112
+ return shape._def.typeName === "ZodArray" ? shape.element : shape;
113
+ }, schema);
114
+ };
31
115
  const getValueByType = (e) => {
32
116
  if (e.dataset.value)
33
117
  return e.dataset.value;
@@ -37,18 +121,94 @@ const getValueByType = (e) => {
37
121
  return e.valueAsNumber;
38
122
  return e.value || e.getAttribute("value");
39
123
  };
40
- export const useForm = (schema, formName) => {
124
+ const getDataTarget = (e) => {
125
+ const target = e.dataset.target;
126
+ if (!target)
127
+ return getValueByType(e);
128
+ const element = document.querySelector(`[data-origin="${target}"]`);
129
+ if (!element)
130
+ return getValueByType(e);
131
+ return getValueByType(element);
132
+ };
133
+ const defaultOptions = {
134
+ state: {},
135
+ loading: false,
136
+ useOnChange: false,
137
+ };
138
+ const getName = (e) => e.dataset.target || e.name;
139
+ export const createFormStorage = (name) => {
140
+ const key = `@use-form/${name}`;
141
+ return {
142
+ get: () => {
143
+ const state = LocalStorage.get(key);
144
+ return isValidJSON(state) ? state : {};
145
+ },
146
+ clear: () => void LocalStorage.delete(key),
147
+ set: (s) => LocalStorage.set(key, s),
148
+ };
149
+ };
150
+ export const useForm = (schema, formName, opts = defaultOptions) => {
41
151
  const [errors, setErrors] = useState(null);
42
152
  const ref = useRef({});
43
- const [state, setState] = useState({});
44
- const datepicker = (name, props) => {
153
+ const [state, setState] = useState(() => {
154
+ if (Is.function(opts?.state))
155
+ return opts.state();
156
+ return opts?.state ?? getDefaults(schema) ?? {};
157
+ });
158
+ const onInvalidField = useCallback((e) => {
159
+ const target = e.target;
160
+ const related = e.currentTarget;
161
+ const isOptional = target.dataset.optional === "true";
162
+ if (isOptional) {
163
+ return;
164
+ }
165
+ const path = getName(target);
166
+ const value = getDataTarget(target) || (related ? getValueByType(related) : "");
167
+ const partialSchema = getSchemaShape(path, schema);
168
+ target.setAttribute("data-initialized", "true");
169
+ if (partialSchema) {
170
+ const validation = partialSchema.safeParse(value);
171
+ const message = validation.success ? undefined : (validation.error.issues[0]?.message ?? "");
172
+ setErrors((prev) => setPath(prev ?? {}, path, message));
173
+ }
174
+ }, []);
175
+ const onInvalid = useCallback((exec) => (event) => {
176
+ const form = event.currentTarget;
177
+ const validationErrors = Object.values(ref.current).reduce((acc, input) => {
178
+ const field = input.element;
179
+ const validation = input.schema.safeParse(getValueByType(field));
180
+ if (field.dataset.ignore === "ignore")
181
+ return acc;
182
+ if (validation.success)
183
+ return acc;
184
+ const errorMessage = validation.error.issues[0]?.message;
185
+ field.setAttribute("data-initialized", "true");
186
+ const name = field.dataset.name || field.name || "";
187
+ return setPath(acc, name, errorMessage);
188
+ }, {});
189
+ const e = Is.empty(validationErrors) ? null : validationErrors;
190
+ setErrors(e);
191
+ console.error(e);
192
+ exec?.({ form, errors: e || {} });
193
+ }, []);
194
+ const datepicker = (name, props = noop) => {
45
195
  const validator = getSchemaShape(name, schema);
196
+ const onChange = (e) => {
197
+ if (!e)
198
+ return;
199
+ const value = e instanceof Date ? e.toISOString() : e.target.value;
200
+ setState((prev) => setPath(prev, name, value));
201
+ props?.onChange?.(e);
202
+ };
46
203
  return {
47
204
  ...props,
205
+ loading: opts.loading,
48
206
  name,
49
207
  id: name,
208
+ onChange,
50
209
  form: formName,
51
- required: !validator.isOptional(),
210
+ date: Is.string(state[name]) ? new Date(state[name]) : state[name],
211
+ required: props.required ?? !validator.isOptional(),
52
212
  error: errors?.[name],
53
213
  ref: (e) => {
54
214
  if (e === null)
@@ -57,53 +217,112 @@ export const useForm = (schema, formName) => {
57
217
  },
58
218
  };
59
219
  };
60
- const select = (name, props) => {
220
+ const select = (name, props = noop) => {
61
221
  const validator = getSchemaShape(name, schema);
222
+ const onChange = (e) => {
223
+ const value = e.target.value;
224
+ setState((prev) => setPath(prev, name, value));
225
+ props?.onChange?.(e);
226
+ };
62
227
  return {
63
228
  ...props,
64
229
  name,
65
230
  id: name,
231
+ onChange,
66
232
  form: formName,
67
- required: !validator.isOptional(),
68
- error: errors?.[name],
69
- ref: (e) => {
70
- if (e === null)
71
- return;
72
- ref.current[name] = { element: e, schema: validator };
73
- },
233
+ loading: opts.loading,
234
+ onInvalid: onInvalidField,
235
+ error: getPath(errors, name, undefined),
236
+ value: getPath(state, name, undefined) || "",
237
+ required: props.required ?? !validator.isOptional(),
238
+ ref: (e) => e !== null
239
+ ? (ref.current[name] = {
240
+ element: e,
241
+ schema: validator,
242
+ })
243
+ : undefined,
74
244
  };
75
245
  };
76
- const checkbox = (name, props) => {
246
+ const checkbox = (name, props = noop) => {
77
247
  const validator = getSchemaShape(name, schema);
248
+ const onChange = (e) => {
249
+ const value = e.target.checked;
250
+ setState((prev) => setPath(prev ?? {}, name, value));
251
+ props?.onChange?.(e);
252
+ };
78
253
  return {
79
254
  ...props,
80
255
  name,
81
256
  id: name,
257
+ onChange,
82
258
  form: formName,
83
- required: !validator.isOptional(),
84
- error: errors?.[name],
85
- ref: (e) => {
86
- if (e === null)
87
- return;
88
- ref.current[name] = { element: e, schema: validator };
89
- },
259
+ loading: opts.loading,
260
+ onInvalid: onInvalidField,
261
+ error: getPath(errors, name, undefined),
262
+ required: props.required ?? !validator.isOptional(),
263
+ ref: (e) => e !== null
264
+ ? void (ref.current[name] = {
265
+ element: e,
266
+ schema: validator,
267
+ })
268
+ : undefined,
90
269
  };
91
270
  };
92
- const input = (name, props) => {
271
+ const textarea = (name, props = noop) => {
93
272
  const validator = getSchemaShape(name, schema);
273
+ const onChange = (e) => {
274
+ e.persist?.();
275
+ const value = e.target.value;
276
+ props?.onChange?.(e);
277
+ setState((prev) => setPath(prev, name, value));
278
+ };
94
279
  return {
95
280
  ...props,
281
+ loading: opts.loading,
96
282
  name,
97
283
  id: name,
98
- required: !validator.isOptional(),
284
+ onChange,
99
285
  form: formName,
100
- type: validator._def.typeName === "ZodNumber" ? "number" : (props?.type ?? "text"),
101
- error: errors?.[name],
102
- ref: (e) => {
103
- if (e === null)
104
- return;
105
- ref.current[name] = { element: e, schema: validator };
106
- },
286
+ onInvalid: onInvalidField,
287
+ error: getPath(errors, name, undefined),
288
+ required: props.required ?? !validator.isOptional(),
289
+ value: getPath(state, name, undefined) || props?.value || "",
290
+ type: Is.instance(validator, ZodNumber) ? "number" : (props?.type ?? "text"),
291
+ ref: (e) => e === null
292
+ ? undefined
293
+ : (ref.current[name] = {
294
+ element: e,
295
+ schema: validator,
296
+ }),
297
+ };
298
+ };
299
+ const input = (name, props = noop) => {
300
+ const validator = getSchemaShape(name, schema);
301
+ const isNumber = validator._def.typeName === "ZodNumber";
302
+ const onChange = (e) => {
303
+ e.persist?.();
304
+ const value = isNumber ? e.target.valueAsNumber : e.target.value;
305
+ props?.onChange?.(e);
306
+ setState((prev) => setPath(prev, name, value));
307
+ };
308
+ return {
309
+ ...props,
310
+ name,
311
+ id: name,
312
+ onChange,
313
+ form: formName,
314
+ loading: opts.loading,
315
+ onInvalid: onInvalidField,
316
+ error: getPath(errors, name, undefined),
317
+ required: props.required ?? !validator.isOptional(),
318
+ type: isNumber ? "number" : (props?.type ?? "text"),
319
+ value: getPath(state, name, undefined) || props?.value || "",
320
+ ref: (e) => e === null
321
+ ? undefined
322
+ : void (ref.current[name] = {
323
+ element: e,
324
+ schema: validator,
325
+ }),
107
326
  };
108
327
  };
109
328
  useEffect(() => {
@@ -113,37 +332,45 @@ export const useForm = (schema, formName) => {
113
332
  : input.element;
114
333
  const validation = input.schema.safeParse(getValueByType(element));
115
334
  const onBlurField = (e) => {
116
- const name = element.dataset.target || element.name;
335
+ const name = getName(e.target);
117
336
  if (!name)
118
- return;
119
- const value = getValueByType(e.target) || (e.relatedTarget ? getValueByType(e.relatedTarget) : "");
337
+ return false;
338
+ const current = e.target;
339
+ const value = getDataTarget(e.target) || (e.relatedTarget ? getValueByType(e.relatedTarget) : "");
120
340
  const validation = input.schema.safeParse(value);
341
+ current.setAttribute("value", value);
121
342
  if (validation.success) {
122
343
  element.setCustomValidity("");
123
- setState((prev) => setPath(prev, name, validation.data));
124
- return setErrors((prev) => {
125
- const { [name]: removed, ...rest } = prev || {};
126
- return rest === null || Is.empty(rest) ? null : rest;
344
+ setErrors((prev) => {
345
+ if (Is.null(prev))
346
+ return null;
347
+ const newErrors = setPath(prev, name, undefined);
348
+ if (Is.empty(prev) || Is.nil(prev))
349
+ return null;
350
+ return Is.empty(newErrors) ? null : newErrors;
127
351
  });
352
+ return false;
128
353
  }
129
354
  if (element.required) {
130
355
  const errorMessage = validation.error.issues[0]?.message || "";
131
356
  element.setCustomValidity(errorMessage);
132
- setErrors((prev) => ({ ...prev, [name]: errorMessage }));
357
+ setErrors((prev) => {
358
+ if (Is.null(prev))
359
+ return null;
360
+ const newErrors = setPath(prev, name, errorMessage || undefined);
361
+ return Is.empty(newErrors) ? null : newErrors;
362
+ });
133
363
  }
364
+ return false;
134
365
  };
366
+ const controller = new AbortController();
135
367
  const trigger = element.getAttribute("data-trigger") || "blur";
136
- element.addEventListener(trigger, onBlurField);
137
- if (element.tagName === "SELECT")
138
- element.addEventListener("change", onBlurField);
368
+ element.addEventListener(trigger, onBlurField, { signal: controller.signal });
369
+ const hasInitialError = element.dataset.shadow ? false : element.required ? !validation.success : false;
139
370
  return {
140
371
  input,
141
- hasInitialError: element.required ? !validation.success : false,
142
- unsubscribe: () => {
143
- element.removeEventListener(trigger, onBlurField);
144
- if (element.tagName === "SELECT")
145
- element.addEventListener("change", onBlurField);
146
- },
372
+ hasInitialError,
373
+ unsubscribe: () => controller.abort(),
147
374
  };
148
375
  });
149
376
  const hasErrors = events.some((x) => x.hasInitialError);
@@ -151,25 +378,7 @@ export const useForm = (schema, formName) => {
151
378
  setErrors((prev) => (prev === null ? {} : prev));
152
379
  return () => events.forEach((item) => item.unsubscribe());
153
380
  });
154
- const onInvalid = useCallback((exec) => (event) => {
155
- const form = event.currentTarget;
156
- const validationErrors = Object.values(ref.current).reduce((acc, input) => {
157
- const field = input.element;
158
- const validation = input.schema.safeParse(getValueByType(field));
159
- if (field.dataset.ignore === "ignore")
160
- return acc;
161
- if (validation.success)
162
- return acc;
163
- const errorMessage = validation.error.issues[0]?.message;
164
- field.setAttribute("data-initialized", "true");
165
- const name = field.dataset.name || field.name || "";
166
- return { ...acc, [name]: errorMessage };
167
- }, {});
168
- const e = Is.empty(validationErrors) ? null : validationErrors;
169
- setErrors(e);
170
- exec?.({ form, errors: e || {} });
171
- }, []);
172
- const onSubmit = useCallback((exec) => (event) => {
381
+ const onSubmit = (exec) => (event) => {
173
382
  event.preventDefault();
174
383
  const form = event.currentTarget;
175
384
  let json = formToJson(form);
@@ -181,43 +390,71 @@ export const useForm = (schema, formName) => {
181
390
  }
182
391
  if (field.tagName === "INPUT") {
183
392
  const input = field;
184
- json = setPath(json, input.name, getValueByType(input));
393
+ const value = getDataTarget(input) || (input ? getValueByType(input) : "");
394
+ json = setPath(json, input.dataset.target || input.name, value);
185
395
  }
186
396
  });
187
- const result = schema.safeParse(state);
397
+ const input = deepMerge(json, state);
398
+ const result = schema.safeParse(input);
399
+ const reset = () => {
400
+ formReset(form);
401
+ opts.interceptor?.clear();
402
+ };
188
403
  if (result.success) {
189
- return exec({
404
+ const jsonString = JSON.stringify(result.data);
405
+ form.setAttribute("data-json", jsonString);
406
+ opts.interceptor?.clear();
407
+ return exec?.(event, {
190
408
  form,
191
- json,
192
- data: result.data,
193
409
  event,
194
- reset: () => formReset(form),
195
- success: true,
410
+ reset,
196
411
  errors: [],
412
+ success: true,
413
+ data: result.data,
414
+ json: result.data,
197
415
  });
198
416
  }
199
- return exec({
417
+ console.group("useForm error");
418
+ console.info(result);
419
+ console.info(result.error.issues);
420
+ console.error(result.error);
421
+ console.groupEnd();
422
+ form.reportValidity();
423
+ return exec?.(event, {
200
424
  form,
201
425
  json,
202
426
  event,
427
+ reset,
203
428
  data: json,
204
429
  success: false,
205
- reset: () => formReset(form),
206
430
  errors: result.error.issues.map((x) => ({ message: x.message, path: x.path.map((x) => String(x)) })),
207
431
  });
208
- }, [formName]);
432
+ };
209
433
  const get = (p) => path(state, p) || "";
434
+ const controller = (props) => ({
435
+ ...props,
436
+ id: formName,
437
+ name: formName,
438
+ });
439
+ useEffect(() => {
440
+ if (opts.interceptor)
441
+ opts.interceptor?.set(state);
442
+ }, [state]);
210
443
  return {
211
- state,
444
+ get,
212
445
  input,
213
- datepicker,
214
- checkbox,
446
+ state,
447
+ errors,
215
448
  select,
449
+ checkbox,
216
450
  onSubmit,
217
- errors,
451
+ setState,
452
+ textarea,
218
453
  onInvalid,
219
- disabled: errors !== null,
454
+ controller,
455
+ datepicker,
220
456
  name: formName,
221
- get,
457
+ disabled: errors !== null,
222
458
  };
223
459
  };
460
+ export const getJsonForm = (form) => !form ? {} : JSON.parse(form.getAttribute("data-json"));
@@ -1,12 +1,13 @@
1
+ export * from "./lib/dom";
2
+ export * from "./lib/fns";
1
3
  export * from "./components";
4
+ export type * from "./types";
5
+ export * from "./styles/theme";
2
6
  export * from "./hooks/use-form";
3
7
  export * from "./hooks/use-previous";
4
8
  export * from "./hooks/use-reactive";
5
- export { ComponentsProvider, useTranslations, type Translations, useLocale, useColorParser } from "./hooks/use-components-provider";
6
- export * from "./lib/dom";
7
- export * from "./lib/fns";
8
- export * from "./styles/theme";
9
+ export * from "./styles/theme.types";
9
10
  export * from "./styles/design-tokens";
10
- export type * from "./types";
11
11
  export { createColumns, createOptionCols, ColType, useTablePreferences } from "./components/table/table-lib";
12
+ export { ComponentsProvider, useTranslations, type Translations, useLocale, useColorParser } from "./hooks/use-components-provider";
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,KAAK,YAAY,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACpI,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC;AACvC,mBAAmB,SAAS,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,mBAAmB,SAAS,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAC7G,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,KAAK,YAAY,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC"}
package/dist/src/index.js CHANGED
@@ -1,10 +1,11 @@
1
+ export * from "./lib/dom";
2
+ export * from "./lib/fns";
1
3
  export * from "./components";
4
+ export * from "./styles/theme";
2
5
  export * from "./hooks/use-form";
3
6
  export * from "./hooks/use-previous";
4
7
  export * from "./hooks/use-reactive";
5
- export { ComponentsProvider, useTranslations, useLocale, useColorParser } from "./hooks/use-components-provider";
6
- export * from "./lib/dom";
7
- export * from "./lib/fns";
8
- export * from "./styles/theme";
8
+ export * from "./styles/theme.types";
9
9
  export * from "./styles/design-tokens";
10
10
  export { createColumns, createOptionCols, ColType, useTablePreferences } from "./components/table/table-lib";
11
+ export { ComponentsProvider, useTranslations, useLocale, useColorParser } from "./hooks/use-components-provider";
@@ -1 +1 @@
1
- {"version":3,"file":"dark.d.ts","sourceRoot":"","sources":["../../../src/styles/dark.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,eAAO,MAAM,UAAU,EAAE,YAsLxB,CAAC"}
1
+ {"version":3,"file":"dark.d.ts","sourceRoot":"","sources":["../../../src/styles/dark.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,eAAO,MAAM,UAAU,EAAE,YAwLxB,CAAC"}
@@ -69,6 +69,7 @@ export const DARK_THEME = {
69
69
  border: "hsla(240, 7%, 27%)",
70
70
  },
71
71
  floating: {
72
+ foreground: "hsla(210, 40%, 98%)",
72
73
  background: "hsla(0, 0%, 14%)",
73
74
  border: "hsla(240, 7%, 27%)",
74
75
  overlay: "hsla(0, 0%, 0%)",
@@ -77,11 +78,12 @@ export const DARK_THEME = {
77
78
  foreground: "hsla(210, 40%, 98%)",
78
79
  background: "hsla(0, 0%, 8%)",
79
80
  border: "hsla(0, 0%, 19%)",
81
+ overlay: "hsla(0, 0%, 0%)",
80
82
  },
81
83
  table: {
82
84
  header: "hsla(0, 0%, 12%)",
83
- background: "hsla(0, 0%, 15%)",
84
85
  border: "hsla(240, 4%, 33%)",
86
+ background: "hsla(0, 0%, 15%)",
85
87
  },
86
88
  button: {
87
89
  primary: {
@@ -1,3 +1,4 @@
1
+ import { CSSProperties } from "react";
1
2
  import { DesignTokens, DesignTokensBuilder, DesignTokensParser, GeneralTokens, Token } from "./theme.types";
2
3
  export declare const parsers: {
3
4
  cssVariable: (_: string, __: string, k: string) => `var(--${string})`;
@@ -15,5 +16,13 @@ export declare const createStyles: {
15
16
  default: (tokens: Token[]) => string;
16
17
  dark: (tokens: Token[]) => string;
17
18
  };
19
+ type TokenParsersType = "colors" | "spacing" | "rounded" | "customTokens";
20
+ type TokenCustomParser = (token: Token) => Token;
21
+ export type TokenRemap = Partial<Record<TokenParsersType, TokenCustomParser> & {
22
+ name: string;
23
+ }>;
18
24
  export declare const createTheme: (theme: DesignTokens, name?: string) => string;
25
+ export declare const createCssProperties: (theme: DesignTokens, map?: TokenRemap) => CSSProperties;
26
+ export declare const createTokenStyles: (theme: DesignTokens, map?: TokenRemap) => string;
27
+ export {};
19
28
  //# sourceMappingURL=design-tokens.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"design-tokens.d.ts","sourceRoot":"","sources":["../../../src/styles/design-tokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE5G,eAAO,MAAM,OAAO;;cAEN,MAAM;aACP,MAAM;aACN,MAAM;cACL,MAAM;aACP,MAAM;aACN,MAAM;4BACS,MAAM,SAAS,MAAM,MAAM,MAAM,KAAK,MAAM;CAC1B,CAAC;AAE/C,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,aAAa,UAAU,CAAC,SAAS,mBAAmB,WAAU,MAAM,WAAe,MAAM,KAAQ,KAAK,EAQnI,CAAC;AAEX,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,aAAa,EAAE,EAAE,SAAS,kBAAkB,UAC7E,CAAC,SACF,EAAE,WACD,MAAM,WACN,MAAM,KACf,CAWY,CAAC;AAmBhB,eAAO,MAAM,YAAY;sBACH,KAAK,EAAE;mBACV,KAAK,EAAE;CACzB,CAAC;AAEF,eAAO,MAAM,WAAW,UAAW,YAAY,SAAS,MAAM,WAa7D,CAAC"}
1
+ {"version":3,"file":"design-tokens.d.ts","sourceRoot":"","sources":["../../../src/styles/design-tokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE5G,eAAO,MAAM,OAAO;;cAEN,MAAM;aACP,MAAM;aACN,MAAM;cACL,MAAM;aACP,MAAM;aACN,MAAM;4BACS,MAAM,SAAS,MAAM,MAAM,MAAM,KAAK,MAAM;CAC1B,CAAC;AAE/C,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,aAAa,UAAU,CAAC,SAAS,mBAAmB,WAAU,MAAM,WAAe,MAAM,KAAQ,KAAK,EAQnI,CAAC;AAEX,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,aAAa,EAAE,EAAE,SAAS,kBAAkB,UAC7E,CAAC,SACF,EAAE,WACD,MAAM,WACN,MAAM,KACf,CAWY,CAAC;AAmBhB,eAAO,MAAM,YAAY;sBACH,KAAK,EAAE;mBACV,KAAK,EAAE;CACzB,CAAC;AAEF,KAAK,gBAAgB,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,cAAc,CAAC;AAE1E,KAAK,iBAAiB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,CAAC;AAEjD,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAgBjG,eAAO,MAAM,WAAW,UAAW,YAAY,SAAS,MAAM,WAIxD,CAAC;AAEP,eAAO,MAAM,mBAAmB,UAAW,YAAY,QAAQ,UAAU,KAAG,aAG3E,CAAC;AAEF,eAAO,MAAM,iBAAiB,UAAW,YAAY,QAAQ,UAAU,WAIjE,CAAC"}
@@ -40,17 +40,26 @@ export const createStyles = {
40
40
  default: (tokens) => createStyleContent(tokens, { result: modifiers.default }),
41
41
  dark: (tokens) => createStyleContent(tokens, { result: modifiers.dark }),
42
42
  };
43
- export const createTheme = (theme, name) => {
44
- const fn = (value, _, key) => ({
45
- key: `--${key}`,
46
- value: `${value}`,
47
- });
48
- const colors = reduceTokens(theme.colors, fn);
49
- const spacing = reduceTokens(theme.spacing, fn);
50
- const rounded = reduceTokens(theme.rounded, fn);
51
- const customTokens = theme.custom ? reduceTokens(theme.custom, fn) : [];
52
- return createStyleContent(colors.concat(spacing, rounded, customTokens), {
53
- result: (variables) => `html${name ? `.${name}` : ""} {${variables}}`,
54
- value: (_, v) => v.replace("hsla(", "").replace(")", ""),
55
- });
43
+ const createTokens = (theme, map) => {
44
+ const fn = (p) => (value, _, key) => {
45
+ const r = { key: `--${key}`, value: `${value}` };
46
+ return p ? p(r) : r;
47
+ };
48
+ const colors = reduceTokens(theme.colors, fn(map?.colors));
49
+ const spacing = reduceTokens(theme.spacing, fn(map?.spacing));
50
+ const rounded = reduceTokens(theme.rounded, fn(map?.rounded));
51
+ const customTokens = theme.custom ? reduceTokens(theme.custom, fn(map?.customTokens)) : [];
52
+ return colors.concat(spacing, rounded, customTokens);
53
+ };
54
+ export const createTheme = (theme, name) => createStyleContent(createTokens(theme), {
55
+ result: (variables) => `html${name ? `.${name}` : ""} {${variables}}`,
56
+ value: (_, v) => v.replace("hsla(", "").replace(")", ""),
57
+ });
58
+ export const createCssProperties = (theme, map) => {
59
+ const tokens = createTokens(theme, map);
60
+ return tokens.reduce((acc, el) => ({ ...acc, [el.key]: el.value }), {});
56
61
  };
62
+ export const createTokenStyles = (theme, map) => createStyleContent(createTokens(theme, map), {
63
+ result: (variables) => `html${map?.name ? `.${map.name}` : ""} {${variables}}`,
64
+ value: (_, v) => v,
65
+ });