Message

A message is a short, light notice that appears at the top of the page after an action ("Saved", "Upload failed", "Loading…") and goes away by itself. It takes no focus and does not stop the user. You open it with a call from code, from anywhere, not by writing an element in a template.

Import
import { message, MessageHost } from 'svelte-ant-design';
Links
src/lib/Message Report an issue Component README

When to use

  • Use it to give feedback on an action: a success, an error, a warning, or work that is in progress.
  • A message is for short feedback that does not block anything. A decision the user must make needs a dialog, and a longer text with a title a notification (both are planned).
  • A message goes away by itself, so do not put information in it that the user must be able to find again.

Examples

Basic

A call from anywhere in your code shows a message at the top of the page: no setup and no markup. The library makes a layer on the page by itself and removes it when the last message has gone. Note: the call returns a function that closes the message and is also thenable, so await message.info(…) waits until the message closes.

Types

Five kinds: info, success, error, warning and loading. Each has a method, and message.open({ type }) takes the kind as an option. A screen reader reads error and warning at once, and the others when it has finished.

duration 1 (seconds; the default is 3)
duration 6
duration 0: until it is closed from code

Duration

duration is in seconds (default 3). 0 keeps the message until it is closed. The function that a call returns closes the message.

Update by key

A call with the key of an open message updates it in place: here a loading message turns into a success one. The box keeps its place and its height, and a screen reader reads the new text.

Sequence

The returned function is thenable: .then runs when the message starts to close, so the next message comes in as the first one goes. For the same reason await message.info(…) waits for the close.

content: string, number, snippet
content: a snippet with a button Undo pressed 0 times

Content

content is a string, a number (0 is shown) or a snippet. A snippet can hold a link or a button. It is drawn in the layer, so it sees the context of the place where the host is, not of the component that wrote it.

Icon

icon replaces the icon of the type: a string (text or an emoji) or a snippet; false removes it. The icon is decorative: a screen reader never reads it.

class, style: on the box
classNames, styles: the parts (root, wrapper, icon, title, close)

Classes and styles

class and style go on the box. classNames and styles address its parts: root, wrapper, icon, title and close. The message is drawn outside the component that opens it, so a class that styles it has to be global.

pauseOnHover (the default), duration 3: keep the pointer over it
pauseOnHover: false
closable, duration 3: Tab into it, and it waits for you

Pause on hover and focus

The timer waits while the pointer (a mouse or a pen) is over the box, while focus is inside it, and while the page is hidden; it goes on with the time that was left. pauseOnHover: false turns off the first. A touch never holds it.

clicked 0, closed 0

Click and close callbacks

onclick is for a mouse or a finger: the box cannot be reached with the keyboard, so put a button in the content for an action. onclose is called once, when the message starts to close, however it was closed.

closable, duration 0: stays until the button is pressed
closable, duration 3

Close button

closable adds a close button (its name is labels.close). Enter and Space press it, and Escape closes the message that has focus inside it. It lets a user dismiss a message that stays (duration: 0). Not in Ant Design.

maxCount not set
The oldest closes to make room: 0 opened

Maximum count

maxCount limits the open messages: a new one closes the oldest to make room. It is a setting of message.config(); the switch sets it and takes it back.

top: 8 (px)

Distance from the top

top is the distance of the first message from the top of the window: a number (pixels) or any CSS length. The default is 8.

stack off

Stack

stack (a setting): the newest message is the first row. After threshold messages (3) the others lie behind it as a pile; the pointer or focus opens the pile, and every timer waits while it is open.

<MessageHost top={72} closable labels={...} /> not mounted

MessageHost

<MessageHost> draws the messages where you put it. Its props are settings and win over message.config(); without one the library makes its own layer. Only one host is active at a time. labels is where translations of the hidden type word and of the close button go.

a modal dialog
container: not set (the body) Press to see where the layer goes.
container: the dialog Press to see where the layer goes.

Container

container is the element that the layer is put in (the body by default). The layer stays position: fixed, so the message is still drawn at the window; what changes is where the layer sits in the page. That matters for a native modal <dialog>: it is shown over everything outside of it, so a layer in the body is covered by it, and a layer in the dialog stands above it and can be used. (The library's Modal does not need this: the layer stays above it.) The setting belongs to message.config() or to a MessageHost, so it applies to every call on the page while it is in force; this example gives it back when the dialog closes.

API

Options of a message

PropertyDescriptionTypeDefault
typeThe kind. Only for message.open; the typed methods fix it.'info' | 'success' | 'error' | 'warning' | 'loading''info'
contentThe text or the content: a string, a number (0 is shown) or a snippet.string | number | Snippet—
durationSeconds before the message goes. 0 keeps it until it is closed. A negative or not finite value falls back to the default, with a warning while developing.number3
iconReplaces the icon of the type: a string (shown as text), a snippet, or false for none.string | Snippet | falsethe icon of the type
keyA call with the key of an open message updates that message in place (new type, content and options, the timer starts again). Without one a key is generated.string | numbergenerated
pauseOnHoverHolds the timer while the pointer (a mouse or a pen) is over the box. Focus inside and a hidden page hold it whatever this says.booleantrue
closableShows a close button. Ant Design has no such option.booleanfalse
classExtra classes of the box.string—
styleInline style of the box: a string, or an object of CSS properties (kebab-case or custom properties; numbers are written as they are).string | Record<string, string | number>—
classNamesExtra classes for the parts root, wrapper, icon, title and close. A value here wins over the one of the host and of message.config().{ root?, wrapper?, icon?, title?, close? }—
stylesInline styles for the same parts, as objects.{ root?, wrapper?, icon?, title?, close? }—
onclickCalled with the event when the box is clicked, not when its close button is. The box cannot be reached with the keyboard: put a button in the content for an action.(event: MouseEvent) => void—
oncloseCalled once, when the message starts to close, however it was closed (the timer, the returned function, destroy, maxCount, the close button, Escape).() => void—

Methods

PropertyDescriptionTypeDefault
message.open(options)Opens a message of the kind options.type (default info), or updates the open one with the same key.MessageHandle—
message.info | success | error | warning | loading(content, duration?, onclose?)One method for each kind. Without a window (on a server) every call does nothing and returns an inert handle.MessageHandle—
message.info | success | error | warning | loading(options)The same with an object of options (without type).MessageHandle—
message.destroy(key?)Closes the message with this key, or every message. Each one runs its normal exit and its onclose.void—
message.config(options)Sets defaults for every message from now on (the settings table below). A setting given as { top: undefined } goes back to its default.void—
handle()What a call returns: a function that closes the message. Closing a message that is closing or closed does nothing.() => void—
handle.then(onfulfilled)The handle is thenable: the promise resolves when the message starts to close. await handle therefore waits for the close.PromiseLike<void>—

Settings: message.config() and the props of MessageHost

PropertyDescriptionTypeDefault
topDistance of the first message from the top of the window. A number is pixels. Without it, the token --ads-message-top (8px).number | string8
durationDefault duration of every message, in seconds.number3
maxCountAt most this many open messages: the oldest closes to make room. A value below 1 or not finite is ignored.number—
containerWhere the layer is moved to: an element, or a function that returns one. Default document.body.HTMLElement | (() => HTMLElement)document.body
closableDefault of closable for every message.booleanfalse
labelsThe word a screen reader hears before the text (info, success, error, warning, loading: "Error: Upload failed") and the name of the close button (close). Pass a translation; an empty string removes a type word.Partial<Record<'info' | 'success' | 'error' | 'warning' | 'loading' | 'close', string>>'Info', 'Success', 'Error', 'Warning', 'Loading', 'Close'
classNamesDefaults for the parts of every message, and the two parts of the layer: list (the fixed layer) and listContent (the column of rows).{ root?, wrapper?, icon?, title?, close?, list?, listContent? }—
stylesThe same for inline styles, as objects.{ root?, wrapper?, icon?, title?, close?, list?, listContent? }—
stacktrue, or { threshold }: the newest message is the first row, and past the threshold (3) the others lie behind it as a pile that the pointer or focus opens. React 6.4.0 has it, Vue and Angular do not.boolean | { threshold?: number }false
Differences from Ant Design: the layer is made by the library (the React useMessage and contextHolder are replaced by <MessageHost>); a role per type and a hidden type word (React: role="alert" for all, Vue and Angular: none); closable is new, and the timer also waits for focus and a hidden page; duration is in seconds as in React and Vue (Angular: milliseconds); stack is in React 6.4.0 only. Not taken: the Vue warn alias, prefixCls and rtl (the direction is inherited). API inspired by Ant Design (MIT); the implementation is original for Svelte.

Design tokens

TokenDescriptionDefault
--ads-message-z-indexLayer of the messages, above every popup (the tooltip is 1070).2010
--ads-message-topDistance of the first message from the top, when top is not set.8px
--ads-message-gapGap between two messages.16px
--ads-message-margin-inlineSpace kept free at the left and right edge of the window.24px
--ads-message-bgBackground of the box. In the dark theme #1f1f1f.#ffffff
--ads-message-colorText color. In the dark theme 85% white.rgba(0, 0, 0, 0.88)
--ads-message-paddingPadding of the box.9px 12px
--ads-message-line-heightHeight of a line of text, and of the icon, so the box is 40px high with or without an icon.22px
--ads-message-radiusCorner radius of the box.8px
--ads-message-shadowShadow of the box.0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05)
--ads-message-icon-sizeSize of the icon.16px
--ads-message-icon-gapGap between the icon, the text and the close button.8px
--ads-message-icon-infoColor of the info and loading icons.#1677ff
--ads-message-icon-successColor of the success icon. tokens-accessible.css darkens it to #389e0d (3:1 on the white box).#52c41a
--ads-message-icon-errorColor of the error icon.#ff4d4f
--ads-message-icon-warningColor of the warning icon. tokens-accessible.css darkens it to #c27c00.#faad14
--ads-message-close-sizeSize of the close button: 24px, the smallest target of WCAG 2.5.8.24px
--ads-message-close-colorColor of the close button (4.5:1 on the box).rgba(0, 0, 0, 0.65)
--ads-message-close-color-hoverColor of the close button on hover.rgba(0, 0, 0, 0.88)
--ads-message-close-bgBackground of the close button at rest: it shows where the button is.rgba(0, 0, 0, 0.04)
--ads-message-close-bg-hoverBackground of the close button on hover.rgba(0, 0, 0, 0.1)
--ads-message-motion-offsetHow far above its place a message starts to come in and ends to leave.64px
--ads-message-stack-offsetStep between the rows of a pile.8px
--ads-message-stack-scale-stepHow much smaller each row of a pile is than the one in front of it.0.06

Accessibility

  • error and warning are role="alert" and are read out at once; info, success and loading are role="status" and wait for the reader to finish. Ant Design React gives role="alert" to all five, Vue and Angular give none.
  • The icon is decorative. A hidden word before the text names the type ("Error: Upload failed"), so the type is not only a color and a shape. Pass translations in labels: the library has no locale system yet and the default is English.
  • A message never takes focus. Without closable nothing in it can be focused; with it, only the close button can (24px, the target size of WCAG 2.5.8). Escape closes the message that has focus inside it.
  • The timer waits while the pointer is over the message, while focus is inside it and while the page is hidden. A message that must stay needs duration: 0, and closable lets the user dismiss it.
  • The layer is the last element of its container, so the keyboard reaches a close button only after the rest of the page, and there is no shortcut that moves focus to the messages. A screen reader reads a message when it opens.
  • Contrast: the text is about 15:1 on the box, and the type icons are held to 3:1 (in the dark theme, and in the light theme with tokens-accessible.css, which darkens the green and the amber).
  • In forced-colors modes such as Windows High Contrast the box and the close button are drawn with a border. The motion (the box comes in from above and fades) is off under prefers-reduced-motion; the timer is the same.
  • Checked with a real screen reader (Orca with Firefox): it reads the role, the type word, the text, the new text after an update and the name of the close button. Not checked: whether a screen reader announces a message when it opens (the Orca setup does not speak even a plain role="alert"), NVDA, JAWS, VoiceOver, a phone screen reader, a real touch device and a real Safari.