Displays a callout for user attention.
1<script lang="ts">
2import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
3import { CheckCircle2Icon, InfoIcon } from "lucide-svelte"
4</script>
Installation
pnpm dlx sprawlify@latest add alertnpx sprawlify@latest add alertyarn sprawlify@latest add alertbunx --bun sprawlify@latest add alert
Install the following dependencies:
pnpm add @sprawlify/primitives @sprawlify/sveltenpm install @sprawlify/primitives @sprawlify/svelteyarn add @sprawlify/primitives @sprawlify/sveltebun add @sprawlify/primitives @sprawlify/svelte
Add the following files to your project:
1<script lang="ts">
2import { cn } from "@/lib/utils"
3import { Sprawlify, type PolymorphicProps } from "@sprawlify/svelte"
4import type { HTMLAttributes } from "svelte/elements";
5
6interface Props extends HTMLAttributes<HTMLDivElement>, PolymorphicProps<"div"> { }
7
8let { class: className, children, ...rest }: Props = $props()
9</script>
10
11<Sprawlify
12 as="div"
13 data-scope="alert"
14 data-part="action"
15 data-slot="alert-action"
16 class={cn("absolute top-2 right-2", className)}
17 {...rest}
18>
19 {@render children?.()}
20</Sprawlify>1<script lang="ts">
2import { cn } from "@/lib/utils"
3import { Sprawlify, type PolymorphicProps } from "@sprawlify/svelte"
4import type { Snippet } from "svelte"
5import type { HTMLAttributes } from "svelte/elements";
6
7interface Props extends HTMLAttributes<HTMLDivElement>, PolymorphicProps<"div"> { }
8
9let { class: className, children, ...rest }: Props = $props()
10</script>
11
12<Sprawlify
13 as="div"
14 data-scope="alert"
15 data-part="description"
16 data-slot="alert-description"
17 class={cn("text-muted-foreground text-sm text-balance md:text-pretty [&_p:not(:last-child)]:mb-4 [&_a]:hover:text-foreground [&_a]:underline [&_a]:underline-offset-3", className)}
18 {...rest}
19>
20 {@render children?.()}
21</Sprawlify>1<script lang="ts">
2import { cn } from "@/lib/utils"
3import { Sprawlify, type PolymorphicProps } from "@sprawlify/svelte"
4import type { HTMLAttributes } from "svelte/elements";
5
6interface Props extends HTMLAttributes<HTMLDivElement>, PolymorphicProps<"div"> { }
7
8let { class: className, children, ...rest }: Props = $props()
9</script>
10
11<Sprawlify
12 as="div"
13 data-scope="alert"
14 data-part="title"
15 data-slot="alert-title"
16 class={cn("font-medium group-has-[>svg]/alert:col-start-2 [&_a]:hover:text-foreground [&_a]:underline [&_a]:underline-offset-3", className)}
17 {...rest}
18>
19 {@render children?.()}
20</Sprawlify>1<script module lang="ts">
2import { cva, type VariantProps } from "class-variance-authority"
3
4export const alertVariants = cva("grid gap-0.5 rounded-lg border px-2.5 py-2 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4 w-full relative group/alert", {
5 variants: {
6 variant: {
7 default: "bg-card text-card-foreground",
8 destructive: "text-destructive bg-card *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current",
9 },
10 },
11 defaultVariants: { variant: "default" },
12})
13</script>
14
15<script lang="ts">
16import { cn } from "@/lib/utils"
17import { Sprawlify, type PolymorphicProps } from "@sprawlify/svelte"
18import type { HTMLAttributes } from "svelte/elements";
19
20interface Props extends HTMLAttributes<HTMLDivElement>, VariantProps<typeof alertVariants>, PolymorphicProps<"div"> { }
21
22let { class: className, variant = "default", children, ...rest }: Props = $props()
23</script>
24
25<Sprawlify
26 as="div"
27 data-scope="alert"
28 data-part="root"
29 data-slot="alert"
30 role="alert"
31 class={cn(alertVariants({ variant }), className)}
32 {...rest}
33>
34 {@render children?.()}
35</Sprawlify>1export { default as Alert, alertVariants } from "./alert.svelte";2export { default as AlertTitle } from "./alert-title.svelte";3export { default as AlertDescription } from "./alert-description.svelte";4export { default as AlertAction } from "./alert-action.svelte";5Update the import paths to match your project setup.
Usage
1import {
2 Alert,
3 AlertAction,
4 AlertDescription,
5 AlertTitle,
6} from "@/components/ui/alert"1<Alert>
2 <InfoIcon />
3 <AlertTitle>Heads up!</AlertTitle>
4 <AlertDescription>
5 You can add components and dependencies to your app using the cli.
6 </AlertDescription>
7 <AlertAction>
8 <Button variant="outline">Enable</Button>
9 </AlertAction>
10</Alert>Examples
Action
Use AlertAction to add a button or other action element to the alert.
1<script lang="ts">
2import {
3 Alert,
4 AlertAction,
Basic
A basic alert with an icon, title and description.
1<script lang="ts">
2import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
3import { CheckCircle2Icon } from "lucide-svelte"
4</script>
Custom Colors
You can customize the alert colors by adding custom classes such as bg-amber-50 dark:bg-amber-950 to the Alert component.
1<script lang="ts">
2import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
3import { AlertTriangleIcon } from "lucide-svelte"
4</script>
Destructive
Use variant="destructive" to create a destructive alert.
1<script lang="ts">
2import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
3import { AlertCircleIcon } from "lucide-svelte"
4</script>
Get PRO
Need premium blocks and templates? Upgrade to PRO and get access.