SwiftUI.js
Components

Alert

Alert is a container that presents an alert dialog. Alert displays a modal dialog with a message and optional action buttons.

Alert is a container that presents an alert dialog. Alert displays a modal dialog with a message and optional action buttons.

Examples

Basic Usage

Preview unavailable for this example in the static docs build.

Show code
function DefaultAlert() {
  const [isVisible, setIsVisible] = useState(false)

  return (
    <>
      <Button onClick={() => setIsVisible(true)}>Show Alert</Button>
      <Alert
        title="Alert"
        message="This is an alert message"
        isVisible={isVisible}
        onDismiss={() => setIsVisible(false)}
      />
    </>
  )
}

Simple Alert

Preview unavailable for this example in the static docs build.

Show code
function SimpleAlert() {
  const [isVisible, setIsVisible] = useState(false)

  return (
    <>
      <Button onClick={() => setIsVisible(true)}>Show Simple Alert</Button>
      <Alert
        title="Notice"
        isVisible={isVisible}
        onDismiss={() => setIsVisible(false)}
      />
    </>
  )
}

With Custom Buttons

Preview unavailable for this example in the static docs build.

Show code
function WithCustomButtonsDemo() {
  const [isVisible, setIsVisible] = useState(false)

  return (
    <>
      <Button onClick={() => setIsVisible(true)}>Show Alert with Custom Buttons</Button>
      <Alert
        title="Delete Item"
        message="Are you sure you want to delete this item? This action cannot be undone."
        isVisible={isVisible}
        onDismiss={() => setIsVisible(false)}
        buttons={[
          { label: 'Cancel', action: () => setIsVisible(false), style: 'cancel' },
          { label: 'Delete', action: () => { setIsVisible(false); alert('Deleted!') }, style: 'destructive' }
        ]}
      />
    </>
  )
}

With Multiple Buttons

Preview unavailable for this example in the static docs build.

Show code
function WithMultipleButtonsDemo() {
  const [isVisible, setIsVisible] = useState(false)

  return (
    <>
      <Button onClick={() => setIsVisible(true)}>Show Alert with Multiple Buttons</Button>
      <Alert
        title="Choose Action"
        message="What would you like to do?"
        isVisible={isVisible}
        onDismiss={() => setIsVisible(false)}
        buttons={[
          { label: 'Cancel', action: () => setIsVisible(false), style: 'cancel' },
          { label: 'Save', action: () => { setIsVisible(false); alert('Saved!') } },
          { label: 'Delete', action: () => { setIsVisible(false); alert('Deleted!') }, style: 'destructive' }
        ]}
      />
    </>
  )
}

Warning Alert

Preview unavailable for this example in the static docs build.

Show code
function WarningAlert() {
  const [isVisible, setIsVisible] = useState(false)

  return (
    <>
      <Button onClick={() => setIsVisible(true)}>Show Warning</Button>
      <Alert
        title="Warning"
        message="This action may have unintended consequences. Please proceed with caution."
        isVisible={isVisible}
        onDismiss={() => setIsVisible(false)}
        buttons={[
          { label: 'Cancel', action: () => setIsVisible(false), style: 'cancel' },
          { label: 'Continue', action: () => setIsVisible(false) }
        ]}
      />
    </>
  )
}

API

PropTypeRequiredDescription
titlestringYesThe title of the alert.
messagestringNoThe message body of the alert.
isVisiblebooleanYesWhether the alert is visible.
onDismiss() => voidYesCallback fired when the alert should be dismissed.
buttonsIAlertButton[]NoThe buttons to display in the alert.

Inherits additional props from IBaseComponent.

Overview

Alert is a container that presents an alert dialog. Alert displays a modal dialog with a message and optional action buttons.

SwiftUI Correspondence: Similar to SwiftUI's alert() modifier.

Simple Alert

Alert without a message:

Notes

  • Alert requires isVisible and onDismiss props
  • Alert supports custom buttons with different styles
  • Button styles: 'default', 'cancel', 'destructive'
  • Alert automatically shows a backdrop
  • Alert is modal and blocks interaction with underlying content

Best Practices

  1. Clear messages: Use clear, concise messages
  2. Button labels: Use descriptive button labels
  3. Destructive actions: Use 'destructive' style for dangerous actions
  4. Dismiss handling: Always provide onDismiss handler
  5. Accessibility: Ensure alerts are accessible and keyboard navigable

On this page