Skip to content

Commit 8253310

Browse files
committed
Add alert-dialog component
1 parent 5910bb0 commit 8253310

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import type {
2+
AlertDialogCloseButtonProps,
3+
AlertDialogContentProps,
4+
AlertDialogDescriptionProps,
5+
AlertDialogTitleProps,
6+
} from "@kobalte/core/alert-dialog";
7+
import { AlertDialog as AlertDialogPrimitive } from "@kobalte/core/alert-dialog";
8+
import type { PolymorphicProps } from "@kobalte/core/polymorphic";
9+
import type { ComponentProps, ParentProps, ValidComponent } from "solid-js";
10+
import { splitProps } from "solid-js";
11+
import { cn } from "~/lib/utils.ts";
12+
import { buttonVariants } from "./button.tsx";
13+
14+
export const AlertDialog = AlertDialogPrimitive;
15+
export const AlertDialogTrigger = AlertDialogPrimitive.Trigger;
16+
17+
type alertDialogContentProps<T extends ValidComponent = "div"> = ParentProps<
18+
AlertDialogContentProps<T> & {
19+
class?: string;
20+
}
21+
>;
22+
23+
export const AlertDialogContent = <T extends ValidComponent = "div">(
24+
props: PolymorphicProps<T, alertDialogContentProps<T>>,
25+
) => {
26+
const [local, rest] = splitProps(props as alertDialogContentProps, [
27+
"class",
28+
"children",
29+
]);
30+
31+
return (
32+
<AlertDialogPrimitive.Portal>
33+
<AlertDialogPrimitive.Overlay
34+
class={cn(
35+
"fixed inset-0 z-50 bg-background/80 data-[expanded]:animate-in data-[closed]:animate-out data-[closed]:fade-out-0 data-[expanded]:fade-in-0",
36+
)}
37+
/>
38+
<AlertDialogPrimitive.Content
39+
class={cn(
40+
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg data-[closed]:duration-200 data-[expanded]:duration-200 data-[expanded]:animate-in data-[closed]:animate-out data-[closed]:fade-out-0 data-[expanded]:fade-in-0 data-[closed]:zoom-out-95 data-[expanded]:zoom-in-95 data-[closed]:slide-out-to-left-1/2 data-[closed]:slide-out-to-top-[48%] data-[expanded]:slide-in-from-left-1/2 data-[expanded]:slide-in-from-top-[48%] sm:rounded-lg md:w-full",
41+
local.class,
42+
)}
43+
{...rest}
44+
>
45+
{local.children}
46+
</AlertDialogPrimitive.Content>
47+
</AlertDialogPrimitive.Portal>
48+
);
49+
};
50+
51+
export const AlertDialogHeader = (props: ComponentProps<"div">) => {
52+
const [local, rest] = splitProps(props, ["class"]);
53+
54+
return (
55+
<div
56+
class={cn(
57+
"flex flex-col space-y-2 text-center sm:text-left",
58+
local.class,
59+
)}
60+
{...rest}
61+
/>
62+
);
63+
};
64+
65+
export const AlertDialogFooter = (props: ComponentProps<"div">) => {
66+
const [local, rest] = splitProps(props, ["class"]);
67+
68+
return (
69+
<div
70+
class={cn(
71+
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
72+
local.class,
73+
)}
74+
{...rest}
75+
/>
76+
);
77+
};
78+
79+
type alertDialogTitleProps<T extends ValidComponent = "h2"> =
80+
& AlertDialogTitleProps<T>
81+
& {
82+
class?: string;
83+
};
84+
85+
export const AlertDialogTitle = <T extends ValidComponent = "h2">(
86+
props: PolymorphicProps<T, alertDialogTitleProps<T>>,
87+
) => {
88+
const [local, rest] = splitProps(props as alertDialogTitleProps, ["class"]);
89+
90+
return (
91+
<AlertDialogPrimitive.Title
92+
class={cn("text-lg font-semibold", local.class)}
93+
{...rest}
94+
/>
95+
);
96+
};
97+
98+
type alertDialogDescriptionProps<T extends ValidComponent = "p"> =
99+
& AlertDialogDescriptionProps<T>
100+
& {
101+
class?: string;
102+
};
103+
104+
export const AlertDialogDescription = <T extends ValidComponent = "p">(
105+
props: PolymorphicProps<T, alertDialogDescriptionProps<T>>,
106+
) => {
107+
const [local, rest] = splitProps(props as alertDialogDescriptionProps, [
108+
"class",
109+
]);
110+
111+
return (
112+
<AlertDialogPrimitive.Description
113+
class={cn("text-sm text-muted-foreground", local.class)}
114+
{...rest}
115+
/>
116+
);
117+
};
118+
119+
type alertDialogCloseProps<T extends ValidComponent = "button"> =
120+
& AlertDialogCloseButtonProps<T>
121+
& {
122+
class?: string;
123+
};
124+
125+
export const AlertDialogClose = <T extends ValidComponent = "button">(
126+
props: PolymorphicProps<T, alertDialogCloseProps<T>>,
127+
) => {
128+
const [local, rest] = splitProps(props as alertDialogCloseProps, ["class"]);
129+
130+
return (
131+
<AlertDialogPrimitive.CloseButton
132+
class={cn(
133+
buttonVariants({
134+
variant: "outline",
135+
}),
136+
"mt-2 md:mt-0",
137+
local.class,
138+
)}
139+
{...rest}
140+
/>
141+
);
142+
};
143+
144+
export const AlertDialogAction = <T extends ValidComponent = "button">(
145+
props: PolymorphicProps<T, alertDialogCloseProps<T>>,
146+
) => {
147+
const [local, rest] = splitProps(props as alertDialogCloseProps, ["class"]);
148+
149+
return (
150+
<AlertDialogPrimitive.CloseButton
151+
class={cn(buttonVariants(), local.class)}
152+
{...rest}
153+
/>
154+
);
155+
};

0 commit comments

Comments
 (0)