Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

popover.tsx

149 lines6.5 KB

1import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
2​
3import { cn } from "~/lib/utils";
4​
5const PopoverCreateHandle = PopoverPrimitive.createHandle;
6​
7const Popover = PopoverPrimitive.Root;
8​
9function PopoverTrigger({ className, children, ...props }: PopoverPrimitive.Trigger.Props) {
10 return (
11 <PopoverPrimitive.Trigger className={className} data-slot="popover-trigger" {...props}>
12 {children}
13 </PopoverPrimitive.Trigger>
14 );
16​
17const popoverPopupWidthClassName = {
18 auto: "",
19 sm: "w-64",
20 md: "w-80",
21 lg: "w-96",
22} as const;
23​
24const popoverViewportPaddingClassName = {
25 default: "py-4 [--viewport-inline-padding:--spacing(4)]",
26 compact: "py-2 [--viewport-inline-padding:--spacing(3)]",
27 none: "rounded-[calc(var(--radius-lg)-1px)] py-0 [--viewport-inline-padding:0px]",
28} as const;
29​
30function PopoverPopup({
31 children,
32 className,
33 padding = "default",
34 variant = "default",
35 width = "auto",
36 side = "bottom",
37 align = "center",
38 sideOffset = 4,
39 alignOffset = 0,
40 collisionAvoidance,
41 tooltipStyle = false,
42 keepMounted = false,
43 anchor,
44 touchBackdrop = false,
45 ...props
46}: PopoverPrimitive.Popup.Props & {
47 padding?: keyof typeof popoverViewportPaddingClassName;
48 variant?: "default" | "panel";
49 side?: PopoverPrimitive.Positioner.Props["side"];
50 align?: PopoverPrimitive.Positioner.Props["align"];
51 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
52 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
53 collisionAvoidance?: PopoverPrimitive.Positioner.Props["collisionAvoidance"];
54 tooltipStyle?: boolean;
55 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
56 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
57 width?: keyof typeof popoverPopupWidthClassName;
58 touchBackdrop?: boolean;
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 {touchBackdrop ? (
66 // A non-modal popover closes on an outside tap and lets that tap reach the
67 // control under the finger. On touch screens the backdrop takes the tap, so
68 // tapping away only closes the popover, as it does for modal menus and selects.
69 <PopoverPrimitive.Backdrop
70 className="fixed inset-0 z-[130] hidden pointer-coarse:block"
71 data-slot="popover-backdrop"
72 />
73 ) : null}
74 <PopoverPrimitive.Positioner
75 align={align}
76 alignOffset={alignOffset}
77 anchor={anchor}
78 collisionAvoidance={collisionAvoidance}
79 collisionPadding={variant === "panel" ? 0 : undefined}
80 className={cn(
81 "h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none",
82 variant === "panel"
83 ? "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"
84 : "z-[130]",
85 )}
86 data-slot="popover-positioner"
87 side={side}
88 sideOffset={sideOffset}
89 >
90 <PopoverPrimitive.Popup
91 className={cn(
92 "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%)]",
93 variant !== "panel" && "dropdown-glass",
94 tooltipStyle &&
95 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
96 !tooltipStyle &&
97 variant !== "panel" &&
98 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
99 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
100 variant === "panel" &&
101 "w-full overflow-visible rounded-none border-0 bg-transparent shadow-none before:hidden [backdrop-filter:none] [-webkit-backdrop-filter:none]",
102 className,
103 )}
104 data-slot="popover-popup"
105 {...props}
106 >
107 <Viewport
108 className={cn(
109 "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",
110 tooltipStyle && padding === "default"
111 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
112 : popoverViewportPaddingClassName[padding],
113 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
114 variant === "panel" &&
115 "overflow-visible py-2 [--viewport-inline-padding:--spacing(2)]",
116 )}
117 data-slot="popover-viewport"
118 >
119 {children}
120 </Viewport>
121 </PopoverPrimitive.Popup>
122 </PopoverPrimitive.Positioner>
123 </PopoverPrimitive.Portal>
124 );
126​
127function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
128 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
130​
131function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
132 return (
133 <PopoverPrimitive.Title
134 className={cn("font-semibold text-sm leading-none", className)}
135 data-slot="popover-title"
136 {...props}
137 />
138 );
140​
141export {
142 PopoverCreateHandle,
143 Popover,
144 PopoverTrigger,
145 PopoverPopup,
146 PopoverPopup as PopoverContent,
147 PopoverTitle,
148 PopoverClose,
149};