/** * Counter component: a live count under a text control. * * @augments IvComponent */ export class Counter extends IvComponent { /** @type {Readonly} */ static defaults: Readonly; /** * Returns the instance attached to `el`, if any. * * @param {Element} el Host element. * @returns {Counter|undefined} The instance or `undefined`. */ static get(el: Element): Counter | undefined; /** * Returns the existing instance or creates one. * * @param {Element} el Host element. * @param {Partial} [options] Options passed in JavaScript. * @returns {Counter} The instance. */ static getOrCreate(el: Element, options?: Partial): Counter; /** * Instantiates every `[data-iv-component="counter"]` inside `root`. * * @param {ParentNode} [root] Subtree to scan. * @returns {Counter[]} Newly created instances. */ static initAll(root?: ParentNode): Counter[]; /** * @param {Element} el A `.iv-field` holding a text control. * @param {Partial} [options] Options passed in JavaScript. * @throws {IvError} `invalid-element` when there is no text control to count. */ constructor(el: Element, options?: Partial); /** @type {CounterControl} The counted control. */ _control: CounterControl; /** @type {HTMLElement} The counter element. */ _counter: HTMLElement; /** @type {CounterSlot[]} Numbers inside the template. */ _slots: CounterSlot[]; /** @type {Map} Control attributes to restore. */ _saved: Map; /** @type {number} Resolved limit; `0` when there is none. */ _max: number; /** @type {number} Last count. */ _count: number; /** @type {boolean} Whether the limit can be exceeded at all. */ _soft: boolean; /** @type {boolean} Whether a custom validity is currently set here. */ _invalid: boolean; /** * Resolved options. * * @returns {Readonly} The options. */ get options(): Readonly; /** * Last count, in characters or words. * * @returns {number} The count. */ get count(): number; /** * Resolved limit. * * @returns {number} The limit, or `0` when the control has none. */ get max(): number; /** * The counted control. * * @returns {CounterControl} The control. */ get control(): CounterControl; /** * Recounts the control and repaints the counter. * * @returns {void} */ update(): void; /** * Applies (or lifts) the custom validity of a soft limit. * * @param {boolean} invalid Whether the control is over its soft limit. * @returns {void} */ _setValidity(invalid: boolean): void; /** * Builds the template nodes of the counter. * * @param {HTMLElement} counter The counter element. * @returns {void} */ _fill(counter: HTMLElement): void; /** * Adds the id of the counter to `aria-describedby` without repeating ids. * * @param {string} id Id of the counter. * @returns {void} */ _describe(id: string): void; /** * Remembers the served value of an attribute of the control, once. * * @param {string} name Attribute name. * @returns {void} */ _remember(name: string): void; /** * Puts an attribute of the control back to the value the author served. * * @param {string} name Attribute name. * @returns {void} */ _restore(name: string): void; } export type CounterOptions = { /** * What is counted: `chars` or `words`. */ mode: string; /** * Limit; `0` falls back to the `maxlength` of the control. */ max: number; /** * Fraction of the limit that turns the counter into a warning. */ warnAt: number; /** * Text of the counter; `{count}`, `{max}` and `{remaining}` are replaced. Empty picks the default for the limit. */ template: string; /** * Custom validity message used past a soft limit. */ overText: string; }; export type CounterControl = HTMLInputElement | HTMLTextAreaElement; /** * One replaceable piece of the template. */ export type CounterSlot = { /** * Node whose text is rewritten on every update. */ node: Node; /** * Which number it shows: `count`, `max` or `remaining`. */ key: string; }; import { IvComponent } from "../core/component.js";