- Docs
- getToaster
getToaster
Return the singleton toaster controller, or null when none is mounted.
Return the singleton toaster controller, or null when none is mounted.
Signature
function getToaster(): ToasterController | null
Returns
A ToasterController when one is mounted, or null otherwise. The reference is the same instance on every call, because there is exactly one singleton per page. This function never mounts anything.
Examples
Inspect the mounted toaster
import { createToaster, getToaster } from '@samline/notify'
import '@samline/notify/style.css'
createToaster({ position: 'bottom-right' })
const controller = getToaster()
controller?.element // the mounted container element
controller?.options // the current merged options
Guard before calling
import { getToaster } from '@samline/notify'
function updateTheme(theme: 'light' | 'dark') {
const toaster = getToaster()
if (!toaster) {
console.warn('No toaster mounted — call createToaster() first.')
return
}
toaster.update({ theme })
}
Live updates from a store
import { getToaster } from '@samline/notify'
store.subscribe(state => {
getToaster()?.update({ theme: state.theme, position: state.position })
})
Edge cases
nullwhen no toaster is mounted. CallcreateToaster()first.- Controllers from
mountToasterare not returned.getToasteronly sees the singleton. If you built a second toaster withmountToaster, keep your own reference. - The reference is stable across calls.
toaster.update(...)mutates its internal state, but the reference does not change.
Related
createToaster— mount the singleton.destroyToaster— unmount the singleton.- Toast lifecycle