> Leaf UI 0.17.3 · Package: @ifhover/leaf-ui · Source: c3221834f84f2f48481a2054026b713456f8e5dc
> Browser documentation: https://ifhover.github.io/leaf-ui/en/components/message.html

> 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.

# Message

Provide brief feedback about an operation. Use [Notification](https://ifhover.github.io/leaf-ui/en/components/notification.md) when you need a title, detail or follow-up actions.


## Basic usage

Wrap the application in [ConfigProvider](https://ifhover.github.io/leaf-ui/en/components/config-provider.md), then call `useMessage()` in a child component. Applications already using it need no additional provider.

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

export function Root() {
  return <ConfigProvider><App /></ConfigProvider>;
}
```


Show message

basic.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/message/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>;
}
```

Expand code


## Examples

### Semantic states

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


InfoSuccessWarningError

states.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/message/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>
  );
}
```

Expand code

### Manual dismissal

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


Show persistent message

manual.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/message/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>
  );
}
```

Expand code

[](#)
### 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.


Save asynchronously

async.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/message/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>
  );
}
```

Expand code

### 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.


Start processingUpdate result

shared.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/message/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>
  );
}
```

Expand code

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.

| Property     | Type                                   | Default                   | Description                                                                                                       |
| ------------ | -------------------------------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `open`       | `boolean`                              | Required                  | Controlled visibility                                                                                             |
| `content`    | `ReactNode`                            | Required                  | Message content                                                                                                   |
| `type`       | `FeedbackType \| 'error' \| 'loading'` | `'info'`                  | Message type; `error` is a compatibility alias                                                                    |
| `duration`   | `number`                               | 3 normally; 0 for loading | Seconds before automatic closing; zero or negative values keep it open. Hover or focus pauses the remaining time. |
| `closable`   | `boolean`                              | `false`                   | Close 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.

| Property  | Type                        | Default | Description                                        |
| --------- | --------------------------- | ------- | -------------------------------------------------- |
| `message` | [`MessageApi`](#messageapi) | —       | Call the queue managed by the application provider |

### MessageApi

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

| Property  | Type                                                         | Default  | Description                                                                                                  |
| --------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------------------------------------------------------ |
| `open`    | `(options: `[`MessageOptions`](#messageoptions)`) => string` | Required | Open or replace a message by key, returning its key.                                                         |
| `success` | `(content: ReactNode, duration?: number) => string`          | Required | Show feedback of the specified type; `error` remains as a compatibility method                               |
| `info`    | `(content: ReactNode, duration?: number) => string`          | Required | Show feedback of the specified type; `error` remains as a compatibility method                               |
| `warning` | `(content: ReactNode, duration?: number) => string`          | Required | Show feedback of the specified type; `error` remains as a compatibility method                               |
| `danger`  | `(content: ReactNode, duration?: number) => string`          | Required | Show feedback of the specified type; `error` remains as a compatibility method                               |
| `error`   | `(content: ReactNode, duration?: number) => string`          | Required | Content displayed instead of data rows after a request failure.                                              |
| `loading` | `(content: ReactNode) => string`                             | Required | Show a loading message without automatic dismissal and return its key; the application updates or closes it. |
| `close`   | `(key?: string) => void`                                     | Required | Close 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.

| Property   | Type                                   | Default                   | Description                                                                                                                     |
| ---------- | -------------------------------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `content`  | `ReactNode`                            | Required                  | Message content                                                                                                                 |
| `type`     | `FeedbackType \| 'error' \| 'loading'` | `'info'`                  | Message type; `error` is a compatibility alias                                                                                  |
| `duration` | `number`                               | 3 normally; 0 for loading | Seconds before automatic closing; zero or negative values keep it open. Hover or focus pauses the remaining time.               |
| `closable` | `boolean`                              | `false`                   | Show a close button                                                                                                             |
| `key`      | `string`                               | Generated                 | Unique 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.

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

export function Root() {
  return <MessageProvider><App /></MessageProvider>;
}
```

| Property   | Type        | Default | Description                                                                                                             |
| ---------- | ----------- | ------- | ----------------------------------------------------------------------------------------------------------------------- |
| `children` | `ReactNode` | —       | Contents sharing this independent message queue; ConfigProvider already supplies one, reused by nested ConfigProviders. |
