# Alert

In-page message for important information, with info, success, warning, and danger variants.

- Category: Feedback
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/alert.json
- CLI: `npx markasui@latest add alert`

## When to use

Telling users something important about the page content, such as a form error or a nearly full quota.

- Use danger only for failures that need action
- For short feedback after an action use Toast, not Alert
- Do not stack more than two alerts in one area

## Code

### Blade

Save at `resources/views/components/markas/alert.blade.php`. No extra dependencies.

```blade
@props([
    'variant' => 'info',
    'title' => null,
])

@php
    $base = 'flex gap-3 rounded-lg border p-4 text-sm text-ink';
    $variants = [
        'info' => 'border-primary-line bg-primary-soft',
        'success' => 'border-success/30 bg-success/10',
        'warning' => 'border-warning/30 bg-warning/10',
        'danger' => 'border-danger/30 bg-danger/10',
    ];
    $iconBase = 'mt-0.5 size-4 shrink-0';
    $iconVariants = [
        'info' => 'text-primary',
        'success' => 'text-success',
        'warning' => 'text-warning',
        'danger' => 'text-danger',
    ];
    $icons = [
        'info' => 'M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM12 16v-4M12 8h.01',
        'success' => 'M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM8.5 12l2.5 2.5 4.5-5',
        'warning' => 'M12 3.5 2.5 20h19L12 3.5ZM12 10v4M12 17h.01',
        'danger' => 'M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM15 9l-6 6M9 9l6 6',
    ];
    $content = 'grid gap-1';
    $titleClass = 'font-semibold';
    $body = 'text-ink';
@endphp

<div {{ $attributes->merge(['role' => 'alert'])->class([$base, $variants[$variant]]) }}>
    <svg class="{{ $iconBase }} {{ $iconVariants[$variant] }}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="{{ $icons[$variant] }}" /></svg>
    <div class="{{ $content }}">
        @if ($title)
            <p class="{{ $titleClass }}">{{ $title }}</p>
        @endif
        <div class="{{ $body }}">{{ $slot }}</div>
    </div>
</div>
```

```blade
<x-markas.alert title="Update available" role="status">Version 2.1 can be installed at any time.</x-markas.alert>
<x-markas.alert variant="danger" title="Could not save">Check your internet connection and try again.</x-markas.alert>
```

### React

Save at `src/components/markas/Alert.tsx`. No extra dependencies.

```tsx
import type { ComponentProps, ReactNode } from "react";

const base = "flex gap-3 rounded-lg border p-4 text-sm text-ink";
const variants = {
  info: "border-primary-line bg-primary-soft",
  success: "border-success/30 bg-success/10",
  warning: "border-warning/30 bg-warning/10",
  danger: "border-danger/30 bg-danger/10",
};
const iconBase = "mt-0.5 size-4 shrink-0";
const iconVariants = {
  info: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const icons = {
  info: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM12 16v-4M12 8h.01",
  success: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM8.5 12l2.5 2.5 4.5-5",
  warning: "M12 3.5 2.5 20h19L12 3.5ZM12 10v4M12 17h.01",
  danger: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM15 9l-6 6M9 9l6 6",
};
const content = "grid gap-1";
const titleClass = "font-semibold";
const body = "text-ink";
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type AlertProps = Omit<ComponentProps<"div">, "title"> & {
  variant?: keyof typeof variants;
  title?: ReactNode;
};

export function Alert({
  variant = "info",
  title,
  role = "alert",
  className,
  children,
  ...props
}: AlertProps) {
  return (
    <div
      role={role}
      className={cx(base, variants[variant], className)}
      {...props}
    >
      <svg
        className={cx(iconBase, iconVariants[variant])}
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
        aria-hidden="true"
      >
        <path d={icons[variant]} />
      </svg>
      <div className={content}>
        {title && <p className={titleClass}>{title}</p>}
        <div className={body}>{children}</div>
      </div>
    </div>
  );
}
```

```tsx
import { Alert } from "@/components/markas/Alert";

<Alert title="Update available" role="status">
  Version 2.1 can be installed at any time.
</Alert>
<Alert variant="danger" title="Could not save">
  Check your internet connection and try again.
</Alert>
```

### Vue

Save at `src/components/markas/Alert.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    variant?: "info" | "success" | "warning" | "danger";
    title?: string;
  }>(),
  { variant: "info", title: undefined },
);

const base = "flex gap-3 rounded-lg border p-4 text-sm text-ink";
const variants = {
  info: "border-primary-line bg-primary-soft",
  success: "border-success/30 bg-success/10",
  warning: "border-warning/30 bg-warning/10",
  danger: "border-danger/30 bg-danger/10",
};
const iconBase = "mt-0.5 size-4 shrink-0";
const iconVariants = {
  info: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const icons = {
  info: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM12 16v-4M12 8h.01",
  success: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM8.5 12l2.5 2.5 4.5-5",
  warning: "M12 3.5 2.5 20h19L12 3.5ZM12 10v4M12 17h.01",
  danger: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM15 9l-6 6M9 9l6 6",
};
const content = "grid gap-1";
const titleClass = "font-semibold";
const body = "text-ink";
</script>

<template>
  <div role="alert" :class="[base, variants[props.variant]]">
    <svg
      :class="[iconBase, iconVariants[props.variant]]"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="1.5"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
    >
      <path :d="icons[props.variant]" />
    </svg>
    <div :class="content">
      <p v-if="props.title" :class="titleClass">{{ props.title }}</p>
      <div :class="body"><slot /></div>
    </div>
  </div>
</template>
```

```vue
<script setup lang="ts">
import Alert from "@/components/markas/Alert.vue";
</script>

<template>
  <Alert title="Update available" role="status">
    Version 2.1 can be installed at any time.
  </Alert>
  <Alert variant="danger" title="Could not save">
    Check your internet connection and try again.
  </Alert>
</template>
```

### Svelte

Save at `src/lib/components/markas/Alert.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLAttributes } from "svelte/elements";

  const base = "flex gap-3 rounded-lg border p-4 text-sm text-ink";
  const variants = {
    info: "border-primary-line bg-primary-soft",
    success: "border-success/30 bg-success/10",
    warning: "border-warning/30 bg-warning/10",
    danger: "border-danger/30 bg-danger/10",
  };
  const iconBase = "mt-0.5 size-4 shrink-0";
  const iconVariants = {
    info: "text-primary",
    success: "text-success",
    warning: "text-warning",
    danger: "text-danger",
  };
  const icons = {
    info: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM12 16v-4M12 8h.01",
    success: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM8.5 12l2.5 2.5 4.5-5",
    warning: "M12 3.5 2.5 20h19L12 3.5ZM12 10v4M12 17h.01",
    danger: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM15 9l-6 6M9 9l6 6",
  };
  const content = "grid gap-1";
  const titleClass = "font-semibold";
  const body = "text-ink";

  type Props = Omit<HTMLAttributes<HTMLDivElement>, "title"> & {
    variant?: keyof typeof variants;
    title?: string;
    children?: Snippet;
  };

  let {
    variant = "info",
    title,
    role = "alert",
    class: className,
    children,
    ...rest
  }: Props = $props();
</script>

<div {role} class={[base, variants[variant], className]} {...rest}>
  <svg
    class={[iconBase, iconVariants[variant]]}
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="1.5"
    stroke-linecap="round"
    stroke-linejoin="round"
    aria-hidden="true"
  >
    <path d={icons[variant]} />
  </svg>
  <div class={content}>
    {#if title}
      <p class={titleClass}>{title}</p>
    {/if}
    <div class={body}>{@render children?.()}</div>
  </div>
</div>
```

```svelte
<script lang="ts">
  import Alert from "$lib/components/markas/Alert.svelte";
</script>

<Alert title="Update available" role="status">
  Version 2.1 can be installed at any time.
</Alert>
<Alert variant="danger" title="Could not save">
  Check your internet connection and try again.
</Alert>
```

### Astro

Save at `src/components/markas/Alert.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

const base = "flex gap-3 rounded-lg border p-4 text-sm text-ink";
const variants = {
  info: "border-primary-line bg-primary-soft",
  success: "border-success/30 bg-success/10",
  warning: "border-warning/30 bg-warning/10",
  danger: "border-danger/30 bg-danger/10",
};
const iconBase = "mt-0.5 size-4 shrink-0";
const iconVariants = {
  info: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const icons = {
  info: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM12 16v-4M12 8h.01",
  success: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM8.5 12l2.5 2.5 4.5-5",
  warning: "M12 3.5 2.5 20h19L12 3.5ZM12 10v4M12 17h.01",
  danger: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM15 9l-6 6M9 9l6 6",
};
const content = "grid gap-1";
const titleClass = "font-semibold";
const body = "text-ink";

type Props = Omit<HTMLAttributes<"div">, "title"> & {
  variant?: keyof typeof variants;
  title?: string;
};

const {
  variant = "info",
  title,
  role = "alert",
  class: className,
  ...rest
} = Astro.props;
---

<div role={role} class:list={[base, variants[variant], className]} {...rest}>
  <svg
    class:list={[iconBase, iconVariants[variant]]}
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="1.5"
    stroke-linecap="round"
    stroke-linejoin="round"
    aria-hidden="true"
  >
    <path d={icons[variant]}></path>
  </svg>
  <div class={content}>
    {title && <p class={titleClass}>{title}</p>}
    <div class={body}><slot /></div>
  </div>
</div>
```

```astro
---
import Alert from "@/components/markas/Alert.astro";
---

<Alert title="Update available" role="status">
  Version 2.1 can be installed at any time.
</Alert>
<Alert variant="danger" title="Could not save">
  Check your internet connection and try again.
</Alert>
```

### Angular

Save at `src/app/markas/alert.component.ts`. No extra dependencies.

```ts
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from "@angular/core";

const base = "flex gap-3 rounded-lg border p-4 text-sm text-ink";
const variants = {
  info: "border-primary-line bg-primary-soft",
  success: "border-success/30 bg-success/10",
  warning: "border-warning/30 bg-warning/10",
  danger: "border-danger/30 bg-danger/10",
};
const iconBase = "mt-0.5 size-4 shrink-0";
const iconVariants = {
  info: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const icons = {
  info: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM12 16v-4M12 8h.01",
  success: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM8.5 12l2.5 2.5 4.5-5",
  warning: "M12 3.5 2.5 20h19L12 3.5ZM12 10v4M12 17h.01",
  danger: "M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM15 9l-6 6M9 9l6 6",
};
const content = "grid gap-1";
const titleClass = "font-semibold";
const body = "text-ink";

export type AlertVariant = keyof typeof variants;

/**
 * Host stays out of layout (display: contents). role, title, and class move to the alert box,
 * so the host attributes are cleared (a title on the host would become a tooltip).
 */
@Component({
  selector: "markas-alert",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { class: "contents", "[attr.role]": "null", "[attr.title]": "null" },
  template: `
    <div [attr.role]="role()" [class]="classes()">
      <svg
        [class]="iconClasses()"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="1.5"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
      >
        <path [attr.d]="icon()" />
      </svg>
      <div [class]="content">
        @if (title()) {
          <p [class]="titleClass">{{ title() }}</p>
        }
        <div [class]="body"><ng-content /></div>
      </div>
    </div>
  `,
})
export class Alert {
  readonly variant = input<AlertVariant>("info");
  readonly title = input<string>();
  readonly role = input("alert");
  readonly userClass = input("", { alias: "class" });

  protected readonly content = content;
  protected readonly titleClass = titleClass;
  protected readonly body = body;
  protected readonly classes = computed(() =>
    [base, variants[this.variant()], this.userClass()]
      .filter(Boolean)
      .join(" "),
  );
  protected readonly iconClasses = computed(
    () => `${iconBase} ${iconVariants[this.variant()]}`,
  );
  protected readonly icon = computed(() => icons[this.variant()]);
}
```

```ts
import { Component } from "@angular/core";
import { Alert } from "./markas/alert.component";

@Component({
  selector: "app-status",
  imports: [Alert],
  template: `
    <markas-alert title="Update available" role="status">
      Version 2.1 can be installed at any time.
    </markas-alert>
    <markas-alert variant="danger" title="Could not save">
      Check your internet connection and try again.
    </markas-alert>
  `,
})
export class Status {}
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `'info' \| 'success' \| 'warning' \| 'danger'` | info | Background, border, and icon color. |
| `title` | `string` | - | Bold heading above the body. Pass the body through the slot or children. |
| `role` | `'alert' \| 'status'` | alert | Native role attribute. Use status for non-urgent information so screen readers do not interrupt. |

## Accessibility

- Defaults to role alert, so screen readers announce an alert that appears after page load right away.
- Set role status for non-urgent information.
- The icon is decorative (aria-hidden). Meaning is carried by the title and text.
- Title and body use the ink color, so contrast meets AA in light and dark mode.

## Notes

- The default `role="alert"` is announced by screen readers right away. For non-urgent information, set `role="status"`.
- No JavaScript, so the Blade version does not need Alpine.js.
