Skip to main content

rc-snackbar

rc-snackbar is a live-region host for brief status messages and optional actions.

Package
@rcarls/rc-snackbar
Element
<rc-snackbar>
Native dependency
Polite status live region
State model
Imperative message queue
Main events
rc-snackbar-actionrc-snackbar-close

Installation

npm install @rcarls/rc-snackbar
import '@rcarls/rc-snackbar/define';

Usage

<rc-snackbar id="snackbar"></rc-snackbar>
document.querySelector('#snackbar').show({
message: 'Document archived',
actionLabel: 'Undo',
duration: 5000,
});

rc-snackbar does not move focus. It announces through a polite status live region and queues messages by default. Positive duration values auto-dismiss the visible message; use 0 only when the application deliberately owns dismissal.

open reflects visibility and follows the library's controlled/uncontrolled convention: host writes are silent, and default-open seeds an initial visible state. show(), close(), and clear() remain the primary way to drive the queue and timer; write open directly only for advanced cases that don't need queueing, such as an initially visible component seeded from server-rendered message/default-open attributes.

The theme-free surface uses system canvas colors and a plain border, while its action retains native button styling. Packaged themes provide the component's color, shape, spacing, and action treatment.

Live demo

API

Properties

PropertyMarkupTypeDefaultDescription
openopenbooleanNot specifiedWhether the snackbar is currently visible. Host writes are silent.
defaultOpendefault-openbooleanNot specifiedInitial visible state for uncontrolled usage, such as an already-queued message shown on first render.
messagemessagestring''Visible message text.
actionLabelaction-labelstring''Optional action button label.
durationdurationnumber4000Auto-close duration in milliseconds. Set to 0 to keep open.
queuePolicyqueue-policyRCSnackbarQueuePolicy'queue'Queue behavior for show calls while open.

Methods

MethodDescription
show(message: string | RCSnackbarShowOptions)Shows a message or queues it according to `queuePolicy`.
close(reason: RCSnackbarCloseReason)Closes the current snackbar.
clear()Clears the current snackbar and all queued messages.

Events

EventDetail typeDescription
rc-snackbar-closeCustomEventFired after a visible snackbar closes.
rc-snackbar-actionCustomEventFired when the action button is activated.

Slots

No slots are documented in the custom elements manifest.

CSS Custom Properties

PropertyDefaultDescription
--rc-snackbar-inset-inline1remInline-axis inset from the viewport edge.
--rc-snackbar-inset-block-end1remBlock-end inset from the viewport edge, before the safe-area inset.
--rc-snackbar-z-index1000Stacking order.
--rc-snackbar-gap0.5remGap between the message and action.
--rc-snackbar-min-block-size3remMinimum surface block size.
--rc-snackbar-max-inline-size30remMaximum surface inline size.
--rc-snackbar-padding-block0.5remSurface block-axis padding.
--rc-snackbar-padding-inline1remSurface inline-axis padding.
--rc-snackbar-border1px solid CanvasTextSurface border.
--rc-snackbar-radius0Surface border radius.
--rc-snackbar-bgCanvasSurface background.
--rc-snackbar-colorCanvasTextSurface text color.
--rc-snackbar-shadownoneSurface box shadow.
--rc-snackbar-action-borderNot specifiedAction button border; defers to native button border when unset.
--rc-snackbar-action-radiusNot specifiedAction button border radius; defers to native button radius when unset.
--rc-snackbar-action-padding-blockNot specifiedAction button block-axis padding; defers to native button padding when unset.
--rc-snackbar-action-padding-inlineNot specifiedAction button inline-axis padding; defers to native button padding when unset.
--rc-snackbar-action-bgNot specifiedAction button background; defers to native button background when unset.
--rc-snackbar-action-colorNot specifiedAction button text color; defers to native button color when unset.
--rc-snackbar-action-fontNot specifiedAction button font shorthand; defers to native button font when unset.
--rc-snackbar-focus-ringNot specifiedAction button focus outline; defers to native focus styling when unset.
--rc-snackbar-focus-ring-offsetNot specifiedAction button focus outline offset; defers to native focus styling when unset.

CSS Parts

PartDescription
surfaceSnackbar surface.
messageMessage text.
actionOptional action button.