# Alert **📖 Live documentation:** https://cds.coinbase.com/components/overlay/Alert/ A dialog that communicates critical information and blocks user interaction. ## Import ```tsx import { Alert } from '@coinbase/cds-web/overlays/Alert' ``` ## Examples :::tip Accessibility tip (Web only) Alerts require an accessibility label, which we set to `title` by default. However, if there's other text that gives the user better context to the alert, then you can pass an element id to `accessibilityLabelledBy`. Alternatively, you may directly provide a contextual label to `accessibilityLabel`. ::: ### Default Alert Controlled using `visible` and `onRequestClose`. ```jsx live function DefaultAlertExample() { const [visible, setVisible] = useState(false); const toggleOn = () => setVisible(true); const toggleOff = () => setVisible(false); return ( console.log('preferred pressed')} dismissActionLabel="Cancel" onDismissActionPress={() => console.log('dismiss pressed')} /> ); } ``` ### Portal Alert Controlled programmatically using the `useAlert` hook. :::warning **Deprecated**: Inserting JSX into the DOM using a function in an event handler is an anti-pattern. This hook will be removed in future version. Use the `visible` and `onRequestClose` props instead ::: ```jsx live function PortalAlertExample() { function PortalAlert() { const alert = useAlert(); const showAlert = () => alert.open( console.log('Save pressed')} />, ); return ; } return ( ); } ``` ### Alert over Modal Alert displays on top of a `Modal`. You must pass stacked as a prop to `Alert` when it is used inside of a `Modal`. ```jsx live function AlertOnModalExample() { function AlertOnModal() { const { openModal, closeModal } = useModal(); const alert = useAlert(); const showAlert = () => alert.open( , ); const handlePress = () => { openModal( {loremIpsum} Save} secondaryAction={} /> , ); }; return ; } return ( ); } ``` ## Props | Prop | Type | Required | Default | Description | | --- | --- | --- | --- | --- | | `body` | `string` | Yes | `-` | Alert body/description | | `onRequestClose` | `() => void` | Yes | `-` | Callback function fired when modal is closed. | | `preferredActionLabel` | `string` | Yes | `-` | Label of the preferred action | | `title` | `string` | Yes | `-` | Alert title | | `visible` | `boolean` | Yes | `false false` | Controls visibility of the Modal | | `accessibilityLabelledBy` | `string` | No | `-` | On web, maps to aria-labelledby and lists the id(s) of the element(s) that label the element on which the attribute is set. On mobile (Android only), a reference to another element nativeID used to build complex forms. | | `actionLayout` | `horizontal \| vertical` | No | `horizontal` | Layout of the actions | | `disablePortal` | `boolean` | No | `-` | Disable React portal integration | | `dismissActionLabel` | `string` | No | `-` | Label of the dismiss action | | `key` | `Key \| null` | No | `-` | - | | `onDidClose` | `((() => void) & (() => void))` | No | `-` | Callback fired after the component is closed. | | `onDismissActionPress` | `(() => void)` | No | `-` | Callback function fired when the dismiss action is pressed | | `onPreferredActionPress` | `(() => void)` | No | `-` | Callback function fired when the preferred action is pressed | | `pictogram` | `PictogramName` | No | `-` | Illustration pictogram name for alert | | `preferredActionVariant` | `primary \| negative` | No | `primary` | Button variant of the preferred action | | `ref` | `((instance: ModalRefBaseProps \| null) => void) \| RefObject \| null` | No | `-` | - | | `stacked` | `boolean` | No | `-` | Indicating if Alert is stacked on top of Modal | | `testID` | `string` | No | `-` | Used to locate this element in unit and end-to-end tests. Under the hood, testID translates to data-testid on Web. On Mobile, testID stays the same - testID |