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
Markdown
# ๐ฆ 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.