Inline alerts display a non-modal message associated with objects in a view. These are often used in form validation, providing a place to aggregate feedback related to multiple fields.
Payment Information
Auto focus
Use the autoFocus prop to focus the alert when it first renders. This is useful for displaying alerts that need immediate attention, such as form submission errors.
import {InlineAlert, Heading, Content} from '@react-spectrum/s2/InlineAlert';
import {Button} from '@react-spectrum/s2/Button';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {useState} from 'react';
function Example() {
let [shown, setShown] = useState(false);
return (
<div className={style({display: 'flex', flexDirection: 'column', gap: 16, alignItems: 'center'})}>
<Button variant="primary" onPress={() => setShown(!shown)}>
{shown ? 'Hide Alert' : 'Show Alert'}
</Button>
{shown &&
<InlineAlert variant="negative" autoFocus>
<Heading>Error</Heading>
<Content>There was an error processing your request. Please try again.</Content>
</InlineAlert>
}
</div>
);
}
API
<InlineAlert>
<Heading />
<Content />
</InlineAlert>
InlineAlert
| Name | Type | Default |
|---|---|---|
children | ReactNode | Default: — |
The contents of the Inline Alert. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
variant | 'informative'
| 'positive'
| 'notice'
| 'negative'
| 'neutral' | Default: neutral
|
The semantic tone of a Inline Alert. | ||
fillStyle | 'border'
| 'subtleFill'
| 'boldFill' | Default: border
|
The visual style of the Inline Alert. | ||
autoFocus | boolean | Default: — |
Whether to automatically focus the Inline Alert when it first renders. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||