@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.mjs CHANGED
@@ -540,84 +540,111 @@ function FileDropzone({ accept, value = null, image = false, disabled = false, r
540
540
  });
541
541
  }
542
542
  //#endregion
543
- //#region packages/ui/AppDropdown.tsx
544
- function cx$2(...values) {
545
- return values.filter(Boolean).join(" ");
546
- }
547
- function normalizeIcon$2(icon) {
548
- if (!icon) return void 0;
549
- if (icon.startsWith("ph ")) return icon;
550
- if (icon.startsWith("ph-")) return `ph ${icon}`;
551
- return `ph ph-${icon}`;
552
- }
553
- function getMenuStyle(containerRef, menuMinWidth = 72) {
554
- if (!containerRef.current) return {
555
- position: "fixed",
556
- visibility: "hidden",
557
- margin: 0
558
- };
559
- const rect = (containerRef.current.querySelector(".nb-dropdown__trigger") ?? containerRef.current).getBoundingClientRect();
560
- const menuWidth = Math.max(rect.width, menuMinWidth);
561
- const left = Math.max(8, Math.min(rect.left, window.innerWidth - menuWidth - 8));
543
+ //#region packages/ui/useFloatingPanel.ts
544
+ /** Style applied to an anchored panel before its first position is computed. */
545
+ const HIDDEN_PANEL_STYLE = {
546
+ position: "fixed",
547
+ visibility: "hidden",
548
+ margin: 0
549
+ };
550
+ /**
551
+ * Fixed-position style for a panel anchored to an element, clamped to the
552
+ * viewport with an 8px gutter and flipped above the anchor when it fits
553
+ * better there.
554
+ */
555
+ function computeAnchoredStyle(anchor, options = {}) {
556
+ if (!anchor) return HIDDEN_PANEL_STYLE;
557
+ const { align = "start", minWidth = 0, matchAnchorWidth = false, offset = 4, flipThreshold = 250, zIndex = 9999 } = options;
558
+ const rect = anchor.getBoundingClientRect();
559
+ const width = Math.max(matchAnchorWidth ? rect.width : 0, minWidth);
560
+ 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));
562
561
  const spaceBelow = window.innerHeight - rect.bottom;
563
- const spaceAbove = rect.top;
564
- if (spaceBelow < 250 && spaceAbove > spaceBelow) return {
562
+ if (spaceBelow < flipThreshold && rect.top > spaceBelow) return {
565
563
  position: "fixed",
566
564
  left: `${left}px`,
567
- width: `${menuWidth}px`,
568
- bottom: `${window.innerHeight - rect.top + 4}px`,
565
+ width: `${width}px`,
566
+ bottom: `${window.innerHeight - rect.top + offset}px`,
569
567
  top: "auto",
570
568
  margin: 0,
571
- zIndex: 9999,
569
+ zIndex,
572
570
  visibility: "visible"
573
571
  };
574
572
  return {
575
573
  position: "fixed",
576
574
  left: `${left}px`,
577
- width: `${menuWidth}px`,
578
- top: `${rect.bottom + 4}px`,
575
+ width: `${width}px`,
576
+ top: `${rect.bottom + offset}px`,
579
577
  bottom: "auto",
580
578
  margin: 0,
581
- zIndex: 9999,
579
+ zIndex,
582
580
  visibility: "visible"
583
581
  };
584
582
  }
585
- 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 }) {
586
- const generatedId = useId();
587
- const id = idProp ?? generatedId;
588
- const [open, setOpen] = useState(false);
583
+ function useFloatingPanel(options = {}) {
584
+ const [open, setOpenState] = useState(false);
585
+ const [panelStyle, setPanelStyle] = useState(HIDDEN_PANEL_STYLE);
589
586
  const containerRef = useRef(null);
590
- const menuRef = useRef(null);
591
- const [menuStyle, setMenuStyle] = useState({
592
- position: "fixed",
593
- visibility: "hidden",
594
- margin: 0
595
- });
596
- const selected = options.find((option) => option.value === value);
597
- const computeMenuStyle = useCallback(() => getMenuStyle(containerRef, menuMinWidth), [menuMinWidth]);
598
- const setMenuOpen = useCallback((next) => {
599
- setOpen(next);
600
- onOpenChange?.(next);
601
- }, [onOpenChange]);
587
+ const panelRef = useRef(null);
588
+ const optionsRef = useRef(options);
589
+ optionsRef.current = options;
590
+ const anchored = !!options.computeStyle;
591
+ const dismissEvent = options.dismissEvent ?? "pointerdown";
592
+ const setOpen = useCallback((next) => {
593
+ setOpenState(next);
594
+ if (!next) optionsRef.current.onClose?.();
595
+ optionsRef.current.onOpenChange?.(next);
596
+ }, []);
597
+ const applyStyle = useCallback(() => {
598
+ const compute = optionsRef.current.computeStyle;
599
+ if (!compute) return;
600
+ const style = compute(containerRef.current);
601
+ if (style) setPanelStyle(style);
602
+ }, []);
603
+ const openPanel = useCallback(() => {
604
+ applyStyle();
605
+ setOpen(true);
606
+ }, [applyStyle, setOpen]);
607
+ const toggle = useCallback(() => open ? setOpen(false) : openPanel(), [
608
+ open,
609
+ openPanel,
610
+ setOpen
611
+ ]);
602
612
  useEffect(() => {
603
613
  if (!open) return;
604
- const handler = (e) => {
614
+ const handleOutside = (e) => {
605
615
  const target = e.target;
606
- if (!containerRef.current?.contains(target) && !menuRef.current?.contains(target)) setMenuOpen(false);
616
+ if (containerRef.current?.contains(target) || panelRef.current?.contains(target)) return;
617
+ setOpen(false);
618
+ };
619
+ const handleKeyDown = (e) => {
620
+ if (e.key !== "Escape") return;
621
+ setOpen(false);
622
+ optionsRef.current.onEscape?.(e);
607
623
  };
608
- document.addEventListener("mousedown", handler);
609
- return () => document.removeEventListener("mousedown", handler);
610
- }, [open, setMenuOpen]);
624
+ document.addEventListener(dismissEvent, handleOutside);
625
+ document.addEventListener("keydown", handleKeyDown);
626
+ return () => {
627
+ document.removeEventListener(dismissEvent, handleOutside);
628
+ document.removeEventListener("keydown", handleKeyDown);
629
+ };
630
+ }, [
631
+ dismissEvent,
632
+ open,
633
+ setOpen
634
+ ]);
611
635
  useLayoutEffect(() => {
612
- if (!open) return;
636
+ if (!open || !anchored) return;
613
637
  const update = () => {
614
- if (!containerRef.current) return;
615
- const rect = containerRef.current.getBoundingClientRect();
616
- if (rect.top >= window.innerHeight || rect.bottom <= 0) {
617
- setMenuOpen(false);
618
- return;
638
+ const container = containerRef.current;
639
+ if (!container) return;
640
+ if (optionsRef.current.closeOnViewportExit) {
641
+ const rect = container.getBoundingClientRect();
642
+ if ((rect.width > 0 || rect.height > 0) && (rect.top >= window.innerHeight || rect.bottom <= 0)) {
643
+ setOpen(false);
644
+ return;
645
+ }
619
646
  }
620
- setMenuStyle(computeMenuStyle());
647
+ applyStyle();
621
648
  };
622
649
  update();
623
650
  window.addEventListener("resize", update);
@@ -630,14 +657,47 @@ function AppDropdown({ id: idProp, label, icon, value, options, onChange, disabl
630
657
  window.removeEventListener("scroll", update, { capture: true });
631
658
  };
632
659
  }, [
660
+ anchored,
661
+ applyStyle,
633
662
  open,
634
- computeMenuStyle,
635
- setMenuOpen
663
+ setOpen
636
664
  ]);
637
- const openMenu = useCallback(() => {
638
- setMenuStyle(computeMenuStyle());
639
- setMenuOpen(true);
640
- }, [computeMenuStyle, setMenuOpen]);
665
+ return {
666
+ open,
667
+ setOpen,
668
+ toggle,
669
+ openPanel,
670
+ containerRef,
671
+ panelRef,
672
+ panelStyle
673
+ };
674
+ }
675
+ //#endregion
676
+ //#region packages/ui/AppDropdown.tsx
677
+ function cx$2(...values) {
678
+ return values.filter(Boolean).join(" ");
679
+ }
680
+ function normalizeIcon$2(icon) {
681
+ if (!icon) return void 0;
682
+ if (icon.startsWith("ph ")) return icon;
683
+ if (icon.startsWith("ph-")) return `ph ${icon}`;
684
+ return `ph ph-${icon}`;
685
+ }
686
+ 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 }) {
687
+ const generatedId = useId();
688
+ const id = idProp ?? generatedId;
689
+ const { open, setOpen: setMenuOpen, openPanel: openMenu, containerRef, panelRef: menuRef, panelStyle: menuStyle } = useFloatingPanel({
690
+ onOpenChange,
691
+ dismissEvent: "mousedown",
692
+ closeOnViewportExit: true,
693
+ computeStyle: (container) => {
694
+ return computeAnchoredStyle(container?.querySelector(".nb-dropdown__trigger") ?? container, {
695
+ matchAnchorWidth: true,
696
+ minWidth: menuMinWidth ?? 72
697
+ });
698
+ }
699
+ });
700
+ const selected = options.find((option) => option.value === value);
641
701
  const control = /* @__PURE__ */ jsxs("div", {
642
702
  ref: containerRef,
643
703
  className: cx$2("nb-dropdown", `nb-dropdown--${variant}`, open && "nb-dropdown--open", disabled && "nb-dropdown--disabled", className),
@@ -1360,48 +1420,15 @@ const Toggle = ({ checked, onChange, label, size = "md", disabled = false, "aria
1360
1420
  "aria-hidden": "true"
1361
1421
  })]
1362
1422
  });
1363
- if (!label) return input;
1364
1423
  return /* @__PURE__ */ jsxs("label", {
1365
1424
  htmlFor: id,
1366
- className: "nb-toggle-row",
1367
- children: [/* @__PURE__ */ jsx("span", { children: label }), input]
1425
+ className: label ? "nb-toggle-row" : void 0,
1426
+ children: [label && /* @__PURE__ */ jsx("span", { children: label }), input]
1368
1427
  });
1369
1428
  };
1370
1429
  //#endregion
1371
1430
  //#region packages/ui/ContextMenu.tsx
1372
1431
  const MENU_MIN_WIDTH = 190;
1373
- function getDropdownStyle(anchor, align) {
1374
- if (!anchor) return {
1375
- position: "fixed",
1376
- visibility: "hidden",
1377
- margin: 0
1378
- };
1379
- const rect = anchor.getBoundingClientRect();
1380
- const menuWidth = MENU_MIN_WIDTH;
1381
- 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));
1382
- const spaceBelow = window.innerHeight - rect.bottom;
1383
- const spaceAbove = rect.top;
1384
- if (spaceBelow < 200 && spaceAbove > spaceBelow) return {
1385
- position: "fixed",
1386
- left: `${left}px`,
1387
- width: `${menuWidth}px`,
1388
- bottom: `${window.innerHeight - rect.top + 4}px`,
1389
- top: "auto",
1390
- margin: 0,
1391
- zIndex: 1600,
1392
- visibility: "visible"
1393
- };
1394
- return {
1395
- position: "fixed",
1396
- left: `${left}px`,
1397
- width: `${menuWidth}px`,
1398
- top: `${rect.bottom + 4}px`,
1399
- bottom: "auto",
1400
- margin: 0,
1401
- zIndex: 1600,
1402
- visibility: "visible"
1403
- };
1404
- }
1405
1432
  /**
1406
1433
  * Three-dot overflow context menu.
1407
1434
  * Replaces the legacy `shared/ui/library/card-menu/CardMenu` component.
@@ -1416,53 +1443,16 @@ function getDropdownStyle(anchor, align) {
1416
1443
  * - Portalled dropdown (avoids clip inside scrollable tables)
1417
1444
  */
1418
1445
  const ContextMenu = ({ items, visible = true, trigger, triggerLabel = "Opciones", align = "right", className = "" }) => {
1419
- const [open, setOpen] = useState(false);
1420
- const containerRef = useRef(null);
1421
- const menuRef = useRef(null);
1422
- const [menuStyle, setMenuStyle] = useState({
1423
- position: "fixed",
1424
- visibility: "hidden",
1425
- margin: 0
1446
+ const { open, setOpen, toggle, containerRef, panelRef: menuRef, panelStyle: menuStyle } = useFloatingPanel({
1447
+ dismissEvent: "mousedown",
1448
+ closeOnViewportExit: true,
1449
+ computeStyle: (container) => computeAnchoredStyle(container, {
1450
+ align: align === "right" ? "end" : "start",
1451
+ minWidth: MENU_MIN_WIDTH,
1452
+ flipThreshold: 200,
1453
+ zIndex: 1600
1454
+ })
1426
1455
  });
1427
- const computeMenuStyle = useCallback(() => getDropdownStyle(containerRef.current, align), [align]);
1428
- useEffect(() => {
1429
- if (!open) return;
1430
- const onMouseDown = (e) => {
1431
- const target = e.target;
1432
- if (!containerRef.current?.contains(target) && !menuRef.current?.contains(target)) setOpen(false);
1433
- };
1434
- const onKeyDown = (e) => {
1435
- if (e.key === "Escape") setOpen(false);
1436
- };
1437
- document.addEventListener("mousedown", onMouseDown);
1438
- document.addEventListener("keydown", onKeyDown);
1439
- return () => {
1440
- document.removeEventListener("mousedown", onMouseDown);
1441
- document.removeEventListener("keydown", onKeyDown);
1442
- };
1443
- }, [open]);
1444
- useLayoutEffect(() => {
1445
- if (!open) return;
1446
- const update = () => {
1447
- if (!containerRef.current) return;
1448
- const rect = containerRef.current.getBoundingClientRect();
1449
- if ((rect.width > 0 || rect.height > 0) && (rect.top >= window.innerHeight || rect.bottom <= 0)) {
1450
- setOpen(false);
1451
- return;
1452
- }
1453
- setMenuStyle(computeMenuStyle());
1454
- };
1455
- update();
1456
- window.addEventListener("resize", update);
1457
- window.addEventListener("scroll", update, {
1458
- capture: true,
1459
- passive: true
1460
- });
1461
- return () => {
1462
- window.removeEventListener("resize", update);
1463
- window.removeEventListener("scroll", update, { capture: true });
1464
- };
1465
- }, [open, computeMenuStyle]);
1466
1456
  if (!visible) return null;
1467
1457
  const dropdown = open ? /* @__PURE__ */ jsx("ul", {
1468
1458
  ref: menuRef,
@@ -1496,17 +1486,14 @@ const ContextMenu = ({ items, visible = true, trigger, triggerLabel = "Opciones"
1496
1486
  ref: containerRef,
1497
1487
  className: `nb-context-menu${className ? ` ${className}` : ""}`,
1498
1488
  children: [trigger ? /* @__PURE__ */ jsx("span", {
1499
- onClick: () => setOpen((v) => !v),
1489
+ onClick: toggle,
1500
1490
  children: trigger
1501
1491
  }) : /* @__PURE__ */ jsx(IconButton, {
1502
1492
  icon: "ph ph-dots-three",
1503
1493
  label: triggerLabel,
1504
1494
  "aria-haspopup": "menu",
1505
1495
  "aria-expanded": open,
1506
- onClick: () => {
1507
- if (!open) setMenuStyle(computeMenuStyle());
1508
- setOpen((v) => !v);
1509
- }
1496
+ onClick: toggle
1510
1497
  }), dropdown && createPortal(dropdown, document.body)]
1511
1498
  });
1512
1499
  };
@@ -2000,18 +1987,35 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
2000
1987
  const selectedDate = parseIsoDate$1(value);
2001
1988
  const resolvedLocale = getLocale$1(locale);
2002
1989
  const displayValue = formatDisplayDate$1(value, resolvedLocale);
2003
- const [open, setOpen] = useState(false);
2004
1990
  const [viewMode, setViewMode] = useState("days");
2005
1991
  const [month, setMonth] = useState(() => startOfMonth$1(selectedDate ?? /* @__PURE__ */ new Date()));
2006
1992
  const [yearRangeStart, setYearRangeStart] = useState(() => getYearRangeStart((/* @__PURE__ */ new Date()).getFullYear()));
2007
- const [panelStyle, setPanelStyle] = useState({});
2008
1993
  const [focusedIso, setFocusedIso] = useState(null);
2009
1994
  const [inputText, setInputText] = useState("");
2010
1995
  const [isEditing, setIsEditing] = useState(false);
2011
- const rootRef = useRef(null);
2012
1996
  const inputRef = useRef(null);
2013
1997
  const triggerRef = useRef(null);
2014
- const panelRef = useRef(null);
1998
+ const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
1999
+ onClose: () => {
2000
+ setFocusedIso(null);
2001
+ setViewMode("days");
2002
+ },
2003
+ onEscape: (e) => {
2004
+ e.preventDefault();
2005
+ inputRef.current?.focus();
2006
+ },
2007
+ computeStyle: () => {
2008
+ const rect = triggerRef.current?.getBoundingClientRect();
2009
+ if (!rect) return null;
2010
+ const panelWidth = panelRef.current?.offsetWidth ?? 296;
2011
+ const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2012
+ const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2013
+ return {
2014
+ left,
2015
+ top: Math.max(8, top)
2016
+ };
2017
+ }
2018
+ });
2015
2019
  const days = useMemo(() => buildCalendarDays$1(month, min, max, resolvedLocale), [
2016
2020
  max,
2017
2021
  min,
@@ -2024,52 +2028,6 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
2024
2028
  useEffect(() => {
2025
2029
  if (selectedDate) setMonth(startOfMonth$1(selectedDate));
2026
2030
  }, [value]);
2027
- useEffect(() => {
2028
- if (!open) {
2029
- setFocusedIso(null);
2030
- setViewMode("days");
2031
- return;
2032
- }
2033
- const updatePosition = () => {
2034
- const rect = triggerRef.current?.getBoundingClientRect();
2035
- if (!rect) return;
2036
- const panelWidth = panelRef.current?.offsetWidth ?? 296;
2037
- const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2038
- const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2039
- setPanelStyle({
2040
- left,
2041
- top: Math.max(8, top)
2042
- });
2043
- };
2044
- updatePosition();
2045
- window.addEventListener("resize", updatePosition);
2046
- window.addEventListener("scroll", updatePosition, true);
2047
- return () => {
2048
- window.removeEventListener("resize", updatePosition);
2049
- window.removeEventListener("scroll", updatePosition, true);
2050
- };
2051
- }, [open]);
2052
- useEffect(() => {
2053
- if (!open) return;
2054
- const handlePointerDown = (e) => {
2055
- const target = e.target;
2056
- if (rootRef.current?.contains(target) || panelRef.current?.contains(target)) return;
2057
- setOpen(false);
2058
- };
2059
- const handleKeyDown = (e) => {
2060
- if (e.key === "Escape") {
2061
- e.preventDefault();
2062
- setOpen(false);
2063
- inputRef.current?.focus();
2064
- }
2065
- };
2066
- document.addEventListener("pointerdown", handlePointerDown);
2067
- document.addEventListener("keydown", handleKeyDown);
2068
- return () => {
2069
- document.removeEventListener("pointerdown", handlePointerDown);
2070
- document.removeEventListener("keydown", handleKeyDown);
2071
- };
2072
- }, [open]);
2073
2031
  useEffect(() => {
2074
2032
  if (!open) return;
2075
2033
  const initialIso = selectedDate ? toIsoDate$1(selectedDate) : toIsoDate$1(/* @__PURE__ */ new Date());
@@ -2084,7 +2042,7 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
2084
2042
  setIsEditing(false);
2085
2043
  setInputText("");
2086
2044
  window.setTimeout(() => inputRef.current?.focus());
2087
- }, [onChange]);
2045
+ }, [onChange, setOpen]);
2088
2046
  const handleToday = useCallback(() => {
2089
2047
  const today = toIsoDate$1(/* @__PURE__ */ new Date());
2090
2048
  if (isIsoDateDisabled(today, min, max)) return;
@@ -2133,7 +2091,7 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
2133
2091
  const handleCalendarPointerDown = (e) => {
2134
2092
  e.preventDefault();
2135
2093
  if (readOnly || disabled) return;
2136
- setOpen((v) => !v);
2094
+ setOpen(!open);
2137
2095
  };
2138
2096
  const handleDayKeyDown = useCallback((e, iso) => {
2139
2097
  if (![
@@ -2169,7 +2127,8 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
2169
2127
  }, [
2170
2128
  days,
2171
2129
  month,
2172
- commitValue
2130
+ commitValue,
2131
+ panelRef
2173
2132
  ]);
2174
2133
  const handleMonthSelect = (monthIndex) => {
2175
2134
  setMonth(new Date(month.getFullYear(), monthIndex, 1));
@@ -2548,18 +2507,14 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2548
2507
  const endDisplay = formatDisplayDate(endValue, resolvedLocale);
2549
2508
  const startIso = parseIsoDate(startValue) ? startValue : null;
2550
2509
  const endIso = parseIsoDate(endValue) ? endValue : null;
2551
- const [open, setOpen] = useState(false);
2552
2510
  const [phase, setPhase] = useState("idle");
2553
2511
  const [pendingStart, setPendingStart] = useState(null);
2554
2512
  const [hoverIso, setHoverIso] = useState(null);
2555
2513
  const [month, setMonth] = useState(() => startOfMonth(parseIsoDate(startValue) ?? /* @__PURE__ */ new Date()));
2556
- const [panelStyle, setPanelStyle] = useState({});
2557
2514
  const [startText, setStartText] = useState("");
2558
2515
  const [endText, setEndText] = useState("");
2559
2516
  const [editingField, setEditingField] = useState(null);
2560
- const rootRef = useRef(null);
2561
2517
  const triggerRef = useRef(null);
2562
- const panelRef = useRef(null);
2563
2518
  const startInputRef = useRef(null);
2564
2519
  const endInputRef = useRef(null);
2565
2520
  const days = useMemo(() => buildCalendarDays(month, min, max, resolvedLocale), [
@@ -2574,54 +2529,31 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2574
2529
  const anchor = parseIsoDate(startValue) ?? parseIsoDate(endValue);
2575
2530
  if (anchor) setMonth(startOfMonth(anchor));
2576
2531
  }, [startValue]);
2577
- useEffect(() => {
2578
- if (!open) return;
2579
- const update = () => {
2532
+ const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
2533
+ onClose: () => {
2534
+ setPhase("idle");
2535
+ setPendingStart(null);
2536
+ setHoverIso(null);
2537
+ },
2538
+ onEscape: (e) => {
2539
+ e.preventDefault();
2540
+ startInputRef.current?.focus();
2541
+ },
2542
+ computeStyle: () => {
2580
2543
  const rect = triggerRef.current?.getBoundingClientRect();
2581
- if (!rect) return;
2544
+ if (!rect) return null;
2582
2545
  const panelWidth = panelRef.current?.offsetWidth ?? 296;
2583
2546
  const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
2584
2547
  const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
2585
- setPanelStyle({
2548
+ return {
2586
2549
  left,
2587
2550
  top: Math.max(8, top)
2588
- });
2589
- };
2590
- update();
2591
- window.addEventListener("resize", update);
2592
- window.addEventListener("scroll", update, true);
2593
- return () => {
2594
- window.removeEventListener("resize", update);
2595
- window.removeEventListener("scroll", update, true);
2596
- };
2597
- }, [open]);
2551
+ };
2552
+ }
2553
+ });
2598
2554
  const closePanel = useCallback(() => {
2599
2555
  setOpen(false);
2600
- setPhase("idle");
2601
- setPendingStart(null);
2602
- setHoverIso(null);
2603
- }, []);
2604
- useEffect(() => {
2605
- if (!open) return;
2606
- const handlePointerDown = (e) => {
2607
- const target = e.target;
2608
- if (rootRef.current?.contains(target) || panelRef.current?.contains(target)) return;
2609
- closePanel();
2610
- };
2611
- const handleKeyDown = (e) => {
2612
- if (e.key === "Escape") {
2613
- e.preventDefault();
2614
- closePanel();
2615
- startInputRef.current?.focus();
2616
- }
2617
- };
2618
- document.addEventListener("pointerdown", handlePointerDown);
2619
- document.addEventListener("keydown", handleKeyDown);
2620
- return () => {
2621
- document.removeEventListener("pointerdown", handlePointerDown);
2622
- document.removeEventListener("keydown", handleKeyDown);
2623
- };
2624
- }, [open, closePanel]);
2556
+ }, [setOpen]);
2625
2557
  const commitRange = useCallback((start, end) => {
2626
2558
  const [a, b] = start <= end ? [start, end] : [end, start];
2627
2559
  onChange?.(a, b);
@@ -2723,7 +2655,7 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2723
2655
  const handleCalendarPointerDown = (e) => {
2724
2656
  e.preventDefault();
2725
2657
  if (readOnly || disabled) return;
2726
- setOpen((v) => !v);
2658
+ setOpen(!open);
2727
2659
  };
2728
2660
  const handleClear = (e) => {
2729
2661
  e.preventDefault();
@@ -2921,41 +2853,6 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
2921
2853
  });
2922
2854
  }
2923
2855
  //#endregion
2924
- //#region packages/ui/useFloatingPanel.ts
2925
- function useFloatingPanel(options = {}) {
2926
- const [open, setOpenState] = useState(false);
2927
- const containerRef = useRef(null);
2928
- const onCloseRef = useRef(options.onClose);
2929
- onCloseRef.current = options.onClose;
2930
- const setOpen = useCallback((next) => {
2931
- setOpenState(next);
2932
- if (!next) onCloseRef.current?.();
2933
- }, []);
2934
- const toggle = useCallback(() => setOpen(!open), [open, setOpen]);
2935
- useEffect(() => {
2936
- if (!open) return;
2937
- const handlePointerDown = (e) => {
2938
- const container = containerRef.current;
2939
- if (container && !container.contains(e.target)) setOpen(false);
2940
- };
2941
- const handleKeyDown = (e) => {
2942
- if (e.key === "Escape") setOpen(false);
2943
- };
2944
- document.addEventListener("pointerdown", handlePointerDown);
2945
- document.addEventListener("keydown", handleKeyDown);
2946
- return () => {
2947
- document.removeEventListener("pointerdown", handlePointerDown);
2948
- document.removeEventListener("keydown", handleKeyDown);
2949
- };
2950
- }, [open, setOpen]);
2951
- return {
2952
- open,
2953
- setOpen,
2954
- toggle,
2955
- containerRef
2956
- };
2957
- }
2958
- //#endregion
2959
2856
  //#region packages/ui/Popover.tsx
2960
2857
  const Popover = ({ trigger, panel, align = "end", ariaLabel, className }) => {
2961
2858
  const { open, setOpen, toggle, containerRef } = useFloatingPanel();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nubitio/ui",
3
- "version": "0.6.10",
3
+ "version": "0.7.1",
4
4
  "type": "module",
5
5
  "description": "Visual primitives and theme system for the Nubit admin stack (dialogs, cards, toolbar, light/dark theme).",
6
6
  "license": "MIT",