@cjlapao/ui-kit 0.2.24 → 0.2.25

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
@@ -21563,6 +21563,17 @@ function StackCarousel({
21563
21563
  swipe = true,
21564
21564
  showNavigators = true,
21565
21565
  showStatus = true,
21566
+ loop = true,
21567
+ expandable = false,
21568
+ expanded,
21569
+ defaultExpanded = false,
21570
+ onExpandedChange,
21571
+ showExpandButton = false,
21572
+ expandOnHover = false,
21573
+ hoverExpandDelay = 2e3,
21574
+ hoverCollapseDelay = 500,
21575
+ expandLabel = "Expand stack",
21576
+ collapseLabel = "Collapse stack",
21566
21577
  ariaLabel = "Card stack",
21567
21578
  previousLabel = "Previous item",
21568
21579
  nextLabel = "Next item",
@@ -21575,12 +21586,51 @@ function StackCarousel({
21575
21586
  transparency,
21576
21587
  className = "",
21577
21588
  onKeyDown,
21589
+ onPointerEnter,
21590
+ onPointerMove,
21591
+ onPointerLeave,
21592
+ onWheelCapture,
21578
21593
  style,
21579
21594
  ...rest
21580
21595
  }) {
21581
21596
  const [internalIndex, setInternalIndex] = (0, import_react187.useState)(defaultActiveIndex);
21582
- const index = wrap(Number.isFinite(activeIndex ?? internalIndex) ? activeIndex ?? internalIndex : 0, items.length);
21583
- const layers = Math.min(items.length, Math.max(1, Math.min(3, Math.trunc(visibleCount) || 1)));
21597
+ const [internalExpanded, setInternalExpanded] = (0, import_react187.useState)(defaultExpanded);
21598
+ const isExpanded = expandable && (expanded ?? internalExpanded);
21599
+ const hoverTimer = (0, import_react187.useRef)(null);
21600
+ const collapseTimer = (0, import_react187.useRef)(null);
21601
+ const hoverOpened = (0, import_react187.useRef)(false);
21602
+ const setExpanded = (0, import_react187.useCallback)((value) => {
21603
+ if (expanded === void 0) setInternalExpanded(value);
21604
+ onExpandedChange?.(value);
21605
+ }, [expanded, onExpandedChange]);
21606
+ const clearHover = (0, import_react187.useCallback)(() => {
21607
+ if (hoverTimer.current !== null) clearTimeout(hoverTimer.current);
21608
+ hoverTimer.current = null;
21609
+ }, []);
21610
+ const clearCollapse = (0, import_react187.useCallback)(() => {
21611
+ if (collapseTimer.current !== null) clearTimeout(collapseTimer.current);
21612
+ collapseTimer.current = null;
21613
+ }, []);
21614
+ (0, import_react187.useEffect)(() => {
21615
+ clearHover();
21616
+ clearCollapse();
21617
+ return () => {
21618
+ clearHover();
21619
+ clearCollapse();
21620
+ };
21621
+ }, [expandable, expandOnHover, hoverExpandDelay, hoverCollapseDelay, isExpanded, clearHover, clearCollapse]);
21622
+ const armHover = (event) => {
21623
+ clearHover();
21624
+ clearCollapse();
21625
+ if (!expandable || !expandOnHover || isExpanded || event?.pointerType === "touch") return;
21626
+ hoverTimer.current = setTimeout(() => {
21627
+ hoverOpened.current = true;
21628
+ setExpanded(true);
21629
+ }, Math.max(0, Number.isFinite(hoverExpandDelay) ? hoverExpandDelay : 2e3));
21630
+ };
21631
+ const normalize = (0, import_react187.useCallback)((value) => loop ? wrap(value, items.length) : Math.max(0, Math.min(items.length - 1, Math.trunc(value))), [loop, items.length]);
21632
+ const index = normalize(Number.isFinite(activeIndex ?? internalIndex) ? activeIndex ?? internalIndex : 0);
21633
+ const layers = Math.min(loop ? items.length : items.length - index, Math.max(1, Math.min(3, Math.trunc(visibleCount) || 1)));
21584
21634
  const offset = Math.max(0, Number.isFinite(stackOffset) ? stackOffset : 12);
21585
21635
  const rootRef = (0, import_react187.useRef)(null);
21586
21636
  const measureRef = (0, import_react187.useRef)(null);
@@ -21591,16 +21641,20 @@ function StackCarousel({
21591
21641
  const wheelDelta = (0, import_react187.useRef)(0);
21592
21642
  const touch = (0, import_react187.useRef)(null);
21593
21643
  const label = (i) => getItemLabel?.(items[i], i) ?? `Item ${i + 1}`;
21644
+ (0, import_react187.useLayoutEffect)(() => {
21645
+ if (!isExpanded && document.activeElement instanceof Element && rootRef.current?.contains(document.activeElement) && document.activeElement.closest("[inert]"))
21646
+ rootRef.current.focus({ preventScroll: true });
21647
+ }, [isExpanded]);
21594
21648
  const navigate = (0, import_react187.useCallback)((next, focus = false) => {
21595
21649
  if (items.length < 2) return;
21596
- const normalized = wrap(next, items.length);
21650
+ const normalized = normalize(next);
21597
21651
  if (normalized === index) return;
21598
21652
  if (focus || measureRef.current?.contains(document.activeElement)) {
21599
21653
  rootRef.current?.focus({ preventScroll: true });
21600
21654
  }
21601
21655
  if (activeIndex === void 0) setInternalIndex(normalized);
21602
21656
  onActiveIndexChange?.(normalized);
21603
- }, [items.length, index, activeIndex, onActiveIndexChange]);
21657
+ }, [items.length, index, activeIndex, onActiveIndexChange, normalize]);
21604
21658
  (0, import_react187.useLayoutEffect)(() => {
21605
21659
  const old = previousIndex.current;
21606
21660
  previousIndex.current = index;
@@ -21621,7 +21675,7 @@ function StackCarousel({
21621
21675
  const observer = new ResizeObserver(measure);
21622
21676
  observer.observe(element);
21623
21677
  return () => observer.disconnect();
21624
- }, [index, items.length, items[index]]);
21678
+ }, [index, items.length, items[index], isExpanded]);
21625
21679
  const canScrollInside = (0, import_react187.useCallback)((target, delta) => {
21626
21680
  let element = target instanceof Element ? target : null;
21627
21681
  while (element && element !== rootRef.current) {
@@ -21633,7 +21687,7 @@ function StackCarousel({
21633
21687
  }, []);
21634
21688
  (0, import_react187.useEffect)(() => {
21635
21689
  const root = rootRef.current;
21636
- if (!root || !swipe || items.length < 2) return;
21690
+ if (!root || !swipe || isExpanded || items.length < 2) return;
21637
21691
  const handleMove = (event) => {
21638
21692
  const start = touch.current;
21639
21693
  if (!start || event.touches.length !== 1) return;
@@ -21643,10 +21697,10 @@ function StackCarousel({
21643
21697
  };
21644
21698
  root.addEventListener("touchmove", handleMove, { passive: false });
21645
21699
  return () => root.removeEventListener("touchmove", handleMove);
21646
- }, [swipe, items.length, canScrollInside]);
21700
+ }, [swipe, isExpanded, items.length, canScrollInside]);
21647
21701
  (0, import_react187.useEffect)(() => {
21648
21702
  const root = rootRef.current;
21649
- if (!root || !wheel || items.length < 2) return;
21703
+ if (!root || !wheel || isExpanded || items.length < 2) return;
21650
21704
  const handleWheel = (event) => {
21651
21705
  if (event.ctrlKey || event.metaKey || !event.deltaY || Math.abs(event.deltaX) > Math.abs(event.deltaY) || canScrollInside(event.target, event.deltaY)) return;
21652
21706
  event.preventDefault();
@@ -21662,11 +21716,11 @@ function StackCarousel({
21662
21716
  };
21663
21717
  root.addEventListener("wheel", handleWheel, { passive: false });
21664
21718
  return () => root.removeEventListener("wheel", handleWheel);
21665
- }, [wheel, items.length, index, navigate, canScrollInside]);
21666
- const visible = Array.from({ length: layers }, (_, rank) => wrap(index + rank, items.length));
21719
+ }, [wheel, isExpanded, items.length, index, navigate, canScrollInside]);
21720
+ const visible = isExpanded ? items.map((_, i) => i) : Array.from({ length: layers }, (_, rank) => normalize(index + rank));
21667
21721
  const rendered = [...visible];
21668
21722
  const transitionFrom = previousIndex.current !== index ? previousIndex.current : outgoing;
21669
- if (transitionFrom !== null && transitionFrom < items.length && !rendered.includes(transitionFrom)) rendered.push(transitionFrom);
21723
+ if (!isExpanded && transitionFrom !== null && transitionFrom < items.length && !rendered.includes(transitionFrom)) rendered.push(transitionFrom);
21670
21724
  return (
21671
21725
  // A focusable carousel region owns arrow navigation; child controls keep
21672
21726
  // their native key handling. It is a region rather than a single button.
@@ -21682,9 +21736,31 @@ function StackCarousel({
21682
21736
  tabIndex: 0,
21683
21737
  className: `min-w-0 outline-none focus-visible:ring-2 focus-visible:ring-blue-400 rounded-lg ${className}`,
21684
21738
  style,
21739
+ onPointerEnter: (event) => {
21740
+ onPointerEnter?.(event);
21741
+ if (!event.defaultPrevented) armHover(event);
21742
+ },
21743
+ onPointerMove: (event) => {
21744
+ onPointerMove?.(event);
21745
+ if (!event.defaultPrevented) armHover(event);
21746
+ },
21747
+ onWheelCapture: (event) => {
21748
+ onWheelCapture?.(event);
21749
+ if (event.deltaX || event.deltaY) armHover();
21750
+ },
21751
+ onPointerLeave: (event) => {
21752
+ onPointerLeave?.(event);
21753
+ clearHover();
21754
+ clearCollapse();
21755
+ if (hoverOpened.current) collapseTimer.current = setTimeout(() => {
21756
+ collapseTimer.current = null;
21757
+ hoverOpened.current = false;
21758
+ setExpanded(false);
21759
+ }, Math.max(0, Number.isFinite(hoverCollapseDelay) ? hoverCollapseDelay : 500));
21760
+ },
21685
21761
  onKeyDown: (event) => {
21686
21762
  onKeyDown?.(event);
21687
- if (event.defaultPrevented || event.target !== event.currentTarget) return;
21763
+ if (event.defaultPrevented || event.target !== event.currentTarget || isExpanded) return;
21688
21764
  const next = event.key === "ArrowDown" || event.key === "ArrowRight" ? index + 1 : event.key === "ArrowUp" || event.key === "ArrowLeft" ? index - 1 : event.key === "Home" ? 0 : event.key === "End" ? items.length - 1 : null;
21689
21765
  if (next !== null && items.length > 1) {
21690
21766
  event.preventDefault();
@@ -21697,9 +21773,10 @@ function StackCarousel({
21697
21773
  {
21698
21774
  className: "relative transition-[height] duration-300 ease-out motion-reduce:transition-none",
21699
21775
  "data-stack-carousel-track": true,
21700
- style: { height: height ? height + Math.max(0, layers - 1) * offset : void 0 },
21776
+ "data-expanded": isExpanded,
21777
+ style: { height: !isExpanded && height ? height + Math.max(0, layers - 1) * offset : void 0 },
21701
21778
  onTouchStart: (event) => {
21702
- if (!swipe || event.touches.length !== 1) {
21779
+ if (!swipe || isExpanded || event.touches.length !== 1) {
21703
21780
  touch.current = null;
21704
21781
  return;
21705
21782
  }
@@ -21719,7 +21796,7 @@ function StackCarousel({
21719
21796
  },
21720
21797
  children: rendered.map((i) => {
21721
21798
  const rank = visible.indexOf(i);
21722
- const active = rank === 0;
21799
+ const active = isExpanded || rank === 0;
21723
21800
  const leaving = rank < 0;
21724
21801
  return /* @__PURE__ */ (0, import_jsx_runtime194.jsxs)(
21725
21802
  "div",
@@ -21727,19 +21804,20 @@ function StackCarousel({
21727
21804
  "data-stack-rank": rank,
21728
21805
  className: `w-full origin-top transition-[transform,opacity,height] duration-300 ease-out motion-reduce:transition-none ${getSurfaceCornerClass(corner)}`,
21729
21806
  style: {
21730
- position: height || !active ? "absolute" : "relative",
21807
+ position: isExpanded ? "relative" : height || !active ? "absolute" : "relative",
21731
21808
  top: 0,
21732
- zIndex: leaving ? 4 : 3 - rank,
21809
+ marginTop: isExpanded && rank > 0 ? offset : void 0,
21810
+ zIndex: isExpanded ? 0 : leaving ? 4 : 3 - rank,
21733
21811
  opacity: leaving ? 0 : 1,
21734
21812
  pointerEvents: leaving ? "none" : void 0,
21735
- transform: leaving ? "translateY(-16px) scaleX(1.02)" : `translateY(${rank * offset}px) scaleX(${1 - rank * 0.04})`,
21813
+ transform: isExpanded ? "none" : leaving ? "translateY(-16px) scaleX(1.02)" : `translateY(${rank * offset}px) scaleX(${1 - rank * 0.04})`,
21736
21814
  height: !active && height ? height : void 0
21737
21815
  },
21738
21816
  children: [
21739
21817
  /* @__PURE__ */ (0, import_jsx_runtime194.jsx)(
21740
21818
  "div",
21741
21819
  {
21742
- ref: active ? measureRef : void 0,
21820
+ ref: i === index ? measureRef : void 0,
21743
21821
  inert: !active || void 0,
21744
21822
  "aria-hidden": !active || void 0,
21745
21823
  style: { height: !active && height ? height : void 0 },
@@ -21774,7 +21852,7 @@ function StackCarousel({
21774
21852
  })
21775
21853
  }
21776
21854
  ),
21777
- /* @__PURE__ */ (0, import_jsx_runtime194.jsxs)("div", { className: showStatus || showNavigators && items.length > 1 ? "mt-3 flex items-center justify-between gap-3 text-xs text-neutral-600 dark:text-neutral-400" : void 0, children: [
21855
+ /* @__PURE__ */ (0, import_jsx_runtime194.jsxs)("div", { className: showStatus || showNavigators && items.length > 1 || expandable && showExpandButton ? "mt-3 flex items-center justify-between gap-3 text-xs text-neutral-600 dark:text-neutral-400" : void 0, children: [
21778
21856
  /* @__PURE__ */ (0, import_jsx_runtime194.jsxs)("span", { className: showStatus ? void 0 : "sr-only", role: "status", "aria-live": "polite", "aria-atomic": "true", children: [
21779
21857
  label(index),
21780
21858
  " \xB7 ",
@@ -21782,27 +21860,37 @@ function StackCarousel({
21782
21860
  " / ",
21783
21861
  items.length
21784
21862
  ] }),
21785
- showNavigators && items.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime194.jsxs)("div", { className: "ml-auto flex items-center gap-1", children: [
21786
- /* @__PURE__ */ (0, import_jsx_runtime194.jsx)(
21787
- "button",
21788
- {
21789
- type: "button",
21790
- "aria-label": previousLabel,
21791
- onClick: () => navigate(index - 1),
21792
- className: "rounded-md px-2 py-1 hover:bg-black/5 focus-visible:outline-2 dark:hover:bg-white/10",
21793
- children: "\u2191"
21794
- }
21795
- ),
21796
- /* @__PURE__ */ (0, import_jsx_runtime194.jsx)(
21797
- "button",
21798
- {
21799
- type: "button",
21800
- "aria-label": nextLabel,
21801
- onClick: () => navigate(index + 1),
21802
- className: "rounded-md px-2 py-1 hover:bg-black/5 focus-visible:outline-2 dark:hover:bg-white/10",
21803
- children: "\u2193"
21804
- }
21805
- )
21863
+ (showNavigators && items.length > 1 || expandable && showExpandButton) && /* @__PURE__ */ (0, import_jsx_runtime194.jsxs)("div", { className: "ml-auto flex items-center gap-1", children: [
21864
+ expandable && showExpandButton && /* @__PURE__ */ (0, import_jsx_runtime194.jsx)("button", { type: "button", "aria-expanded": isExpanded, onClick: () => {
21865
+ hoverOpened.current = false;
21866
+ clearHover();
21867
+ clearCollapse();
21868
+ setExpanded(!isExpanded);
21869
+ }, className: "rounded-md px-2 py-1 hover:bg-black/5 dark:hover:bg-white/10", children: isExpanded ? collapseLabel : expandLabel }),
21870
+ showNavigators && !isExpanded && items.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime194.jsxs)(import_jsx_runtime194.Fragment, { children: [
21871
+ /* @__PURE__ */ (0, import_jsx_runtime194.jsx)(
21872
+ "button",
21873
+ {
21874
+ type: "button",
21875
+ "aria-label": previousLabel,
21876
+ disabled: !loop && index === 0,
21877
+ onClick: () => navigate(index - 1),
21878
+ className: "rounded-md px-2 py-1 hover:bg-black/5 focus-visible:outline-2 dark:hover:bg-white/10",
21879
+ children: "\u2191"
21880
+ }
21881
+ ),
21882
+ /* @__PURE__ */ (0, import_jsx_runtime194.jsx)(
21883
+ "button",
21884
+ {
21885
+ type: "button",
21886
+ "aria-label": nextLabel,
21887
+ disabled: !loop && index === items.length - 1,
21888
+ onClick: () => navigate(index + 1),
21889
+ className: "rounded-md px-2 py-1 hover:bg-black/5 focus-visible:outline-2 dark:hover:bg-white/10",
21890
+ children: "\u2193"
21891
+ }
21892
+ )
21893
+ ] })
21806
21894
  ] })
21807
21895
  ] })
21808
21896
  ] })