Notification

A notification is a box with a title, a text and, if you want, buttons. It stands at a corner or at the top or bottom edge of the window, closes itself after a time, and takes no focus. It is heavier than a message: it says more, and it stays at the edge of the window and not above the content. You open it with a call from code, from anywhere, not by writing an element in a template.

Import
import { notification, NotificationHost } from 'svelte-ant-design';
Links
src/lib/Notification Report an issue Component README

When to use

  • Use it for feedback that needs a title and a text: a finished upload, a failed sync, a new version, a message from someone.
  • A short feedback that says one thing is a Message. A decision the user must make needs a dialog (Modal).
  • A notification goes away by itself, so do not put information in it that the user must be able to find again, or give it duration: 0 and let the user close it.

Examples

Basic

A call from anywhere in your code shows a notification at the top right of the window: no setup and no markup. A notification has a title and an optional description, and goes away after 4.5 seconds. The call returns a function that closes it.

open: no type icon

Types

success, info, warning and error have an icon of their type; open has none. A screen reader reads error and warning at once, and the others when it has finished, and says the type before the title.

Placement

Six places: topLeft, top, topRight, bottomLeft, bottom and bottomRight. Each is its own list. The newest notification stands nearest the window edge, and the older ones are pushed away from it. A notification comes in from the side of its place.

duration 10
duration 0: until it is closed

Duration

duration is in seconds (default 4.5). 0 or false keeps the notification until it is closed. The function that a call returns closes it.

Update by key

A call with the key of an open notification updates it in place: here an upload that is going on turns into a finished one. The box keeps its place, its type changes, and a screen reader reads the new text.

Actions

actions is content under the text, at the inline end: usually buttons. They are your own elements, so the keyboard reaches them after the text and before the close button.

icon: a string
icon: a snippet
icon: false

Icon

icon replaces the icon of the type: a string (an emoji), a snippet, or false for none. The box keeps its height with or without an icon.

closable: false
closeIcon: a string
closeIcon: a snippet

Close button

The close button is there by default. closable: false or closeIcon: false removes it; closeIcon replaces the cross with a string or a snippet. Its name for a screen reader is labels.close.

showProgress, duration 8: hold the pointer over it
showProgress, pauseOnHover: false

Progress bar

showProgress draws a strip that runs down with the time left. It is drawn from the timer that closes the notification, so it stands still whenever the timer does (the pointer, the focus, an open pile, a hidden page). It is decorative and is not announced.

pauseOnHover (the default), duration 5: keep the pointer over it
pauseOnHover: false
duration 5: Tab to the close button, and it waits for you

Pause

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

stack on (the default), threshold 3

Stack

On by default: past 3 notifications in a place (the threshold) the older ones lie behind the newest one, and the pointer or focus opens the pile and holds every timer. stack: false turns it off, { threshold } changes the number. Each place has its own pile.

maxCount not set
0 opened, pushed out by the limit: 0

Maximum count

maxCount limits the open notifications of all places together: a new one closes the oldest, and its onclose gets the reason 'replaced'.

class, style: on the box
classNames, styles: the parts (root, icon, section, title, description, actions, close, progress)
props: data-* and aria-* on the box

Styling

class and style go on the box; classNames and styles address its parts (root, icon, section, title, description, actions, close, progress); props puts data-* and aria-* attributes on it.

box clicks: 0; closed by: —

Click and close

onclick is called with the event when the box is clicked (not when the close button is). onclose is called once, when the notification starts to close, with the reason: 'timeout', 'user' (the close button or Escape), 'replaced' (the limit) or 'destroy' (code).

Content

title, description and actions are a string, a number (0 is shown) or a snippet. A snippet is drawn in the layer, so it sees the context of the place where the host is, not of the component that wrote it.

<NotificationHost placement="bottomLeft" bottom={72} ... /> not mounted

NotificationHost

<NotificationHost> draws the notifications where you put it; its props are the settings and win over notification.config(). Without one the library makes a layer on the page by itself. Only one host is active: the one mounted last.

Destroy

notification.destroy(key) closes one notification, notification.destroy() closes every one. Each runs its normal exit and its onclose with the reason 'destroy'.

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 notification 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 notification.config() or to a NotificationHost, 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 notification

PropertyDescriptionTypeDefault
titleThe first line: a string, a number (0 is shown) or a snippet. Required. This is the name title that Ant Design React 6 uses (message is deprecated there).NotificationContent—
descriptionThe text under the title. Optional: a title alone is a valid notification.NotificationContent—
actionsContent under the text, at the inline end: usually buttons. The elements you put here are the ones the keyboard reaches (React actions, Vue btn).NotificationContent—
iconReplaces the icon of the type; false removes it. open has no icon unless one is given.string | Snippet | falsethe icon of the type
durationSeconds before the notification goes away. 0 or false keeps it until it is closed. A negative or non-finite value falls back to the default (with a warning while developing). Angular counts in milliseconds; we do not.number | false4.5
keyA call with the key of an open notification updates that notification in place (new type, texts and options; the timer starts again). Without one a key is generated.string | numbergenerated
placementWhere it stands: its own place, or the default of the host and of notification.config().'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight''topRight'
closableShows the close button. The default of the host and of notification.config() applies when it is not set.booleantrue
closeIconReplaces the cross of the close button; false removes the button. The name of the button for a screen reader stays labels.close. The object form of React closable is not taken: it is closeIcon and onclose.string | Snippet | falsethe cross
showProgressShows a strip at the bottom of the box that runs down with the time left. It stands still while the timer does, and there is none with duration: 0.booleanfalse
pauseOnHoverHolds the timer while the pointer (a mouse or a pen) is over the box. Focus inside, an open pile and a hidden page hold it whatever this says.booleantrue
roleOverrides the role. By default error and warning are alert (read out at once) and the others status.'alert' | 'status'by type
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. A value here wins over the one of the host and of notification.config().{ root?, icon?, section?, title?, description?, actions?, close?, progress? }—
stylesThe same for inline styles, as objects.{ root?, icon?, section?, title?, description?, actions?, close?, progress? }—
propsAttributes put on the box (React props): data-* and aria-*.Record<`data-${string}` | `aria-${string}`, string>—
onclickCalled with the event when the box is clicked, not when its close button is. A click does not close the notification.(event: MouseEvent) => void—
oncloseCalled once, when the notification starts to close, with the reason: the time ran out, the user closed it (the close button or Escape), the limit pushed it out, or code closed it (destroy or the returned function). Angular says only whether the user closed it; React and Vue pass nothing.(reason: 'timeout' | 'user' | 'replaced' | 'destroy') => void—

Methods

PropertyDescriptionTypeDefault
notification.open(options)Opens a notification with no type icon, or updates the open one with the same key. Returns a function that closes it. It is not thenable (Message's is).() => void—
notification.info | success | error | warning(options)One method for each kind, with the same options. Without a window (on a server) every call does nothing and returns a function that does nothing.() => void—
notification.destroy(key?)Closes the notification with this key, or every notification. Each one runs its normal exit and its onclose with the reason 'destroy'.void—
notification.config(options)Sets defaults for every notification from now on (the settings table below). A setting given as { top: undefined } goes back to its default.void—

Settings: notification.config() and the props of NotificationHost

PropertyDescriptionTypeDefault
topDistance of the top places from the top of the window. A number is pixels.number | string24
bottomDistance of the bottom places from the bottom of the window. A number is pixels.number | string24
placementThe place of every notification that does not name one.'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight''topRight'
durationDefault duration in seconds. 0 and false never close.number | false4.5
maxCountAt most this many open notifications, in all places together; the oldest is closed to make room (reason 'replaced'). Ant Design React and Angular count the same way, Vue counts for each place.number—
containerThe element that the layer is put in. Only where it sits in the page: the layer stays position: fixed, drawn at the window (Ant Design getContainer). Useful for a native modal <dialog>, which covers everything outside of it.HTMLElement | (() => HTMLElement)document.body
closableDefault of closable.booleantrue
closeIconDefault of closeIcon.string | Snippet | falsethe cross
showProgressDefault of showProgress.booleanfalse
pauseOnHoverDefault of pauseOnHover.booleantrue
labelsThe type word read before the title (an empty word is none, which is the default of open), and the name of the close button. Pass a translation here: the library has no locale system yet.{ open?, info?, success?, error?, warning?, close? }{ info: "Info", success: "Success", error: "Error", warning: "Warning", close: "Close" }
classNamesDefaults for the parts of every notification, and the two parts of a place: list (the scrolling column) and listContent (the rows).{ root?, icon?, section?, title?, description?, actions?, close?, progress?, list?, listContent? }—
stylesThe same for inline styles, as objects.{ root?, icon?, section?, title?, description?, actions?, close?, progress?, list?, listContent? }—
stackOn by default (as in Ant Design React): past the threshold (3) the older notifications of a place lie behind the newest, and the pointer or focus opens the pile. false turns it off, { threshold } changes the number.boolean | { threshold?: number }true
Differences from Ant Design: the layer is made by the library (the React useNotification and contextHolder are replaced by <NotificationHost>); title and actions are the current React names (message and btn are deprecated there); stack is on by default as in React; the newest notification stands nearest the window edge in every place, as in React; a role per type and a hidden type word (React: role="alert" for all); the timer also waits for focus, an open pile and a hidden page; onclose carries the reason, and a call returns a function that closes the notification; duration is in seconds as in React and Vue (Angular: milliseconds). Not taken: the Vue warn alias, prefixCls, rtl (the direction is inherited) and the object form of closable. API inspired by Ant Design (MIT); the implementation is original for Svelte.

Design tokens

TokenDescriptionDefault
--ads-notification-z-indexLayer of the notifications, above the messages (2010) and every popup (the tooltip is 1070).2050
--ads-notification-widthWidth of the box. It is never wider than the window.384px
--ads-notification-topDistance of the top places from the top of the window, when top is not set.24px
--ads-notification-bottomDistance of the bottom places from the bottom of the window, when bottom is not set.24px
--ads-notification-marginSpace kept free at the sides of the window, and at the far end of a place (it also leaves room for the shadow).24px
--ads-notification-gapGap between two notifications.16px
--ads-notification-bgBackground of the box. In the dark theme #1f1f1f.#ffffff
--ads-notification-colorText color. In the dark theme 85% white.rgba(0, 0, 0, 0.88)
--ads-notification-padding-blockPadding of the box above and below.20px
--ads-notification-padding-inlinePadding of the box at the sides. The close button stands at this distance from the edge.24px
--ads-notification-paddingPadding of the box (the two above).var(--ads-notification-padding-block) var(--ads-notification-padding-inline)
--ads-notification-line-heightHeight of a line of the description.22px
--ads-notification-radiusCorner radius of the box.8px
--ads-notification-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-notification-title-sizeFont size of the title.16px
--ads-notification-title-line-heightHeight of a line of the title; the icon and the close button are centred on it.24px
--ads-notification-description-gapGap between the title and the description.8px
--ads-notification-actions-gapGap between the text and the actions.12px
--ads-notification-icon-sizeSize of the icon.24px
--ads-notification-icon-gapGap between the icon and the text.12px
--ads-notification-icon-infoColor of the info icon.#1677ff
--ads-notification-icon-successColor of the success icon. tokens-accessible.css darkens it to #389e0d (3:1 on the white box).#52c41a
--ads-notification-icon-errorColor of the error icon.#ff4d4f
--ads-notification-icon-warningColor of the warning icon. tokens-accessible.css darkens it to #c27c00.#faad14
--ads-notification-close-sizeSize of the close button: 24px, the smallest target of WCAG 2.5.8 (Ant Design has 22px).24px
--ads-notification-close-icon-sizeSize of the cross in the close button.14px
--ads-notification-close-roomSpace kept free at the end of the first line of the title, for the close button.26px
--ads-notification-close-paddingAir at both ends of a close button that holds more than the cross, a word for example; the cross stays a 24 px square.4px
--ads-notification-close-colorColor of the close button. tokens-accessible.css makes it rgba(0, 0, 0, 0.65) (4.5:1 on the box).rgba(0, 0, 0, 0.45)
--ads-notification-close-color-hoverColor of the close button on hover.rgba(0, 0, 0, 0.88)
--ads-notification-close-bgBackground of the close button at rest.transparent
--ads-notification-close-bg-hoverBackground of the close button on hover.rgba(0, 0, 0, 0.06)
--ads-notification-progress-bgFill of the progress strip.linear-gradient(90deg, #69b1ff, #1677ff)
--ads-notification-progress-trackTrack of the progress strip.rgba(0, 0, 0, 0.04)
--ads-notification-progress-heightHeight of the progress strip.2px
--ads-notification-progress-insetDistance of the progress strip from the sides of the box.8px
--ads-notification-progress-radiusCorner radius of the progress strip.8px
--ads-notification-motion-offsetHow far from its place a notification starts to come in and ends to leave (from its side: the right places from the right, top from above).64px
--ads-notification-stack-offsetStep between the rows of a pile.8px
--ads-notification-stack-scale-stepHow much smaller each row of a pile is than the one in front of it (the scale stops at the second step).0.06

Accessibility

  • error and warning are role="alert" and are read out at once; info, success and open are role="status" and wait for the reader to finish. Ant Design React gives role="alert" to all, which interrupts a reader for a plain “Saved”; the role option sets it per notification.
  • The icon is decorative. A hidden word before the title 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 notification never takes focus. What can be focused in it is the close button and the elements you put in actions; the close button is 24px, the target size of WCAG 2.5.8. Escape closes the notification that has focus inside it.
  • The timer waits while the pointer is over the notification, while focus is inside it, while a pile is open and while the page is hidden. A notification that must stay needs duration: 0, and the close button lets the user dismiss it (WCAG 2.2.1, time limits).
  • The layer is the last element of its container, so the keyboard reaches the buttons of a notification only after the rest of the page, and there is no shortcut that moves focus to the notifications. In a pile the older notifications are inert and hidden from a screen reader; the pile opens when focus comes into it, so a keyboard user reaches them.
  • Contrast: the text is about 15:1 on the box; the type icons are held to 3:1 and the close button to 4.5:1 (in the dark theme, and in the light theme with tokens-accessible.css, which darkens the green, the amber and the close button).
  • In forced-colors modes such as Windows High Contrast the box, the close button and the progress strip are drawn with a border. The motion (the box comes in from the side of its place and fades) is off under prefers-reduced-motion; the timer and the strip are the same. A touch does not hold the timer and does not open a pile.
  • Checked with a real screen reader (Orca with Firefox): it reads the role, the type word, the title, the description, the new text after an update by key and the name of the close button. Not checked: whether a screen reader announces a notification 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 (touch is emulated in Chromium) and a real Safari.