@patternfly/react-core
Version:
This library provides a set of common React components for use with the PatternFly reference implementation.
318 lines (286 loc) • 13.3 kB
Markdown
---
id: Alert
section: components
cssPrefix: pf-c-alert
propComponents: ['Alert', 'AlertActionCloseButton', 'AlertActionLink']
ouia: true
---
import './alert.css';
import UsersIcon from '@patternfly/react-icons/dist/esm/icons/users-icon';
import BoxIcon from '@patternfly/react-icons/dist/esm/icons/box-icon';
import DatabaseIcon from '@patternfly/react-icons/dist/esm/icons/database-icon';
import ServerIcon from '@patternfly/react-icons/dist/esm/icons/server-icon';
import LaptopIcon from '@patternfly/react-icons/dist/esm/icons/laptop-icon';
## Examples
### Types
```ts
import React from 'react';
import { Alert } from '@patternfly/react-core';
<React.Fragment>
<Alert title="Default alert title" />
<Alert variant="info" title="Info alert title" />
<Alert variant="success" title="Success alert title" />
<Alert variant="warning" title="Warning alert title" />
<Alert variant="danger" title="Danger alert title" />
</React.Fragment>
```
### Variations
```ts
import React from 'react';
import { Alert, AlertActionCloseButton, AlertActionLink } from '@patternfly/react-core';
<React.Fragment>
<Alert
variant="success"
title="Success alert title"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
actionLinks={
<React.Fragment>
<AlertActionLink onClick={() => alert('Clicked on View details')}>View details</AlertActionLink>
<AlertActionLink onClick={() => alert('Clicked on Ignore')}>Ignore</AlertActionLink>
</React.Fragment>
}
>
<p>Success alert description. This should tell the user more information about the alert.</p>
</Alert>
<Alert variant="success" title="Success alert title" actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}>
<p>
Success alert description. This should tell the user more information about the alert.{' '}
<a href="#">This is a link.</a>
</p>
</Alert>
<Alert
variant="success"
title="Success alert title"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
actionLinks={
<React.Fragment>
<AlertActionLink onClick={() => alert('Clicked on View details')}>View details</AlertActionLink>
<AlertActionLink onClick={() => alert('Clicked on Ignore')}>Ignore</AlertActionLink>
</React.Fragment>
}
/>
<Alert variant="success" title="Success alert title" actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />} />
<Alert variant="success" title="h1 Success alert title" titleHeadingLevel="h1" />
<Alert variant="success" title="h6 Success alert title" titleHeadingLevel="h6" />
</React.Fragment>
```
### Inline types
```ts
import React from 'react';
import { Alert } from '@patternfly/react-core';
<React.Fragment>
<Alert variant="default" isInline title="Default inline alert title" />
<Alert variant="info" isInline title="Info inline alert title" />
<Alert variant="success" isInline title="Success inline alert title" />
<Alert variant="warning" isInline title="Warning inline alert title" />
<Alert variant="danger" isInline title="Danger inline alert title" />
</React.Fragment>
```
### Inline variations
```ts
import React from 'react';
import { Alert, AlertActionCloseButton, AlertActionLink } from '@patternfly/react-core';
<React.Fragment>
<Alert
isInline
variant="success"
title="Success alert title"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
actionLinks={
<React.Fragment>
<AlertActionLink onClick={() => alert('Clicked on View details')}>View details</AlertActionLink>
<AlertActionLink onClick={() => alert('Clicked on Ignore')}>Ignore</AlertActionLink>
</React.Fragment>
}
>
<p>Success alert description. This should tell the user more information about the alert.</p>
</Alert>
<Alert isInline variant="success" title="Success alert title" actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}>
<p>
Success alert description. This should tell the user more information about the alert.{' '}
<a href="#">This is a link.</a>
</p>
</Alert>
<Alert
isInline
variant="success"
title="Success alert title"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
actionLinks={
<React.Fragment>
<AlertActionLink onClick={() => alert('Clicked on View details')}>View details</AlertActionLink>
<AlertActionLink onClick={() => alert('Clicked on Ignore')}>Ignore</AlertActionLink>
</React.Fragment>
}
/>
<Alert
isInline
variant="success"
title="Success alert title"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
/>
<Alert isInline variant="success" title="Success alert title" />
</React.Fragment>
```
### Inline plain types
```ts
import React from 'react';
import { Alert } from '@patternfly/react-core';
<React.Fragment>
<Alert variant="default" isInline isPlain title="Default inline alert title" />
<Alert variant="info" isInline isPlain title="Info inline alert title" />
<Alert variant="success" isInline isPlain title="Success inline alert title" />
<Alert variant="warning" isInline isPlain title="Warning inline alert title" />
<Alert variant="danger" isInline isPlain title="Danger inline alert title" />
</React.Fragment>
```
### Inline plain variations
It is not recommended to use an inline plain alert with actionClose nor actionLinks.
```ts
import React from 'react';
import { Alert, AlertActionCloseButton, AlertActionLink } from '@patternfly/react-core';
<Alert
isInline
isPlain
variant="success"
title="Success alert title"
>
<p>Success alert description. This should tell the user more information about the alert.</p>
</Alert>
```
### Expandable
It is not recommended to use an expandable alert within a toast Alert group. In such a case, the Alert could timeout before users would have time to interact and view the entire Alert.
```ts
import React from 'react';
import { Alert, AlertActionCloseButton, AlertActionLink } from '@patternfly/react-core';
<React.Fragment>
<Alert
isExpandable
variant="success"
title="Success alert title"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
actionLinks={
<React.Fragment>
<AlertActionLink onClick={() => alert('Clicked on View details')}>View details</AlertActionLink>
<AlertActionLink onClick={() => alert('Clicked on Ignore')}>Ignore</AlertActionLink>
</React.Fragment>
}
>
<p>Success alert description. This should tell the user more information about the alert.</p>
</Alert>
<Alert
isExpandable
isInline
variant="success"
title="Success alert title"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
actionLinks={
<React.Fragment>
<AlertActionLink onClick={() => alert('Clicked on View details')}>View details</AlertActionLink>
<AlertActionLink onClick={() => alert('Clicked on Ignore')}>Ignore</AlertActionLink>
</React.Fragment>
}
>
<p>Success alert description. This should tell the user more information about the alert.</p>
</Alert>
</React.Fragment>
```
### Static live region alert
```ts
import React from 'react';
import { Alert, AlertActionCloseButton } from '@patternfly/react-core';
<React.Fragment>
<Alert
isLiveRegion
variant="info"
title="Default live region configuration"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
>
This alert uses the recommended <code>isLiveRegion</code> prop to automatically set ARIA attributes and CSS classes.
</Alert>
<Alert
aria-live="assertive"
aria-relevant="additions text"
aria-atomic="true"
variant="info"
title="Customized live region"
actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />}
>
You can alternatively omit the <code>isLiveRegion</code> prop to specify ARIA attributes and CSS manually on
the containing element.
</Alert>
</React.Fragment>
```
### Dynamic live region alert
Alerts asynchronously appended into dynamic AlertGroups with isLiveRegion will be announced to assistive technology at the moment the change happens, following the strategy used for aria-atomic, which defaults to false. This means only changes of type "addition" will be announced.
```ts file="AlertDynamicLiveRegion.tsx"
```
### Async live region alert
This shows how an alert could be triggered by an asynchronous event in the application. Note that you can customize how the alert will be announced to assistive technology. See the alert accessibility tab for more information.
```ts file="AlertAsyncLiveRegion.tsx"
```
### Alert timeout
```ts
import React from 'react';
import { Alert, AlertActionLink, AlertGroup, Button } from '@patternfly/react-core';
const AlertTimeout: React.FunctionComponent = () => {
const [alerts, setAlerts] = React.useState<React.ReactNode[]>([]);
const onClick = () => {
const timeout = 8000;
setAlerts(prevAlerts => {
return [...prevAlerts,
<Alert title="Default timeout Alert" timeout={timeout} actionLinks={
<React.Fragment>
<AlertActionLink>View details</AlertActionLink>
<AlertActionLink>Ignore</AlertActionLink>
</React.Fragment>
}>
This alert will dismiss after {`${timeout / 1000} seconds`}
</Alert>
]
});
}
return (
<React.Fragment>
<Button variant="secondary" onClick={onClick}>Add alert</Button>
<Button variant="secondary" onClick={() => setAlerts([])}>Remove all alerts</Button>
<AlertGroup isLiveRegion>
{alerts}
</AlertGroup>
</React.Fragment>
);
};
```
### Truncate
```ts
import React from 'react';
import { Alert } from '@patternfly/react-core';
<React.Fragment>
<Alert variant="info" truncateTitle={1} title={`
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque cursus enim fringilla tincidunt. Proin lobortis aliquam dictum. Nam vel ullamcorper nulla, nec blandit dolor. Vivamus pellentesque neque justo, nec accumsan nulla rhoncus id. Suspendisse mollis, tortor quis faucibus volutpat, sem leo fringilla turpis, ac lacinia augue metus in nulla. Cras vestibulum lacinia orci. Pellentesque sodales consequat interdum. Sed porttitor tincidunt metus nec iaculis. Pellentesque non commodo justo. Morbi feugiat rhoncus neque, vitae facilisis diam aliquam nec. Sed dapibus vitae quam at tristique. Nunc vel commodo mi. Mauris et rhoncus leo.
`} />
<Alert variant="warning" truncateTitle={2} title={`
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque cursus enim fringilla tincidunt. Proin lobortis aliquam dictum. Nam vel ullamcorper nulla, nec blandit dolor. Vivamus pellentesque neque justo, nec accumsan nulla rhoncus id. Suspendisse mollis, tortor quis faucibus volutpat, sem leo fringilla turpis, ac lacinia augue metus in nulla. Cras vestibulum lacinia orci. Pellentesque sodales consequat interdum. Sed porttitor tincidunt metus nec iaculis. Pellentesque non commodo justo. Morbi feugiat rhoncus neque, vitae facilisis diam aliquam nec. Sed dapibus vitae quam at tristique. Nunc vel commodo mi. Mauris et rhoncus leo.
`} />
<Alert variant="danger" truncateTitle={3} title={`
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque cursus enim fringilla tincidunt. Proin lobortis aliquam dictum. Nam vel ullamcorper nulla, nec blandit dolor. Vivamus pellentesque neque justo, nec accumsan nulla rhoncus id. Suspendisse mollis, tortor quis faucibus volutpat, sem leo fringilla turpis, ac lacinia augue metus in nulla. Cras vestibulum lacinia orci. Pellentesque sodales consequat interdum. Sed porttitor tincidunt metus nec iaculis. Pellentesque non commodo justo. Morbi feugiat rhoncus neque, vitae facilisis diam aliquam nec. Sed dapibus vitae quam at tristique. Nunc vel commodo mi. Mauris et rhoncus leo.
`} />
</React.Fragment>
```
### Custom icons
```ts
import React from 'react';
import { Alert } from '@patternfly/react-core';
import UsersIcon from '@patternfly/react-icons/dist/esm/icons/users-icon';
import BoxIcon from '@patternfly/react-icons/dist/esm/icons/box-icon';
import DatabaseIcon from '@patternfly/react-icons/dist/esm/icons/database-icon';
import ServerIcon from '@patternfly/react-icons/dist/esm/icons/server-icon';
import LaptopIcon from '@patternfly/react-icons/dist/esm/icons/laptop-icon';
<React.Fragment>
<Alert customIcon={<UsersIcon />} title="Default alert title" />
<Alert customIcon={<BoxIcon />} variant="info" title="Info alert title" />
<Alert customIcon={<DatabaseIcon />} variant="success" title="Success alert title" />
<Alert customIcon={<ServerIcon />} variant="warning" title="Warning alert title" />
<Alert customIcon={<LaptopIcon />} variant="danger" title="Danger alert title" />
</React.Fragment>
```