- Spec files
npm-react-aria
Describes: pkg:npm/react-aria@3.43.x
- Description
- Comprehensive library of unstyled React hooks providing accessible UI primitives with full WAI-ARIA compliance and internationalization support.
- Author
- tessl
- Last updated
toast-notifications.md docs/
1# Toast Notifications23Toast notification system for displaying temporary messages and alerts with full accessibility support including screen reader announcements and proper focus management.45## Capabilities67### Toast89Provides toast notification behavior with automatic dismissal and accessibility features.1011```typescript { .api }12/**13* Provides toast notification behavior and accessibility14* @param props - Toast configuration options15* @param state - Toast state from useToastState16* @param ref - Ref to the toast element17* @returns Toast props and state18*/19function useToast(props: AriaToastProps, state: ToastState, ref: RefObject<Element>): ToastAria;2021interface AriaToastProps {22/** Priority level of the toast for screen readers */23priority?: 'low' | 'high';24/** Whether the toast can be dismissed by the user */25isDismissible?: boolean;26/** Handler called when the toast is dismissed */27onClose?: () => void;28/** Timeout in milliseconds before auto-dismissal */29timeout?: number;30/** Animation duration for enter/exit transitions */31animation?: 'none' | 'slide' | 'fade';32}3334interface ToastAria {35/** Props to spread on the toast element */36toastProps: DOMAttributes<Element>;37/** Props for the close button (if dismissible) */38closeButtonProps: DOMAttributes<Element>;39/** Whether the toast is currently visible */40isVisible: boolean;41}42```4344**Usage Example:**4546```typescript47import { useToast } from "react-aria";48import { useToastState } from "react-stately";4950function Toast(props) {51let state = useToastState(props);52let ref = useRef();53let { toastProps, closeButtonProps, isVisible } = useToast(props, state, ref);5455if (!isVisible) return null;5657return (58<div {...toastProps} ref={ref} className="toast">59<div className="toast-content">{props.children}</div>60{props.isDismissible && (61<button {...closeButtonProps} className="toast-close">62×63</button>64)}65</div>66);67}68```6970### Toast Region7172Manages a region containing multiple toasts with proper ARIA live region announcements.7374```typescript { .api }75/**76* Provides toast region behavior for managing multiple toast notifications77* @param props - Toast region configuration78* @param state - Toast region state from useToastRegionState79* @param ref - Ref to the toast region element80* @returns Toast region props81*/82function useToastRegion(props: AriaToastRegionProps, state: ToastRegionState, ref: RefObject<Element>): ToastRegionAria;8384interface AriaToastRegionProps {85/** Maximum number of toasts to display simultaneously */86maxToasts?: number;87/** Position of the toast region on screen */88position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';89/** Whether toasts should be announced to screen readers */90hasLiveRegion?: boolean;91/** ARIA live region politeness setting */92politeness?: 'polite' | 'assertive';93}9495interface ToastRegionAria {96/** Props to spread on the toast region container */97regionProps: DOMAttributes<Element>;98/** Props for the ARIA live region */99liveRegionProps: DOMAttributes<Element>;100}101```102103**Usage Example:**104105```typescript106import { useToastRegion } from "react-aria";107import { useToastRegionState } from "react-stately";108109function ToastRegion(props) {110let state = useToastRegionState(props);111let ref = useRef();112let { regionProps, liveRegionProps } = useToastRegion(props, state, ref);113114return (115<div {...regionProps} ref={ref} className="toast-region">116<div {...liveRegionProps} />117{state.toasts.map((toast) => (118<Toast key={toast.key} {...toast} />119))}120</div>121);122}123```124125## Types126127```typescript { .api }128interface ToastState {129/** Collection of active toasts */130toasts: Collection<ToastItem>;131/** Add a new toast to the region */132add(toast: ToastItem): string;133/** Remove a toast by key */134remove(key: string): void;135/** Clear all toasts */136clear(): void;137}138139interface ToastRegionState {140/** Collection of active toast regions */141regions: Collection<ToastRegion>;142/** Default toast region */143defaultRegion: ToastRegion;144}145146interface ToastItem {147/** Unique identifier for the toast */148key: string;149/** Toast content */150content: ReactNode;151/** Toast priority level */152priority: 'low' | 'high';153/** Whether the toast is dismissible */154isDismissible: boolean;155/** Auto-dismiss timeout */156timeout?: number;157}158```