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 touchBackdrop = false,
47 ...props
48}: PopoverPrimitive.Popup.Props & {
49 padding?: keyof typeof popoverViewportPaddingClassName;
50 variant?: "default" | "panel";
51 side?: PopoverPrimitive.Positioner.Props["side"];
52 align?: PopoverPrimitive.Positioner.Props["align"];
53 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
54 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
55 collisionAvoidance?: PopoverPrimitive.Positioner.Props["collisionAvoidance"];
56 tooltipStyle?: boolean;
57 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
58 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
59 width?: keyof typeof popoverPopupWidthClassName;
60 touchBackdrop?: boolean;
61}) {
62 // Viewport rekeys its children when the active trigger clears on close. Persistent
63 // single-trigger forms need a stable container to retain drafts and submit guards.
64 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
65 return (
66 <PopoverPrimitive.Portal keepMounted={keepMounted}>
67 {touchBackdrop ? (
68 // A non-modal popover closes on an outside tap and lets that tap reach the
69 // control under the finger. On touch screens the backdrop takes the tap, so
70 // tapping away only closes the popover, as it does for modal menus and selects.
71 <PopoverPrimitive.Backdrop
72 className="fixed inset-0 z-[130] hidden pointer-coarse:block"
73 data-slot="popover-backdrop"
74 />
75 ) : null}
76 <PopoverPrimitive.Positioner
77 align={align}
78 alignOffset={alignOffset}
79 anchor={anchor}
80 collisionAvoidance={collisionAvoidance}
81 collisionPadding={variant === "panel" ? 0 : undefined}
82 className={cn(
83 "h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none",
84 variant === "panel"
85 ? "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"
86 : "z-[130]",
87 )}
88 data-slot="popover-positioner"
89 side={side}
90 sideOffset={sideOffset}
91 >
92 <PopoverPrimitive.Popup
93 className={cn(
94 "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%)]",
95 variant !== "panel" && "dropdown-glass",
96 tooltipStyle &&
97 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
98 !tooltipStyle &&
99 variant !== "panel" &&
100 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
101 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
102 variant === "panel" &&
103 "w-full overflow-visible rounded-none border-0 bg-transparent shadow-none before:hidden [backdrop-filter:none] [-webkit-backdrop-filter:none]",
104 className,
105 )}
106 data-slot="popover-popup"
107 {...props}
108 >
109 <Viewport
110 className={cn(
111 "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",
112 tooltipStyle && padding === "default"
113 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
114 : popoverViewportPaddingClassName[padding],
115 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
116 variant === "panel" &&
117 "overflow-visible py-2 [--viewport-inline-padding:--spacing(2)]",
118 )}
119 data-slot="popover-viewport"
120 >
121 {children}
122 </Viewport>
123 </PopoverPrimitive.Popup>
124 </PopoverPrimitive.Positioner>
125 </PopoverPrimitive.Portal>
126 );
127}
128
129function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
130 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
131}
132
133function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
134 return (
135 <PopoverPrimitive.Title
136 className={cn("font-semibold text-sm leading-none", className)}
137 data-slot="popover-title"
138 {...props}
139 />
140 );
141}
142
143export {
144 PopoverCreateHandle,
145 Popover,
146 PopoverTrigger,
147 PopoverPopup,
148 PopoverPopup as PopoverContent,
149 PopoverTitle,
150 PopoverClose,
151};