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
19// Popovers hold prose and forms, so a width is fixed rather than a minimum,
20// and every width is capped to the viewport.
21const popoverPopupWidthClassName = {
22 auto: "",
23 sm: "w-64",
24 md: "w-80",
25 lg: "w-96",
26} as const;
27
28// The inset around the content. "compact" suits dense content (a list, a code excerpt, a
29// row of reactions); "none" is for content that draws its own frame edge to edge.
30const popoverViewportPaddingClassName = {
31 default: "py-4 [--viewport-inline-padding:--spacing(4)]",
32 compact: "py-2 [--viewport-inline-padding:--spacing(3)]",
33 // Rounded to the popup so edge-to-edge content clips to its corners.
34 none: "rounded-[calc(var(--radius-lg)-1px)] py-0 [--viewport-inline-padding:0px]",
35} as const;
36
37function PopoverPopup({
38 children,
39 className,
40 padding = "default",
41 width = "auto",
42 side = "bottom",
43 align = "center",
44 sideOffset = 4,
45 alignOffset = 0,
46 tooltipStyle = false,
47 keepMounted = false,
48 anchor,
49 ...props
50}: PopoverPrimitive.Popup.Props & {
51 padding?: keyof typeof popoverViewportPaddingClassName;
52 side?: PopoverPrimitive.Positioner.Props["side"];
53 align?: PopoverPrimitive.Positioner.Props["align"];
54 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
55 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
56 tooltipStyle?: boolean;
57 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
58 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
59 width?: keyof typeof popoverPopupWidthClassName;
60}) {
61 // Viewport rekeys its children when the active trigger clears on close. Persistent
62 // single-trigger forms need a stable container to retain drafts and submit guards.
63 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
64 return (
65 <PopoverPrimitive.Portal keepMounted={keepMounted}>
66 <PopoverPrimitive.Positioner
67 align={align}
68 alignOffset={alignOffset}
69 anchor={anchor}
70 className="z-[130] h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none"
71 data-slot="popover-positioner"
72 side={side}
73 sideOffset={sideOffset}
74 >
75 <PopoverPrimitive.Popup
76 className={cn(
77 "dropdown-glass 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-starting-style:scale-98 data-starting-style:opacity-0 dark:before:shadow-[0_-1px_--theme(--color-white/6%)]",
78 tooltipStyle &&
79 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
80 !tooltipStyle &&
81 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
82 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
83 className,
84 )}
85 data-slot="popover-popup"
86 {...props}
87 >
88 <Viewport
89 className={cn(
90 "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",
91 tooltipStyle && padding === "default"
92 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
93 : popoverViewportPaddingClassName[padding],
94 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
95 )}
96 data-slot="popover-viewport"
97 >
98 {children}
99 </Viewport>
100 </PopoverPrimitive.Popup>
101 </PopoverPrimitive.Positioner>
102 </PopoverPrimitive.Portal>
103 );
104}
105
106function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
107 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
108}
109
110function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
111 return (
112 <PopoverPrimitive.Title
113 className={cn("font-semibold text-sm leading-none", className)}
114 data-slot="popover-title"
115 {...props}
116 />
117 );
118}
119
120export {
121 PopoverCreateHandle,
122 Popover,
123 PopoverTrigger,
124 PopoverPopup,
125 PopoverPopup as PopoverContent,
126 PopoverTitle,
127 PopoverClose,
128};