Introducing PrimeReact v11-alpha 🎉Discover Now

Message

An unstyled message component for displaying inline notifications with close and auto-dismiss support.

Build fully custom inline messages with complete control over layout and styling.

Upgrade now and save 5%.

basic-demo

Pre-styled Versions

Choose a pre-styled library to use Message with ready-made designs.
For hook-based usage without components, see the Headless API.

Features#

  • Compound component API with five sub-components: Root, Content, Icon, Text, Close
  • Auto-close with configurable life duration
  • Close callback on dismissal

Usage#

import { Message } from 'primereact/message';
<Message.Root>
    <Message.Content>
        <Message.Icon />
        <Message.Text></Message.Text>
        <Message.Close />
    </Message.Content>
</Message.Root>

Behavior#

Polymorphic Rendering#

Use as on any sub-component to change the rendered HTML element.

<Message.Root as="section"></Message.Root>
<Message.Text as="span"></Message.Text>

Default elements: Root=div, Content=div, Icon=span, Text=p, Close=button.

Render Function Children#

Sub-components accept a render function as children, providing access to the component instance.

<Message.Text>{(instance) => <span>{instance.message?.state.visible ? 'Visible' : 'Hidden'}</span>}</Message.Text>

Pass Through#

Some parts may not be visible in the preview depending on the component's current state.

Loading...
/* Select a part to see its CSS selector for custom styling */

API#

MessageRoot#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: MessageRootInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: MessageRootInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceMessageRootInstance—
The instance to pass to the component.
ptSafeRecord<MessageRootPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: MessageRootInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
lifenumber—
Delay in milliseconds to close the message automatically.
onClose() => void—
Callback to invoke when the message is closed.
severity"secondary" | "info" | "success" | "warn" | "contrast" | "error"info
Severity level of the message.
variant"outlined" | "simple"undefined
Specifies the variant of the component.
size"small" | "large"—
Defines the size of the component.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"message"
data-part"root"

Defines passthrough(pt) options of Message component.

labeltypedescription
rootMessageRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.
contentMessageRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the content's DOM element.
iconMessageRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the icon's DOM element.
textMessageRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the text's DOM element.
closeMessageRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the close's DOM element.

MessageContent#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: MessageContentInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: MessageContentInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceMessageContentInstance—
The instance to pass to the component.
ptSafeRecord<MessageContentPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: MessageContentInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"message"
data-part"content"

Defines passthrough(pt) options of MessageContent component.

labeltypedescription
rootMessageContentPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.

MessageIcon#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: MessageIconInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: MessageIconInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceMessageIconInstance—
The instance to pass to the component.
ptSafeRecord<MessageIconPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: MessageIconInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"message"
data-part"icon"

Defines passthrough(pt) options of MessageIcon component.

labeltypedescription
rootMessageIconPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.

MessageText#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: MessageTextInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: MessageTextInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceMessageTextInstance—
The instance to pass to the component.
ptSafeRecord<MessageTextPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: MessageTextInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"message"
data-part"text"

Defines passthrough(pt) options of MessageText component.

labeltypedescription
rootMessageTextPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.

MessageClose#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: MessageCloseInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: MessageCloseInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceMessageCloseInstance—
The instance to pass to the component.
ptSafeRecord<MessageClosePassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: MessageCloseInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"message"
data-part"close"

Defines passthrough(pt) options of MessageClose component.

labeltypedescription
rootMessageClosePassThroughType<HTMLAttributes<HTMLButtonElement>>Used to pass attributes to the root's DOM element.

Accessibility#

Screen Reader#

Message uses the alert role which implicitly sets aria-live="assertive" and aria-atomic="true", ensuring the message content is announced immediately by screen readers. The close button is a native <button> element — provide an aria-label for accessible labeling.

Keyboard Support#

KeyFunction
tabMoves focus to the close button.
enterCloses the message.
spaceCloses the message.