UNPKG

@base-ui/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

64 lines 1.42 kB
import { generateId } from '@base-ui/utils/generateId'; /** * Creates a new toast manager. */ export function createToastManager() { const listeners = new Set(); function emit(data) { listeners.forEach(listener => listener(data)); } return { // This should be private aside from ToastProvider needing to access it. // https://x.com/drosenwasser/status/1816947740032872664 ' subscribe': function subscribe(listener) { listeners.add(listener); return () => { listeners.delete(listener); }; }, add(options) { const id = options.id || generateId('toast'); const toastToAdd = { ...options, id, transitionStatus: 'starting' }; emit({ action: 'add', options: toastToAdd }); return id; }, close(id) { emit({ action: 'close', options: { id } }); }, update(id, updates) { emit({ action: 'update', options: { ...updates, id } }); }, promise(promiseValue, options) { let handledPromise = promiseValue; emit({ action: 'promise', options: { ...options, promise: promiseValue, setPromise(promise) { handledPromise = promise; } } }); return handledPromise; } }; }