gyosjs 0.2.2 → 0.2.3

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/gyos.auto.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * GyosJS v0.2.2
2
+ * GyosJS v0.2.3
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -1584,6 +1584,162 @@
1584
1584
  });
1585
1585
  }
1586
1586
 
1587
+ const DEFAULT_OPTIONS = {
1588
+ classNames: ['is-revealed'],
1589
+ rootMargin: '0px',
1590
+ threshold: 0.1
1591
+ };
1592
+ const pools = new Map();
1593
+ const mountedRecords = new WeakMap();
1594
+ function clampThreshold(value) {
1595
+ const number = Number(value);
1596
+ if (!Number.isFinite(number))
1597
+ return DEFAULT_OPTIONS.threshold;
1598
+ return Math.min(1, Math.max(0, number));
1599
+ }
1600
+ function normalizeThreshold(value) {
1601
+ if (!Array.isArray(value))
1602
+ return clampThreshold(value);
1603
+ const values = Array.from(new Set(value.map(clampThreshold))).sort((a, b) => a - b);
1604
+ return values.length ? values : DEFAULT_OPTIONS.threshold;
1605
+ }
1606
+ function normalizeOptions(binding) {
1607
+ const value = binding.value && typeof binding.value === 'object' && !Array.isArray(binding.value)
1608
+ ? binding.value
1609
+ : {};
1610
+ const args = new Set(binding.arg || []);
1611
+ const classNames = typeof value.className === 'string'
1612
+ ? value.className.split(/\s+/).filter(Boolean)
1613
+ : DEFAULT_OPTIONS.classNames;
1614
+ return {
1615
+ classNames,
1616
+ once: typeof value.once === 'boolean' ? value.once : !args.has('repeat'),
1617
+ rootMargin: typeof value.rootMargin === 'string' ? value.rootMargin : DEFAULT_OPTIONS.rootMargin,
1618
+ target: value.target === 'parent' || args.has('parent') ? 'parent' : 'self',
1619
+ threshold: normalizeThreshold(value.threshold ?? DEFAULT_OPTIONS.threshold)
1620
+ };
1621
+ }
1622
+ function poolKey(options) {
1623
+ return JSON.stringify([options.rootMargin, options.threshold]);
1624
+ }
1625
+ function setRevealed(record, revealed) {
1626
+ const { element, options } = record;
1627
+ if (revealed) {
1628
+ element.setAttribute('data-gyos-revealed', '');
1629
+ for (const className of options.classNames)
1630
+ element.classList.add(className);
1631
+ }
1632
+ else {
1633
+ element.removeAttribute('data-gyos-revealed');
1634
+ for (const className of options.classNames)
1635
+ element.classList.remove(className);
1636
+ }
1637
+ }
1638
+ function removeRecord(record) {
1639
+ if (mountedRecords.get(record.element) !== record)
1640
+ return;
1641
+ mountedRecords.delete(record.element);
1642
+ const records = record.pool.records.get(record.observedTarget);
1643
+ if (records) {
1644
+ records.delete(record);
1645
+ if (records.size === 0) {
1646
+ record.pool.records.delete(record.observedTarget);
1647
+ record.pool.observer.unobserve(record.observedTarget);
1648
+ }
1649
+ }
1650
+ if (record.pool.records.size === 0) {
1651
+ record.pool.observer.disconnect();
1652
+ pools.delete(record.pool.key);
1653
+ }
1654
+ }
1655
+ function createPool(options) {
1656
+ const key = poolKey(options);
1657
+ const existing = pools.get(key);
1658
+ if (existing)
1659
+ return existing;
1660
+ let pool;
1661
+ try {
1662
+ const records = new Map();
1663
+ const observer = new IntersectionObserver(entries => {
1664
+ for (const entry of entries) {
1665
+ const current = Array.from(records.get(entry.target) || []);
1666
+ for (const record of current) {
1667
+ if (entry.isIntersecting) {
1668
+ setRevealed(record, true);
1669
+ if (record.options.once)
1670
+ removeRecord(record);
1671
+ }
1672
+ else if (!record.options.once) {
1673
+ setRevealed(record, false);
1674
+ }
1675
+ }
1676
+ }
1677
+ }, { rootMargin: options.rootMargin, threshold: options.threshold });
1678
+ pool = { key, observer, records };
1679
+ }
1680
+ catch {
1681
+ return null;
1682
+ }
1683
+ pools.set(key, pool);
1684
+ return pool;
1685
+ }
1686
+ function prefersReducedMotion() {
1687
+ return typeof window.matchMedia === 'function'
1688
+ && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
1689
+ }
1690
+ function mountReveal(element, binding) {
1691
+ unmountReveal(element);
1692
+ document.documentElement.classList.add('gyos-reveal-ready');
1693
+ const options = normalizeOptions(binding);
1694
+ if (options.once && element.hasAttribute('data-gyos-revealed')) {
1695
+ for (const className of options.classNames)
1696
+ element.classList.add(className);
1697
+ return;
1698
+ }
1699
+ if (prefersReducedMotion() || typeof window.IntersectionObserver !== 'function') {
1700
+ const fallback = { element, options };
1701
+ setRevealed(fallback, true);
1702
+ return;
1703
+ }
1704
+ const pool = createPool(options);
1705
+ if (!pool) {
1706
+ const fallback = { element, options };
1707
+ setRevealed(fallback, true);
1708
+ return;
1709
+ }
1710
+ const observedTarget = options.target === 'parent' ? element.parentElement || element : element;
1711
+ const record = { element, observedTarget, options, pool };
1712
+ let records = pool.records.get(observedTarget);
1713
+ if (!records) {
1714
+ records = new Set();
1715
+ pool.records.set(observedTarget, records);
1716
+ pool.observer.observe(observedTarget);
1717
+ }
1718
+ records.add(record);
1719
+ mountedRecords.set(element, record);
1720
+ }
1721
+ function unmountReveal(element) {
1722
+ const record = mountedRecords.get(element);
1723
+ if (!record)
1724
+ return;
1725
+ removeRecord(record);
1726
+ if (!record.options.once)
1727
+ setRevealed(record, false);
1728
+ }
1729
+ const revealDirective = {
1730
+ mounted: mountReveal,
1731
+ updated(element, binding) {
1732
+ const previous = normalizeOptions({ ...binding, value: binding.oldValue });
1733
+ const next = normalizeOptions(binding);
1734
+ for (const className of previous.classNames) {
1735
+ if (!next.classNames.includes(className))
1736
+ element.classList.remove(className);
1737
+ }
1738
+ mountReveal(element, binding);
1739
+ },
1740
+ unmounted: unmountReveal
1741
+ };
1742
+
1587
1743
  const directives = new Map();
1588
1744
  /**
1589
1745
  * Register a custom directive
@@ -1758,6 +1914,7 @@
1758
1914
  el.innerHTML = html;
1759
1915
  },
1760
1916
  });
1917
+ directive('reveal', revealDirective);
1761
1918
 
1762
1919
  /**
1763
1920
  * Transitions System - Smooth animations for enter/leave
@@ -2307,11 +2464,14 @@
2307
2464
  if (directive) {
2308
2465
  // g-on needs the handler itself. The general evaluator intentionally
2309
2466
  // invokes bare functions to support function-style computed values.
2310
- const value = directiveName === 'on' && typeof scope[attr.value] === 'function'
2311
- ? scope[attr.value]
2312
- : evaluateExpression(attr.value, scope, false);
2313
- if (value === undefined)
2314
- return; // Skip if value is undefined
2467
+ const evaluateValue = () => {
2468
+ if (attr.value.trim() === '')
2469
+ return undefined;
2470
+ return directiveName === 'on' && typeof scope[attr.value] === 'function'
2471
+ ? scope[attr.value]
2472
+ : evaluateExpression(attr.value, scope, false);
2473
+ };
2474
+ const value = evaluateValue();
2315
2475
  let oldValue = directive.mounted ? value : undefined;
2316
2476
  let initialized = !directive.mounted;
2317
2477
  // Call mounted hook
@@ -2337,7 +2497,7 @@
2337
2497
  // Setup reactive update if has updated hook
2338
2498
  if (directive.updated) {
2339
2499
  queueReactiveEffect(element, () => {
2340
- const newValue = evaluateExpression(attr.value, scope, false);
2500
+ const newValue = evaluateValue();
2341
2501
  if (!initialized) {
2342
2502
  initialized = true;
2343
2503
  oldValue = newValue;
@@ -2392,28 +2552,108 @@
2392
2552
  /**
2393
2553
  * Reactive attribute bindings (:class, :style, :aria-*, :data-*, and custom attributes).
2394
2554
  */
2395
- function handleClassBinding(el, value) {
2555
+ function createClassBindingState(el) {
2556
+ return {
2557
+ staticClasses: new Set(el.classList),
2558
+ dynamicClasses: new Set()
2559
+ };
2560
+ }
2561
+ function classNamesFromBinding(value) {
2396
2562
  if (typeof value === 'string') {
2397
- el.className = value;
2563
+ return new Set(value.split(/\s+/).filter(Boolean));
2398
2564
  }
2399
- else if (value && typeof value === 'object') {
2400
- Object.entries(value).forEach(([className, condition]) => {
2401
- el.classList.toggle(className, Boolean(condition));
2565
+ if (value && typeof value === 'object') {
2566
+ return new Set(Object.entries(value)
2567
+ .filter(([, condition]) => Boolean(condition))
2568
+ .flatMap(([className]) => className.split(/\s+/).filter(Boolean)));
2569
+ }
2570
+ return new Set();
2571
+ }
2572
+ function handleClassBinding(el, value, state) {
2573
+ const nextClasses = classNamesFromBinding(value);
2574
+ if (value && typeof value === 'object') {
2575
+ for (const [className, condition] of Object.entries(value)) {
2576
+ if (!condition) {
2577
+ for (const token of className.split(/\s+/).filter(Boolean))
2578
+ el.classList.remove(token);
2579
+ }
2580
+ }
2581
+ }
2582
+ for (const className of state.dynamicClasses) {
2583
+ if (!nextClasses.has(className) && !state.staticClasses.has(className)) {
2584
+ el.classList.remove(className);
2585
+ }
2586
+ }
2587
+ for (const className of nextClasses) {
2588
+ el.classList.add(className);
2589
+ }
2590
+ state.dynamicClasses = nextClasses;
2591
+ }
2592
+ function readStyleValues(style) {
2593
+ const values = new Map();
2594
+ for (let index = 0; index < style.length; index++) {
2595
+ const property = style.item(index);
2596
+ values.set(property, {
2597
+ priority: style.getPropertyPriority(property),
2598
+ value: style.getPropertyValue(property)
2402
2599
  });
2403
2600
  }
2601
+ return values;
2602
+ }
2603
+ function normalizeStyleProperty(property) {
2604
+ if (property.startsWith('--') || property.includes('-'))
2605
+ return property.toLowerCase();
2606
+ return property.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`);
2404
2607
  }
2405
- function handleStyleBinding(el, value) {
2608
+ function styleValuesFromBinding(value) {
2609
+ const scratch = document.createElement('div').style;
2610
+ const mentioned = new Set();
2406
2611
  if (typeof value === 'string') {
2407
- el.setAttribute('style', value);
2612
+ scratch.cssText = value;
2613
+ for (const property of readStyleValues(scratch).keys())
2614
+ mentioned.add(property);
2408
2615
  }
2409
2616
  else if (value && typeof value === 'object') {
2410
- Object.entries(value).forEach(([property, propertyValue]) => {
2411
- el.style[property] = propertyValue ?? '';
2412
- });
2617
+ for (const [property, propertyValue] of Object.entries(value)) {
2618
+ const normalized = normalizeStyleProperty(property);
2619
+ mentioned.add(normalized);
2620
+ if (propertyValue == null || propertyValue === false)
2621
+ continue;
2622
+ if (property.startsWith('--'))
2623
+ scratch.setProperty(property, String(propertyValue));
2624
+ else
2625
+ scratch[property] = propertyValue;
2626
+ }
2413
2627
  }
2414
- else if (value == null || value === false) {
2415
- el.removeAttribute('style');
2628
+ return { mentioned, values: readStyleValues(scratch) };
2629
+ }
2630
+ function createStyleBindingState(el) {
2631
+ return {
2632
+ staticStyles: readStyleValues(el.style),
2633
+ dynamicProperties: new Set()
2634
+ };
2635
+ }
2636
+ function restoreStaticStyle(el, property, state) {
2637
+ const original = state.staticStyles.get(property);
2638
+ if (original)
2639
+ el.style.setProperty(property, original.value, original.priority);
2640
+ else
2641
+ el.style.removeProperty(property);
2642
+ }
2643
+ function handleStyleBinding(el, value, state) {
2644
+ const next = styleValuesFromBinding(value);
2645
+ for (const property of state.dynamicProperties) {
2646
+ if (!next.mentioned.has(property))
2647
+ restoreStaticStyle(el, property, state);
2648
+ }
2649
+ for (const property of next.mentioned) {
2650
+ const styleValue = next.values.get(property);
2651
+ if (styleValue)
2652
+ el.style.setProperty(property, styleValue.value, styleValue.priority);
2653
+ else
2654
+ el.style.removeProperty(property);
2416
2655
  }
2656
+ state.dynamicProperties = next.mentioned;
2417
2657
  }
2418
2658
  const booleanAttributes = new Set([
2419
2659
  'allowfullscreen', 'async', 'autofocus', 'autoplay', 'checked', 'controls',
@@ -2508,13 +2748,15 @@
2508
2748
  for (const attribute of boundAttributes(element)) {
2509
2749
  const attrName = attribute.name.slice(1);
2510
2750
  const expression = attribute.value;
2751
+ const classState = attrName === 'class' ? createClassBindingState(element) : undefined;
2752
+ const styleState = attrName === 'style' ? createStyleBindingState(element) : undefined;
2511
2753
  queueReactiveEffect(element, () => {
2512
2754
  const currentScope = getScopeFromElement(element) || scope;
2513
2755
  const value = evaluateExpression(expression, currentScope);
2514
2756
  if (attrName === 'class')
2515
- handleClassBinding(element, value);
2757
+ handleClassBinding(element, value, classState);
2516
2758
  else if (attrName === 'style')
2517
- handleStyleBinding(element, value);
2759
+ handleStyleBinding(element, value, styleState);
2518
2760
  else
2519
2761
  applyAttributeBinding(element, attrName, value);
2520
2762
  });
@@ -2529,9 +2771,9 @@
2529
2771
  const attrName = attribute.name.slice(1);
2530
2772
  const value = evaluateExpression(attribute.value, scope);
2531
2773
  if (attrName === 'class')
2532
- handleClassBinding(element, value);
2774
+ handleClassBinding(element, value, createClassBindingState(element));
2533
2775
  else if (attrName === 'style')
2534
- handleStyleBinding(element, value);
2776
+ handleStyleBinding(element, value, createStyleBindingState(element));
2535
2777
  else
2536
2778
  applyAttributeBinding(element, attrName, value);
2537
2779
  }
@@ -3542,12 +3784,12 @@
3542
3784
  // e.stopPropagation(); // Optional: prevent other click handlers
3543
3785
  const target = e.target;
3544
3786
  // Check if click is outside element AND element is still in DOM
3545
- if (!document.body.contains(element) ||
3546
- (target instanceof Element && target.hasAttribute('g-ignore-outside-click'))) {
3547
- // Element removed, cleanup
3787
+ if (!document.body.contains(element)) {
3548
3788
  cleanup();
3549
3789
  return;
3550
3790
  }
3791
+ if (target instanceof Element && target.closest('[g-ignore-outside-click]'))
3792
+ return;
3551
3793
  if (element !== target && !element.contains(target)) {
3552
3794
  const currentScope = getScopeFromElement(element) || scope;
3553
3795
  if (modifiers.includes('once'))
@@ -3673,6 +3915,39 @@
3673
3915
  });
3674
3916
  }
3675
3917
 
3918
+ const PARENT_SCOPE = Symbol('gyos.parent-scope');
3919
+ function attachParentScope(childScope, parentScope) {
3920
+ Object.defineProperty(childScope, PARENT_SCOPE, {
3921
+ value: parentScope,
3922
+ configurable: false,
3923
+ enumerable: false,
3924
+ writable: false
3925
+ });
3926
+ }
3927
+ function parentOf(scope) {
3928
+ return scope?.[PARENT_SCOPE];
3929
+ }
3930
+ function ensureModelPropertyOwner(scope, property) {
3931
+ if (!parentOf(scope) || property in scope)
3932
+ return;
3933
+ let owner = scope;
3934
+ let parent = parentOf(owner);
3935
+ while (parent) {
3936
+ owner = parent;
3937
+ parent = parentOf(owner);
3938
+ }
3939
+ if (!(property in owner))
3940
+ owner[property] = undefined;
3941
+ Object.defineProperty(scope, property, {
3942
+ get: () => owner[property],
3943
+ set: value => {
3944
+ owner[property] = value;
3945
+ },
3946
+ configurable: true,
3947
+ enumerable: true
3948
+ });
3949
+ }
3950
+
3676
3951
  /**
3677
3952
  * Two-Way Binding (g-model)
3678
3953
  * Handles form input binding with optional modifiers (debounce, number, trim, etc.)
@@ -3782,6 +4057,9 @@
3782
4057
  DEBUG() && console.log('[g-model] Expression:', expr, 'Modifiers:', modifiers, 'Debounce:', debounceMs);
3783
4058
  // Get current scope from element
3784
4059
  const currentScope = getScopeFromElement(element) || scope;
4060
+ const modelParts = parsePath(expr);
4061
+ if (typeof modelParts[0] === 'string')
4062
+ ensureModelPropertyOwner(currentScope, modelParts[0]);
3785
4063
  // Set initial value (no pipes in v-model)
3786
4064
  const initialValue = evaluateExpression(expr, currentScope, false);
3787
4065
  if (inputElement.type === 'checkbox') {
@@ -4038,8 +4316,15 @@
4038
4316
  proxy = new Proxy(raw, {
4039
4317
  get(target, key, receiver) {
4040
4318
  const value = Reflect.get(target, key, receiver);
4041
- // Không tạo signal cho property không phải own (vd forEach, map, filter...)
4319
+ // Missing own fields can become reactive later; prototype APIs stay untouched.
4042
4320
  if (!Object.prototype.hasOwnProperty.call(target, key)) {
4321
+ if (typeof key !== 'symbol' && !isReservedKey(key) && !(key in target)) {
4322
+ const existingSig = fieldSignals.get(target)?.get(key);
4323
+ if (hasCurrentEffect())
4324
+ return (existingSig || getFieldSignal(target, key, undefined)).value;
4325
+ if (existingSig)
4326
+ return existingSig.peek;
4327
+ }
4043
4328
  return value;
4044
4329
  }
4045
4330
  // symbol / key internal bỏ qua reactive
@@ -4130,6 +4415,7 @@
4130
4415
  return true;
4131
4416
  },
4132
4417
  ownKeys(target) {
4418
+ ensureVersion(target).value;
4133
4419
  return Reflect.ownKeys(target).filter(key => !isReservedKey(key));
4134
4420
  },
4135
4421
  getOwnPropertyDescriptor(target, key) {
@@ -4240,6 +4526,7 @@
4240
4526
  */
4241
4527
  function createChildScope(parentScope, itemVar, list, index) {
4242
4528
  const childScope = {};
4529
+ attachParentScope(childScope, parentScope);
4243
4530
  const state = signal({ list, index });
4244
4531
  Object.defineProperty(childScope, FOR_SCOPE_STATE, { value: state });
4245
4532
  // handle itemVar include index (e.g., "item, idx in items")
@@ -4278,6 +4565,9 @@
4278
4565
  if (!(key in childScope)) {
4279
4566
  Object.defineProperty(childScope, key, {
4280
4567
  get: () => parentScope[key],
4568
+ set: (value) => {
4569
+ parentScope[key] = value;
4570
+ },
4281
4571
  enumerable: true,
4282
4572
  configurable: true,
4283
4573
  });
@@ -8009,7 +8299,7 @@
8009
8299
  onAfterNavigate,
8010
8300
  portalCreate,
8011
8301
  portalDestroy,
8012
- version: '0.2.2'
8302
+ version: '0.2.3'
8013
8303
  };
8014
8304
 
8015
8305
  var _a, _b, _c;