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.

157 lines (100 loc) โ€ข 4.36 kB
# โœจ `TinyTextarea` A lightweight JavaScript utility for automatically resizing `<textarea>` elements as users type โ€” without ugly scrollbars! --- ## ๐Ÿ“ฆ Features * ๐Ÿ” **Auto-resizing**: Grows or shrinks with the content * ๐Ÿšซ **No scrollbars** (until limit): Prevents `overflow-y` unless needed * ๐Ÿ”ง **Configurable**: Set `maxRows`, `extraHeight`, and hooks * ๐Ÿ“ก **Live events**: Hooks for `onInput` and `onResize` * ๐Ÿงผ **Clean lifecycle**: Includes `.refresh()` and `.destroy()` --- ## ๐Ÿš€ Usage ```js import TinyTextarea from './TinyTextarea.js'; const textarea = document.querySelector('textarea'); const autoResize = new TinyTextarea(textarea, { maxRows: 8, extraHeight: 0, onResize: (info) => console.log('Resized:', info), onInput: (info) => console.log('Input event:', info), }); ``` To stop behavior and clean up: ```js autoResize.destroy(); ``` --- ## ๐Ÿ”ง Constructor ```js new TinyTextarea(textarea, options?); ``` ### Parameters | Name | Type | Description | | --------------------- | ----------------------------- | ----------------------------------------------------- | | `textarea` | `HTMLTextAreaElement` | The `<textarea>` element to be managed. | | `options` | `Object` *(optional)* | Configuration options. | | `options.maxRows` | `number` | Max number of visible rows before scrollbars appear. | | `options.extraHeight` | `number` | Extra height (in px) to add on top of computed value. | | `options.onResize` | `(info: OnInputInfo) => void` | Called when number of visible rows changes. | | `options.onInput` | `(info: OnInputInfo) => void` | Called on every `input` event. | --- ## ๐Ÿ“š API ### `.getData() โ†’ { height, rows }` Returns the last known height and number of visible rows. ```js const { height, rows } = autoResize.getData(); ``` --- ### `.refresh()` Forces a manual recalculation and resize of the `<textarea>`. ```js autoResize.refresh(); ``` --- ### `.destroy()` Cleans up listeners and disables resizing behavior. Use this if you remove the element or want to stop automatic behavior. ```js autoResize.destroy(); ``` --- ### ๐Ÿ” Getters TinyTextarea exposes several readonly properties to let you inspect the current configuration and state of the managed `<textarea>`. #### ๐Ÿ“ `lineHeight: number` Returns the height in pixels of a single line of text in the textarea, computed from its current CSS styles. Useful for calculating exact space usage or aligning UI elements. --- #### ๐Ÿ“ `maxRows: number` Returns the maximum number of visible text rows allowed before the textarea begins to scroll. This value reflects the limit set via `options.maxRows`. --- #### โž• `extraHeight: number` Returns the number of extra pixels added to the calculated height of the textarea. This is useful for custom padding or visual adjustments. --- #### ๐Ÿ“ `currentHeight: number` Returns the most recently applied height (in pixels) that was set on the `<textarea>` after resizing. --- #### ๐Ÿ“Š `currentRows: number` Returns the most recent number of visible text rows that the textarea is currently using. This value is calculated dynamically and may change as the user types. --- #### ๐Ÿงฉ `textarea: HTMLTextAreaElement` Returns the original `<textarea>` DOM element that was enhanced by this instance. You can use this for direct styling, value changes, or other DOM-level operations. --- ## ๐Ÿ“ค Event Payload: `OnInputInfo` Whenever `onResize` or `onInput` is triggered, the following object is passed: ```ts { breakLines: number; // Total number of '\n' in the textarea height: number; // Final computed height (px) scrollHeight: number; // Natural scrollHeight of textarea maxHeight: number; // Max height before scrollbars show lineHeight: number; // Height of one line of text maxRows: number; // Maximum number of visible rows allowed rows: number; // Final calculated number of rows used } ``` --- ## ๐Ÿงช Beta Notice > ๐Ÿงช This library is currently marked as `@beta`. The API is stable but may be adjusted slightly during updates.