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

popover.tsx

150 lines6.4 KB

1import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
2​
3import { cn } from "~/lib/utils";
4​
5import { renderClosingPositioner } from "./closing-positioner";
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 );
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 on close; persistent single-trigger forms need stable containers for drafts and submit
63 // guards.
64 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
65 return (
66 <PopoverPrimitive.Portal keepMounted={keepMounted}>
67 {touchBackdrop ? (
68 // On touch, the backdrop consumes outside taps so closing cannot also activate the control beneath.
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 render={renderClosingPositioner}
88 side={side}
89 sideOffset={sideOffset}
90 >
91 <PopoverPrimitive.Popup
92 className={cn(
93 "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%)]",
94 variant !== "panel" && "dropdown-glass",
95 tooltipStyle &&
96 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
97 !tooltipStyle &&
98 variant !== "panel" &&
99 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
100 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
101 variant === "panel" &&
102 "w-full overflow-visible rounded-none border-0 bg-transparent shadow-none before:hidden [backdrop-filter:none] [-webkit-backdrop-filter:none]",
103 className,
104 )}
105 data-slot="popover-popup"
106 {...props}
107 >
108 <Viewport
109 className={cn(
110 "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",
111 tooltipStyle && padding === "default"
112 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
113 : popoverViewportPaddingClassName[padding],
114 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
115 variant === "panel" &&
116 "overflow-visible py-2 [--viewport-inline-padding:--spacing(2)]",
117 )}
118 data-slot="popover-viewport"
119 >
120 {children}
121 </Viewport>
122 </PopoverPrimitive.Popup>
123 </PopoverPrimitive.Positioner>
124 </PopoverPrimitive.Portal>
125 );
127​
128function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
129 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
131​
132function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
133 return (
134 <PopoverPrimitive.Title
135 className={cn("font-semibold text-sm leading-none", className)}
136 data-slot="popover-title"
137 {...props}
138 />
139 );
141​
142export {
143 PopoverCreateHandle,
144 Popover,
145 PopoverTrigger,
146 PopoverPopup,
147 PopoverPopup as PopoverContent,
148 PopoverTitle,
149 PopoverClose,
150};