@42-central/ui-react 0.20.1 → 0.20.2

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.
Files changed (84) hide show
  1. package/dist/{chunk-MKKQE3TM.js → chunk-2BNIHOMT.js} +3 -3
  2. package/dist/{chunk-MKKQE3TM.js.map → chunk-2BNIHOMT.js.map} +1 -1
  3. package/dist/{chunk-LBAE6FP6.js → chunk-37VQGKD6.js} +4 -4
  4. package/dist/{chunk-LBAE6FP6.js.map → chunk-37VQGKD6.js.map} +1 -1
  5. package/dist/{chunk-SXNAUSEB.js → chunk-45TG7H5Q.js} +3 -3
  6. package/dist/{chunk-SXNAUSEB.js.map → chunk-45TG7H5Q.js.map} +1 -1
  7. package/dist/{chunk-NJPHZBLI.js → chunk-5CNVVA7I.js} +4 -4
  8. package/dist/{chunk-NJPHZBLI.js.map → chunk-5CNVVA7I.js.map} +1 -1
  9. package/dist/{chunk-BNWQ7IAZ.js → chunk-C74TV64U.js} +5 -5
  10. package/dist/{chunk-BNWQ7IAZ.js.map → chunk-C74TV64U.js.map} +1 -1
  11. package/dist/{chunk-RCF7HTG5.js → chunk-FDCPMUJK.js} +4 -4
  12. package/dist/{chunk-RCF7HTG5.js.map → chunk-FDCPMUJK.js.map} +1 -1
  13. package/dist/{chunk-E63VD25L.js → chunk-HQPU73WV.js} +4 -4
  14. package/dist/{chunk-E63VD25L.js.map → chunk-HQPU73WV.js.map} +1 -1
  15. package/dist/{chunk-POKLPMSB.js → chunk-LFQDB5OD.js} +3 -3
  16. package/dist/{chunk-POKLPMSB.js.map → chunk-LFQDB5OD.js.map} +1 -1
  17. package/dist/{chunk-5HUEBSIS.js → chunk-N66NC6QK.js} +120 -24
  18. package/dist/chunk-N66NC6QK.js.map +1 -0
  19. package/dist/{chunk-IOYZC7SA.js → chunk-UCCJUFOG.js} +3 -3
  20. package/dist/{chunk-IOYZC7SA.js.map → chunk-UCCJUFOG.js.map} +1 -1
  21. package/dist/{chunk-KMZENCEP.js → chunk-UVQXRDRO.js} +3 -3
  22. package/dist/{chunk-KMZENCEP.js.map → chunk-UVQXRDRO.js.map} +1 -1
  23. package/dist/chunk-V6PY7C6S.js +18 -0
  24. package/dist/chunk-V6PY7C6S.js.map +1 -0
  25. package/dist/{chunk-TWLJQE75.js → chunk-WXL5RP6P.js} +3 -3
  26. package/dist/{chunk-TWLJQE75.js.map → chunk-WXL5RP6P.js.map} +1 -1
  27. package/dist/{chunk-VTDBWU6H.js → chunk-YZJUPYPO.js} +3 -3
  28. package/dist/{chunk-VTDBWU6H.js.map → chunk-YZJUPYPO.js.map} +1 -1
  29. package/dist/{chunk-3BXKTWWD.js → chunk-Z4MBCKG5.js} +4 -4
  30. package/dist/{chunk-3BXKTWWD.js.map → chunk-Z4MBCKG5.js.map} +1 -1
  31. package/dist/components/autocomplete/index.js +3 -2
  32. package/dist/components/autocomplete/index.js.map +1 -1
  33. package/dist/components/camera/index.js +5 -4
  34. package/dist/components/code-block/chrome.js +4 -4
  35. package/dist/components/code-block/index.js +5 -5
  36. package/dist/components/combobox/index.js +3 -2
  37. package/dist/components/combobox-list/index.js +5 -4
  38. package/dist/components/data-table/index.js +12 -12
  39. package/dist/components/date-picker/index.js +6 -5
  40. package/dist/components/date-picker/index.js.map +1 -1
  41. package/dist/components/hover-card/hover-card.d.ts +44 -26
  42. package/dist/components/hover-card/hover-card.d.ts.map +1 -1
  43. package/dist/components/hover-card/index.js +35 -53
  44. package/dist/components/hover-card/index.js.map +1 -1
  45. package/dist/components/menu/index.js +3 -3
  46. package/dist/components/multi-combobox-list/index.js +5 -4
  47. package/dist/components/multi-select/index.js +3 -2
  48. package/dist/components/multi-select/index.js.map +1 -1
  49. package/dist/components/number-input/index.js +3 -2
  50. package/dist/components/photo-capture/index.js +5 -4
  51. package/dist/components/photo-capture/index.js.map +1 -1
  52. package/dist/components/popover/index.d.ts +1 -1
  53. package/dist/components/popover/index.d.ts.map +1 -1
  54. package/dist/components/popover/index.js +2 -1
  55. package/dist/components/popover/popover.d.ts +44 -29
  56. package/dist/components/popover/popover.d.ts.map +1 -1
  57. package/dist/components/schedule/index.js +2 -2
  58. package/dist/components/select/index.js +4 -3
  59. package/dist/components/spotlight/index.js +3 -2
  60. package/dist/components/spotlight/index.js.map +1 -1
  61. package/dist/components/tags-input/index.js +2 -1
  62. package/dist/components/tags-input/index.js.map +1 -1
  63. package/dist/components/time-input/index.js +4 -3
  64. package/dist/components/time-input/index.js.map +1 -1
  65. package/dist/components/tooltip/index.js +2 -2
  66. package/dist/components/tree-multi-select/index.js +3 -2
  67. package/dist/components/tree-multi-select/index.js.map +1 -1
  68. package/dist/components/tree-select/index.js +3 -2
  69. package/dist/components/tree-select/index.js.map +1 -1
  70. package/dist/lib/placement.d.ts +42 -1
  71. package/dist/lib/placement.d.ts.map +1 -1
  72. package/dist/lib/slide-on-switch.d.ts +69 -0
  73. package/dist/lib/slide-on-switch.d.ts.map +1 -0
  74. package/package.json +1 -1
  75. package/src/components/hover-card/hover-card.tsx +89 -167
  76. package/src/components/popover/index.ts +2 -0
  77. package/src/components/popover/popover.test.tsx +131 -0
  78. package/src/components/popover/popover.tsx +191 -53
  79. package/src/lib/placement.test.ts +37 -0
  80. package/src/lib/placement.ts +60 -1
  81. package/src/lib/slide-on-switch.ts +99 -0
  82. package/dist/chunk-5HUEBSIS.js.map +0 -1
  83. package/dist/chunk-G52U24GR.js +0 -10
  84. package/dist/chunk-G52U24GR.js.map +0 -1
@@ -1,5 +1,6 @@
1
1
  "use client"
2
2
 
3
+ import { useLocaleContext } from "@ark-ui/react/locale"
3
4
  import { Popover as ArkPopover, usePopoverContext } from "@ark-ui/react/popover"
4
5
  import { Portal } from "@ark-ui/react/portal"
5
6
  import { cva } from "class-variance-authority"
@@ -8,6 +9,7 @@ import {
8
9
  createContext,
9
10
  type MouseEvent,
10
11
  type PointerEvent,
12
+ type RefObject,
11
13
  useContext,
12
14
  useEffect,
13
15
  useRef,
@@ -23,7 +25,19 @@ import {
23
25
  useLayerGuard,
24
26
  useNestedLayer,
25
27
  } from "../../lib/overlay-layer"
28
+ import {
29
+ type LogicalPosition,
30
+ type Placement,
31
+ type PlacementProps,
32
+ toPositioning,
33
+ } from "../../lib/placement"
26
34
  import { props } from "../../lib/props"
35
+ import {
36
+ activeGroupContent,
37
+ slideStyle,
38
+ splitGroupChildren,
39
+ useSlideAfterPlaced,
40
+ } from "../../lib/slide-on-switch"
27
41
  import type { WithTestId } from "../../lib/test-id"
28
42
  import { TEXT_COLOR_CLASSES } from "../../lib/typography-size"
29
43
  import { useUncontrolled } from "../../lib/use-uncontrolled"
@@ -191,7 +205,9 @@ export const popoverEmpty = cn(
191
205
  * touch and never takes focus).
192
206
  */
193
207
 
194
- type PopoverPosition = NonNullable<NonNullable<ArkPopover.RootProps["positioning"]>["placement"]>
208
+ /** `position` accepts Ark's physical placements plus the logical, RTL-aware
209
+ * horizontal sides `start` / `end` (resolved to `left` / `right` by direction). */
210
+ export type PopoverPosition = LogicalPosition<Placement>
195
211
 
196
212
  /** Elements a pinned panel can move focus to — the first one wins, else the
197
213
  * panel itself (it carries `tabIndex={-1}`). Mirrors what zag's own
@@ -215,12 +231,14 @@ type HoverSource = "hover" | "press"
215
231
  type PopoverHover = {
216
232
  /** Open and opened by a press (or upgraded from a preview). */
217
233
  pinned: boolean
218
- /** Pointer entering the trigger or the panel. */
219
- onPointerEnter: (event: PointerEvent) => void
234
+ /** Pointer entering the trigger or the panel. `value` is the entered
235
+ * trigger's, inside `Popover.Group`; the panel passes none. */
236
+ onPointerEnter: (event: PointerEvent, value?: string) => void
220
237
  /** Pointer leaving the trigger or the panel. */
221
238
  onPointerLeave: (event: PointerEvent) => void
222
239
  /** A trigger click about to reach Ark — flags a click on an open preview,
223
- * so the close Ark answers it with becomes a pin instead. */
240
+ * so whatever Ark answers it with (a close, or a switch to another
241
+ * trigger) becomes a pin instead. */
224
242
  onTriggerClickCapture: (event: MouseEvent<HTMLElement>) => void
225
243
  /** A press or focus inside the panel. */
226
244
  pin: () => void
@@ -234,11 +252,15 @@ type PopoverContextValue = {
234
252
  layers: OverlayLayers
235
253
  /** Set only in `mode="hover"`. */
236
254
  hover?: PopoverHover
255
+ /** `Popover.Group`'s shared panel, past its open's first placement —
256
+ * gates the slide between triggers (see `useSlideAfterPlaced`). */
257
+ sliding: boolean
237
258
  }
238
259
  const PopoverContext = createContext<PopoverContextValue>({
239
260
  width: "target",
240
261
  withinPortal: true,
241
262
  layers: new Set(),
263
+ sliding: false,
242
264
  })
243
265
 
244
266
  type PopoverHoverDelays = {
@@ -260,37 +282,11 @@ type PopoverModeProps =
260
282
  } & UnavailableProps<PopoverHoverDelays, 'requires mode="hover"'>)
261
283
  | ({ mode: "hover" } & PopoverHoverDelays)
262
284
 
263
- export type PopoverProps = ArkPopover.RootProps &
264
- PopoverModeProps & {
285
+ export type PopoverProps = Omit<ArkPopover.RootProps, "positioning"> &
286
+ PopoverModeProps &
287
+ PlacementProps & {
265
288
  /** Match the trigger's width (`"target"`, default) or size to content (`"auto"`). */
266
289
  width?: "target" | "auto"
267
- /** Panel placement relative to the trigger. Defaults to Ark's `"bottom"`. */
268
- position?: PopoverPosition
269
- /** Gap in px between the trigger and the panel. */
270
- gutter?: number
271
- /** Fine placement, in px: `mainAxis` is the gap from the trigger (it replaces `gutter` when
272
- * set), `crossAxis` slides the panel along the trigger's edge. Floating UI's `offset()`. */
273
- offset?:
274
- | {
275
- mainAxis?: number | undefined
276
- crossAxis?: number | undefined
277
- }
278
- | undefined
279
- /** Hide the panel (`visibility: hidden`, no pointer events) once the
280
- * trigger is fully clipped by its own scrolling ancestor, and restore it
281
- * once back in view — Floating UI's `hide()` middleware
282
- * (`strategy: "referenceHidden"`), already wired into Ark's positioning
283
- * engine, just not exposed until now. Rides the positioner's existing
284
- * scroll/resize listeners, so no extra observer is needed. Default `true`
285
- * — a panel that can't see its own trigger is never in a state worth
286
- * keeping visible; pass `false` to restore the old "keep following the
287
- * trigger's last known position" behavior. */
288
- hideWhenDetached?: boolean
289
- /** Ark's full positioning options (`flip`, `slide`, `overlap`, `boundary`,
290
- * `getAnchorElement`, …), for placements the props above can't express.
291
- * Applied over them, so a key set here wins. */
292
- positioning?: ArkPopover.RootProps["positioning"]
293
-
294
290
  /** Render the panel in a Portal so it escapes clipping ancestors. Default `true`. */
295
291
  withinPortal?: boolean
296
292
  /** Portal target. Defaults to the enclosing `Modal`/`Drawer` when there is
@@ -314,7 +310,31 @@ const focusInto = (trigger: HTMLElement, initialFocusEl?: () => HTMLElement | nu
314
310
  target.focus({ preventScroll: true })
315
311
  })
316
312
 
313
+ /** Ark's live Popover API, for the hover path to send zag events through. */
314
+ type PopoverApi = ReturnType<typeof usePopoverContext>
315
+
316
+ /** `Popover.Group`'s link into the Ark root, rendered inside it: keeps `api`
317
+ * pointing at the machine's current API for the hover hook (which runs
318
+ * above the root), and re-anchors the panel whenever the active trigger
319
+ * changes. zag's trigger switch does reposition, but in the same action,
320
+ * against the render it was sent from — still the old trigger — so on its
321
+ * own the panel would swap content in place. (HoverCard's machine gets
322
+ * away with it because its switch re-enters the open state.) */
323
+ const GroupBridge = ({ api }: { api: RefObject<PopoverApi | null> }) => {
324
+ const live = usePopoverContext()
325
+ useEffect(() => {
326
+ api.current = live
327
+ })
328
+ // biome-ignore lint/correctness/useExhaustiveDependencies: keyed on the active trigger only; `live` is a new object every render.
329
+ useEffect(() => {
330
+ if (live.open) live.reposition()
331
+ }, [live.triggerValue])
332
+ return null
333
+ }
334
+
317
335
  type UsePopoverHoverOptions = {
336
+ /** The machine's API (see `GroupBridge`); set once the Ark root mounts. */
337
+ api: RefObject<PopoverApi | null>
318
338
  open?: boolean
319
339
  defaultOpen?: boolean
320
340
  openDelay: number
@@ -336,8 +356,21 @@ type UsePopoverHoverOptions = {
336
356
  * `source` is deliberately not reset on close: zag reads `restoreFocus` when
337
357
  * the close lands, so a preview closing must still read as `"hover"` there,
338
358
  * or it would yank focus to a trigger the user never focused.
359
+ *
360
+ * Inside `Popover.Group`, hovering a trigger also makes it the active one,
361
+ * through zag's own `setTriggerValue` — the one path that re-anchors the
362
+ * panel too (zag repositions in the same action, reading the value it just
363
+ * set; a controlled `triggerValue` prop would still hold the old one there).
364
+ * So zag owns the value, and the hook never mirrors it. Over an open preview
365
+ * that switch is immediate (the panel slides, no delay replays); a pinned
366
+ * panel ignores hover and stays on its trigger. A click on an open preview
367
+ * pins it whichever answer zag gives: a close for the active trigger, a
368
+ * switch for another one. Which comes is zag's call, so the hook never
369
+ * predicts it — by the time a click lands, a hover may have moved the
370
+ * trigger already.
339
371
  */
340
372
  const usePopoverHover = ({
373
+ api,
341
374
  open,
342
375
  defaultOpen,
343
376
  openDelay,
@@ -355,7 +388,7 @@ const usePopoverHover = ({
355
388
  const [source, setSource] = useState<HoverSource>("press")
356
389
  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
357
390
  // The trigger of a click that landed on an open preview, between its
358
- // capture phase and the close Ark answers it with.
391
+ // capture phase and the close or switch Ark answers it with.
359
392
  const upgrading = useRef<HTMLElement | null>(null)
360
393
  const clear = () => {
361
394
  clearTimeout(timer.current)
@@ -374,8 +407,10 @@ const usePopoverHover = ({
374
407
  const pinned = isOpen && source === "press"
375
408
  const hover: PopoverHover = {
376
409
  pinned,
377
- onPointerEnter: (event) => {
410
+ onPointerEnter: (event, value) => {
378
411
  if (event.pointerType === "touch" || pinned) return
412
+ if (value !== undefined && api.current && api.current.triggerValue !== value)
413
+ api.current.setTriggerValue(value)
379
414
  if (isOpen) return clear()
380
415
  setSource("hover")
381
416
  later(true, openDelay)
@@ -407,10 +442,27 @@ const usePopoverHover = ({
407
442
  setOpen(next)
408
443
  }
409
444
 
410
- return { open: isOpen, press: source === "press", hover, onArkOpenChange }
445
+ return {
446
+ open: isOpen,
447
+ press: source === "press",
448
+ hover,
449
+ onArkOpenChange,
450
+ /** zag switched triggers — pins it if a click on a preview caused it. */
451
+ onArkTriggerValueChange: () => {
452
+ const trigger = upgrading.current
453
+ upgrading.current = null
454
+ if (!trigger) return
455
+ clear()
456
+ setSource("press")
457
+ if (autoFocus) focusInto(trigger, initialFocusEl)
458
+ },
459
+ }
411
460
  }
412
461
 
413
- const Root = ({
462
+ /** `Root` and `Group` — one machine; `group` swaps plain children for
463
+ * `Trigger`s plus the active trigger's `Content`. */
464
+ const Machine = ({
465
+ group,
414
466
  mode = "click",
415
467
  openDelay,
416
468
  closeDelay,
@@ -418,13 +470,16 @@ const Root = ({
418
470
  position,
419
471
  gutter,
420
472
  offset,
421
- hideWhenDetached = true,
473
+ hideWhenDetached,
422
474
  positioning,
423
475
  withinPortal = true,
424
476
  container,
425
477
  children,
426
478
  open,
427
479
  defaultOpen,
480
+ triggerValue,
481
+ defaultTriggerValue,
482
+ onTriggerValueChange,
428
483
  autoFocus = true,
429
484
  restoreFocus = true,
430
485
  modal = false,
@@ -432,13 +487,18 @@ const Root = ({
432
487
  onClose,
433
488
  onPointerDownOutside,
434
489
  ...rest
435
- }: PopoverProps) => {
490
+ }: PopoverProps & { group: boolean }) => {
491
+ const { dir } = useLocaleContext()
436
492
  const guard = useLayerGuard(onPointerDownOutside)
493
+ const slide = useSlideAfterPlaced()
494
+ const api = useRef<PopoverApi | null>(null)
437
495
  const notify = (details: { open: boolean }) => {
438
496
  onOpenChange?.(details)
497
+ if (group) slide.onOpenChange(details.open)
439
498
  if (!details.open) onClose?.()
440
499
  }
441
500
  const hovering = usePopoverHover({
501
+ api,
442
502
  open,
443
503
  defaultOpen,
444
504
  openDelay: delay(openDelay, 600),
@@ -455,32 +515,102 @@ const Root = ({
455
515
  const state = hover
456
516
  ? { open: hovering.open, onOpenChange: hovering.onArkOpenChange }
457
517
  : { open, defaultOpen, onOpenChange: notify }
518
+ const parts = group ? splitGroupChildren(children, Content) : undefined
458
519
  return (
459
520
  <PopoverContext.Provider
460
- value={{ width, withinPortal, container, layers: guard.layers, hover }}
521
+ value={{
522
+ width,
523
+ withinPortal,
524
+ container,
525
+ layers: guard.layers,
526
+ hover,
527
+ sliding: group && slide.sliding,
528
+ }}
461
529
  >
462
530
  <ArkPopover.Root
531
+ // A group's panel mounts on each open, like HoverCard's, so its first
532
+ // frame never inherits a stale position to slide from.
533
+ lazyMount={group}
534
+ unmountOnExit={group}
463
535
  onPointerDownOutside={guard.onPointerDownOutside}
464
- positioning={{
465
- sameWidth: width === "target",
466
- placement: position,
467
- gutter,
468
- offset,
469
- hideWhenDetached,
470
- ...positioning,
471
- }}
536
+ positioning={toPositioning(
537
+ {
538
+ position,
539
+ gutter,
540
+ offset,
541
+ hideWhenDetached,
542
+ positioning: group
543
+ ? {
544
+ ...positioning,
545
+ updatePosition: slide.updatePosition(positioning?.updatePosition),
546
+ }
547
+ : positioning,
548
+ },
549
+ dir,
550
+ width,
551
+ )}
472
552
  autoFocus={press && autoFocus}
473
553
  restoreFocus={press && restoreFocus}
474
554
  modal={press && modal}
555
+ triggerValue={triggerValue}
556
+ defaultTriggerValue={defaultTriggerValue}
557
+ onTriggerValueChange={(details) => {
558
+ onTriggerValueChange?.(details)
559
+ hovering.onArkTriggerValueChange()
560
+ }}
475
561
  {...state}
476
562
  {...rest}
477
563
  >
478
- {children}
564
+ {parts ? (
565
+ <>
566
+ <GroupBridge api={api} />
567
+ {parts.triggers}
568
+ <ArkPopover.Context>
569
+ {(api) => activeGroupContent(parts.contents, api.triggerValue)}
570
+ </ArkPopover.Context>
571
+ </>
572
+ ) : (
573
+ children
574
+ )}
479
575
  </ArkPopover.Root>
480
576
  </PopoverContext.Provider>
481
577
  )
482
578
  }
483
579
 
580
+ const Root = (props: PopoverProps) => <Machine group={false} {...props} />
581
+
582
+ export type PopoverGroupProps = PopoverProps
583
+
584
+ /**
585
+ * One shared panel across several triggers (e.g. a row of avatars) — the
586
+ * same `Popover.Trigger` / `Popover.Content` parts `Popover.Root` uses, just
587
+ * several of each, matched by a shared `value`. The Popover counterpart of
588
+ * `HoverCard.Group`, with the same contract, for cards people act on:
589
+ *
590
+ * <Popover.Group mode="hover" width="auto">
591
+ * <Popover.Trigger value="ada"><Avatar name="Ada Lovelace" /></Popover.Trigger>
592
+ * <Popover.Trigger value="grace"><Avatar name="Grace Hopper" /></Popover.Trigger>
593
+ *
594
+ * <Popover.Content value="ada">Ada's card</Popover.Content>
595
+ * <Popover.Content value="grace">Grace's card</Popover.Content>
596
+ * </Popover.Group>
597
+ *
598
+ * zag's Popover supports several named triggers on one machine: clicking
599
+ * another trigger while the panel is open moves it there instead of closing
600
+ * it, and only the active trigger gets `aria-expanded`. In `mode="hover"`,
601
+ * hovering another trigger while the preview is open moves it too, with no
602
+ * delay replay; a click, tap or Enter on any trigger, or a press or focus in
603
+ * the panel, pins it exactly like a single `mode="hover"` Popover. Either
604
+ * way the panel slides between triggers (`slideStyle`), never on the first
605
+ * open.
606
+ *
607
+ * `triggerValue` / `defaultTriggerValue` / `onTriggerValueChange` read and
608
+ * drive the active trigger. `Trigger`/`Content` must be `Group`'s own direct
609
+ * children (it scans one level, like `HoverCard.Group`); `Group` renders no
610
+ * DOM node, so wrap it from the outside for row layout (`Avatar.Group`).
611
+ */
612
+ const Group = (props: PopoverGroupProps) => <Machine group {...props} />
613
+
484
614
  export type PopoverTriggerProps = ArkPopover.TriggerProps & WithTestId
485
615
 
486
616
  /** Ark's trigger, plus the hover handlers in `mode="hover"`. Ark merges them
@@ -497,7 +627,7 @@ const Trigger = ({
497
627
  <ArkPopover.Trigger
498
628
  onPointerEnter={(event) => {
499
629
  onPointerEnter?.(event)
500
- hover?.onPointerEnter(event)
630
+ hover?.onPointerEnter(event, rest.value)
501
631
  }}
502
632
  onPointerLeave={(event) => {
503
633
  onPointerLeave?.(event)
@@ -513,7 +643,13 @@ const Trigger = ({
513
643
  )
514
644
  }
515
645
 
516
- export type PopoverContentProps = NoAsChild<ArkPopover.ContentProps> & WithTestId
646
+ export type PopoverContentProps = NoAsChild<ArkPopover.ContentProps> &
647
+ WithTestId & {
648
+ /** Which `Popover.Group` trigger this content belongs to — only
649
+ * meaningful inside `Popover.Group`, where `Group` renders the one whose
650
+ * `value` matches the active trigger. Never forwarded to the DOM. */
651
+ value?: string
652
+ }
517
653
 
518
654
  const Content = ({
519
655
  className,
@@ -524,15 +660,16 @@ const Content = ({
524
660
  onPointerDown,
525
661
  onFocus,
526
662
  asChild: _asChild,
663
+ value: _value,
527
664
  ...rest
528
665
  }: PopoverContentProps) => {
529
- const { width, withinPortal, container, layers, hover } = useContext(PopoverContext)
666
+ const { width, withinPortal, container, layers, hover, sliding } = useContext(PopoverContext)
530
667
  const portalContainer = useOverlayContainer(container)
531
668
  // A child of the enclosing Modal/Drawer/Popover (read above this popover's
532
669
  // own provider), and the parent of whatever opens from its body.
533
670
  useNestedLayer(usePopoverContext().open)
534
671
  const panel = (
535
- <ArkPopover.Positioner>
672
+ <ArkPopover.Positioner style={sliding ? slideStyle : undefined}>
536
673
  <ArkPopover.Content
537
674
  tabIndex={-1}
538
675
  // target → fill the sameWidth positioner; auto → size to content (max-content).
@@ -605,6 +742,7 @@ export type PopoverAnchorProps = ArkPopover.AnchorProps
605
742
 
606
743
  export const Popover = {
607
744
  Root,
745
+ Group,
608
746
  Trigger,
609
747
  /** Positions the panel without attaching any click/keyboard trigger
610
748
  * semantics — for a host that already owns its own click resolution
@@ -0,0 +1,37 @@
1
+ import { describe, expect, it } from "vitest"
2
+ import { toPositioning } from "./placement"
3
+
4
+ describe("toPositioning", () => {
5
+ it("defaults to bottom, hidden when detached, sized to content", () => {
6
+ expect(toPositioning({}, "ltr", "auto")).toEqual({
7
+ sameWidth: false,
8
+ placement: "bottom",
9
+ gutter: undefined,
10
+ offset: undefined,
11
+ hideWhenDetached: true,
12
+ })
13
+ })
14
+
15
+ it("resolves the logical sides for the direction", () => {
16
+ expect(toPositioning({ position: "start" }, "ltr", "auto").placement).toBe("left")
17
+ expect(toPositioning({ position: "start" }, "rtl", "auto").placement).toBe("right")
18
+ expect(toPositioning({ position: "end" }, "ltr", "auto").placement).toBe("right")
19
+ expect(toPositioning({ position: "end" }, "rtl", "auto").placement).toBe("left")
20
+ expect(toPositioning({ position: "top-start" }, "rtl", "auto").placement).toBe("top-start")
21
+ })
22
+
23
+ it("matches the trigger's width for width=target", () => {
24
+ expect(toPositioning({}, "ltr", "target").sameWidth).toBe(true)
25
+ })
26
+
27
+ it("lets the positioning escape hatch win over the props", () => {
28
+ const built = toPositioning(
29
+ { position: "top", hideWhenDetached: true, positioning: { placement: "left", flip: false } },
30
+ "ltr",
31
+ "auto",
32
+ )
33
+ expect(built.placement).toBe("left")
34
+ expect(built.flip).toBe(false)
35
+ expect(built.hideWhenDetached).toBe(true)
36
+ })
37
+ })
@@ -1,5 +1,7 @@
1
+ import type { Popover as ArkPopover } from "@ark-ui/react/popover"
2
+
1
3
  /**
2
- * Shared by Menu/Tooltip (and any future floating-panel component with the
4
+ * Shared by Menu/Tooltip/HoverCard/Popover (and any future floating-panel component with the
3
5
  * same contract): `position` accepts a component's own physical placement
4
6
  * union `P` (Ark/Floating UI's vocabulary) plus the logical, RTL-aware
5
7
  * horizontal sides `start` / `end`, resolved to `left` / `right` by direction.
@@ -21,3 +23,60 @@ export function resolveLogicalPlacement<P extends string>(
21
23
  if (position === "end") return (dir === "rtl" ? "left" : "right") as P
22
24
  return position as P
23
25
  }
26
+
27
+ /** Ark's positioning options — the same zag `PositioningOptions` for every
28
+ * floating machine (Popover, HoverCard, Menu, Tooltip). */
29
+ export type Positioning = NonNullable<ArkPopover.RootProps["positioning"]>
30
+
31
+ /** Physical placements Ark understands — `bottom`, `top-start`, … */
32
+ export type Placement = NonNullable<Positioning["placement"]>
33
+
34
+ /**
35
+ * The placement props Popover and HoverCard share, so a consumer can swap
36
+ * one for the other (or `HoverCard.Group` for `Popover.Group`) without
37
+ * rewriting where the panel goes. Built into Ark's `positioning` by
38
+ * `toPositioning`.
39
+ */
40
+ export type PlacementProps = {
41
+ /** Panel placement relative to the trigger — Ark's physical placements,
42
+ * plus the RTL-aware `start` / `end` sides. Defaults to `"bottom"`. */
43
+ position?: LogicalPosition<Placement>
44
+ /** Gap in px between the trigger and the panel. */
45
+ gutter?: number
46
+ /** Fine placement, in px: `mainAxis` is the gap from the trigger (it replaces `gutter` when
47
+ * set), `crossAxis` slides the panel along the trigger's edge. Floating UI's `offset()`. */
48
+ offset?:
49
+ | {
50
+ mainAxis?: number | undefined
51
+ crossAxis?: number | undefined
52
+ }
53
+ | undefined
54
+ /** Hide the panel (`visibility: hidden`, no pointer events) once the
55
+ * trigger is fully clipped by its own scrolling ancestor, and restore it
56
+ * once back in view — Floating UI's `hide()` middleware
57
+ * (`strategy: "referenceHidden"`), already wired into Ark's positioning
58
+ * engine. Rides the positioner's existing scroll/resize listeners, so no
59
+ * extra observer is needed. Default `true` — a panel that can't see its
60
+ * own trigger is never in a state worth keeping visible; pass `false` to
61
+ * keep following the trigger's last known position. */
62
+ hideWhenDetached?: boolean
63
+ /** Ark's full positioning options (`flip`, `slide`, `overlap`, `boundary`,
64
+ * `getAnchorElement`, …), for placements the props above can't express.
65
+ * Applied over them, so a key set here wins. */
66
+ positioning?: Positioning
67
+ }
68
+
69
+ /** Build Ark's `positioning` from `PlacementProps` — `start` / `end`
70
+ * resolved for `dir`, `sameWidth` from the panel's `width`. */
71
+ export const toPositioning = (
72
+ { position = "bottom", gutter, offset, hideWhenDetached = true, positioning }: PlacementProps,
73
+ dir: "ltr" | "rtl",
74
+ width: "target" | "auto",
75
+ ): Positioning => ({
76
+ sameWidth: width === "target",
77
+ placement: resolveLogicalPlacement(position, dir),
78
+ gutter,
79
+ offset,
80
+ hideWhenDetached,
81
+ ...positioning,
82
+ })
@@ -0,0 +1,99 @@
1
+ import {
2
+ Children,
3
+ cloneElement,
4
+ type ElementType,
5
+ isValidElement,
6
+ type ReactElement,
7
+ type ReactNode,
8
+ useRef,
9
+ useState,
10
+ } from "react"
11
+ import type { Positioning } from "./placement"
12
+
13
+ /**
14
+ * Shared by `HoverCard.Group` and `Popover.Group`: several named triggers on
15
+ * one Ark machine, one panel that slides from the active trigger to the next.
16
+ */
17
+
18
+ /** Slide transition for a group's shared positioner. Ark sets the
19
+ * positioner's `transform: translate3d(var(--x), var(--y), 0)` inline
20
+ * (`@zag-js/popper`); animating `transform` itself makes the browser
21
+ * interpolate that translate between two trigger anchors when the group's
22
+ * active trigger changes. Passed as `style` (not a class) so it merges with,
23
+ * rather than replaces, Ark's own inline `transform`/`position` (Ark's
24
+ * `mergeProps` shallow-merges style objects); the same override-the-real-style
25
+ * technique `Tabs.Indicator` / `SegmentGroup` use for their sliding pill.
26
+ *
27
+ * Only applied once `useSlideAfterPlaced` reports `sliding`, never on an
28
+ * open's first placement: before it, the positioner sits at its unplaced
29
+ * origin (far off the trigger), and animating *that* jump reads as the panel
30
+ * flying in across the page instead of just appearing. A standalone `Root`
31
+ * never gets this at all: it also repositions on scroll/resize
32
+ * (`autoUpdate`), where an animated `transform` would look laggy rather than
33
+ * responsive. */
34
+ export const slideStyle = {
35
+ transitionProperty: "transform",
36
+ transitionDuration: "200ms",
37
+ transitionTimingFunction: "cubic-bezier(0.32, 0.72, 0, 1)",
38
+ } as const
39
+
40
+ type UpdatePosition = NonNullable<Positioning["updatePosition"]>
41
+
42
+ /**
43
+ * `sliding` turns true once an open's first placement has been applied, and
44
+ * false on every close. Waiting for the placement itself, not a frame count:
45
+ * the first one after an open can take several frames (the panel mounts,
46
+ * zag defers to the next frame, floating-ui computes asynchronously), and a
47
+ * transition switched on before it lands slides the panel in from its
48
+ * unplaced origin.
49
+ *
50
+ * Feed every open change to `onOpenChange`, and pass `updatePosition(...)`
51
+ * as the machine's `positioning.updatePosition` — zag calls it for every
52
+ * placement, with its own `updatePosition` to run. A consumer's own
53
+ * `updatePosition`, if any, is wrapped rather than replaced.
54
+ */
55
+ export const useSlideAfterPlaced = () => {
56
+ const [sliding, setSliding] = useState(false)
57
+ // Read by placements zag finishes asynchronously, after a close may have
58
+ // landed — one of those must not re-arm the slide for the next open.
59
+ const open = useRef(false)
60
+ const onOpenChange = (next: boolean) => {
61
+ open.current = next
62
+ if (!next) setSliding(false)
63
+ }
64
+ const updatePosition =
65
+ (custom?: UpdatePosition): UpdatePosition =>
66
+ async (data) => {
67
+ await (custom ? custom(data) : data.updatePosition())
68
+ if (open.current) setSliding(true)
69
+ }
70
+ return { sliding, onOpenChange, updatePosition }
71
+ }
72
+
73
+ /** Split a group's children into its `Content` parts (matched by `type`)
74
+ * and everything else (the triggers), one level deep. */
75
+ export const splitGroupChildren = <P extends { value?: string }>(
76
+ children: ReactNode,
77
+ content: ElementType<P>,
78
+ ) => {
79
+ const items = Children.toArray(children)
80
+ const isContent = (node: ReactNode): node is ReactElement<P> =>
81
+ isValidElement(node) && node.type === content
82
+ return { contents: items.filter(isContent), triggers: items.filter((item) => !isContent(item)) }
83
+ }
84
+
85
+ /** The `Content` whose `value` matches the active trigger, under one forced,
86
+ * stable `key` — overriding whatever each was authored with (e.g.
87
+ * `key={person.handle}` in a `.map()`). The slot only ever renders one at a
88
+ * time, but a *different* key switching in reads to React as a different
89
+ * component, unmounting the real Positioner/Content DOM tree and mounting a
90
+ * fresh one instead of updating it in place. That fresh mount breaks the
91
+ * "one shared, sliding panel" contract: its position starts unset again, so
92
+ * every switch would look like the panel jumping in from the top. */
93
+ export const activeGroupContent = (
94
+ contents: ReactElement<{ value?: string }>[],
95
+ triggerValue: string | null,
96
+ ) => {
97
+ const active = contents.find((content) => content.props.value === triggerValue)
98
+ return active ? cloneElement(active, { key: "group-content" }) : null
99
+ }