# Alert

Pesan penting di dalam halaman dengan varian info, success, warning, dan danger.

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

## Kapan dipakai

Memberi tahu hal penting yang berkaitan dengan isi halaman, misalnya error form atau kuota hampir habis.

- Gunakan danger hanya untuk kegagalan yang butuh tindakan
- Untuk umpan balik singkat setelah aksi gunakan Toast, bukan Alert
- Jangan menumpuk lebih dari dua alert di satu area

## Kode

### Blade

Simpan di `resources/views/components/markas/alert.blade.php`. Tanpa dependensi tambahan.

```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="Pembaruan tersedia" role="status">Versi 2.1 bisa dipasang kapan saja.</x-markas.alert>
<x-markas.alert variant="danger" title="Gagal menyimpan">Periksa koneksi internet lalu coba lagi.</x-markas.alert>
```

### React

Simpan di `src/components/markas/Alert.tsx`. Tanpa dependensi tambahan.

```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="Pembaruan tersedia" role="status">
  Versi 2.1 bisa dipasang kapan saja.
</Alert>
<Alert variant="danger" title="Gagal menyimpan">
  Periksa koneksi internet lalu coba lagi.
</Alert>
```

### Vue

Simpan di `src/components/markas/Alert.vue`. Tanpa dependensi tambahan.

```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="Pembaruan tersedia" role="status">
    Versi 2.1 bisa dipasang kapan saja.
  </Alert>
  <Alert variant="danger" title="Gagal menyimpan">
    Periksa koneksi internet lalu coba lagi.
  </Alert>
</template>
```

### Svelte

Simpan di `src/lib/components/markas/Alert.svelte`. Tanpa dependensi tambahan.

```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

Simpan di `src/components/markas/Alert.astro`. Tanpa dependensi tambahan.

```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

Simpan di `src/app/markas/alert.component.ts`. Tanpa dependensi tambahan.

```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

| Nama | Tipe | Default | Keterangan |
| --- | --- | --- | --- |
| `variant` | `'info' \| 'success' \| 'warning' \| 'danger'` | info | Warna latar, border, dan ikon. |
| `title` | `string` | - | Judul tebal di atas isi. Isi alert lewat slot atau children. |
| `role` | `'alert' \| 'status'` | alert | Atribut role native. Pakai status untuk info yang tidak mendesak agar pembaca layar tidak menyela. |

## Aksesibilitas

- Default role alert, jadi pembaca layar langsung membacakan alert yang muncul setelah halaman dimuat.
- Isi role status untuk informasi yang tidak mendesak.
- Ikon hanya hiasan (aria-hidden), makna tetap dibawa oleh judul dan teks.
- Judul dan isi memakai warna ink sehingga kontras AA di mode terang dan gelap.

## Catatan

- Default `role="alert"` langsung dibacakan pembaca layar. Untuk info yang tidak mendesak, isi `role="status"`.
- Tanpa JavaScript, jadi versi Blade tidak butuh Alpine.js.
