| sidebar_position | 1 |
|---|
The client-side code is built with TypeScript and runs via the WordPress Interactivity API. All interactive behavior lives in the wp-tosuto store namespace.
Namespace: wp-tosuto
Access the store from other blocks or scripts:
import { store } from '@wordpress/interactivity';
const { actions } = store( 'wp-tosuto' );State is internal. Use actions to add and remove toasts.
Add a new toast notification.
actions.add( content: string, options?: RawToast ): ToastIdParameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
content |
string |
yes | — | HTML content displayed in the toast. Sanitized via DOMPurify. |
options.id |
ToastId |
no | auto-generated | Custom ID. Auto-generated when omitted. |
options.variant |
ToastVariant |
no | 'default' |
Visual style. |
options.duration |
number |
no | 5000 |
Auto-dismiss delay in ms. Use 0 or negative to disable. |
options.dismissable |
boolean |
no | true |
Show a dismiss button. |
Returns ToastId — the ID of the added toast.
Example
const id = actions.add( '<strong>Saved!</strong>', {
variant: 'success',
duration: 3000,
dismissable: true,
} );Remove a toast immediately.
actions.remove( id: ToastId ): voidExample
actions.remove( id );Pause the auto-dismiss timer for a toast.
actions.pauseTimer( id: ToastId ): voidCalled automatically on mouseenter. Can also be called programmatically.
Resume a paused auto-dismiss timer.
actions.resumeTimer( id: ToastId ): voidCalled automatically on mouseleave.
type ToastId = string;String identifying a toast. Returned by actions.add().
type ToastVariant = 'default' | 'success' | 'error' | 'warning' | 'info';Controls the visual style of a toast.
type RawToast = Partial<Readonly<{
id?: ToastId;
content: string;
variant?: ToastVariant;
duration?: number;
dismissable?: boolean;
}>>;Input shape for actions.add(). All fields optional except content.
PHP-queued toasts are passed to JavaScript via wp_interactivity_state() in the wp_footer hook. The store hydrates automatically on page load — no consumer action required.
PHP Queue → Renderer (wp_footer) → wp_interactivity_state()
↓
store hydration → actions.add() × N