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

alert.tsx

72 lines2.3 KB

1import { cva, type VariantProps } from "class-variance-authority";
2import type * as React from "react";
3​
4import { cn } from "~/lib/utils";
5​
6const alertVariants = cva(
7 "relative grid w-full items-start gap-x-2 gap-y-0.5 rounded-xl border px-3.5 py-3 text-card-foreground text-sm has-[>svg]:has-data-[slot=alert-action]:grid-cols-[calc(var(--spacing)*4)_1fr_auto] has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] has-data-[slot=alert-action]:grid-cols-[1fr_auto] has-[>svg]:gap-x-2 [&>svg]:h-lh [&>svg]:w-4",
8 {
9 defaultVariants: {
10 variant: "default",
11 },
12 variants: {
13 variant: {
14 default: "bg-transparent dark:bg-input/32 [&>svg]:text-muted-foreground",
15 error: "border-destructive/32 bg-destructive/4 [&>svg]:text-destructive",
16 info: "border-info/32 bg-info/4 [&>svg]:text-info",
17 success: "border-success/32 bg-success/4 [&>svg]:text-success",
18 warning: "border-warning/32 bg-warning/4 [&>svg]:text-warning",
19 },
20 },
21 },
22);
23​
24function Alert({
25 className,
26 variant,
27 ...props
28}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
29 return (
30 <div
31 className={cn(alertVariants({ variant }), className)}
32 data-slot="alert"
33 role="alert"
34 {...props}
35 />
36 );
38​
39function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
40 return (
41 <div
42 className={cn("font-medium [svg~&]:col-start-2", className)}
43 data-slot="alert-title"
44 {...props}
45 />
46 );
48​
49function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
50 return (
51 <div
52 className={cn("flex flex-col gap-2.5 text-muted-foreground [svg~&]:col-start-2", className)}
53 data-slot="alert-description"
54 {...props}
55 />
56 );
58​
59function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
60 return (
61 <div
62 className={cn(
63 "flex gap-1 max-sm:col-start-2 max-sm:mt-2 sm:row-start-1 sm:row-end-3 sm:self-center sm:[[data-slot=alert-description]~&]:col-start-2 sm:[[data-slot=alert-title]~&]:col-start-2 sm:[svg~&]:col-start-2 sm:[svg~[data-slot=alert-description]~&]:col-start-3 sm:[svg~[data-slot=alert-title]~&]:col-start-3",
64 className,
65 )}
66 data-slot="alert-action"
67 {...props}
68 />
69 );
71​
72export { Alert, AlertTitle, AlertDescription, AlertAction };