Reference

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

  • null when no toaster is mounted. Call createToaster() first.
  • Controllers from mountToaster are not returned. getToaster only sees the singleton. If you built a second toaster with mountToaster, keep your own reference.
  • The reference is stable across calls. toaster.update(...) mutates its internal state, but the reference does not change.