Installation
Install the dependencies
pnpm add cn class-variance-authorityCopy the component
Copy the following files into src/lib/components/ui/alert.
<script lang="ts">
import type { AlertProps } from './types.js';
import { cn } from 'cn';
import { alertVariants } from './variants.js';
let { children, class: classProp, variant, ...props }: AlertProps = $props();
</script>
<div data-slot="alert" role="alert" {...props} class={cn(alertVariants({ variant }), classProp)}>
{@render children?.()}
</div><script lang="ts">
import type { AlertActionProps } from './types.js';
import { cn } from 'cn';
let { children, class: classProp, ...props }: AlertActionProps = $props();
</script>
<div data-slot="alert-action" {...props} class={cn('absolute top-2 right-2', classProp)}>
{@render children?.()}
</div><script lang="ts">
import type { AlertDescriptionProps } from './types.js';
import { cn } from 'cn';
let { children, class: classProp, ...props }: AlertDescriptionProps = $props();
</script>
<div data-slot="alert-description" {...props} class={cn('text-muted-foreground text-sm text-balance md:text-pretty [&_p:not(:last-child)]:mb-4 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground', classProp)}>
{@render children?.()}
</div><script lang="ts">
import type { AlertTitleProps } from './types.js';
import { cn } from 'cn';
let { children, class: classProp, ...props }: AlertTitleProps = $props();
</script>
<div data-slot="alert-title" {...props} class={cn('font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground', classProp)}>
{@render children?.()}
</div>import type { HTMLAttributes } from 'svelte/elements';
import type { VariantProps } from 'class-variance-authority';
import type { alertVariants } from './variants.js';
type NativeAlertProps = HTMLAttributes<HTMLDivElement>;
export type AlertProps = NativeAlertProps & VariantProps<typeof alertVariants>;
export type AlertTitleProps = NativeAlertProps;
export type AlertDescriptionProps = NativeAlertProps;
export type AlertActionProps = NativeAlertProps;import { cva } from 'class-variance-authority';
export 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 group/alert relative w-full", {
variants: { variant: { default: 'bg-card text-card-foreground', destructive: 'text-destructive bg-card *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current' } },
defaultVariants: { variant: 'default' },
});export { default as Alert } from './Alert.svelte';
export { default as AlertTitle } from './AlertTitle.svelte';
export { default as AlertDescription } from './AlertDescription.svelte';
export { default as AlertAction } from './AlertAction.svelte';
export type { AlertProps, AlertTitleProps, AlertDescriptionProps, AlertActionProps } from './types.js';Update the import paths
Change the imports to match your project setup.
Usage
<script lang="ts">
import { Alert, AlertAction, AlertDescription, AlertTitle } from '$lib/components/ui/alert';
</script><Alert>
<AlertTitle>Heads up!</AlertTitle>
<AlertDescription>You can add components to your app by copying their source.</AlertDescription>
<AlertAction>
<Button variant="outline">Enable</Button>
</AlertAction>
</Alert>