Open History to see the last commit
1import { cva } from 'class-variance-authority'
2import { ark } from '@ark-ui/solid/factory'
3import { splitProps } from 'solid-js'
4import type { JSX } from 'solid-js'
5import type { VariantProps } from 'class-variance-authority'
6import type { HTMLArkProps } from '@ark-ui/solid/factory'
7import type { SeparatorProps } from '@/components/ui/separator'
8import { Separator } from '@/components/ui/separator'
9import { cn } from '@/utils'
10β
11// Ported from the original shadcn/ui + Radix UI + React component.
12// Item uses an Ark factory element for Solid's render-function asChild API;
13// the remaining parts are styled, non-interactive DOM primitives.
14// https://ui.shadcn.com/
15β
16type DivProps = JSX.HTMLElementTags['div']
17β
18function ItemGroup(props: DivProps) {
19 const [local, others] = splitProps(props, ['class', 'role'])
20β
21 return (
22 <div
23 {...others}
24 role={local.role ?? 'list'}
25 data-slot="item-group"
26 class={cn(
27 'group/item-group flex w-full flex-col gap-4 has-data-[size=sm]:gap-2.5 has-data-[size=xs]:gap-2',
28 local.class,
29 )}
30 />
31 )
32}
33β
34function ItemSeparator(props: SeparatorProps) {
35 const [local, others] = splitProps(props, [
36 'class',
37 'orientation',
38 'data-slot',
39 ])
40β
41 return (
42 <Separator
43 {...others}
44 data-slot="item-separator"
45 orientation={local.orientation ?? 'horizontal'}
46 class={cn('my-2', local.class)}
47 />
48 )
49}
50β
51const itemVariants = cva(
52 'group/item flex w-full flex-wrap items-center rounded-lg border text-sm transition-colors duration-100 outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 [a]:transition-colors [a]:hover:bg-muted',
53 {
54 variants: {
55 variant: {
56 default: 'border-transparent',
57 outline: 'border-border',
58 muted: 'border-transparent bg-muted/50',
59 },
60 size: {
61 default: 'gap-2.5 px-3 py-2.5',
62 sm: 'gap-2.5 px-3 py-2.5',
63 xs: 'gap-2 px-2.5 py-2 in-data-[slot=dropdown-menu-content]:p-0',
64 },
65 },
66 defaultVariants: {
67 variant: 'default',
68 size: 'default',
69 },
70 },
71)
72β
73type ItemProps = HTMLArkProps<'div'> & VariantProps<typeof itemVariants>
74β
75/**
76 * A flexible row for displaying media, content, metadata, and actions.
77 *
78 * Use Ark's render-function `asChild` prop when the item should render as a
79 * link or another element.
80 */
81function Item(props: ItemProps) {
82 const [local, others] = splitProps(props, ['class', 'variant', 'size'])
83β
84 const variant = () => local.variant ?? 'default'
85 const size = () => local.size ?? 'default'
86β
87 return (
88 <ark.div
89 {...others}
90 data-slot="item"
91 data-variant={variant()}
92 data-size={size()}
93 class={cn(
94 itemVariants({ variant: variant(), size: size() }),
95 local.class,
96 )}
97 />
98 )
99}
100β
101const itemMediaVariants = cva(
102 'flex shrink-0 items-center justify-center gap-2 group-has-data-[slot=item-description]/item:translate-y-0.5 group-has-data-[slot=item-description]/item:self-start [&_svg]:pointer-events-none',
103 {
104 variants: {
105 variant: {
106 default: 'bg-transparent',
107 icon: "[&_svg:not([class*='size-'])]:size-4",
108 image:
109 'size-10 overflow-hidden rounded-sm group-data-[size=sm]/item:size-8 group-data-[size=xs]/item:size-6 [&_img]:size-full [&_img]:object-cover',
110 },
111 },
112 defaultVariants: {
113 variant: 'default',
114 },
115 },
116)
117β
118type ItemMediaProps = HTMLArkProps<'div'> &
119 VariantProps<typeof itemMediaVariants>
120β
121function ItemMedia(props: ItemMediaProps) {
122 const [local, others] = splitProps(props, ['class', 'variant'])
123β
124 const variant = () => local.variant ?? 'default'
125β
126 return (
127 <ark.div
128 {...others}
129 data-slot="item-media"
130 data-variant={variant()}
131 class={cn(itemMediaVariants({ variant: variant() }), local.class)}
132 />
133 )
134}
135β
136function ItemContent(props: DivProps) {
137 const [local, others] = splitProps(props, ['class'])
138β
139 return (
140 <div
141 {...others}
142 data-slot="item-content"
143 class={cn(
144 'flex flex-1 flex-col gap-1 group-data-[size=xs]/item:gap-0 [&+[data-slot=item-content]]:flex-none',
145 local.class,
146 )}
147 />
148 )
149}
150β
151function ItemTitle(props: DivProps) {
152 const [local, others] = splitProps(props, ['class'])
153β
154 return (
155 <div
156 {...others}
157 data-slot="item-title"
158 class={cn(
159 'line-clamp-1 flex w-fit items-center gap-2 text-sm leading-snug font-medium underline-offset-4',
160 local.class,
161 )}
162 />
163 )
164}
165β
166type ItemDescriptionProps = JSX.HTMLElementTags['p']
167β
168function ItemDescription(props: ItemDescriptionProps) {
169 const [local, others] = splitProps(props, ['class'])
170β
171 return (
172 <p
173 {...others}
174 data-slot="item-description"
175 class={cn(
176 'line-clamp-2 text-left text-sm leading-normal font-normal text-muted-foreground group-data-[size=xs]/item:text-xs [&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary',
177 local.class,
178 )}
179 />
180 )
181}
182β
183function ItemActions(props: DivProps) {
184 const [local, others] = splitProps(props, ['class'])
185β
186 return (
187 <div
188 {...others}
189 data-slot="item-actions"
190 class={cn('flex items-center gap-2', local.class)}
191 />
192 )
193}
194β
195function ItemHeader(props: DivProps) {
196 const [local, others] = splitProps(props, ['class'])
197β
198 return (
199 <div
200 {...others}
201 data-slot="item-header"
202 class={cn(
203 'flex basis-full items-center justify-between gap-2',
204 local.class,
205 )}
206 />
207 )
208}
209β
210function ItemFooter(props: DivProps) {
211 const [local, others] = splitProps(props, ['class'])
212β
213 return (
214 <div
215 {...others}
216 data-slot="item-footer"
217 class={cn(
218 'flex basis-full items-center justify-between gap-2',
219 local.class,
220 )}
221 />
222 )
223}
224β
225export {
226 Item,
227 ItemMedia,
228 ItemContent,
229 ItemActions,
230 ItemGroup,
231 ItemSeparator,
232 ItemTitle,
233 ItemDescription,
234 ItemHeader,
235 ItemFooter,
236}