Open History to see the last commit
1import { Popover as PopoverPrimitive, usePopoverContext } from '@ark-ui/solid'
2import { Show, splitProps } from 'solid-js'
3import { Portal } from './portal'
4import type { JSX } from 'solid-js'
5import { cn } from '@/utils'
6β
7/**
8 * The root component that coordinates popover trigger, anchor, and content.
9 *
10 * @example
11 * ```tsx
12 * <Popover>
13 * <PopoverAnchor class="inline-block">Workspace</PopoverAnchor>
14 * <PopoverTrigger>Open</PopoverTrigger>
15 * <PopoverContent>
16 * <PopoverHeader>
17 * <PopoverTitle>Workspace</PopoverTitle>
18 * <PopoverDescription>Manage shared access.</PopoverDescription>
19 * </PopoverHeader>
20 * </PopoverContent>
21 * </Popover>
22 * ```
23 */
24function Popover(props: PopoverPrimitive.RootProps) {
25 const [local, others] = splitProps(props, ['children', 'portalled'])
26β
27 return (
28 <PopoverPrimitive.Root
29 data-slot="popover"
30 portalled={local.portalled ?? true}
31 {...others}
32 >
33 {local.children}
34 </PopoverPrimitive.Root>
35 )
36}
37β
38/**
39 * The control that opens and closes a Popover.
40 *
41 * @example
42 * ```tsx
43 * <Popover>
44 * <PopoverAnchor class="inline-block">Workspace</PopoverAnchor>
45 * <PopoverTrigger>Open</PopoverTrigger>
46 * <PopoverContent>
47 * <PopoverHeader>
48 * <PopoverTitle>Workspace</PopoverTitle>
49 * <PopoverDescription>Manage shared access.</PopoverDescription>
50 * </PopoverHeader>
51 * </PopoverContent>
52 * </Popover>
53 * ```
54 */
55function PopoverTrigger(props: PopoverPrimitive.TriggerProps) {
56 const [local, others] = splitProps(props, ['class'])
57β
58 return (
59 <PopoverPrimitive.Trigger
60 data-slot="popover-trigger"
61 class={local.class}
62 {...others}
63 />
64 )
65}
66β
67function PopoverPositionedContent(props: PopoverPrimitive.ContentProps) {
68 const [local, others] = splitProps(props, ['children', 'class'])
69β
70 return (
71 <PopoverPrimitive.Positioner data-slot="popover-positioner">
72 <PopoverPrimitive.Content
73 data-slot="popover-content"
74 class={cn(
75 'max-w-(--available-width) max-h-(--available-height) overflow-auto z-50 flex w-72 origin-(--transform-origin) flex-col gap-2.5 rounded-lg bg-popover p-2.5 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95',
76 local.class,
77 )}
78 {...others}
79 >
80 {local.children}
81 </PopoverPrimitive.Content>
82 </PopoverPrimitive.Positioner>
83 )
84}
85β
86/**
87 * The floating surface rendered when a Popover is open.
88 *
89 * @example
90 * ```tsx
91 * <Popover>
92 * <PopoverAnchor class="inline-block">Workspace</PopoverAnchor>
93 * <PopoverTrigger>Open</PopoverTrigger>
94 * <PopoverContent>
95 * <PopoverHeader>
96 * <PopoverTitle>Workspace</PopoverTitle>
97 * <PopoverDescription>Manage shared access.</PopoverDescription>
98 * </PopoverHeader>
99 * </PopoverContent>
100 * </Popover>
101 * ```
102 */
103function PopoverContent(props: PopoverPrimitive.ContentProps) {
104 const info = usePopoverContext()
105β
106 return (
107 <Show
108 when={info().portalled}
109 fallback={<PopoverPositionedContent {...props} />}
110 >
111 <Portal>
112 <PopoverPositionedContent {...props} />
113 </Portal>
114 </Show>
115 )
116}
117β
118/**
119 * An optional positioning anchor for aligning PopoverContent separately from the
120 * trigger.
121 *
122 * @example
123 * ```tsx
124 * <Popover>
125 * <PopoverAnchor class="inline-block">Workspace</PopoverAnchor>
126 * <PopoverTrigger>Open</PopoverTrigger>
127 * <PopoverContent>
128 * <PopoverHeader>
129 * <PopoverTitle>Workspace</PopoverTitle>
130 * <PopoverDescription>Manage shared access.</PopoverDescription>
131 * </PopoverHeader>
132 * </PopoverContent>
133 * </Popover>
134 * ```
135 */
136function PopoverAnchor(props: PopoverPrimitive.AnchorProps) {
137 const [local, others] = splitProps(props, ['class'])
138β
139 return (
140 <PopoverPrimitive.Anchor
141 data-slot="popover-anchor"
142 class={local.class}
143 {...others}
144 />
145 )
146}
147β
148/**
149 * A compact header layout for PopoverTitle and PopoverDescription.
150 *
151 * @example
152 * ```tsx
153 * <Popover>
154 * <PopoverAnchor class="inline-block">Workspace</PopoverAnchor>
155 * <PopoverTrigger>Open</PopoverTrigger>
156 * <PopoverContent>
157 * <PopoverHeader>
158 * <PopoverTitle>Workspace</PopoverTitle>
159 * <PopoverDescription>Manage shared access.</PopoverDescription>
160 * </PopoverHeader>
161 * </PopoverContent>
162 * </Popover>
163 * ```
164 */
165function PopoverHeader(props: JSX.HTMLElementTags['div']) {
166 const [local, others] = splitProps(props, ['class'])
167β
168 return (
169 <div
170 data-slot="popover-header"
171 class={cn('flex flex-col gap-0.5 text-sm', local.class)}
172 {...others}
173 />
174 )
175}
176β
177/**
178 * The accessible title for PopoverContent.
179 *
180 * @example
181 * ```tsx
182 * <Popover>
183 * <PopoverAnchor class="inline-block">Workspace</PopoverAnchor>
184 * <PopoverTrigger>Open</PopoverTrigger>
185 * <PopoverContent>
186 * <PopoverHeader>
187 * <PopoverTitle>Workspace</PopoverTitle>
188 * <PopoverDescription>Manage shared access.</PopoverDescription>
189 * </PopoverHeader>
190 * </PopoverContent>
191 * </Popover>
192 * ```
193 */
194function PopoverTitle(props: PopoverPrimitive.TitleProps) {
195 const [local, others] = splitProps(props, ['class'])
196β
197 return (
198 <PopoverPrimitive.Title
199 data-slot="popover-title"
200 class={cn('font-medium', local.class)}
201 {...others}
202 />
203 )
204}
205β
206/**
207 * Supporting descriptive text for PopoverContent.
208 *
209 * @example
210 * ```tsx
211 * <Popover>
212 * <PopoverAnchor class="inline-block">Workspace</PopoverAnchor>
213 * <PopoverTrigger>Open</PopoverTrigger>
214 * <PopoverContent>
215 * <PopoverHeader>
216 * <PopoverTitle>Workspace</PopoverTitle>
217 * <PopoverDescription>Manage shared access.</PopoverDescription>
218 * </PopoverHeader>
219 * </PopoverContent>
220 * </Popover>
221 * ```
222 */
223function PopoverDescription(props: PopoverPrimitive.DescriptionProps) {
224 const [local, others] = splitProps(props, ['class'])
225β
226 return (
227 <PopoverPrimitive.Description
228 data-slot="popover-description"
229 class={cn('text-muted-foreground', local.class)}
230 {...others}
231 />
232 )
233}
234β
235export {
236 Popover,
237 PopoverAnchor,
238 PopoverContent,
239 PopoverDescription,
240 PopoverHeader,
241 PopoverTitle,
242 PopoverTrigger,
243}