Skip to content

CdsToast

New in v4.0

A toast is a pop-up notification about the result of an action: the changes are saved, the file is uploaded, the request failed. It appears above the page, does not interrupt the work and closes itself once timeout runs out.

Storybook GitLab
Test coverage: 100% — good coverage
API in specs
69 / 69 props, events, slots and exposed members with a test
Test cases
68 unit · 0 browser · 1 planned
Known bugs
6 pinned by an it.fails test
Code coverage
100% lines · 95.2% branches measured in v4.0.0-alpha.13
A11y (axe)
9 recommendations in 10 stories measured in v4.0.0-alpha.11

For a stream of events where notifications arrive one after another use CdsToastQueue. A message that has to stay on the page belongs in CdsNotification, and a question that requires a decision from the user — in the CdsModal dialog.

Anatomy ​

A toast consists of a container and three zones: prepend with the status icon, an avatar or a loading indicator; the content with a heading and text; and the actions zone. The timer prop adds a bar of the remaining time.

Variations ​

Controlling the visibility ​

Show the toast through v-model or the activator slot — in the second case the component manages its own state. The toast closes automatically after timeout (5000 ms by default); hovering the cursor or focusing inside the toast pauses the countdown, and on touch screens the toast can be swiped away.

Status ​

The status prop picks the color and the icon by the meaning of the message: info, success, warning, danger, progress. The icon can be replaced by the icon prop or hidden with the value false.

Timer ​

The timeout prop sets the delay before auto-closing in milliseconds, and -1 turns it off. The bar of the remaining time is enabled by the timer prop (true/top — on top, bottom — at the bottom), reverse-timer flips its direction and timer-color changes its color.

Position ​

The location prop puts the toast into one of the six positions: the side (top, bottom) plus the alignment (left, center, right). The toast is rendered above the page content by CdsOverlay.

Content and actions ​

The heading and the text come from the title and text props, the avatar before the content from prepend-avatar, and loading replaces the icon with a loading indicator. The actions slot adds buttons: inside it they get variant="solid", color="inverse" and size="sm" by default. The vertical layout (vertical) moves the actions below the content.

Slots ​

Slots replace the content coming from props when you need your own markup.

SlotDescription
activatorThe element that opens the toast
headerA header across the full width of the toast
prependContent before the text. Replaces the icon, the avatar and the loading indicator
titleHeading. Replaces the text from title
textText. Replaces the text from text
defaultMain content after the text
actionsActions of the toast

Guidelines ​

Scenarios ​

Undoing an action ​

The toast confirms the deletion and gives time to change one's mind: the "Отменить" button in the actions slot brings the document back, and the timer bar shows how much time is left to decide.

A background task ​

A single toast accompanies the task from the start to the result. While the processing runs, loading shows the indicator and timeout: -1 keeps the notification from disappearing; once it is done the same toast switches to status="success" and closes itself.

An error with a retry ​

Do not hide an error automatically: timeout: -1 keeps the toast until the user reacts, and the "Повторить" button in actions runs the request again.

API ​

Props

NameTypeDefaultDescription
absoluteboolean | undefined—Positions the element absolutely instead of fixed. This is useful for overlays that must stay inside a certain area, for example inside a container with position: relative.
activator(string & {}) | Element | "parent" | ComponentPublicInstance | undefined—The element that activates the component
activatorPropsRecord<string, any> | undefined{}Props for the activator element
attachstring | boolean | Element | undefined—Sets the DOM element the overlay content is moved to. It accepts a direct element reference, a querySelector string, or true to disable moving. By default the body element is used. Changing this prop is usually not recommended except for edge cases: the default positioning algorithm handles most scenarios better than it can without moving, and unexpected behavior may appear if the menu ends up being a child of its activator.
bgColorstring | undefined—Background color of the component
closeDelaystring | number | undefined—Delay in milliseconds before the component closes. Works only together with open-on-hover.
closeOnBackboolean | undefinedfalseCloses the overlay content when the browser Back button is pressed or $router.back() is called, cancelling the original navigation. persistent overlays cancel the navigation and animate as if they were clicked outside instead of closing.
closeOnContentClickboolean | undefined—Hide the component on a click on the content
collapsed{ width: number; height: number; } | undefined—Collapsed state of a toast in the queue: the size the toast collapses to. Managed by the CdsToastQueue component.
containedboolean | undefined—Limits the size of the component and of its grid to the offset parent. Implies the absolute and attach props. (Note: the parent element must have position: relative.)
contentstring | number | undefined—Content displayed in the component.
contentClassany—Applies a custom class to the content element. This is handy because the content is moved to the beginning of the cds-app component (unless attach is set) and cannot be selected with classes passed to the component directly.
contentPropsany—Applies custom props to the moved content
disabledboolean | null | undefinednullDisabled state
eagerboolean | undefined—Renders the content of the component right away. Useful when you have content that is not in the DOM but has to be indexed for SEO.
heightstring | number | undefined—Component height
iconfalse | IconValue | undefinednullIcon before the content. Taken from status by default; false hides the icon.
loadingboolean | undefined—Shows a loading indicator instead of the icon.
locationAnchor | null | undefined'top center'Position of the element relative to its container, for example "top left", "center", "bottom right".
locationStrategy"static" | "connected" | LocationStrategyFunction | undefined'static'Sets the way the overlay content is positioned. By default static is used, which centers the content in its container. Use connected to attach the content to the activator element.
maxHeightstring | number | undefined—Maximum component height
maxWidthstring | number | undefined—Maximum component width
minHeightstring | number | undefined—Minimum component height
minWidthstring | number | undefined—Minimum component width
modelValueboolean | undefined—The v-model value of the component. If the component supports the multiple prop, an empty array is used by default.
opacitystring | number | undefined—Opacity of the scrim element. Applies only when scrim=true.
openDelaystring | number | undefined—Delay in milliseconds before the component opens. Works only together with open-on-hover.
openOnClickboolean | undefined—Show the component on a click on the activator
openOnFocusboolean | undefined—Show the component when the activator gets focus
openOnHoverboolean | undefined—Show the component on hover over the activator
originAnchor | "auto" | "overlap" | undefined'auto'Sets the anchor point of the overlay content that is aligned with the location anchor of the target. With "auto" the opposite side of the location point is used. With "overlap" the same anchor point is used, so the overlay overlaps the target. It also sets the CSS transform-origin property for the enter/leave transitions.
position"absolute" | "static" | "fixed" | "relative" | "sticky" | undefined—CSS positioning of the component — the value of the position property
prependAvatarstring | undefined—Source of the avatar image before the content. Takes priority over the icon.
queueGapnumber | undefined—Gap between the toasts in the queue, in pixels. Managed by the CdsToastQueue component.
queueIndexnumber | undefined—Index of the toast in the queue. Managed by the CdsToastQueue component.
reverseTimerboolean | undefined—Reverses the timer bar: it fills up instead of draining.
status"progress" | "danger" | "success" | "warning" | "info" | undefined'info'Semantic status — drives the color and the default icon.
target(string & {}) | Element | "parent" | ComponentPublicInstance | "cursor" | [x: number, y: number] | undefined—When locationStrategy is set to "connected", specify the element or the array of x, y coordinates the overlay is positioned against. By default the activator element is used.
textstring | number | undefined—Text displayed in the component.
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme
timeoutstring | number | undefined5000Delay in milliseconds before the toast hides automatically. The value -1 turns auto-closing off.
timerboolean | "top" | "bottom" | undefinedfalseShows the timer bar until auto-closing: true/top — on top, bottom — at the bottom.
timerColorstring | undefined—Color of the timer bar.
titlestring | undefined—Heading of the toast.
transitionstring | boolean | (TransitionProps & { component?: Component | undefined; }) | null | undefined'cds-toast-transition'Transition animation
verticalboolean | undefined—Vertical layout: the actions move below the content.
widthstring | number | undefined—Component width
zIndexstring | number | undefined2000The z-index value of the component.

Events

NameTypeDescription
afterEnter[]The enter animation finished.
afterLeave[]The leave animation finished.
update:modelValue[value: boolean]The visibility of the toast changed.

Slots

NameTypeDescription
actions{ isActive: Ref<boolean, boolean>; }Slot for the actions of the toast.
activator{ isActive: boolean; props: Record<string, any>; }Slot for the element that acts as the activator of the toast.
defaultanyMain content of the toast.
headeranySlot for the full-width header of the toast.
prependanySlot before the content (icon, avatar or loading indicator).
textanyText slot.
titleanyHeading slot.

Exposed

NameTypeDescription
activatorElHTMLElement | undefinedThe DOM element of the activator.
activatorRefTemplateRefTemplate ref for assigning the activator manually.
animateClick() => voidRuns the bounce animation of the content — the same as on a click outside a persistent overlay.
contentElHTMLElement | undefinedThe DOM element of the overlay content.
globalTopbooleanWhether the overlay is the top one in the global stack of open overlays.
rootElHTMLElement | undefinedThe root DOM element of the overlay.
scrimElHTMLElement | undefinedThe DOM element of the scrim that dims the page.
targetElHTMLElement | undefinedThe DOM element of the positioning target.
targetRefTemplateRefTemplate ref for assigning the positioning target manually.
updateLocation((e: Event) => void) | undefinedRecalculates the position of the content relative to the target.