lilact 0.27.0 → 0.27.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 (73) hide show
  1. package/dist/lilact.development.js +238 -135
  2. package/dist/lilact.development.js.map +3 -3
  3. package/dist/lilact.development.min.js +57 -57
  4. package/dist/lilact.development.min.js.map +3 -3
  5. package/dist/lilact.production.min.js +57 -57
  6. package/docs/classes/accessories.ErrorBoundary.html +3 -3
  7. package/docs/classes/accessories.Suspense.html +2 -2
  8. package/docs/classes/components.Component.html +3 -3
  9. package/docs/classes/components.HTMLComponent.html +4 -4
  10. package/docs/classes/components.RootComponent.html +5 -5
  11. package/docs/functions/components.cloneComponent.html +1 -1
  12. package/docs/functions/components.createComponent.html +1 -1
  13. package/docs/functions/components.createPortal.html +1 -1
  14. package/docs/functions/components.createRoot.html +1 -1
  15. package/docs/functions/components.memo.html +1 -1
  16. package/docs/functions/components.render.html +1 -1
  17. package/docs/functions/hooks.createContext.html +1 -1
  18. package/docs/functions/hooks.startTransition.html +1 -1
  19. package/docs/functions/hooks.useActionState.html +1 -1
  20. package/docs/functions/hooks.useContext.html +1 -1
  21. package/docs/functions/hooks.useDebugValue.html +1 -1
  22. package/docs/functions/hooks.useDeferredValue.html +1 -1
  23. package/docs/functions/hooks.useEffect.html +1 -1
  24. package/docs/functions/hooks.useId.html +1 -1
  25. package/docs/functions/hooks.useImperativeHandle.html +1 -1
  26. package/docs/functions/hooks.useInsertionEffect.html +1 -1
  27. package/docs/functions/hooks.useLayoutEffect.html +1 -1
  28. package/docs/functions/hooks.useLocalStorage.html +1 -1
  29. package/docs/functions/hooks.useMemo.html +1 -1
  30. package/docs/functions/hooks.useReducer.html +1 -1
  31. package/docs/functions/hooks.useRef.html +1 -1
  32. package/docs/functions/hooks.useTransition.html +1 -1
  33. package/docs/functions/misc.Fragment.html +2 -2
  34. package/docs/functions/misc.Portal.html +2 -2
  35. package/docs/functions/misc.deepEqual.html +1 -1
  36. package/docs/functions/misc.findDOMNode.html +1 -1
  37. package/docs/functions/misc.forwardRef.html +1 -1
  38. package/docs/functions/misc.getComponentByPointer.html +1 -1
  39. package/docs/functions/misc.isAsync.html +1 -1
  40. package/docs/functions/misc.isClass.html +1 -1
  41. package/docs/functions/misc.isEmpty.html +1 -1
  42. package/docs/functions/misc.isError.html +1 -1
  43. package/docs/functions/misc.isThenable.html +1 -1
  44. package/docs/functions/misc.shallowEqual.html +1 -1
  45. package/docs/functions/misc.toBool.html +1 -1
  46. package/docs/functions/run.lazy.html +1 -1
  47. package/docs/functions/run.require.html +1 -1
  48. package/docs/functions/run.run.html +1 -1
  49. package/docs/functions/run.runScripts.html +1 -1
  50. package/docs/functions/timers.timeoutPromise.html +10 -10
  51. package/docs/static/index.html +1 -1
  52. package/docs/static/lilact.development.js +238 -135
  53. package/docs/static/lilact.development.js.map +3 -3
  54. package/docs/static/lilact.development.min.js +57 -57
  55. package/docs/static/lilact.development.min.js.map +3 -3
  56. package/docs/static/lilact.production.min.js +57 -57
  57. package/docs/variables/components.cloneElement.html +1 -1
  58. package/docs/variables/misc.Children.html +10 -10
  59. package/docs/variables/misc.isValidElement.html +1 -1
  60. package/docs/variables/run.required_scripts.html +1 -1
  61. package/examples/index.html +1 -1
  62. package/examples/lilact.development.js +238 -135
  63. package/examples/lilact.development.js.map +3 -3
  64. package/examples/lilact.development.min.js +57 -57
  65. package/examples/lilact.development.min.js.map +3 -3
  66. package/examples/lilact.production.min.js +57 -57
  67. package/package.json +1 -1
  68. package/src/components.jsx +24 -13
  69. package/src/errors.jsx +310 -310
  70. package/src/hooks.jsx +60 -44
  71. package/src/lilact.jsx +4 -2
  72. package/src/misc.jsx +46 -18
  73. package/src/run.jsx +406 -291
package/src/hooks.jsx CHANGED
@@ -87,8 +87,12 @@ export function useState(initialValue)
87
87
  */
88
88
  export function useCallback(callback, deps=undefined)
89
89
  {
90
- if( deps!==undefined && (typeof(deps)!=='object' || deps.constructor.name!=='Array') ) {
91
- throw new Error("Callback dependencies must be an array or omitted.");
90
+ if(
91
+ deps !== undefined && !Array.isArray(deps) &&
92
+ deps !== null && typeof deps !== "object"
93
+ )
94
+ {
95
+ throw new Error("Callback dependencies must be an array, object or omitted.");
92
96
  }
93
97
 
94
98
  const hk = useHook();
@@ -133,22 +137,17 @@ export function createContext(defaultValue)
133
137
  * @param {any} context - Context object created by `createContext`.
134
138
  * @returns {any} Current context value.
135
139
  */
136
- export function useContext(context)
137
- {
138
- let core = Lilact.current_component[0].parent;
140
+ export function useContext(context) {
141
+ let core = Lilact.current_component[0]?.parent;
139
142
 
140
- while(core.entity!==context.Provider && core.parent) {
143
+ while (core && core.entity !== context.Provider) {
141
144
  core = core.parent;
142
145
  }
143
146
 
144
- if(core.parent) {
145
- let v = core.props?.value;
146
- return v??=context.default;
147
- }
148
-
149
- return context.default;
147
+ return core
148
+ ? core.props?.value ?? context.default
149
+ : context.default;
150
150
  }
151
-
152
151
  /**
153
152
  * Returns a stable, unique ID that is consistent across renders.
154
153
  *
@@ -178,26 +177,24 @@ export function useTransition()
178
177
  if( isEmpty(hk) ) {
179
178
  hk.count=0;
180
179
 
181
- hk.func =
182
-
183
- (async function(core, hk, fn) {
184
-
185
- if(hk.count===0) {
180
+ hk.func = (async function(core, hk, fn) {
181
+ if (hk.count === 0) {
186
182
  core.component.forceUpdate();
187
183
  }
188
184
 
189
185
  hk.count++;
190
186
 
191
- await fn();
187
+ try {
188
+ return await fn();
189
+ } finally {
190
+ hk.count--;
192
191
 
193
- hk.count--;
194
-
195
- if(hk.count===0) {
196
- core.component.forceUpdate();
192
+ if (hk.count === 0) {
193
+ core.component.forceUpdate();
194
+ }
197
195
  }
198
- }
196
+ }).bind(undefined, Lilact.current_component[0], hk);
199
197
 
200
- ).bind(undefined, Lilact.current_component[0], hk);
201
198
  }
202
199
 
203
200
  return [ hk.count!=0, hk.func ];
@@ -275,7 +272,11 @@ export function useRef(initialValue = null)
275
272
  */
276
273
  export async function useLayoutEffect(effect, deps=undefined)
277
274
  {
278
- if( deps!==undefined && (typeof(deps)!=='object' && deps.constructor.name!=='Array') ) {
275
+ if(
276
+ deps !== undefined && !Array.isArray(deps) &&
277
+ deps !== null && typeof deps !== "object"
278
+ )
279
+ {
279
280
  throw new Error("Layout effect dependencies must be an array, object or omitted.");
280
281
  }
281
282
 
@@ -287,8 +288,8 @@ export async function useLayoutEffect(effect, deps=undefined)
287
288
 
288
289
  if(hk?.cleanup) {
289
290
  await hk.cleanup();
290
- hk.cleanup = undefined;
291
- }
291
+ hk.cleanup = undefined;
292
+ }
292
293
 
293
294
  hk.deps = deps;
294
295
  Lilact.layout_effects.add( async ()=>{ hk.cleanup = await effect(); });
@@ -307,7 +308,11 @@ export async function useLayoutEffect(effect, deps=undefined)
307
308
  */
308
309
  export async function useEffect(effect, deps=undefined)
309
310
  {
310
- if( deps!==undefined && (typeof(deps)!=='object' && deps.constructor.name!=='Array') ) {
311
+ if(
312
+ deps !== undefined && !Array.isArray(deps) &&
313
+ deps !== null && typeof deps !== "object"
314
+ )
315
+ {
311
316
  throw new Error("Effect dependencies must be an array, object or omitted.");
312
317
  }
313
318
 
@@ -319,7 +324,7 @@ export async function useEffect(effect, deps=undefined)
319
324
 
320
325
  if(hk?.cleanup) {
321
326
  await hk.cleanup();
322
- hk.cleanup = undefined;
327
+ hk.cleanup = undefined;
323
328
  }
324
329
 
325
330
  hk.deps = deps;
@@ -340,7 +345,11 @@ export async function useEffect(effect, deps=undefined)
340
345
  */
341
346
  export async function useInsertionEffect(effect, deps=undefined)
342
347
  {
343
- if( deps!==undefined && (typeof(deps)!=='object' && deps.constructor.name!=='Array') ) {
348
+ if(
349
+ deps !== undefined && !Array.isArray(deps) &&
350
+ deps !== null && typeof deps !== "object"
351
+ )
352
+ {
344
353
  throw new Error("Insertion effect dependencies must be an array, object, or omitted.");
345
354
  }
346
355
 
@@ -352,7 +361,7 @@ export async function useInsertionEffect(effect, deps=undefined)
352
361
 
353
362
  if(hk?.cleanup) {
354
363
  await hk.cleanup();
355
- hk.cleanup = undefined;
364
+ hk.cleanup = undefined;
356
365
  }
357
366
 
358
367
  hk.deps = deps;
@@ -373,7 +382,11 @@ export async function useInsertionEffect(effect, deps=undefined)
373
382
  */
374
383
  export function useMemo(factory,deps=undefined)
375
384
  {
376
- if( deps!==undefined && (typeof(deps)!=='object' && deps.constructor.name!=='Array') ) {
385
+ if(
386
+ deps !== undefined && !Array.isArray(deps) &&
387
+ deps !== null && typeof deps !== "object"
388
+ )
389
+ {
377
390
  throw new Error("Memo dependencies must be an array or omitted.");
378
391
  }
379
392
 
@@ -407,12 +420,12 @@ export function useActionState(action, initialState)
407
420
 
408
421
  hk.state = initialState;
409
422
 
410
- hk.form_action = (sub)=>{
423
+ hk.form_action = (event)=>{
411
424
  event.preventDefault();
412
425
 
413
426
  tran_start_func(
414
427
  async ()=> {
415
- const form_data = new FormData(sub.target, sub.submitter);
428
+ const form_data = new FormData(event.target, event.submitter);
416
429
  hk.state = await action(hk.state, form_data);
417
430
  },
418
431
  []
@@ -433,27 +446,30 @@ export function useActionState(action, initialState)
433
446
  * @param {Function} [init] - Optional initializer function for lazy initial state.
434
447
  * @returns {any} Hook result `[state, dispatch]`.
435
448
  */
436
- export function useReducer(reducer, initialArg, init)
437
- {
449
+ export function useReducer(reducer, initialArg, init) {
438
450
  const hk = useHook();
439
451
 
440
- if( isEmpty(hk) ) {
441
- hk.reducer = reducer;
442
- hk.state = init?init(initialArg):initialArg;
452
+ if(isEmpty(hk)) {
453
+ hk.state = init ? init(initialArg) : initialArg;
454
+
443
455
  hk.dispatch = function(core, hk, action) {
444
- const newst = hk.reducer(hk.state, action);
445
- if(!Lilact.defaultIsEqual(newst,hk.state)) {
446
- hk.state = newst;
456
+ const newState = hk.reducer(hk.state, action);
457
+
458
+ if (!Lilact.defaultIsEqual(newState, hk.state)) {
459
+ hk.state = newState;
447
460
  core.component.forceUpdate();
448
461
  }
449
462
  }.bind(undefined, Lilact.current_component[0], hk);
450
463
  }
451
464
 
465
+ hk.reducer = reducer;
466
+
452
467
  return [hk.state, hk.dispatch];
453
468
  }
454
469
 
455
470
 
456
471
 
472
+
457
473
  // todo: very simple implementation. and not lilactish at all! must be modified
458
474
 
459
475
  /**
@@ -526,7 +542,7 @@ export function useImperativeHandle(ref, factory, deps=undefined)
526
542
  if(typeof ref?.current !== 'object') {
527
543
  ref.current = {};
528
544
  }
529
- Object.assign( ref.current, factory(), 0 );
545
+ Object.assign( ref.current, factory());
530
546
 
531
547
  }
532
548
 
package/src/lilact.jsx CHANGED
@@ -143,8 +143,10 @@ globalThis.createComponent = Lilact.createComponent;
143
143
  globalThis.Fragment = Lilact.Fragment;
144
144
  globalThis.require = Lilact.require;
145
145
 
146
- document.addEventListener('DOMContentLoaded', () => {
147
- Lilact.runScripts();
146
+ document.addEventListener("DOMContentLoaded", () => {
147
+ Lilact.runScripts().catch(error => {
148
+ Lilact.globalErrorHandler?.(error);
149
+ });
148
150
  });
149
151
 
150
152
  if(DEBUG) {
package/src/misc.jsx CHANGED
@@ -54,9 +54,12 @@ const typeOf = (input) => {
54
54
  * @param value - Value to inspect.
55
55
  * @returns True if the value is a class component; otherwise false.
56
56
  */
57
- export const isValidComponent = (value) => {
58
- return value[CORE]!==undefined || value[TEXT]!==undefined;
59
- }
57
+ export const isValidComponent = value => {
58
+ return Boolean(
59
+ value &&
60
+ (value[CORE] !== undefined || value[TEXT] !== undefined)
61
+ );
62
+ };
60
63
 
61
64
  /**
62
65
  * Checks whether a value is a Lilact component. It is the same as `isValidComponent`.
@@ -359,23 +362,48 @@ export function isEmpty(value) {
359
362
  * @returns True if shallowly equal; otherwise false.
360
363
  */
361
364
  export const shallowEqual = (source, target, ignore) => {
362
- if (typeOf(source) !== typeOf(target)) {
363
- return false;
364
- }
365
+ if (Object.is(source, target)) {
366
+ return true;
367
+ }
365
368
 
366
- if (typeOf(source) === "array") {
367
- if (source.length !== target.length) {
368
- return false;
369
- }
370
- return source.every((el, index) => el === target[index] || index===ignore );
371
- } else if (typeOf(source) === "object") {
372
- return Object.keys(source).every((key) => source[key] === target[key] || key===ignore );
373
- } else if (typeOf(source) === "date") {
374
- return source.getTime() === target.getTime();
375
- }
369
+ if (typeOf(source) !== typeOf(target)) {
370
+ return false;
371
+ }
376
372
 
377
- return source === target;
378
- }
373
+ if (typeOf(source) === "array") {
374
+ if (source.length !== target.length) {
375
+ return false;
376
+ }
377
+
378
+ return source.every(
379
+ (value, index) =>
380
+ index === ignore || Object.is(value, target[index])
381
+ );
382
+ }
383
+
384
+ if (typeOf(source) === "object") {
385
+ const sourceKeys = Object.keys(source)
386
+ .filter(key => key !== ignore);
387
+
388
+ const targetKeys = Object.keys(target)
389
+ .filter(key => key !== ignore);
390
+
391
+ if (sourceKeys.length !== targetKeys.length) {
392
+ return false;
393
+ }
394
+
395
+ return sourceKeys.every(key =>
396
+ Object.prototype.hasOwnProperty.call(target, key) &&
397
+ Object.is(source[key], target[key])
398
+ );
399
+ }
400
+
401
+ if (typeOf(source) === "date") {
402
+ return source.getTime() === target.getTime();
403
+ }
404
+
405
+ return Object.is(source, target);
406
+ };
379
407
 
380
408
 
381
409
  /**