Skip to content

feat: add optional close button for toast dismissal#26

Open
lubauss wants to merge 1 commit intohiaaryan:mainfrom
lubauss:feat/close-button
Open

feat: add optional close button for toast dismissal#26
lubauss wants to merge 1 commit intohiaaryan:mainfrom
lubauss:feat/close-button

Conversation

@lubauss
Copy link
Copy Markdown
Contributor

@lubauss lubauss commented Feb 21, 2026

Summary

Add a closeButton prop to <Toaster> that renders an X dismiss button on each toast, visible on hover. This provides an alternative to swipe-to-dismiss for desktop users.

Problem

Currently the only way to dismiss a toast (besides waiting for auto-dismiss) is swiping. Desktop users expect a close button, especially for sticky toasts (duration: null) that don't auto-dismiss.

Solution

New closeButton prop

<Toaster closeButton />

When enabled, each toast renders an X button (using the existing X icon) in the top-right corner. The button:

  • Is hidden by default, fades in on toast hover
  • Calls onDismiss() on click
  • Is fully accessible: role="button", tabIndex, aria-label, keyboard support (Enter/Space)
  • Does not interfere with swipe-to-dismiss

CSS

[data-sileo-close] {
  position: absolute;
  top: 0; right: 0; z-index: 30;
  opacity: 0;
  transition: opacity 150ms ease;
}
[data-sileo-toast]:hover [data-sileo-close] { opacity: 1; }

Files Changed

File Changes
src/toast.tsx Add closeButton prop to SileoToasterProps, pass to <Sileo>
src/sileo.tsx Accept closeButton prop, render X button with a11y attributes
src/styles.css Add [data-sileo-close] styles with hover reveal

Backward Compatibility

  • closeButton defaults to false — no visual change unless opted in
  • Swipe-to-dismiss still works alongside the button

Testing

  1. <Toaster closeButton /> — hover over a toast, X button should fade in
  2. Click X — toast should dismiss
  3. Tab to button, press Enter — should dismiss
  4. Verify swipe-to-dismiss still works
  5. Without closeButton prop — verify no X button appears

Add a `closeButton` prop to `<Toaster>` that renders an X dismiss
button on each toast, visible on hover. This provides an alternative
to swipe-to-dismiss for desktop users, especially useful for sticky
toasts (duration: null) that don't auto-dismiss.

- closeButton defaults to false (no visual change unless opted in)
- Accessible: role="button", tabIndex, aria-label, keyboard support
- Swipe-to-dismiss still works alongside the button
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant