Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

card.tsx

237 lines6.0 KB

1import { splitProps } from 'solid-js'
2import type { JSX } from 'solid-js'
3import { cn } from '@/utils'
4​
5// Ported from the original shadcn/ui + Radix UI + React component.
6// This wraps the Ark UI + SolidJS primitive and preserves shadcn's Tailwind-based
7// design-system API, variants, and styling.
8// https://ui.shadcn.com/
9​
10type DivProps = JSX.HTMLElementTags['div']
11​
12type CardProps = JSX.HTMLElementTags['div'] & { size?: 'default' | 'sm' }
13​
14/**
15 * A flexible container for grouped content, metadata, and actions.
16 *
17 * @example
18 * ```tsx
19 * <Card>
20 * <CardHeader>
21 * <CardTitle>Workspace</CardTitle>
22 * <CardDescription>Project health and recent activity.</CardDescription>
23 * <CardAction>Active</CardAction>
24 * </CardHeader>
25 * <CardContent>All systems are running normally.</CardContent>
26 * <CardFooter>Updated just now</CardFooter>
27 * </Card>
28 * ```
29 */
30function Card(props: CardProps) {
31 const [local, others] = splitProps(props, ['class', 'size'])
32​
33 const size = () => local.size ?? 'default'
34​
35 return (
36 <div
37 data-slot="card"
38 data-size={size()}
39 class={cn(
40 'group/card flex flex-col gap-(--card-spacing) overflow-hidden rounded-xl bg-card py-(--card-spacing) text-sm text-card-foreground ring-1 ring-foreground/10 [--card-spacing:--spacing(4)] has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(3)] data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl',
41 local.class,
42 )}
43 {...others}
44 />
45 )
47​
48/**
49 * The top section of a Card, usually containing title, description, and action
50 * content.
51 *
52 * @example
53 * ```tsx
54 * <Card>
55 * <CardHeader>
56 * <CardTitle>Workspace</CardTitle>
57 * <CardDescription>Project health and recent activity.</CardDescription>
58 * <CardAction>Active</CardAction>
59 * </CardHeader>
60 * <CardContent>All systems are running normally.</CardContent>
61 * <CardFooter>Updated just now</CardFooter>
62 * </Card>
63 * ```
64 */
65function CardHeader(props: DivProps) {
66 const [local, others] = splitProps(props, ['class'])
67​
68 return (
69 <div
70 data-slot="card-header"
71 class={cn(
72 'group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-(--card-spacing)',
73 local.class,
74 )}
75 {...others}
76 />
77 )
79​
80/**
81 * The primary heading inside a CardHeader.
82 *
83 * @example
84 * ```tsx
85 * <Card>
86 * <CardHeader>
87 * <CardTitle>Workspace</CardTitle>
88 * <CardDescription>Project health and recent activity.</CardDescription>
89 * <CardAction>Active</CardAction>
90 * </CardHeader>
91 * <CardContent>All systems are running normally.</CardContent>
92 * <CardFooter>Updated just now</CardFooter>
93 * </Card>
94 * ```
95 */
96function CardTitle(props: DivProps) {
97 const [local, others] = splitProps(props, ['class'])
98​
99 return (
100 <div
101 data-slot="card-title"
102 class={cn(
103 'font-heading text-base leading-snug font-medium group-data-[size=sm]/card:text-sm',
104 local.class,
105 )}
106 {...others}
107 />
108 )
110​
111/**
112 * Supporting text for the CardTitle.
113 *
114 * @example
115 * ```tsx
116 * <Card>
117 * <CardHeader>
118 * <CardTitle>Workspace</CardTitle>
119 * <CardDescription>Project health and recent activity.</CardDescription>
120 * <CardAction>Active</CardAction>
121 * </CardHeader>
122 * <CardContent>All systems are running normally.</CardContent>
123 * <CardFooter>Updated just now</CardFooter>
124 * </Card>
125 * ```
126 */
127function CardDescription(props: DivProps) {
128 const [local, others] = splitProps(props, ['class'])
129​
130 return (
131 <div
132 data-slot="card-description"
133 class={cn('text-sm text-muted-foreground', local.class)}
134 {...others}
135 />
136 )
138​
139/**
140 * An action or status area aligned to the end of a CardHeader.
141 *
142 * @example
143 * ```tsx
144 * <Card>
145 * <CardHeader>
146 * <CardTitle>Workspace</CardTitle>
147 * <CardDescription>Project health and recent activity.</CardDescription>
148 * <CardAction>Active</CardAction>
149 * </CardHeader>
150 * <CardContent>All systems are running normally.</CardContent>
151 * <CardFooter>Updated just now</CardFooter>
152 * </Card>
153 * ```
154 */
155function CardAction(props: DivProps) {
156 const [local, others] = splitProps(props, ['class'])
157​
158 return (
159 <div
160 data-slot="card-action"
161 class={cn(
162 'col-start-2 row-span-2 row-start-1 self-start justify-self-end',
163 local.class,
164 )}
165 {...others}
166 />
167 )
169​
170/**
171 * The main body section of a Card.
172 *
173 * @example
174 * ```tsx
175 * <Card>
176 * <CardHeader>
177 * <CardTitle>Workspace</CardTitle>
178 * <CardDescription>Project health and recent activity.</CardDescription>
179 * <CardAction>Active</CardAction>
180 * </CardHeader>
181 * <CardContent>All systems are running normally.</CardContent>
182 * <CardFooter>Updated just now</CardFooter>
183 * </Card>
184 * ```
185 */
186function CardContent(props: DivProps) {
187 const [local, others] = splitProps(props, ['class'])
188​
189 return (
190 <div
191 data-slot="card-content"
192 class={cn('px-(--card-spacing)', local.class)}
193 {...others}
194 />
195 )
197​
198/**
199 * The bottom section of a Card, commonly used for summary text or actions.
200 *
201 * @example
202 * ```tsx
203 * <Card>
204 * <CardHeader>
205 * <CardTitle>Workspace</CardTitle>
206 * <CardDescription>Project health and recent activity.</CardDescription>
207 * <CardAction>Active</CardAction>
208 * </CardHeader>
209 * <CardContent>All systems are running normally.</CardContent>
210 * <CardFooter>Updated just now</CardFooter>
211 * </Card>
212 * ```
213 */
214function CardFooter(props: DivProps) {
215 const [local, others] = splitProps(props, ['class'])
216​
217 return (
218 <div
219 data-slot="card-footer"
220 class={cn(
221 'flex items-center rounded-b-xl border-t bg-muted/50 p-(--card-spacing)',
222 local.class,
223 )}
224 {...others}
225 />
226 )
228​
229export {
230 Card,
231 CardHeader,
232 CardFooter,
233 CardTitle,
234 CardAction,
235 CardDescription,
236 CardContent,