# Alert

> An in-page status message with a type icon, optional actions and close button.

```tsx title="hero.tsx"
import { Alert, AlertDescription, AlertTitle } from '@langgenius/dify-ui/alert'

export function AlertHeroDemo() {
  return (
    <Alert type="info" className="w-full max-w-90">
      <AlertTitle>Some logs in this time range may have been archived.</AlertTitle>
      <AlertDescription>Archived logs are kept for 30 days and can be exported.</AlertDescription>
    </Alert>
  )
}
```

## Usage guidelines

- Use `Alert` for a status that stays relevant while the user works on the surrounding content, such as an archived range, a conflicting edit or a form rejection that belongs to no field. Feedback on an action that is over once it is seen is a `Toast`, and a decision the user must answer before continuing is an `AlertDialog`.
- `type` sets the icon, the halo and the default role. An `error` alert uses `role="alert"` and interrupts assistive technology; the other types use `role="status"` and wait for a pause.
- Pass `role` when the default does not fit. An error that is already present when the page loads, rather than the result of something the user did, takes a non-live role such as `note` so it does not interrupt.
- The icon comes from `type` and cannot be replaced. A banner that needs a product or business icon is not an `Alert`.
- Every alert has an `AlertTitle`. A one-sentence message uses the title alone; add `AlertDescription` for detail and `AlertActions` for buttons below the text, using small `Button`s.
- `AlertClose` is a stateless button with a required `aria-label`. The caller owns whether the alert is visible, whether the dismissal persists and where focus goes, because the focused close button leaves the page with the alert.

## Anatomy

```tsx
import {
  Alert,
  AlertActions,
  AlertClose,
  AlertDescription,
  AlertTitle,
} from '@langgenius/dify-ui/alert'
```

```tsx
<Alert type="warning">
  <AlertTitle />
  <AlertDescription />
  <AlertActions />
  <AlertClose aria-label="Dismiss" />
</Alert>
```

`Alert` renders the icon and halo for its `type` and lays out the content beside it. `AlertClose` sits at the inline end and the content reserves space for it.

## Examples

### Types

`info`, `success`, `warning` and `error` set the icon, the halo and the default role. These alerts are present when the page loads, so the error alert passes `role="note"`.

```tsx title="types.tsx"
import { Alert, AlertTitle } from '@langgenius/dify-ui/alert'

export function AlertTypesDemo() {
  return (
    <div className="flex w-full max-w-90 flex-col gap-3">
      <Alert type="info">
        <AlertTitle>Changes to this connection apply to every app that uses it.</AlertTitle>
      </Alert>
      <Alert type="success">
        <AlertTitle>The connection is ready.</AlertTitle>
      </Alert>
      <Alert type="warning">
        <AlertTitle>This connection was updated by someone else.</AlertTitle>
      </Alert>
      <Alert type="error" role="note">
        <AlertTitle>The last sync of this connection failed.</AlertTitle>
      </Alert>
    </div>
  )
}
```

### Actions

`AlertActions` places buttons below the title and description.

```tsx title="actions.tsx"
import { Alert, AlertActions, AlertDescription, AlertTitle } from '@langgenius/dify-ui/alert'
import { Button } from '@langgenius/dify-ui/button'

export function AlertActionsDemo() {
  return (
    <Alert type="warning" className="w-full max-w-90">
      <AlertTitle>Knowledge base index is out of date.</AlertTitle>
      <AlertDescription>
        Three documents changed since the last sync. Re-index to include the latest edits in
        retrieval.
      </AlertDescription>
      <AlertActions>
        <Button size="small" variant="primary">
          Re-index now
        </Button>
        <Button size="small">View changes</Button>
      </AlertActions>
    </Alert>
  )
}
```

### Dismissible

`AlertClose` only reports the click. The caller removes the alert, moves focus to the button that replaces it and decides when the alert comes back.

```tsx title="dismissible.tsx"
import { Alert, AlertClose, AlertTitle } from '@langgenius/dify-ui/alert'
import { Button } from '@langgenius/dify-ui/button'
import * as React from 'react'

export function AlertDismissibleDemo() {
  const [visible, setVisible] = React.useState(true)
  const focusOnMountRef = React.useRef(false)

  const toggle = (nextVisible: boolean) => {
    focusOnMountRef.current = true
    setVisible(nextVisible)
  }
  const focusOnMount = (node: HTMLElement | null) => {
    if (node && focusOnMountRef.current) {
      focusOnMountRef.current = false
      node.focus()
    }
  }

  if (!visible) {
    return (
      <Button ref={focusOnMount} onClick={() => toggle(true)}>
        Show notice again
      </Button>
    )
  }

  return (
    <Alert ref={focusOnMount} tabIndex={-1} type="info" className="w-full max-w-90">
      <AlertTitle>
        This sample app has limited trial messages. Create your own copy to keep using it.
      </AlertTitle>
      <AlertClose aria-label="Dismiss notice" onClick={() => toggle(false)} />
    </Alert>
  )
}
```

## API reference

### Alert

An in-page status message with a semantic icon and background halo. Renders a `<div>` element.

Sets `role="alert"` when `type="error"` and `role="status"` otherwise; pass `role` explicitly when an error is already present on load.

**Props**

| Prop   | Type                                          | Default | Description                                                              |
| ------ | --------------------------------------------- | ------- | ------------------------------------------------------------------------ |
| `type` | `'error' \| 'info' \| 'success' \| 'warning'` | —       | The tone of the message, which sets its icon, color, and default `role`. |

**Data attributes**

| Attribute   | Description                                                         |
| ----------- | ------------------------------------------------------------------- |
| `data-type` | The alert type, which sets the icon, the halo and the default role. |

### AlertActions

Accepts the props of the element it renders.

### AlertClose

A stateless close button for dismissing an alert; callers own visibility state. Renders a `<button>` element.

**Props**

| Prop         | Type     | Default | Description                                      |
| ------------ | -------- | ------- | ------------------------------------------------ |
| `aria-label` | `string` | —       | Names the close button for assistive technology. |

Inherits `className`, `render`, `style`, `nativeButton`, `focusableWhenDisabled` from [Base UI Button](https://base-ui.com/react/components/button).

**Data attributes**

| Attribute       | Description                          |
| --------------- | ------------------------------------ |
| `data-disabled` | Present when the button is disabled. |

### AlertDescription

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).

### AlertTitle

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).
