/* Copyright Notice * bootstrap5-toggle v5.3.3 * https://palcarazm.github.io/bootstrap5-toggle/ * @author 2011-2014 Min Hur (https://github.com/minhur) * @author 2018-2019 Brent Ely (https://github.com/gitbrent) * @author 2022 Pablo Alcaraz Martínez (https://github.com/palcarazm) * @funding GitHub Sponsors * @see https://github.com/sponsors/palcarazm * @license MIT * @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE */ interface ToggleOptions { onlabel: string; offlabel: string; onstyle: ToggleStyle; offstyle: ToggleStyle; onvalue: string | null; offvalue: string | null; ontitle: string | null; offtitle: string | null; size: ToggleSize | ""; style: string; width: string | null; height: string | null; tabindex: number; tristate: boolean; name: string | null; aria: AriaToggleOptions; tooltip?: TooltipOptions; } type AriaToggleOptions = { label: string; }; type TooltipOptions = { placement: PlacementOptions; title: { on: string; off: string; mixed?: string; }; }; type UserOptions = Partial & { on?: string; off?: string; width?: string | number; height?: string | number; }; type ToggleStyle = "primary" | "secondary" | "success" | "danger" | "warning" | "info" | "light" | "dark" | "outline-primary" | "outline-secondary" | "outline-success" | "outline-danger" | "outline-warning" | "outline-info" | "outline-light" | "outline-dark"; type ToggleSize = "large" | "normal" | "small" | "mini" | "lg" | "md" | "sm" | "xs"; declare enum PlacementOptions { TOP = "top", BOTTOM = "bottom", LEFT = "left", RIGHT = "right" } declare class Toggle { private readonly element; private readonly userOptions; private readonly options; private readonly stateReducer; private readonly domBuilder; private pointer; private readonly SCROLL_THRESHOLD; private eventsBound; private suppressExternalSync; private readonly originalDescriptors; /** * Initializes a new instance of the BootstrapToggle class. * @param element The HTMLInputElement element which represents the toggle. * @param options The options for the toggle. * @returns The constructed BootstrapToggle instance. */ constructor(element: HTMLInputElement, options: UserOptions); /** * Intercepts the following input properties to detect external changes: * - checked * - disabled * - readonly * - indeterminate * This method is used to detect changes made to the input element directly, * rather than through the BootstrapToggle API. It is used to maintain the * state of the toggle in cases where the user changes the input element * directly, rather than through the API. * @returns void */ private interceptInputProperties; /** * Restores the original input properties of the toggle element. * This method is used to restore the original descriptors of the input properties * which were intercepted by the BootstrapToggle to detect external changes. * @returns void */ private restoreInputProperties; /** * Handles the change event of the input element of the toggle. * This event listener is responsible for detecting when the input element * of the toggle changes its state and triggering the update method to keep the toggle in sync. */ private readonly onExternalChange; /** * Binds event listeners to the toggle element. * This method is called by the constructor and is responsible for * binding the following event listeners: * - Pointer events (click, touchstart, touchend) * - Keyboard events (keydown, keyup) * - Label events (click) * If the event listeners are already bound (i.e. this.eventsBound is true), * this method does nothing. * @returns void */ private bindEventListeners; /** * Unbinds all event listeners from the toggle element. * This method is called by the destructor and is responsible for * unbinding the following event listeners: * - Pointer events (click, touchstart, touchend) * - Keyboard events (keydown, keyup) * - Label events (click) * If the event listeners are not bound (i.e. this.eventsBound is false), * this method does nothing. * @returns void */ private unbindEventListeners; private bindFormResetListener; private unbindFormResetListener; private readonly onFormReset; /** * Binds a pointerdown event listener to the root element of the toggle. * The event listener is responsible for handling pointer events (e.g. mouse clicks, touch events) * and triggering the toggle's state change when a pointer event occurs. * The event listener is bound with the passive option, which means that it will not block * other event listeners from being triggered. */ private bindPointerEventListener; /** * Unbinds the pointerdown event listener from the root element of the toggle. * This method is responsible for unbinding the pointerdown event listener that was * previously bound by the bindPointerEventListener method. * If the event listener is not bound (i.e. this.eventsBound is false), this method does nothing. * @returns void */ private unbindPointerEventListener; /** * Handles pointer down events by initiating the toggle action and setting up * listeners for pointer movement, release, and cancellation. * * The method early exits if: * - the pointer event is not a primary mouse button click * - the toggle cannot be interacted with (`disabled` or `readonly`) * @param e The PointerEvent object representing the pointer down event. */ private readonly onPointerDown; /** * Handles pointer move events by checking the distance moved from the initial pointer down position. * If the pointer has moved beyond a certain threshold, the pointer interaction is cancelled. * * Allows dragging within the width of the toggle but cancels if vertical movement exceeds the scroll threshold. * @param e The PointerEvent object representing the pointer move event. */ private readonly onPointerMove; /** * Handles pointer up events by determining if the pointer interaction * should trigger a toggle action based on the distance moved. * * If the pointer has moved beyond a certain threshold, the pointer interaction is cancelled. * Allows dragging within the width of the toggle but cancels if vertical movement exceeds the scroll threshold. * Finally, it cleans up by calling the pointer cancel handler. * * If the pointer event is not a primary mouse button click, the interaction is cancelled. * @param e The PointerEvent object representing the pointer up event. */ private readonly onPointerUp; /** * Cleans up pointer event listeners after a pointer interaction is completed or cancelled. * * This method removes the `pointermove`, `pointerup`, and `pointercancel` event listeners * from the root element of the toggle. * However, `pointerdown` listener remains active for future interactions. */ private readonly onPointerCancel; /** * Binds a keydown event listener to the root element of the toggle. * The event listener is responsible for handling keydown events * and triggering the toggle's state change when a keydown event occurs. */ private bindKeyboardEventListener; /** * Unbinds the keydown event listener from the root element of the toggle. * This method is responsible for unbinding the keydown event listener that was * previously bound by the bindKeyboardEventListener method. * If the event listener is not bound (i.e. this.eventsBound is false), this method does nothing. * @returns void */ private unbindKeyboardEventListener; private readonly handlerKeyboardEvent; /** * Binds a click event listener to all labels that are associated with the toggle's input element. * The event listener is responsible for handling click events and triggering the toggle's state change when a click event occurs. * The event listener is bound with the passive option set to false, which means that it will block other event listeners from being triggered until it has finished its execution. * This method is called by the constructor and is responsible for binding the event listener to the toggle's labels. * If the toggle's input element does not have an id (i.e. this.element.id is null or undefined), this method does nothing. * @returns void */ private bindLabelEventListener; /** * Unbinds the click event listener from all labels that are associated with the toggle's input element. * This method is responsible for unbinding the event listener that was previously bound by the bindLabelEventListener method. * If the toggle's input element does not have an id (i.e. this.element.id is null or undefined), this method does nothing. * @returns void */ private unbindLabelEventListener; private readonly handlerLabelEvent; /** * Applies a toggle action to the toggle state and renders the toggle element. * If the action is successful, this method will render the toggle element with the new state. * If the silent parameter is false, this method will also trigger the change event. * @param action The toggle action to apply. * @param silent A boolean indicating whether to trigger the change event after applying the action. */ private apply; /** * Toggles the state of the toggle. * If the toggle is currently in the on state, it will be set to the off state. * If the toggle is currently in the off state, it will be set to the on state. * If the toggle is currently in the indeterminate state, it will be set to the on state. * If the silent parameter is false, this method will also trigger the change event. * @param silent A boolean indicating whether to trigger the change event after applying the action. */ toggle(silent?: boolean): void; /** * Sets the toggle state to on. * If the silent parameter is false, this method will also trigger the change event. * @param silent A boolean indicating whether to trigger the change event after applying the action. */ on(silent?: boolean): void; /** * Sets the toggle state to off. * If the silent parameter is false, this method will also trigger the change event. * @param silent A boolean indicating whether to trigger the change event after applying the action. */ off(silent?: boolean): void; /** * Sets the toggle state to indeterminate. * If the silent parameter is false, this method will also trigger the change event. * @param {boolean} silent A boolean indicating whether to trigger the change event after applying the action. */ indeterminate(silent?: boolean): void; /** * Sets the toggle state to determinate. * If the silent parameter is false, this method will also trigger the change event. * @param {boolean} silent A boolean indicating whether to trigger the change event after applying the action. */ determinate(silent?: boolean): void; /** * Enables the toggle. * If the toggle is currently disabled, this method will set the toggle state to enabled. * If the silent parameter is false, this method will also trigger the change event. * @param {boolean} silent A boolean indicating whether to trigger the change event after applying the action. * @returns void */ enable(silent?: boolean): void; /** * Disables the toggle. * If the toggle is currently enabled, this method will set the toggle state to disabled. * If the silent parameter is false, this method will also trigger the change event. * @param {boolean} silent A boolean indicating whether to trigger the change event after applying the action. */ disable(silent?: boolean): void; /** * Sets the toggle state to readonly. * If the toggle is currently disabled or enabled, this method will set the toggle state to readonly. * If the silent parameter is false, this method will also trigger the change event. * @param {boolean} silent A boolean indicating whether to trigger the change event after applying the action. * @returns void */ readonly(silent?: boolean): void; /** * Synchronizes the toggle state with the input element and renders the toggle. */ update(): void; /** * Triggers the change event on the toggle's input element and the appropriate toggle event. * This method is called after a toggle action is applied to notify listeners of the state change. * @param {ToggleActionType} action The toggle action that was applied. * @param {ToggleState} state The state of the toggle once the action was applied. */ private trigger; /** * Returns the corresponding toggle event for the given toggle action and state. * This method is used to determine which toggle event to trigger after a toggle action is applied. * @param {ToggleActionType} action The toggle action that was applied. * @param {ToggleState} state The previous state of the toggle before the action was applied. * @returns {ToggleEvents} The corresponding toggle event for the given toggle action and state. */ private getEventForAction; /** * Returns the corresponding toggle event for the given toggle state. * This method is used to determine which toggle event to trigger after a toggle action is applied. * @param {ToggleState} state The previous state of the toggle before the action was applied. * @returns {ToggleEvents} The corresponding toggle event for the given toggle state. */ private getValueEvent; /** * Destroys the toggle element and unbinds all event listeners. *This method is useful when you need to remove the toggle element from the DOM. *After calling this method, the toggle element will be removed from the DOM and all event listeners will be unbound. */ destroy(): void; /** * Destroys the toggle element and reinitializes it with the same options. *This method is useful when you need to reinitialize the toggle element with the same options. */ rerender(): void; } interface ToggleState { value: ToggleStateValue; checked: boolean; status: ToggleStateStatus; indeterminate: boolean; } declare enum ToggleStateValue { ON = "on", OFF = "off", MIXED = "mixed" } declare enum ToggleStateStatus { ENABLED = "enabled", DISABLED = "disabled", READONLY = "readonly" } declare enum ToggleEvents { ON = "toggle:on", OFF = "toggle:off", MIXED = "toggle:mixed", ENABLED = "toggle:enabled", DISABLED = "toggle:disabled", READONLY = "toggle:readonly" } interface ToggleEventDetail { state: ToggleState; } declare enum ToggleMethods { ON = "on", OFF = "off", TOGGLE = "toggle", DETERMINATE = "determinate", INDETERMINATE = "indeterminate", ENABLE = "enable", DISABLE = "disable", READONLY = "readonly", DESTROY = "destroy", RERENDER = "rerender" } interface BootstrapToggleElement extends HTMLInputElement { bootstrapToggle(options?: ToggleMethods | Record, silent?: boolean): void; bsToggle?: Toggle; } interface BootstrapToggleElementEventMap extends HTMLElementEventMap { "toggle:on": CustomEvent; "toggle:off": CustomEvent; "toggle:mixed": CustomEvent; "toggle:enabled": CustomEvent; "toggle:disabled": CustomEvent; "toggle:readonly": CustomEvent; } export { Toggle as BootstrapToggle, ToggleEvents as Events, ToggleMethods as Methods, ToggleStateStatus as StateStatus, ToggleStateValue as StateValue }; export type { BootstrapToggleElement, BootstrapToggleElementEventMap, ToggleEventDetail as EventDetail, ToggleOptions as Options, ToggleSize as Size, ToggleState as State, ToggleStyle as Style, UserOptions };