Open History to see the last commit
1"use client";
2
3import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
4
5import { cn } from "~/lib/utils";
6
7const PopoverCreateHandle = PopoverPrimitive.createHandle;
8
9const Popover = PopoverPrimitive.Root;
10
11function PopoverTrigger({ className, children, ...props }: PopoverPrimitive.Trigger.Props) {
12 return (
13 <PopoverPrimitive.Trigger className={className} data-slot="popover-trigger" {...props}>
14 {children}
15 </PopoverPrimitive.Trigger>
16 );
17}
18
19const popoverPopupWidthClassName = {
20 auto: "",
21 sm: "w-64",
22 md: "w-80",
23 lg: "w-96",
24} as const;
25
26const popoverViewportPaddingClassName = {
27 default: "py-4 [--viewport-inline-padding:--spacing(4)]",
28 compact: "py-2 [--viewport-inline-padding:--spacing(3)]",
29 none: "rounded-[calc(var(--radius-lg)-1px)] py-0 [--viewport-inline-padding:0px]",
30} as const;
31
32function PopoverPopup({
33 children,
34 className,
35 padding = "default",
36 variant = "default",
37 width = "auto",
38 side = "bottom",
39 align = "center",
40 sideOffset = 4,
41 alignOffset = 0,
42 collisionAvoidance,
43 tooltipStyle = false,
44 keepMounted = false,
45 anchor,
46 ...props
47}: PopoverPrimitive.Popup.Props & {
48 padding?: keyof typeof popoverViewportPaddingClassName;
49 variant?: "default" | "panel";
50 side?: PopoverPrimitive.Positioner.Props["side"];
51 align?: PopoverPrimitive.Positioner.Props["align"];
52 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
53 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
54 collisionAvoidance?: PopoverPrimitive.Positioner.Props["collisionAvoidance"];
55 tooltipStyle?: boolean;
56 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
57 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
58 width?: keyof typeof popoverPopupWidthClassName;
59}) {
60 // Viewport rekeys its children when the active trigger clears on close. Persistent
61 // single-trigger forms need a stable container to retain drafts and submit guards.
62 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
63 return (
64 <PopoverPrimitive.Portal keepMounted={keepMounted}>
65 <PopoverPrimitive.Positioner
66 align={align}
67 alignOffset={alignOffset}
68 anchor={anchor}
69 collisionAvoidance={collisionAvoidance}
70 collisionPadding={variant === "panel" ? 0 : undefined}
71 className={cn(
72 "h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none",
73 variant === "panel"
74 ? "z-(--z-sheet) w-[min(calc(var(--thread-details-panel-width)+--spacing(4)),calc(var(--anchor-width)---spacing(2)))] transition-[top] duration-150 ease-out data-instant:transition-[top] motion-reduce:transition-none motion-reduce:data-instant:transition-none"
75 : "z-[130]",
76 )}
77 data-slot="popover-positioner"
78 side={side}
79 sideOffset={sideOffset}
80 >
81 <PopoverPrimitive.Popup
82 className={cn(
83 "relative flex h-(--popup-height,auto) w-(--popup-width,auto) origin-(--transform-origin) rounded-lg text-popover-foreground outline-none transition-[width,height,scale,opacity] before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] has-data-[slot=calendar]:rounded-xl has-data-[slot=calendar]:before:rounded-[calc(var(--radius-xl)-1px)] data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:before:shadow-[0_-1px_--theme(--color-white/6%)]",
84 variant !== "panel" && "dropdown-glass",
85 tooltipStyle &&
86 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
87 !tooltipStyle &&
88 variant !== "panel" &&
89 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
90 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
91 variant === "panel" &&
92 "w-full overflow-visible rounded-none border-0 bg-transparent shadow-none before:hidden [backdrop-filter:none] [-webkit-backdrop-filter:none]",
93 className,
94 )}
95 data-slot="popover-popup"
96 {...props}
97 >
98 <Viewport
99 className={cn(
100 "relative size-full max-h-(--available-height) overflow-clip px-(--viewport-inline-padding) has-data-[slot=calendar]:p-2 data-instant:transition-none **:data-current:data-ending-style:opacity-0 **:data-current:data-starting-style:opacity-0 **:data-previous:data-ending-style:opacity-0 **:data-previous:data-starting-style:opacity-0 **:data-current:w-[calc(var(--popup-width)-2*var(--viewport-inline-padding)-2px)] **:data-previous:w-[calc(var(--popup-width)-2*var(--viewport-inline-padding)-2px)] **:data-current:opacity-100 **:data-previous:opacity-100 **:data-current:transition-opacity **:data-previous:transition-opacity",
101 tooltipStyle && padding === "default"
102 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
103 : popoverViewportPaddingClassName[padding],
104 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
105 variant === "panel" &&
106 "overflow-visible py-2 [--viewport-inline-padding:--spacing(2)]",
107 )}
108 data-slot="popover-viewport"
109 >
110 {children}
111 </Viewport>
112 </PopoverPrimitive.Popup>
113 </PopoverPrimitive.Positioner>
114 </PopoverPrimitive.Portal>
115 );
116}
117
118function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
119 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
120}
121
122function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
123 return (
124 <PopoverPrimitive.Title
125 className={cn("font-semibold text-sm leading-none", className)}
126 data-slot="popover-title"
127 {...props}
128 />
129 );
130}
131
132export {
133 PopoverCreateHandle,
134 Popover,
135 PopoverTrigger,
136 PopoverPopup,
137 PopoverPopup as PopoverContent,
138 PopoverTitle,
139 PopoverClose,
140};