UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

190 lines (122 loc) โ€ข 4.35 kB
# ๐Ÿ“ฃ TinyNotifications > A utility class to manage browser notifications with sound, custom icons, text truncation, and click behavior. --- ## ๐Ÿš€ Features * โœ… Request and manage user permission for notifications * ๐Ÿ”” Play a sound when a notification is shown * ๐Ÿ–ผ๏ธ Support for default avatar/icon * โœ‚๏ธ Automatic body text truncation * ๐Ÿ’ฅ Strong validation with `TypeError` and runtime checks * โ— Prevents use of `send()` before permission request --- ## ๐Ÿ—๏ธ Constructor ```js new TinyNotifications(options?) ``` ### Parameters | Name | Type | Default | Description | | ---------------- | ----------------------------------------- | ------- | -------------------------------------------------- | | `audio` | `string` \| `HTMLAudioElement` \| `null` | `null` | Sound to be played with the notification | | `defaultIcon` | `string` \| `null` | `null` | Default icon to use in notifications | | `bodyLimit` | `number` | `100` | Maximum number of characters in body text | | `defaultOnClick` | `(this: Notification, evt: Event) => any` | `fn` | Function executed when the notification is clicked | ### Throws * `TypeError` if any parameter is of invalid type. --- ## ๐Ÿง  Methods ### ๐Ÿ” `requestPerm()` ```js requestPerm(): Promise<boolean> ``` Requests permission from the browser to send notifications. * Sets internal `#allowed` and `#permissionRequested` flags. * Must be called before using `send()`. **Returns:** `Promise<boolean>` โ€” `true` if permission was granted. --- ### โš™๏ธ `isCompatible()` ```js isCompatible(): boolean ``` Checks if the current browser supports the Notification API. **Returns:** `true` or `false` --- ### ๐Ÿ“ค `send(title, config?)` ```js send(title: string, config?: NotificationOptions): Notification | null ``` Sends a notification to the user with optional configuration. Truncates long body texts and plays a sound if set. #### Parameters * `title`: *(string)* โ€” Title of the notification * `config`: *(object)* โ€” Optional notification settings (`body`, `icon`, `vibrate`, etc.) #### Throws * `Error` if `requestPerm()` was never called * `TypeError` if `title` is not a string or `config` is not an object #### Returns * `Notification` instance if allowed * `null` if permission was denied --- ## ๐Ÿ“ฅ Getters & Setters ### ๐Ÿ“Œ `wasPermissionRequested()` ```js wasPermissionRequested(): boolean ``` Checks if `requestPerm()` was called. --- ### ๐ŸŸข `isAllowed()` ```js isAllowed(): boolean ``` Checks if permission has been granted. --- ### ๐Ÿ”Š `getAudio()` / `setAudio(value)` ```js getAudio(): HTMLAudioElement | null setAudio(value: HTMLAudioElement | string | null) ``` Set or retrieve the notification sound. **Throws:** `TypeError` if invalid type is used. --- ### ๐Ÿ“ `getBodyLimit()` / `setBodyLimit(value)` ```js getBodyLimit(): number setBodyLimit(value: number) ``` Set or retrieve the maximum number of characters in the notification body. **Throws:** `TypeError` if value is not a non-negative number. --- ### ๐Ÿ–ผ๏ธ `getDefaultAvatar()` / `setDefaultAvatar(value)` ```js getDefaultAvatar(): string | null setDefaultAvatar(value: string | null) ``` Get or set the default icon for notifications. **Throws:** `TypeError` if value is not a string or `null`. --- ### ๐Ÿ–ฑ๏ธ `getDefaultOnClick()` / `setDefaultOnClick(value)` ```js getDefaultOnClick(): (this: Notification, evt: Event) => any setDefaultOnClick(value: function) ``` Set or retrieve the default click handler for all notifications. **Throws:** `TypeError` if value is not a function. --- ## ๐Ÿ“ฆ Example ```js import TinyNotifications from './TinyNotifications.js'; const notify = new TinyNotifications({ audio: '/sounds/ping.mp3', defaultIcon: '/img/icon.png', bodyLimit: 80 }); await notify.requestPerm(); notify.send('Hello World!', { body: 'This message will be truncated if too long.', vibrate: [100, 50, 100] }); ``` --- ## ๐Ÿงช Safety Notes * Always call `requestPerm()` **before** using `send()` * Invalid or unsafe inputs throw strong errors * Compatible with all modern browsers that support `Notification` API