@nubitio/ui 0.6.10 → 0.7.0

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),
@@ -1394,38 +1454,6 @@ const Toggle = ({ checked, onChange, label, size = "md", disabled = false, "aria
1394
1454
  //#endregion
1395
1455
  //#region packages/ui/ContextMenu.tsx
1396
1456
  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
1457
  /**
1430
1458
  * Three-dot overflow context menu.
1431
1459
  * Replaces the legacy `shared/ui/library/card-menu/CardMenu` component.
@@ -1440,53 +1468,16 @@ function getDropdownStyle(anchor, align) {
1440
1468
  * - Portalled dropdown (avoids clip inside scrollable tables)
1441
1469
  */
1442
1470
  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
1471
+ const { open, setOpen, toggle, containerRef, panelRef: menuRef, panelStyle: menuStyle } = useFloatingPanel({
1472
+ dismissEvent: "mousedown",
1473
+ closeOnViewportExit: true,
1474
+ computeStyle: (container) => computeAnchoredStyle(container, {
1475
+ align: align === "right" ? "end" : "start",
1476
+ minWidth: MENU_MIN_WIDTH,
1477
+ flipThreshold: 200,
1478
+ zIndex: 1600
1479
+ })
1450
1480
  });
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
1481
  if (!visible) return null;
1491
1482
  const dropdown = open ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
1492
1483
  ref: menuRef,
@@ -1520,17 +1511,14 @@ const ContextMenu = ({ items, visible = true, trigger, triggerLabel = "Opciones"
1520
1511
  ref: containerRef,
1521
1512
  className: `nb-context-menu${className ? ` ${className}` : ""}`,
1522
1513
  children: [trigger ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1523
- onClick: () => setOpen((v) => !v),
1514
+ onClick: toggle,
1524
1515
  children: trigger
1525
1516
  }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(IconButton, {
1526
1517
  icon: "ph ph-dots-three",
1527
1518
  label: triggerLabel,
1528
1519
  "aria-haspopup": "menu",
1529
1520
  "aria-expanded": open,
1530
- onClick: () => {
1531
- if (!open) setMenuStyle(computeMenuStyle());
1532
- setOpen((v) => !v);
1533
- }
1521
+ onClick: toggle
1534
1522
  }), dropdown && (0, react_dom.createPortal)(dropdown, document.body)]
1535
1523
  });
1536
1524
  };
@@ -2024,18 +2012,35 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2024
2012
  const selectedDate = parseIsoDate$1(value);
2025
2013
  const resolvedLocale = getLocale$1(locale);
2026
2014
  const displayValue = formatDisplayDate$1(value, resolvedLocale);
2027
- const [open, setOpen] = (0, react.useState)(false);
2028
2015
  const [viewMode, setViewMode] = (0, react.useState)("days");
2029
2016
  const [month, setMonth] = (0, react.useState)(() => startOfMonth$1(selectedDate ?? /* @__PURE__ */ new Date()));
2030
2017
  const [yearRangeStart, setYearRangeStart] = (0, react.useState)(() => getYearRangeStart((/* @__PURE__ */ new Date()).getFullYear()));
2031
- const [panelStyle, setPanelStyle] = (0, react.useState)({});
2032
2018
  const [focusedIso, setFocusedIso] = (0, react.useState)(null);
2033
2019
  const [inputText, setInputText] = (0, react.useState)("");
2034
2020
  const [isEditing, setIsEditing] = (0, react.useState)(false);
2035
- const rootRef = (0, react.useRef)(null);
2036
2021
  const inputRef = (0, react.useRef)(null);
2037
2022
  const triggerRef = (0, react.useRef)(null);
2038
- const panelRef = (0, react.useRef)(null);
2023
+ const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
2024
+ onClose: () => {
2025
+ setFocusedIso(null);
2026
+ setViewMode("days");
2027
+ },
2028
+ onEscape: (e) => {
2029
+ e.preventDefault();
2030
+ inputRef.current?.focus();
2031
+ },
2032
+ computeStyle: () => {
2033
+ const rect = triggerRef.current?.getBoundingClientRect();
2034
+ if (!rect) return null;
2035
+ const panelWidth = panelRef.current?.offsetWidth ?? 296;
2036
+ const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2037
+ const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2038
+ return {
2039
+ left,
2040
+ top: Math.max(8, top)
2041
+ };
2042
+ }
2043
+ });
2039
2044
  const days = (0, react.useMemo)(() => buildCalendarDays$1(month, min, max, resolvedLocale), [
2040
2045
  max,
2041
2046
  min,
@@ -2048,52 +2053,6 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2048
2053
  (0, react.useEffect)(() => {
2049
2054
  if (selectedDate) setMonth(startOfMonth$1(selectedDate));
2050
2055
  }, [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
2056
  (0, react.useEffect)(() => {
2098
2057
  if (!open) return;
2099
2058
  const initialIso = selectedDate ? toIsoDate$1(selectedDate) : toIsoDate$1(/* @__PURE__ */ new Date());
@@ -2108,7 +2067,7 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2108
2067
  setIsEditing(false);
2109
2068
  setInputText("");
2110
2069
  window.setTimeout(() => inputRef.current?.focus());
2111
- }, [onChange]);
2070
+ }, [onChange, setOpen]);
2112
2071
  const handleToday = (0, react.useCallback)(() => {
2113
2072
  const today = toIsoDate$1(/* @__PURE__ */ new Date());
2114
2073
  if (isIsoDateDisabled(today, min, max)) return;
@@ -2157,7 +2116,7 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2157
2116
  const handleCalendarPointerDown = (e) => {
2158
2117
  e.preventDefault();
2159
2118
  if (readOnly || disabled) return;
2160
- setOpen((v) => !v);
2119
+ setOpen(!open);
2161
2120
  };
2162
2121
  const handleDayKeyDown = (0, react.useCallback)((e, iso) => {
2163
2122
  if (![
@@ -2193,7 +2152,8 @@ const DatePicker = (0, react.forwardRef)(function DatePicker({ id, name, value,
2193
2152
  }, [
2194
2153
  days,
2195
2154
  month,
2196
- commitValue
2155
+ commitValue,
2156
+ panelRef
2197
2157
  ]);
2198
2158
  const handleMonthSelect = (monthIndex) => {
2199
2159
  setMonth(new Date(month.getFullYear(), monthIndex, 1));
@@ -2572,18 +2532,14 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2572
2532
  const endDisplay = formatDisplayDate(endValue, resolvedLocale);
2573
2533
  const startIso = parseIsoDate(startValue) ? startValue : null;
2574
2534
  const endIso = parseIsoDate(endValue) ? endValue : null;
2575
- const [open, setOpen] = (0, react.useState)(false);
2576
2535
  const [phase, setPhase] = (0, react.useState)("idle");
2577
2536
  const [pendingStart, setPendingStart] = (0, react.useState)(null);
2578
2537
  const [hoverIso, setHoverIso] = (0, react.useState)(null);
2579
2538
  const [month, setMonth] = (0, react.useState)(() => startOfMonth(parseIsoDate(startValue) ?? /* @__PURE__ */ new Date()));
2580
- const [panelStyle, setPanelStyle] = (0, react.useState)({});
2581
2539
  const [startText, setStartText] = (0, react.useState)("");
2582
2540
  const [endText, setEndText] = (0, react.useState)("");
2583
2541
  const [editingField, setEditingField] = (0, react.useState)(null);
2584
- const rootRef = (0, react.useRef)(null);
2585
2542
  const triggerRef = (0, react.useRef)(null);
2586
- const panelRef = (0, react.useRef)(null);
2587
2543
  const startInputRef = (0, react.useRef)(null);
2588
2544
  const endInputRef = (0, react.useRef)(null);
2589
2545
  const days = (0, react.useMemo)(() => buildCalendarDays(month, min, max, resolvedLocale), [
@@ -2598,54 +2554,31 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2598
2554
  const anchor = parseIsoDate(startValue) ?? parseIsoDate(endValue);
2599
2555
  if (anchor) setMonth(startOfMonth(anchor));
2600
2556
  }, [startValue]);
2601
- (0, react.useEffect)(() => {
2602
- if (!open) return;
2603
- const update = () => {
2557
+ const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
2558
+ onClose: () => {
2559
+ setPhase("idle");
2560
+ setPendingStart(null);
2561
+ setHoverIso(null);
2562
+ },
2563
+ onEscape: (e) => {
2564
+ e.preventDefault();
2565
+ startInputRef.current?.focus();
2566
+ },
2567
+ computeStyle: () => {
2604
2568
  const rect = triggerRef.current?.getBoundingClientRect();
2605
- if (!rect) return;
2569
+ if (!rect) return null;
2606
2570
  const panelWidth = panelRef.current?.offsetWidth ?? 296;
2607
2571
  const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2608
2572
  const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2609
- setPanelStyle({
2573
+ return {
2610
2574
  left,
2611
2575
  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]);
2576
+ };
2577
+ }
2578
+ });
2622
2579
  const closePanel = (0, react.useCallback)(() => {
2623
2580
  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]);
2581
+ }, [setOpen]);
2649
2582
  const commitRange = (0, react.useCallback)((start, end) => {
2650
2583
  const [a, b] = start <= end ? [start, end] : [end, start];
2651
2584
  onChange?.(a, b);
@@ -2747,7 +2680,7 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2747
2680
  const handleCalendarPointerDown = (e) => {
2748
2681
  e.preventDefault();
2749
2682
  if (readOnly || disabled) return;
2750
- setOpen((v) => !v);
2683
+ setOpen(!open);
2751
2684
  };
2752
2685
  const handleClear = (e) => {
2753
2686
  e.preventDefault();
@@ -2945,41 +2878,6 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2945
2878
  });
2946
2879
  }
2947
2880
  //#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
2881
  //#region packages/ui/Popover.tsx
2984
2882
  const Popover = ({ trigger, panel, align = "end", ariaLabel, className }) => {
2985
2883
  const { open, setOpen, toggle, containerRef } = useFloatingPanel();