Alert

An alert gives people critical information they need right away.

Pro component

Requires all-access to use the source code

Apple
Apple
import * as React from 'react';
import { Dimensions, Platform, ScrollView, View } from 'react-native';

import { Alert, AlertAnchor } from '../Alert';
import type { AlertMethods } from '../Alert/types';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/nativewindui/Avatar';
import { Button } from '@/components/nativewindui/Button';
import { Text } from '@/components/nativewindui/Text';
import { cn } from '@/lib/cn';

const MATERIAL_WIDTH = Dimensions.get('window').width - 80;

export default function AlertScreen() {
  const alertRef = React.useRef<AlertMethods>(null);
  const alertAnchorRef = React.useRef<AlertMethods>(null);

  React.useEffect(() => {
    const timeout = setTimeout(() => {
      showAlert();
    }, 350);

    return () => {
      clearTimeout(timeout);
    };
  }, []);

  function showAlert() {
    alertRef.current?.show();
  }

  function showConfirmActionAlert() {
    alertAnchorRef.current?.prompt({
      title: 'Password Confirmation',
      message: 'Please enter your password to confirm deleting this contact.',
      materialWidth: MATERIAL_WIDTH,
      prompt: {
        type: 'secure-text',
        keyboardType: 'default',
      },
      buttons: [
        {
          text: 'Cancel',
          style: 'cancel',
        },
        {
          text: 'Confirm',
          style: 'destructive',
        },
      ],
    });
  }

  return (
    <>
      <ScrollView contentInsetAdjustmentBehavior="automatic" className="pt-8">
        <Avatar alt="Zach Nugent Avatar" className="mx-auto mb-8 h-24 w-24">
          <AvatarImage />
          <AvatarFallback>
            <Text
              className={cn(
                'pt-1 font-medium text-white',
                Platform.OS === 'android' && 'dark:text-background'
              )}
              variant="largeTitle">
              ZN
            </Text>
          </AvatarFallback>
        </Avatar>

        <Section>
          <Row>
            <Text>Zach</Text>
          </Row>
          <Separator />
          <Row>
            <Text>Nugent</Text>
          </Row>
          <Separator />
          <Row>
            <Text>NativewindUI</Text>
          </Row>
        </Section>
        <Section>
          <Alert
            ref={alertRef}
            title="Delete contact?"
            message="This action cannot be undone."
            materialWidth={MATERIAL_WIDTH}
            buttons={[
              {
                text: 'Cancel',
                style: 'cancel',
              },
              {
                text: 'Delete',
                style: 'destructive',
                onPress: showConfirmActionAlert,
              },
            ]}>
            <Button variant="plain" size="none" className="justify-start">
              <Row>
                <Text className="text-destructive">Delete Contact</Text>
              </Row>
            </Button>
          </Alert>
        </Section>
      </ScrollView>
      <AlertAnchor ref={alertAnchorRef} />
    </>
  );
}

function Section({ children }: { children: React.ReactNode }) {
  return (
    <View
      className={cn(
        'bg-muted/15 border-muted/30 bg-card/70 dark:border-border/80 mb-8 border-b border-t',
        Platform.OS === 'ios' && 'dark:bg-muted/50 dark:border-muted/30'
      )}>
      {children}
    </View>
  );
}

function Row({ children }: { children: React.ReactNode }) {
  return <View className="px-4 py-2.5">{children}</View>;
}

function Separator() {
  return (
    <View
      className={cn(
        'bg-muted/30 dark:bg-border/80 h-px flex-1',
        Platform.OS === 'ios' && 'bg-muted/30 dark:bg-muted/50 ml-4'
      )}
    />
  );
}
Logo

Unlock All Pro Templates & Components

Elevate your app with powerful, native-feeling components and templates. Build faster with beautiful ready-to-use designs.

Get all-access

Usage

import { Alert } from '@/components/nativewindui/Alert';
<Alert
  title="Discard draft?"
  message="Some message that is kind of important since it is in an alert."
  buttons={[
    {
      text: 'Cancel',
      style: 'cancel',
      onPress: (text) => {
        console.log('Cancel Pressed', text);
      },
    },
    {
      text: 'OK',
      onPress: (text) => {
        console.log('OK Pressed', text);
      },
    },
  ]}>
  <Button>
    <Text>Show Alert</Text>
  </Button>
</Alert>

Props

Alert

PropTypeDescription
titlestringThe title of the alert.
buttons(Omit<AlertButton, "onPress"> & { onPress?: (text: AlertInputValue) => void })[]An array of button configurations for the alert.
messagestringThe message content of the alert.
promptobjectConfiguration for a prompt within the alert, if applicable.
prompt.typeExclude<AlertType, "default">The type of prompt to display.
prompt.defaultValuestringThe default value of the prompt input.
prompt.keyboardTypeKeyboardTypeThe keyboard type for the prompt input.
materialPortalHoststringThe portal host for Android elements.
materialIconPick<IconProps<'material'>, 'color' | 'size'> & { name: MaterialIconName }Configuration for a material icon in the alert.
materialWidthnumberThe width of the Android alert.
childrenReact.ReactNodeThe children elements to render inside the alert.
© Ronin Technologies LLC 2025