Messages is used to display inline messages with various severities.
Message component is used to display inline messages mostly within forms.
import { Messages } from 'primereact/messages';
import { Message } from 'primereact/message';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/messages/messages.min.js"></script>
<script src="https://unpkg.com/primereact/message/message.min.js"></script>
A single message is specified by the Message interface in PrimeReact that defines various properties such as severity, summary and detail. Messages are displayed by using the show method on the ref of the Messages instance.
Note that for animations, messages requires react-transition-group package.
<Messages ref={messages}></Messages>
messages.current.show({severity: 'success', summary: 'Success Message', detail: 'Order submitted'});
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique id of the message. |
severity | string | null | Severity of the message. |
content | element | null | Template of the message. |
summary | element | null | Summary content of the message. |
detail | element | null | Detail content of the message. |
closable | boolean | true | Whether the message can be closed manually using the close icon. |
sticky | element | null | When enabled, message is not removed automatically. |
life | number | 3000 | Delay in milliseconds to close the message automatically. |
icon | IconType | Defaults to severiy icon | Defines the icon to display. |
There are four possible values for the severity of a message.
Show method accepts either a single message or an array of messages.
<Messages ref={messages}></Messages>
<Button onClick={showSuccess} label="Success" className="p-button-success" />
<Button onClick={showInfo} label="Info" className="p-button-info" />
<Button onClick={showWarn} label="Warn" className="p-button-warning" />
<Button onClick={showError} label="Error" className="p-button-danger" />
<Button onClick={showMultiple} label="Multiple" />
const showSuccess = () => {
messages.current.show({ severity: 'success', summary: 'Success Message', detail: 'Order submitted' });
}
const showInfo = () => {
messages.current.show({ severity: 'info', summary: 'Info Message', detail: 'PrimeReact rocks' });
}
const showWarn = () => {
messages.current.show({ severity: 'warn', summary: 'Warn Message', detail: 'There are unsaved changes' });
}
const showError = () => {
messages.current.show({ severity: 'error', summary: 'Error Message', detail: 'Validation failed' });
}
const showMultiple = () => {
messages.current.show([
{severity:'info', summary:'Message 1', detail:'PrimeReact rocks'},
{severity:'info', summary:'Message 2', detail:'PrimeReact rocks'},
{severity:'info', summary:'Message 3', detail:'PrimeFaces rocks'}
]);
}
clear() method removes all messages.
messages.current.clear();
replace(newMessages) method adds new messages after removing all old messages.
messages.current.replace(newMessages);
Messages are closable by default resulting in a close icon being displayed on top right corner. In order to disable closable messages, set closable to false.
messages.current.show({closable: false, severity: 'error', summary: 'Error Message', detail: 'Validation failed'});
Messages are cleared automatically after the timeout defined by life property which is 3 seconds by default. Use sticky mode to make them stay until they are manually removed.
//sticky
messages.current.show({ sticky: true, severity: 'error', summary: 'Error Message', detail: 'Validation failed' });
//automatically removed after 5 seconds
messages.current.show({ life: 5000, severity: 'error', summary: 'Error Message', detail: 'Validation failed' });
Message component is useful in cases where a single message needs to be displayed related to an element such as forms. It has two properties, severity and text of the message.
<h5>Inline Message CSS</h5>
<p>CSS helpers to display inline messages mostly within forms.</p>
<Message severity="info" text="PrimeNG Rocks"></Message>
<Message severity="success" text="Record Saved"></Message>
<Message severity="warn" text="Are you sure?"></Message>
<Message severity="error" text="Field is required"></Message>
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
className | string | null | Style class of the element. |
style | string | null | Inline style of the element. |
severity | string | null | Severity level of the message. |
text | any | null | Text of the message. |
style | string | null | Message text. |
content | element | null | Template of the message. |
icon | string | based on severity | Icon for the message. If not set it will default to severity icon. |
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
className | string | null | Style class of the element. |
style | string | null | Inline style of the element. |
transitionOptions | object | null | The properties of CSSTransition can be customized, except for "nodeRef" and "in" properties. |
Name | Parameters | Description |
---|---|---|
onRemove | message: Removed message | Callback to invoke when a message is removed. |
onClick | message: Clicked message | Callback to invoke when a message gets clicked. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-messages | Container element. |
p-messages-info | Container element when displaying info messages. |
p-messages-warn | Container element when displaying warning messages. |
p-messages-error | Container element when displaying error messages. |
p-messages-success | Container element when displaying success messages. |
p-messages-close | Close icon. |
p-messages-icon | Severity icon. |
p-messages-summary | Summary of a message. |
p-messages-detail | Detail of a message. |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.