For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/en/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/en/components/message.md.

Message

Provide brief feedback about an operation. Use Notification when you need a title, detail or follow-up actions.

Basic usage

Wrap the application in ConfigProvider, then call useMessage() in a child component. Applications already using it need no additional provider.

import { ConfigProvider } from '@ifhover/leaf-ui';

export function Root() {
  return <ConfigProvider><App /></ConfigProvider>;
}
basic.tsx
import { Button, useMessage } from '@ifhover/leaf-ui';

export function MessageBasic() {
  const { message } = useMessage();
  return <Button onClick={() => message.info('Changes saved')}>Show message</Button>;
}

Examples

Semantic states

Use the shortcut methods to display success, information, warning and danger messages.

states.tsx
import { Button, Space, useMessage } from '@ifhover/leaf-ui';

export function MessageStates() {
  const { message } = useMessage();
  return (
    <Space wrap>
      <Button onClick={() => message.info('You have a new comment')}>Info</Button>
      <Button onClick={() => message.success('Changes saved')}>Success</Button>
      <Button onClick={() => message.warning('You have unsaved changes')}>Warning</Button>
      <Button onClick={() => message.danger('Unable to save. Please try again.')}>Error</Button>
    </Space>
  );
}

Manual dismissal

Set duration: 0 to keep a message visible and closable: true to provide a close button.

manual.tsx
import { Button, useMessage } from '@ifhover/leaf-ui';

export function MessageManual() {
  const { message } = useMessage();
  return (
    <Button
      onClick={() =>
        message.open({
          content: 'This message stays until you close it',
          duration: 0,
          closable: true,
        })
      }
    >
      Show persistent message
    </Button>
  );
}

Async updates

Use the key returned by loading() to replace the loading message with a result. A matching key replaces the options rather than merging them, and restarts the timer with the new duration.

async.tsx
import { Button, useMessage } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function MessageAsync() {
  const { message } = useMessage();
  const [saving, setSaving] = useState(false);
  async function save() {
    setSaving(true);
    const key = message.loading('Saving');
    await new Promise((resolve) => setTimeout(resolve, 1200));
    message.open({ key, type: 'success', content: 'Changes saved' });
    setSaving(false);
  }
  return (
    <Button loading={saving} onClick={save}>
      Save asynchronously
    </Button>
  );
}

Share between components

The two buttons below belong to separate components. Components under the same provider share a queue and can update the same message with a shared key. Nested ConfigProvider regions share that queue while keeping the calling component's theme and language.

shared.tsx
import { Button, Space, useMessage } from '@ifhover/leaf-ui';
import { useId } from 'react';

function StartAction({ messageKey }: { messageKey: string }) {
  const { message } = useMessage();
  return (
    <Button
      onClick={() => message.open({ key: messageKey, type: 'loading', content: 'Processing' })}
    >
      Start processing
    </Button>
  );
}

function ResultAction({ messageKey }: { messageKey: string }) {
  const { message } = useMessage();
  return (
    <Button
      onClick={() =>
        message.open({ key: messageKey, type: 'success', content: 'Processing complete' })
      }
    >
      Update result
    </Button>
  );
}

export function MessageShared() {
  const messageKey = useId();
  return (
    <Space wrap>
      <StartAction messageKey={messageKey} />
      <ResultAction messageKey={messageKey} />
    </Space>
  );
}

Messages remain after the calling component unmounts; ongoing work can still update them. Unmounting the entry provider removes its messages and timers.

API

Message API

For declarative rendering, handle onClose by setting open to false. afterClose runs after the exit animation.

PropertyTypeDefaultDescription
openbooleanRequiredControlled visibility
contentReactNodeRequiredMessage content
typeFeedbackType | 'error' | 'loading''info'Message type; error is a compatibility alias
durationnumber3 normally; 0 for loadingSeconds before automatic closing; zero or negative values keep it open. Hover or focus pauses the remaining time.
closablebooleanfalseClose button
onClose() => void—Called when closing is requested; declarative Message requires the application to set open to false.
afterClose() => void—Called after the closing animation and content unmount; does not replace updating controlled state in onClose.

useMessage API

Access the message queue managed by the current provider.

PropertyTypeDefaultDescription
messageMessageApi—Call the queue managed by the application provider

MessageApi

close(key) closes one message; close() closes every message in the shared queue.

PropertyTypeDefaultDescription
open(options: MessageOptions) => stringRequiredOpen or replace a message by key, returning its key.
success(content: ReactNode, duration?: number) => stringRequiredShow feedback of the specified type; error remains as a compatibility method
info(content: ReactNode, duration?: number) => stringRequiredShow feedback of the specified type; error remains as a compatibility method
warning(content: ReactNode, duration?: number) => stringRequiredShow feedback of the specified type; error remains as a compatibility method
danger(content: ReactNode, duration?: number) => stringRequiredShow feedback of the specified type; error remains as a compatibility method
error(content: ReactNode, duration?: number) => stringRequiredContent displayed instead of data rows after a request failure.
loading(content: ReactNode) => stringRequiredShow a loading message without automatic dismissal and return its key; the application updates or closes it.
close(key?: string) => voidRequiredClose a key; omitting it closes every message in the same Provider, not only this caller's messages.

MessageOptions

Timers pause when the pointer or focus enters a message. After resuming, they use the remaining duration.

PropertyTypeDefaultDescription
contentReactNodeRequiredMessage content
typeFeedbackType | 'error' | 'loading''info'Message type; error is a compatibility alias
durationnumber3 normally; 0 for loadingSeconds before automatic closing; zero or negative values keep it open. Hover or focus pauses the remaining time.
closablebooleanfalseShow a close button
keystringGeneratedUnique key within a Provider; opening the same key replaces that message and restarts its dismissal timer instead of appending.
onClose() => void—Called when closing is requested; declarative Message requires the application to set open to false.

MessageProvider API

Use MessageProvider instead of ConfigProvider for standalone message support, or add it explicitly to create an independent queue. Separate React applications and SSR requests have independent queues.

import { MessageProvider } from '@ifhover/leaf-ui';

export function Root() {
  return <MessageProvider><App /></MessageProvider>;
}
PropertyTypeDefaultDescription
childrenReactNode—Contents sharing this independent message queue; ConfigProvider already supplies one, reused by nested ConfigProviders.