@morze/ui 0.2.0 → 0.2.1

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/index.js CHANGED
@@ -2398,20 +2398,27 @@ function useColumnLayout({
2398
2398
  () => controlled ? merge(baseline, controlled, baseline) : internal,
2399
2399
  [controlled, internal, baseline]
2400
2400
  );
2401
+ const onLayoutChangeRef = React39.useRef(onLayoutChange);
2402
+ React39.useEffect(() => {
2403
+ onLayoutChangeRef.current = onLayoutChange;
2404
+ });
2405
+ const internalRef = React39.useRef(internal);
2406
+ React39.useEffect(() => {
2407
+ internalRef.current = internal;
2408
+ }, [internal]);
2401
2409
  const update = React39.useCallback(
2402
2410
  (patch, options) => {
2403
- setInternal((current) => {
2404
- const base = controlled ? merge(baseline, controlled, baseline) : current;
2405
- const next = patch(base);
2406
- if (next === base) return current;
2407
- if (options?.persist !== false) {
2408
- writeStored2(persistKey, next);
2409
- onLayoutChange?.(next);
2410
- }
2411
- return next;
2412
- });
2411
+ const base = controlled ? merge(baseline, controlled, baseline) : internalRef.current;
2412
+ const next = patch(base);
2413
+ if (next === base) return;
2414
+ internalRef.current = next;
2415
+ setInternal(next);
2416
+ if (options?.persist !== false) {
2417
+ writeStored2(persistKey, next);
2418
+ onLayoutChangeRef.current?.(next);
2419
+ }
2413
2420
  },
2414
- [controlled, baseline, persistKey, onLayoutChange]
2421
+ [controlled, baseline, persistKey]
2415
2422
  );
2416
2423
  const byId = React39.useMemo(
2417
2424
  () => new Map(columns.map((c) => [c.id, c])),
@@ -2595,6 +2602,7 @@ function useRowSelection(options) {
2595
2602
  }
2596
2603
  var SELECT_WIDTH = 44;
2597
2604
  var EXPAND_WIDTH = 40;
2605
+ var FIT_CYCLE_MS = 250;
2598
2606
  function DataTable({
2599
2607
  columns,
2600
2608
  data,
@@ -2658,22 +2666,28 @@ function DataTable({
2658
2666
  }),
2659
2667
  pageKeys
2660
2668
  });
2661
- const lastFitWidth = React39.useRef(0);
2669
+ const recentFits = React39.useRef([]);
2670
+ const hiddenKey = layout.hidden.join("\0");
2671
+ const orderKey = layout.order.join("\0");
2662
2672
  React39.useEffect(() => {
2663
2673
  const scroller = scrollerRef.current;
2664
2674
  if (!scroller || !autoFit) return;
2665
2675
  const controls = (selectable ? SELECT_WIDTH : 0) + (renderExpanded ? EXPAND_WIDTH : 0);
2676
+ recentFits.current = [];
2666
2677
  const run = () => {
2667
2678
  const available = scroller.clientWidth - controls - 2;
2668
- if (Math.abs(available - lastFitWidth.current) < 1) return;
2669
- lastFitWidth.current = available;
2679
+ const now = Date.now();
2680
+ const recent = recentFits.current.filter((entry) => now - entry.at < FIT_CYCLE_MS);
2681
+ recentFits.current = recent;
2682
+ if (recent.some((entry) => Math.abs(available - entry.width) < 1)) return;
2683
+ recent.push({ width: available, at: now });
2670
2684
  fitTo(available);
2671
2685
  };
2672
2686
  run();
2673
2687
  const observer = new ResizeObserver(run);
2674
2688
  observer.observe(scroller);
2675
2689
  return () => observer.disconnect();
2676
- }, [autoFit, fitTo, selectable, renderExpanded, layout.hidden, layout.order]);
2690
+ }, [autoFit, fitTo, selectable, renderExpanded, hiddenKey, orderKey]);
2677
2691
  React39.useEffect(() => {
2678
2692
  const scroller = scrollerRef.current;
2679
2693
  if (!scroller) return;