HextaUI

Alert: Cancel a dismiss

Call event.preventDefault() in the close button’s onClick to keep the alert open, for example to ask for confirmation first.

"use client"

import * as React from "react"
import { IconAlertTriangle } from "@tabler/icons-react"

import {
  Alert,
  AlertClose,
  AlertDescription,
  AlertTitle,
} from "@/components/ui/alert"

export function AlertCancelDismiss() {
  const [confirmed, setConfirmed] = React.useState(false)

  return (
    <Alert variant="warning" className="max-w-lg">
      <IconAlertTriangle />
      <AlertTitle>Unsaved changes</AlertTitle>
      <AlertDescription>
        {confirmed
          ? "Click dismiss again to close."
          : "The first click is cancelled with event.preventDefault()."}
      </AlertDescription>
      <AlertClose
        onClick={(event) => {
          if (!confirmed) {
            event.preventDefault()
            setConfirmed(true)
          }
        }}
      />
    </Alert>
  )
}

This adds the example as components/examples/alert/cancel-dismiss.tsx, together with the Alert component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/alert-cancel-dismiss.json

To add only the component, follow the Alert installation.

Props in this example

The Alert props this example sets, from the API reference.

PropTypeDefault
variantColors the icon. destructive and warning are announced assertively.
"default" | "destructive" | "success" | "info" | "warning""default"
PropTypeDefault
onClickCall event.preventDefault() to keep the alert open.
(event: MouseEvent) => void–

Every prop is in the Alert API reference.

See them all, with keyboard support and accessibility notes, on the Alert page.