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.
310 lines (191 loc) β’ 8.74 kB
Markdown
# πβ¨ `TinyToastNotify` Class
A lightweight notification system designed to display timed messages inside a container. It supports positioning, duration customization, HTML rendering, click handling, and optional avatar display.
## π§ Features
* π **Positioning**:
Customize horizontal (`x`: `left`, `center`, `right`) and vertical (`y`: `top`, `bottom`) alignment.
* β³ **Dynamic display time**:
Automatically adjusts based on message length.
* π **Flexible content**:
* Optional `title` π·οΈ
* Optional `avatar` πΌοΈ
* Optional `onClick` handler π±οΈ
* Optional raw HTML rendering π‘
* π **Fade-out animation**:
With customizable duration.
* π‘οΈ **Robust validation**:
Ensures safe and predictable behavior.
## βοΈ Customization Setters
* `setX(position: 'left' | 'center' | 'right')` β Sets horizontal alignment.
* `setY(position: 'top' | 'bottom')` β Sets vertical alignment.
* `setBaseDuration(ms: number)` β Base visible time in milliseconds.
* `setExtraPerChar(ms: number)` β Additional duration per character.
* `setFadeOutDuration(ms: number)` β Duration of fade-out animation.
## π§© Types
### π `CloseToastFunc`
```ts
type CloseToastFunc = () => void;
```
A callback function used to manually close a notification.
Itβs passed as the **second argument** to `onClick` handlers, allowing programmatic dismissal.
### π¬ `NotifyData`
```ts
type NotifyData =
| string
| {
message: string; // The main message to display
title?: string; // Optional title above the message
onClick?: (event: MouseEvent, close: CloseToastFunc) => void; // Optional click handler
html?: boolean; // If true, message will be rendered as raw HTML
avatar?: string; // Optional avatar image URL
};
```
Represents the input used to display a notification.
It can either be a plain string (used as the message), or a detailed object for richer customization.
## π§ Constructor: `new TinyToastNotify(...)`
Initializes a new `TinyToastNotify` instance with positioning and timing preferences.
```ts
new TinyToastNotify(
y?: 'top' | 'bottom',
x?: 'left' | 'right' | 'center',
baseDuration?: number,
extraPerChar?: number,
fadeOutDuration?: number,
selector?: string
)
```
### π Parameters
| Parameter | Type | Default | Description |
| ----------------- | ----------------------------------- | --------------------- | ----------------------------------------------------- |
| `y` | `'top'` \| `'bottom'` | `'top'` | Vertical alignment of the notification container |
| `x` | `'left'` \| `'center'` \| `'right'` | `'right'` | Horizontal alignment of the notification container |
| `baseDuration` | `number` | `3000` | Base display time in milliseconds |
| `extraPerChar` | `number` | `50` | Extra milliseconds added per character in the message |
| `fadeOutDuration` | `number` | `300` | Time (ms) for fade-out animation |
| `selector` | `string` | `'.notify-container'` | CSS selector used to find or create the container |
### π§ Behavior
* Validates all input values strictly to prevent misconfigurations.
* Attempts to locate an existing container via the selector and alignment classes.
* If not found, it creates a new container and appends it to `document.body`.
## π¦ Method: `getContainer()`
Returns the `HTMLElement` used to host all notifications.
```ts
getContainer(): HTMLElement
```
### π Returns
* The current notification container element.
### β οΈ Throws
* Throws an error if the container is not a valid `HTMLElement`.
## βοΈ Position and Timing Getters & Setters
### π `getY()`
Returns the current vertical position.
* **Returns:**
`'top' | 'bottom'` β The vertical alignment of the notification container.
### π `setY(value)`
Sets the vertical position of the notification container and updates its CSS classes.
* **Parameters:**
* `value: 'top' | 'bottom'` β The new vertical position to set.
* **Throws:**
* `Error` if the value is invalid.
### βοΈ `getX()`
Returns the current horizontal position.
* **Returns:**
`'left' | 'center' | 'right'` β The horizontal alignment of the notification container.
### π `setX(value)`
Sets the horizontal position of the notification container and updates its CSS classes.
* **Parameters:**
* `value: 'left' | 'center' | 'right'` β The new horizontal position to set.
* **Throws:**
* `Error` if the value is invalid.
### β²οΈ `getBaseDuration()`
Returns the base display duration for notifications.
* **Returns:**
`number` β Base time in milliseconds that a notification stays visible.
### β³ `setBaseDuration(value)`
Sets the base display duration for notifications.
* **Parameters:**
* `value: number` β Base display time in milliseconds.
* **Throws:**
* `Error` if `value` is not a valid non-negative finite number.
### β `getExtraPerChar()`
Returns extra display time added per character.
* **Returns:**
`number` β Extra milliseconds added per character in the notification.
### βοΈ `setExtraPerChar(value)`
Sets the extra display time added per character.
* **Parameters:**
* `value: number` β Extra milliseconds per character.
* **Throws:**
* `Error` if `value` is not a valid non-negative finite number.
### π `getFadeOutDuration()`
Returns the fade-out animation duration.
* **Returns:**
`number` β Duration in milliseconds of the fade-out effect.
### ποΈ `setFadeOutDuration(value)`
Sets the fade-out animation duration.
* **Parameters:**
* `value: number` β Fade-out time in milliseconds.
* **Throws:**
* `Error` if `value` is not a valid non-negative finite number.
## π `show(data)`
Displays a notification with customizable content and duration based on message length.
### π Parameters
* `data: NotifyData` β The notification data, which can be either:
* A **string** β Used as the message text directly.
* An **object** with the following optional properties:
* `message: string` β The main message text (required in object form).
* `title?: string` β Optional title shown above the message.
* `onClick?: function(MouseEvent, CloseToastFunc): void` β Optional click handler, receives the click event and a function to programmatically close the toast.
* `html?: boolean` β Whether the message should be interpreted as raw HTML (default is plain text).
* `avatar?: string` β Optional URL for an avatar image shown to the left of the notification.
### π οΈ Behavior
* Creates a notification `<div>` element with class `notify` and adds entrance animation.
* Supports optional avatar image, title, and click handler.
* Shows a close button (`Γ`) with hover effect.
* Calculates display duration as:
`baseDuration + (message length Γ extraPerChar) + fadeOutDuration` milliseconds.
* Automatically fades out and removes the notification after the calculated total time.
* Clicking the close button or calling the programmatic `close` function dismisses the notification.
* If an `onClick` handler is provided, clicking the notification (except on the close button) triggers it.
### β οΈ Errors
* Throws an error if `data` is neither a string nor a valid object with a string `message` property.
* Throws an error if `onClick` is defined but is not a function.
## π₯ `destroy()`
Destroys the current **TinyToastNotify** instance.
It **removes the notification container from the DOM** and **clears all pending notifications**. This method is useful when the notification system is no longer needed or when changing views in a single-page application.
### Syntax
```js
tinyToastNotify.destroy();
```
### Behavior
* Removes the entire container (`.notify-container`) from the DOM.
* Automatically cancels any pending or visible toasts.
* Cleans up internal references.
## π¨ CSS Files Location
The CSS files for the TinyNotify project build can be found in the following folder:
```
dist/v1/css
```
Inside this folder, you'll find the main stylesheets:
- `TinyNotify.css` β the full, unminified CSS file
- `TinyNotify.min.css` β the minified, optimized CSS file for production π
Use these files to style your notifications!
Happy coding! β¨