@nubitio/ui 0.6.10 → 0.7.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.cjs CHANGED
@@ -564,84 +564,111 @@ function FileDropzone({ accept, value = null, image = false, disabled = false, r
564
564
  });
565
565
  }
566
566
  //#endregion
567
- //#region packages/ui/AppDropdown.tsx
568
- function cx$2(...values) {
569
- return values.filter(Boolean).join(" ");
570
- }
571
- function normalizeIcon$2(icon) {
572
- if (!icon) return void 0;
573
- if (icon.startsWith("ph ")) return icon;
574
- if (icon.startsWith("ph-")) return `ph ${icon}`;
575
- return `ph ph-${icon}`;
576
- }
577
- function getMenuStyle(containerRef, menuMinWidth = 72) {
578
- if (!containerRef.current) return {
579
- position: "fixed",
580
- visibility: "hidden",
581
- margin: 0
582
- };
583
- const rect = (containerRef.current.querySelector(".nb-dropdown__trigger") ?? containerRef.current).getBoundingClientRect();
584
- const menuWidth = Math.max(rect.width, menuMinWidth);
585
- const left = Math.max(8, Math.min(rect.left, window.innerWidth - menuWidth - 8));
567
+ //#region packages/ui/useFloatingPanel.ts
568
+ /** Style applied to an anchored panel before its first position is computed. */
569
+ const HIDDEN_PANEL_STYLE = {
570
+ position: "fixed",
571
+ visibility: "hidden",
572
+ margin: 0
573
+ };
574
+ /**
575
+ * Fixed-position style for a panel anchored to an element, clamped to the
576
+ * viewport with an 8px gutter and flipped above the anchor when it fits
577
+ * better there.
578
+ */
579
+ function computeAnchoredStyle(anchor, options = {}) {
580
+ if (!anchor) return HIDDEN_PANEL_STYLE;
581
+ const { align = "start", minWidth = 0, matchAnchorWidth = false, offset = 4, flipThreshold = 250, zIndex = 9999 } = options;
582
+ const rect = anchor.getBoundingClientRect();
583
+ const width = Math.max(matchAnchorWidth ? rect.width : 0, minWidth);
584
+ const left = align === "end" ? Math.max(8, Math.min(rect.right - width, window.innerWidth - width - 8)) : Math.max(8, Math.min(rect.left, window.innerWidth - width - 8));
586
585
  const spaceBelow = window.innerHeight - rect.bottom;
587
- const spaceAbove = rect.top;
588
- if (spaceBelow < 250 && spaceAbove > spaceBelow) return {
586
+ if (spaceBelow < flipThreshold && rect.top > spaceBelow) return {
589
587
  position: "fixed",
590
588
  left: `${left}px`,
591
- width: `${menuWidth}px`,
592
- bottom: `${window.innerHeight - rect.top + 4}px`,
589
+ width: `${width}px`,
590
+ bottom: `${window.innerHeight - rect.top + offset}px`,
593
591
  top: "auto",
594
592
  margin: 0,
595
- zIndex: 9999,
593
+ zIndex,
596
594
  visibility: "visible"
597
595
  };
598
596
  return {
599
597
  position: "fixed",
600
598
  left: `${left}px`,
601
- width: `${menuWidth}px`,
602
- top: `${rect.bottom + 4}px`,
599
+ width: `${width}px`,
600
+ top: `${rect.bottom + offset}px`,
603
601
  bottom: "auto",
604
602
  margin: 0,
605
- zIndex: 9999,
603
+ zIndex,
606
604
  visibility: "visible"
607
605
  };
608
606
  }
609
- function AppDropdown({ id: idProp, label, icon, value, options, onChange, disabled = false, placeholder = "Seleccionar…", variant = "form", className, showFieldLabel = variant === "form", menuMinWidth, error, helpText, menuHeader, menuFooter, onOpenChange }) {
610
- const generatedId = (0, react.useId)();
611
- const id = idProp ?? generatedId;
612
- const [open, setOpen] = (0, react.useState)(false);
607
+ function useFloatingPanel(options = {}) {
608
+ const [open, setOpenState] = (0, react.useState)(false);
609
+ const [panelStyle, setPanelStyle] = (0, react.useState)(HIDDEN_PANEL_STYLE);
613
610
  const containerRef = (0, react.useRef)(null);
614
- const menuRef = (0, react.useRef)(null);
615
- const [menuStyle, setMenuStyle] = (0, react.useState)({
616
- position: "fixed",
617
- visibility: "hidden",
618
- margin: 0
619
- });
620
- const selected = options.find((option) => option.value === value);
621
- const computeMenuStyle = (0, react.useCallback)(() => getMenuStyle(containerRef, menuMinWidth), [menuMinWidth]);
622
- const setMenuOpen = (0, react.useCallback)((next) => {
623
- setOpen(next);
624
- onOpenChange?.(next);
625
- }, [onOpenChange]);
611
+ const panelRef = (0, react.useRef)(null);
612
+ const optionsRef = (0, react.useRef)(options);
613
+ optionsRef.current = options;
614
+ const anchored = !!options.computeStyle;
615
+ const dismissEvent = options.dismissEvent ?? "pointerdown";
616
+ const setOpen = (0, react.useCallback)((next) => {
617
+ setOpenState(next);
618
+ if (!next) optionsRef.current.onClose?.();
619
+ optionsRef.current.onOpenChange?.(next);
620
+ }, []);
621
+ const applyStyle = (0, react.useCallback)(() => {
622
+ const compute = optionsRef.current.computeStyle;
623
+ if (!compute) return;
624
+ const style = compute(containerRef.current);
625
+ if (style) setPanelStyle(style);
626
+ }, []);
627
+ const openPanel = (0, react.useCallback)(() => {
628
+ applyStyle();
629
+ setOpen(true);
630
+ }, [applyStyle, setOpen]);
631
+ const toggle = (0, react.useCallback)(() => open ? setOpen(false) : openPanel(), [
632
+ open,
633
+ openPanel,
634
+ setOpen
635
+ ]);
626
636
  (0, react.useEffect)(() => {
627
637
  if (!open) return;
628
- const handler = (e) => {
638
+ const handleOutside = (e) => {
629
639
  const target = e.target;
630
- if (!containerRef.current?.contains(target) && !menuRef.current?.contains(target)) setMenuOpen(false);
640
+ if (containerRef.current?.contains(target) || panelRef.current?.contains(target)) return;
641
+ setOpen(false);
642
+ };
643
+ const handleKeyDown = (e) => {
644
+ if (e.key !== "Escape") return;
645
+ setOpen(false);
646
+ optionsRef.current.onEscape?.(e);
631
647
  };
632
- document.addEventListener("mousedown", handler);
633
- return () => document.removeEventListener("mousedown", handler);
634
- }, [open, setMenuOpen]);
648
+ document.addEventListener(dismissEvent, handleOutside);
649
+ document.addEventListener("keydown", handleKeyDown);
650
+ return () => {
651
+ document.removeEventListener(dismissEvent, handleOutside);
652
+ document.removeEventListener("keydown", handleKeyDown);
653
+ };
654
+ }, [
655
+ dismissEvent,
656
+ open,
657
+ setOpen
658
+ ]);
635
659
  (0, react.useLayoutEffect)(() => {
636
- if (!open) return;
660
+ if (!open || !anchored) return;
637
661
  const update = () => {
638
- if (!containerRef.current) return;
639
- const rect = containerRef.current.getBoundingClientRect();
640
- if (rect.top >= window.innerHeight || rect.bottom <= 0) {
641
- setMenuOpen(false);
642
- return;
662
+ const container = containerRef.current;
663
+ if (!container) return;
664
+ if (optionsRef.current.closeOnViewportExit) {
665
+ const rect = container.getBoundingClientRect();
666
+ if ((rect.width > 0 || rect.height > 0) && (rect.top >= window.innerHeight || rect.bottom <= 0)) {
667
+ setOpen(false);
668
+ return;
669
+ }
643
670
  }
644
- setMenuStyle(computeMenuStyle());
671
+ applyStyle();
645
672
  };
646
673
  update();
647
674
  window.addEventListener("resize", update);
@@ -654,14 +681,47 @@ function AppDropdown({ id: idProp, label, icon, value, options, onChange, disabl
654
681
  window.removeEventListener("scroll", update, { capture: true });
655
682
  };
656
683
  }, [
684
+ anchored,
685
+ applyStyle,
657
686
  open,
658
- computeMenuStyle,
659
- setMenuOpen
687
+ setOpen
660
688
  ]);
661
- const openMenu = (0, react.useCallback)(() => {
662
- setMenuStyle(computeMenuStyle());
663
- setMenuOpen(true);
664
- }, [computeMenuStyle, setMenuOpen]);
689
+ return {
690
+ open,
691
+ setOpen,
692
+ toggle,
693
+ openPanel,
694
+ containerRef,
695
+ panelRef,
696
+ panelStyle
697
+ };
698
+ }
699
+ //#endregion
700
+ //#region packages/ui/AppDropdown.tsx
701
+ function cx$2(...values) {
702
+ return values.filter(Boolean).join(" ");
703
+ }
704
+ function normalizeIcon$2(icon) {
705
+ if (!icon) return void 0;
706
+ if (icon.startsWith("ph ")) return icon;
707
+ if (icon.startsWith("ph-")) return `ph ${icon}`;
708
+ return `ph ph-${icon}`;
709
+ }
710
+ function AppDropdown({ id: idProp, label, icon, value, options, onChange, disabled = false, placeholder = "Seleccionar…", variant = "form", className, showFieldLabel = variant === "form", menuMinWidth, error, helpText, menuHeader, menuFooter, onOpenChange }) {
711
+ const generatedId = (0, react.useId)();
712
+ const id = idProp ?? generatedId;
713
+ const { open, setOpen: setMenuOpen, openPanel: openMenu, containerRef, panelRef: menuRef, panelStyle: menuStyle } = useFloatingPanel({
714
+ onOpenChange,
715
+ dismissEvent: "mousedown",
716
+ closeOnViewportExit: true,
717
+ computeStyle: (container) => {
718
+ return computeAnchoredStyle(container?.querySelector(".nb-dropdown__trigger") ?? container, {
719
+ matchAnchorWidth: true,
720
+ minWidth: menuMinWidth ?? 72
721
+ });
722
+ }
723
+ });
724
+ const selected = options.find((option) => option.value === value);
665
725
  const control = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
666
726
  ref: containerRef,
667
727
  className: cx$2("nb-dropdown", `nb-dropdown--${variant}`, open && "nb-dropdown--open", disabled && "nb-dropdown--disabled", className),
@@ -1384,48 +1444,15 @@ const Toggle = ({ checked, onChange, label, size = "md", disabled = false, "aria
1384
1444
  "aria-hidden": "true"
1385
1445
  })]
1386
1446
  });
1387
- if (!label) return input;
1388
1447
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
1389
1448
  htmlFor: id,
1390
- className: "nb-toggle-row",
1391
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: label }), input]
1449
+ className: label ? "nb-toggle-row" : void 0,
1450
+ children: [label && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: label }), input]
1392
1451
  });
1393
1452
  };
1394
1453
  //#endregion
1395
1454
  //#region packages/ui/ContextMenu.tsx
1396
1455
  const MENU_MIN_WIDTH = 190;
1397
- function getDropdownStyle(anchor, align) {
1398
- if (!anchor) return {
1399
- position: "fixed",
1400
- visibility: "hidden",
1401
- margin: 0
1402
- };
1403
- const rect = anchor.getBoundingClientRect();
1404
- const menuWidth = MENU_MIN_WIDTH;
1405
- const left = align === "right" ? Math.max(8, Math.min(rect.right - menuWidth, window.innerWidth - menuWidth - 8)) : Math.max(8, Math.min(rect.left, window.innerWidth - menuWidth - 8));
1406
- const spaceBelow = window.innerHeight - rect.bottom;
1407
- const spaceAbove = rect.top;
1408
- if (spaceBelow < 200 && spaceAbove > spaceBelow) return {
1409
- position: "fixed",
1410
- left: `${left}px`,
1411
- width: `${menuWidth}px`,
1412
- bottom: `${window.innerHeight - rect.top + 4}px`,
1413
- top: "auto",
1414
- margin: 0,
1415
- zIndex: 1600,
1416
- visibility: "visible"
1417
- };
1418
- return {
1419
- position: "fixed",
1420
- left: `${left}px`,
1421
- width: `${menuWidth}px`,
1422
- top: `${rect.bottom + 4}px`,
1423
- bottom: "auto",
1424
- margin: 0,
1425
- zIndex: 1600,
1426
- visibility: "visible"
1427
- };
1428
- }
1429
1456
  /**
1430
1457
  * Three-dot overflow context menu.
1431
1458
  * Replaces the legacy `shared/ui/library/card-menu/CardMenu` component.
@@ -1440,53 +1467,16 @@ function getDropdownStyle(anchor, align) {
1440
1467
  * - Portalled dropdown (avoids clip inside scrollable tables)
1441
1468
  */
1442
1469
  const ContextMenu = ({ items, visible = true, trigger, triggerLabel = "Opciones", align = "right", className = "" }) => {
1443
- const [open, setOpen] = (0, react.useState)(false);
1444
- const containerRef = (0, react.useRef)(null);
1445
- const menuRef = (0, react.useRef)(null);
1446
- const [menuStyle, setMenuStyle] = (0, react.useState)({
1447
- position: "fixed",
1448
- visibility: "hidden",
1449
- margin: 0
1470
+ const { open, setOpen, toggle, containerRef, panelRef: menuRef, panelStyle: menuStyle } = useFloatingPanel({
1471
+ dismissEvent: "mousedown",
1472
+ closeOnViewportExit: true,
1473
+ computeStyle: (container) => computeAnchoredStyle(container, {
1474
+ align: align === "right" ? "end" : "start",
1475
+ minWidth: MENU_MIN_WIDTH,
1476
+ flipThreshold: 200,
1477
+ zIndex: 1600
1478
+ })
1450
1479
  });
1451
- const computeMenuStyle = (0, react.useCallback)(() => getDropdownStyle(containerRef.current, align), [align]);
1452
- (0, react.useEffect)(() => {
1453
- if (!open) return;
1454
- const onMouseDown = (e) => {
1455
- const target = e.target;
1456
- if (!containerRef.current?.contains(target) && !menuRef.current?.contains(target)) setOpen(false);
1457
- };
1458
- const onKeyDown = (e) => {
1459
- if (e.key === "Escape") setOpen(false);
1460
- };
1461
- document.addEventListener("mousedown", onMouseDown);
1462
- document.addEventListener("keydown", onKeyDown);
1463
- return () => {
1464
- document.removeEventListener("mousedown", onMouseDown);
1465
- document.removeEventListener("keydown", onKeyDown);
1466
- };
1467
- }, [open]);
1468
- (0, react.useLayoutEffect)(() => {
1469
- if (!open) return;
1470
- const update = () => {
1471
- if (!containerRef.current) return;
1472
- const rect = containerRef.current.getBoundingClientRect();
1473
- if ((rect.width > 0 || rect.height > 0) && (rect.top >= window.innerHeight || rect.bottom <= 0)) {
1474
- setOpen(false);
1475
- return;
1476
- }
1477
- setMenuStyle(computeMenuStyle());
1478
- };
1479
- update();
1480
- window.addEventListener("resize", update);
1481
- window.addEventListener("scroll", update, {
1482
- capture: true,
1483
- passive: true
1484
- });
1485
- return () => {
1486
- window.removeEventListener("resize", update);
1487
- window.removeEventListener("scroll", update, { capture: true });
1488
- };
1489
- }, [open, computeMenuStyle]);
1490
1480
  if (!visible) return null;
1491
1481
  const dropdown = open ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
1492
1482
  ref: menuRef,
@@ -1520,17 +1510,14 @@ const ContextMenu = ({ items, visible = true, trigger, triggerLabel = "Opciones"
1520
1510
  ref: containerRef,
1521
1511
  className: `nb-context-menu${className ? ` ${className}` : ""}`,
1522
1512
  children: [trigger ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1523
- onClick: () => setOpen((v) => !v),
1513
+ onClick: toggle,
1524
1514
  children: trigger
1525
1515
  }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(IconButton, {
1526
1516
  icon: "ph ph-dots-three",
1527
1517
  label: triggerLabel,
1528
1518
  "aria-haspopup": "menu",
1529
1519
  "aria-expanded": open,
1530
- onClick: () => {
1531
- if (!open) setMenuStyle(computeMenuStyle());
1532
- setOpen((v) => !v);
1533
- }
1520
+ onClick: toggle
1534
1521
  }), dropdown && (0, react_dom.createPortal)(dropdown, document.body)]
1535
1522
  });
1536
1523
  };
@@ -2024,18 +2011,35 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2024
2011
  const selectedDate = parseIsoDate$1(value);
2025
2012
  const resolvedLocale = getLocale$1(locale);
2026
2013
  const displayValue = formatDisplayDate$1(value, resolvedLocale);
2027
- const [open, setOpen] = (0, react.useState)(false);
2028
2014
  const [viewMode, setViewMode] = (0, react.useState)("days");
2029
2015
  const [month, setMonth] = (0, react.useState)(() => startOfMonth$1(selectedDate ?? /* @__PURE__ */ new Date()));
2030
2016
  const [yearRangeStart, setYearRangeStart] = (0, react.useState)(() => getYearRangeStart((/* @__PURE__ */ new Date()).getFullYear()));
2031
- const [panelStyle, setPanelStyle] = (0, react.useState)({});
2032
2017
  const [focusedIso, setFocusedIso] = (0, react.useState)(null);
2033
2018
  const [inputText, setInputText] = (0, react.useState)("");
2034
2019
  const [isEditing, setIsEditing] = (0, react.useState)(false);
2035
- const rootRef = (0, react.useRef)(null);
2036
2020
  const inputRef = (0, react.useRef)(null);
2037
2021
  const triggerRef = (0, react.useRef)(null);
2038
- const panelRef = (0, react.useRef)(null);
2022
+ const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
2023
+ onClose: () => {
2024
+ setFocusedIso(null);
2025
+ setViewMode("days");
2026
+ },
2027
+ onEscape: (e) => {
2028
+ e.preventDefault();
2029
+ inputRef.current?.focus();
2030
+ },
2031
+ computeStyle: () => {
2032
+ const rect = triggerRef.current?.getBoundingClientRect();
2033
+ if (!rect) return null;
2034
+ const panelWidth = panelRef.current?.offsetWidth ?? 296;
2035
+ const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2036
+ const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2037
+ return {
2038
+ left,
2039
+ top: Math.max(8, top)
2040
+ };
2041
+ }
2042
+ });
2039
2043
  const days = (0, react.useMemo)(() => buildCalendarDays$1(month, min, max, resolvedLocale), [
2040
2044
  max,
2041
2045
  min,
@@ -2048,52 +2052,6 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2048
2052
  (0, react.useEffect)(() => {
2049
2053
  if (selectedDate) setMonth(startOfMonth$1(selectedDate));
2050
2054
  }, [value]);
2051
- (0, react.useEffect)(() => {
2052
- if (!open) {
2053
- setFocusedIso(null);
2054
- setViewMode("days");
2055
- return;
2056
- }
2057
- const updatePosition = () => {
2058
- const rect = triggerRef.current?.getBoundingClientRect();
2059
- if (!rect) return;
2060
- const panelWidth = panelRef.current?.offsetWidth ?? 296;
2061
- const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2062
- const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2063
- setPanelStyle({
2064
- left,
2065
- top: Math.max(8, top)
2066
- });
2067
- };
2068
- updatePosition();
2069
- window.addEventListener("resize", updatePosition);
2070
- window.addEventListener("scroll", updatePosition, true);
2071
- return () => {
2072
- window.removeEventListener("resize", updatePosition);
2073
- window.removeEventListener("scroll", updatePosition, true);
2074
- };
2075
- }, [open]);
2076
- (0, react.useEffect)(() => {
2077
- if (!open) return;
2078
- const handlePointerDown = (e) => {
2079
- const target = e.target;
2080
- if (rootRef.current?.contains(target) || panelRef.current?.contains(target)) return;
2081
- setOpen(false);
2082
- };
2083
- const handleKeyDown = (e) => {
2084
- if (e.key === "Escape") {
2085
- e.preventDefault();
2086
- setOpen(false);
2087
- inputRef.current?.focus();
2088
- }
2089
- };
2090
- document.addEventListener("pointerdown", handlePointerDown);
2091
- document.addEventListener("keydown", handleKeyDown);
2092
- return () => {
2093
- document.removeEventListener("pointerdown", handlePointerDown);
2094
- document.removeEventListener("keydown", handleKeyDown);
2095
- };
2096
- }, [open]);
2097
2055
  (0, react.useEffect)(() => {
2098
2056
  if (!open) return;
2099
2057
  const initialIso = selectedDate ? toIsoDate$1(selectedDate) : toIsoDate$1(/* @__PURE__ */ new Date());
@@ -2108,7 +2066,7 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2108
2066
  setIsEditing(false);
2109
2067
  setInputText("");
2110
2068
  window.setTimeout(() => inputRef.current?.focus());
2111
- }, [onChange]);
2069
+ }, [onChange, setOpen]);
2112
2070
  const handleToday = (0, react.useCallback)(() => {
2113
2071
  const today = toIsoDate$1(/* @__PURE__ */ new Date());
2114
2072
  if (isIsoDateDisabled(today, min, max)) return;
@@ -2157,7 +2115,7 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2157
2115
  const handleCalendarPointerDown = (e) => {
2158
2116
  e.preventDefault();
2159
2117
  if (readOnly || disabled) return;
2160
- setOpen((v) => !v);
2118
+ setOpen(!open);
2161
2119
  };
2162
2120
  const handleDayKeyDown = (0, react.useCallback)((e, iso) => {
2163
2121
  if (![
@@ -2193,7 +2151,8 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2193
2151
  }, [
2194
2152
  days,
2195
2153
  month,
2196
- commitValue
2154
+ commitValue,
2155
+ panelRef
2197
2156
  ]);
2198
2157
  const handleMonthSelect = (monthIndex) => {
2199
2158
  setMonth(new Date(month.getFullYear(), monthIndex, 1));
@@ -2572,18 +2531,14 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2572
2531
  const endDisplay = formatDisplayDate(endValue, resolvedLocale);
2573
2532
  const startIso = parseIsoDate(startValue) ? startValue : null;
2574
2533
  const endIso = parseIsoDate(endValue) ? endValue : null;
2575
- const [open, setOpen] = (0, react.useState)(false);
2576
2534
  const [phase, setPhase] = (0, react.useState)("idle");
2577
2535
  const [pendingStart, setPendingStart] = (0, react.useState)(null);
2578
2536
  const [hoverIso, setHoverIso] = (0, react.useState)(null);
2579
2537
  const [month, setMonth] = (0, react.useState)(() => startOfMonth(parseIsoDate(startValue) ?? /* @__PURE__ */ new Date()));
2580
- const [panelStyle, setPanelStyle] = (0, react.useState)({});
2581
2538
  const [startText, setStartText] = (0, react.useState)("");
2582
2539
  const [endText, setEndText] = (0, react.useState)("");
2583
2540
  const [editingField, setEditingField] = (0, react.useState)(null);
2584
- const rootRef = (0, react.useRef)(null);
2585
2541
  const triggerRef = (0, react.useRef)(null);
2586
- const panelRef = (0, react.useRef)(null);
2587
2542
  const startInputRef = (0, react.useRef)(null);
2588
2543
  const endInputRef = (0, react.useRef)(null);
2589
2544
  const days = (0, react.useMemo)(() => buildCalendarDays(month, min, max, resolvedLocale), [
@@ -2598,54 +2553,31 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2598
2553
  const anchor = parseIsoDate(startValue) ?? parseIsoDate(endValue);
2599
2554
  if (anchor) setMonth(startOfMonth(anchor));
2600
2555
  }, [startValue]);
2601
- (0, react.useEffect)(() => {
2602
- if (!open) return;
2603
- const update = () => {
2556
+ const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
2557
+ onClose: () => {
2558
+ setPhase("idle");
2559
+ setPendingStart(null);
2560
+ setHoverIso(null);
2561
+ },
2562
+ onEscape: (e) => {
2563
+ e.preventDefault();
2564
+ startInputRef.current?.focus();
2565
+ },
2566
+ computeStyle: () => {
2604
2567
  const rect = triggerRef.current?.getBoundingClientRect();
2605
- if (!rect) return;
2568
+ if (!rect) return null;
2606
2569
  const panelWidth = panelRef.current?.offsetWidth ?? 296;
2607
2570
  const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2608
2571
  const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2609
- setPanelStyle({
2572
+ return {
2610
2573
  left,
2611
2574
  top: Math.max(8, top)
2612
- });
2613
- };
2614
- update();
2615
- window.addEventListener("resize", update);
2616
- window.addEventListener("scroll", update, true);
2617
- return () => {
2618
- window.removeEventListener("resize", update);
2619
- window.removeEventListener("scroll", update, true);
2620
- };
2621
- }, [open]);
2575
+ };
2576
+ }
2577
+ });
2622
2578
  const closePanel = (0, react.useCallback)(() => {
2623
2579
  setOpen(false);
2624
- setPhase("idle");
2625
- setPendingStart(null);
2626
- setHoverIso(null);
2627
- }, []);
2628
- (0, react.useEffect)(() => {
2629
- if (!open) return;
2630
- const handlePointerDown = (e) => {
2631
- const target = e.target;
2632
- if (rootRef.current?.contains(target) || panelRef.current?.contains(target)) return;
2633
- closePanel();
2634
- };
2635
- const handleKeyDown = (e) => {
2636
- if (e.key === "Escape") {
2637
- e.preventDefault();
2638
- closePanel();
2639
- startInputRef.current?.focus();
2640
- }
2641
- };
2642
- document.addEventListener("pointerdown", handlePointerDown);
2643
- document.addEventListener("keydown", handleKeyDown);
2644
- return () => {
2645
- document.removeEventListener("pointerdown", handlePointerDown);
2646
- document.removeEventListener("keydown", handleKeyDown);
2647
- };
2648
- }, [open, closePanel]);
2580
+ }, [setOpen]);
2649
2581
  const commitRange = (0, react.useCallback)((start, end) => {
2650
2582
  const [a, b] = start <= end ? [start, end] : [end, start];
2651
2583
  onChange?.(a, b);
@@ -2747,7 +2679,7 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2747
2679
  const handleCalendarPointerDown = (e) => {
2748
2680
  e.preventDefault();
2749
2681
  if (readOnly || disabled) return;
2750
- setOpen((v) => !v);
2682
+ setOpen(!open);
2751
2683
  };
2752
2684
  const handleClear = (e) => {
2753
2685
  e.preventDefault();
@@ -2945,41 +2877,6 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2945
2877
  });
2946
2878
  }
2947
2879
  //#endregion
2948
- //#region packages/ui/useFloatingPanel.ts
2949
- function useFloatingPanel(options = {}) {
2950
- const [open, setOpenState] = (0, react.useState)(false);
2951
- const containerRef = (0, react.useRef)(null);
2952
- const onCloseRef = (0, react.useRef)(options.onClose);
2953
- onCloseRef.current = options.onClose;
2954
- const setOpen = (0, react.useCallback)((next) => {
2955
- setOpenState(next);
2956
- if (!next) onCloseRef.current?.();
2957
- }, []);
2958
- const toggle = (0, react.useCallback)(() => setOpen(!open), [open, setOpen]);
2959
- (0, react.useEffect)(() => {
2960
- if (!open) return;
2961
- const handlePointerDown = (e) => {
2962
- const container = containerRef.current;
2963
- if (container && !container.contains(e.target)) setOpen(false);
2964
- };
2965
- const handleKeyDown = (e) => {
2966
- if (e.key === "Escape") setOpen(false);
2967
- };
2968
- document.addEventListener("pointerdown", handlePointerDown);
2969
- document.addEventListener("keydown", handleKeyDown);
2970
- return () => {
2971
- document.removeEventListener("pointerdown", handlePointerDown);
2972
- document.removeEventListener("keydown", handleKeyDown);
2973
- };
2974
- }, [open, setOpen]);
2975
- return {
2976
- open,
2977
- setOpen,
2978
- toggle,
2979
- containerRef
2980
- };
2981
- }
2982
- //#endregion
2983
2880
  //#region packages/ui/Popover.tsx
2984
2881
  const Popover = ({ trigger, panel, align = "end", ariaLabel, className }) => {
2985
2882
  const { open, setOpen, toggle, containerRef } = useFloatingPanel();