Dynamic Island-Style JavaScript Toasts – TikTiktoast

Category: Javascript , Notification | November 24, 2026
Authorianpurif
Last UpdateNovember 24, 2026
LicenseMIT
Views0 views
Dynamic Island-Style JavaScript Toasts – TikTiktoast

TikTiktoast is a vanilla JavaScript toast notification library inspired by Apple’s Dynamic Island.

It displays status messages in compact animated pills that expand into cards on hover.

Features

  • Four status types: success, error, info, and warning.
  • Top-center and bottom-center placement.
  • Automatic queuing when the notification stack is full.
  • Optional countdown progress bars.
  • Pause-on-hover dismissal timers.
  • Mouse and touch swipe dismissal.
  • Click and dismissal callbacks.
  • RTL layouts and ARIA live announcements.
  • Reduced-motion support.

How To Use It

Installation And Basic Usage

Load the core JavaScript before the closing </body> tag. TikTiktoast creates its own notification container and injects the required CSS.

<button id="save-button">Show notification</button>
<script src="https://unpkg.com/tiktiktoast/dist/tiktik.umd.js"></script>
<script>
  const toast = window.Tiktik.Tiktik ?? window.Tiktik;
  document.getElementById('save-button').addEventListener('click', () => {
    toast.success('Changes saved.');
  });
</script>

Or install & import it with NPM:

npm install tiktiktoast
import { Tiktik as toast } from 'tiktiktoast';
toast.success('Profile saved.');
toast.error('Connection lost.');
toast.warning('Your session expires soon.');
toast.info('A new report is available.');

Set Default Position And Timing

Use configure() to set defaults for subsequent notifications.

toast.configure({
  position: 'bottom',
  duration: 5000,
  progress: true
});

Update An Existing Toast

Reuse a toast ID to update the message and status during a multi-step operation. The original dismissal timer and other options stay in effect. For a task with an unknown completion time, use Infinity and dismiss the result programmatically.

const id = toast.info('Preparing export...', {
  id: 'report-export',
  duration: Infinity
});
setTimeout(() => {
  toast.success('Report exported.', { id });
  setTimeout(() => toast.dismiss(id), 3000);
}, 1500);

Track An Async Operation

Use promise() to display a loading message and update it when the operation resolves or rejects. This example simulates generating a CSV report.

const request = new Promise(resolve => {
  setTimeout(() => resolve({ filename: 'report.csv' }), 1200);
});
toast.promise(request, {
  loading: 'Generating report...',
  success: result => `${result.filename} is ready.`,
  error: () => 'Could not generate the report.',
  duration: 4000
});

Add A Custom Icon And Click Action

Use the icon option with an SVG element or a trusted SVG string.

toast.showToast({
  message: 'Your report is ready. Click to view details.',
  type: 'info',
  duration: Infinity,
  icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 3v12m-5-5 5 5 5-5M4 16v5h16v-5"/></svg>',
  onClick: () => toast.info('Report format: CSV'),
  onDismiss: () => console.log('Notification closed')
});

Monitor The Notification Stack

The five-toast limit applies across both positions. Queued notifications move into the stack when a toast at the matching position closes. Use one position for a continuous sequence of notifications.

Register a stack listener once during app initialization.

toast.onStackChange((visible, queued) => {
  console.log(`${visible} displayed, ${queued} waiting`);
});

Customize The Toast Styles

Use html:root to override the library’s injected theme values, even when your stylesheet loads before the first notification.

html:root {
  --tiktik-bg: #20252b;
  --tiktik-text: #f5f5f5;
  --tiktik-radius-card: 16px;
  --tiktik-font-size: 15px;
}

Configuration & API Reference

Toast Options

Pass an options object as the first argument to Tiktik.showToast() or the second argument to a status shortcut.

OptionDescription
messagestring. Required message text for showToast(). The shortcuts accept it as their first argument.
typesuccess, error, info, or warning. Default: info. Each status shortcut sets its own type.
durationnumber. Auto-dismiss delay in milliseconds. Default: 3000. Use Infinity for manual dismissal.
positiontop or bottom. Default: top.
iconstring or SVGElement. Defaults to the SVG icon for the chosen status type.
progressboolean. Shows a countdown bar for a finite duration. Default: false.
idstring. Identifies a toast for updates or dismissal. Generated automatically when omitted.
onClick() => void. Runs when the clickable toast body receives a click or Enter/Space activation. Default: unset.
onDismiss() => void. Runs after dismissal and exit animation, or during cleanup if the toast is still queued. Default: unset.

Global Configuration

OptionDescription
typeDefault toast type. Initial value: info.
durationDefault display time in milliseconds. Initial value: 3000.
positionDefault position (top or bottom). Initial value: top.
progressDefault progress bar setting. Initial value: false.
renderer(toast: ToastData) => HTMLElement. Custom template callback option. Default: unset. Currently inactive in the toast renderer.
audioboolean. Audio cue option. Default: false. Audio playback is currently unimplemented.

Promise Options

Tiktik.promise(promise, options) accepts a JavaScript Promise and an options object.

OptionDescription
loadingstring. Required message for the pending state.
successstring or (data) => string. Required message or formatter for a resolved value.
errorstring or (err) => string. Required message or formatter for a rejection.
durationnumber. Time before dismissal after settlement, in milliseconds. Defaults to 3000 on success and 5000 on error. Use Infinity for a persistent result.
positiontop or bottom. Optional; inherits the global position.
idstring. Optional toast identifier. Generated automatically when omitted.

API Methods

MethodDescription
Tiktik.showToast(options)Creates a toast or updates its message and type if the ID already exists. Returns its ID.
Tiktik.success(message, options?)Displays a success toast and returns its ID.
Tiktik.error(message, options?)Displays an error toast and returns its ID.
Tiktik.info(message, options?)Displays an informational toast and returns its ID.
Tiktik.warning(message, options?)Displays a warning toast and returns its ID.
Tiktik.promise(promise, options)Tracks a promise and returns the toast ID as a string.
Tiktik.dismiss(id?)Dismisses a toast by ID. Omit the ID to dismiss visible and queued notifications. Returns void.
Tiktik.configure(defaults)Updates global defaults for future toasts. Returns void.
Tiktik.onStackChange(listener)Registers a listener that receives the visible and queued counts as numbers whenever the stack changes. Returns void.

CSS Custom Properties

CSS VariableDescription
--tiktik-bgBackground color. Default: #000000.
--tiktik-textText color. Default: #ffffff.
--tiktik-radius-pillCollapsed pill border radius. Default: 9999px.
--tiktik-radius-cardExpanded card border radius. Default: 24px.
--tiktik-shadowBox shadow. Default: 0 8px 32px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.08).
--tiktik-z-indexContainer stacking level. Default: 999999.
--tiktik-font-sizeBase font size. Default: 14px.
--tiktik-durationCSS pill-to-card transition time. Default: 320ms.

Alternatives

FAQs

Q: Can I use TikTiktoast in Next.js or Nuxt?

A: Yes. Import the library in your app and trigger notifications in client-side event handlers or effects.

Q: How do I enable right-to-left notifications?

A: Set dir="rtl" on the HTML element before creating the first notification.

Q: Why does the first swipe sometimes fail to dismiss a toast?

A: Gesture handlers attach after the first mouse or touch interaction. Swipe again or use the close button.

Q: How do I dismiss a toast using the keyboard?

A: Tab to the close button and activate it with Enter or Space. Alt+T focuses the first toast in the first container if that toast has an onClick callback.

You Might Be Interested In:


Leave a Reply