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.

109 lines (77 loc) โ€ข 4.58 kB
# ๐Ÿ“ฆ TinyUploadClicker A tiny but good JavaScript utility that lets you bind any clickable element (buttons, icons, divs, etc.) to trigger a hidden `<input type="file">`. This allows full control over file upload behavior, visual customization, and events โ€” without ever showing the native file input element. --- ## ๐Ÿš€ Features * ๐ŸŽฏ **Use any HTML element** as a file input trigger * ๐ŸŽจ **Custom attributes and styles** for the input element * ๐Ÿ” **Multiple file selection** support * ๐Ÿง  **Strong runtime validations** with helpful errors * ๐Ÿช **Lifecycle hooks** for click and file selection * ๐Ÿงน **Clean `destroy()` method** to remove everything * ๐Ÿ” **No dependencies**, pure JS --- ## โœจ Example ```js const uploader = new TinyUploadClicker({ triggers: '#uploadBtn', accept: ['.png', '.jpg'], multiple: true, inputAttributes: { 'data-role': 'upload' }, inputStyles: { display: 'none' }, onClick: (el) => console.log('Trigger clicked:', el), onFileLoad: (files, el) => console.log('Files selected:', files) }); // Later, cleanup: uploader.destroy(); ``` --- ## ๐Ÿงฑ Constructor ### `new TinyUploadClicker(options)` Creates a new instance of the upload click manager. #### ๐Ÿ” Throws * `TypeError` if any part of the config is invalid * `Error` if a trigger selector resolves to no element #### ๐Ÿ’ก Parameters | Name | Type | Required | Description | | --------- | ----------------------------------------- | -------- | ------------------------------------------- | | `options` | [`UploaderConfig`](#-uploaderconfig-type) | โœ… Yes | Configuration object for the upload clicker | --- ## ๐Ÿงฉ `UploaderConfig` Type ```ts type UploaderConfig = { triggers: string | HTMLElement | Array<string | HTMLElement>, multiple?: boolean, accept?: string | string[], inputAttributes?: Record<string, string>, inputStyles?: Partial<CSSStyleDeclaration>, onClick?: ((triggerElement: HTMLElement) => void) | null, onFileLoad?: ((files: FileList, triggerElement: HTMLElement) => void) | null } ``` | Property | Type | Default | Description | | ----------------- | ------------------------------------------------------- | --------------------- | --------------------------------------------------------------------- | | `triggers` | `string \| HTMLElement \| Array<string \| HTMLElement>` | โ€” | Elements (or selectors) that will trigger file selection when clicked | | `multiple` | `boolean` | `false` | Whether multiple files can be selected | | `accept` | `string \| string[]` | `""` | Accepted file types (e.g., `".png"` or `"image/*"`) | | `inputAttributes` | `Record<string, string>` | `{}` | Extra attributes to apply to the `<input type="file">` | | `inputStyles` | `Partial<CSSStyleDeclaration>` | `{ display: 'none' }` | Custom inline styles for the hidden input | | `onClick` | `(HTMLElement) => void \| null` | `null` | Hook triggered when a trigger is clicked (before file dialog opens) | | `onFileLoad` | `(FileList, HTMLElement) => void \| null` | `null` | Hook triggered after files are selected | --- ## ๐Ÿ”ง Methods ### `destroy(): void` Destroys all internal listeners and DOM elements created by the instance. Cleans up memory and detaches everything from the DOM. --- ## ๐Ÿ›ก๏ธ Runtime Validations The constructor performs strict type validation and helpful error reporting: * โŒ Missing or invalid `triggers` throws immediately. * โŒ Selectors that resolve to `null` will throw. * โŒ Invalid types for `accept`, `onClick`, `onFileLoad`, `inputStyles`, and `inputAttributes` are rejected. * โœ… Supports string or array for `accept`. --- ## ๐Ÿ› ๏ธ Internal Design * Each trigger gets its own file input. * Inputs are stored in a `WeakMap<HTMLElement, HTMLInputElement>`. * After a file is selected, the input is reset so the same file can be selected again. * All inputs are appended to `document.body` and styled invisibly by default.