@supunlakmal/hooks
Version:
A collection of reusable React hooks
33 lines (32 loc) • 1.56 kB
TypeScript
interface UseNotificationOptions extends NotificationOptions {
/** Title for the notification. */
title: string;
/** Optional: Function to call when the notification is clicked. */
onClick?: (event: Event) => void;
/** Optional: Function to call when the notification is closed. */
onClose?: (event: Event) => void;
/** Optional: Function to call when an error occurs showing the notification. */
onError?: (event: Event) => void;
/** Optional: Function to call when the notification is shown. */
onShow?: (event: Event) => void;
}
interface UseNotificationReturn {
/** The current permission status ('granted', 'denied', 'default'). */
permission: NotificationPermission;
/** Function to request notification permission from the user. */
requestPermission: () => Promise<NotificationPermission>;
/** Function to display the notification with the configured options. */
showNotification: (overrideOptions?: Partial<UseNotificationOptions>) => Notification | null;
/** Error object if permission request or notification fails, null otherwise. */
error: Error | null;
/** Indicates if the Notification API is supported by the browser. */
isSupported: boolean;
}
/**
* Hook to manage web notifications.
*
* @param defaultOptions Default options for the notification (title is required).
* @returns State and functions to request permission and show notifications.
*/
export declare function useNotification(defaultOptions: UseNotificationOptions): UseNotificationReturn;
export {};