/* * Copyright Notice * bs-darkmode-toggle v1.1.0 * https://palcarazm.github.io/bs-darkmode-toggle/ * @author 2023 Pablo Alcaraz Martínez (https://github.com/palcarazm) * @funding GitHub Sponsors * @see https://github.com/sponsors/palcarazm * @license MIT * @see https://github.com/palcarazm/bs-darkmode-toggle/blob/master/LICENSE */ import { ExtendableEventMap, Component, Monitor } from 'component-lifecycle'; interface DarkModeOptions { state?: boolean; root?: string; storage?: StorageType; lightLabel?: string; darkLabel?: string; lightColorMode?: string; darkColorMode?: string; style?: ToggleStyle; layout?: Layout; lightAriaLabel?: string; darkAriaLabel?: string; } type ToggleStyle = "primary" | "secondary" | "success" | "danger" | "warning" | "info" | "light" | "dark" | "link" | "outline-primary" | "outline-secondary" | "outline-success" | "outline-danger" | "outline-warning" | "outline-info" | "outline-light" | "outline-dark"; declare enum StorageType { COOKIE = "cookie", LOCAL = "local", NONE = "none" } declare enum Layout { BUTTON = "button", TOGGLE = "toggle" } interface DarkModeToggleEventDetail { isLight: boolean; theme: string; source: HTMLElement; roots: HTMLElement[]; } declare enum CustomEventTypes { CHANGE = "change" } type DarkModeToggleEventMap = ExtendableEventMap<"darkmode", { [CustomEventTypes.CHANGE]: DarkModeToggleEventDetail; }>; declare class DarkModeToggle extends Component<"darkmode", DarkModeToggleEventMap> { protected readonly PREFIX = "darkmode"; private readonly toggleOptions; private readonly toggleState; private storage?; private dom?; constructor(element: HTMLElement, opts?: {}); /** * Factory method to create an instance of DarkModeToggle. * @param element the root element for the dark mode toggle component. The component will look for configuration options in this element's attributes. * @param opts the user provided options to configure the dark mode toggle instance. These options will override any configuration found in the element's attributes. * @returns A promise that resolves to the created and initialized DarkModeToggle instance */ static create(element: HTMLElement, opts?: {}): Promise; protected doInit(): Promise<{ cancelled: boolean; reason?: string; }>; protected doAttach(): Promise<{ cancelled: boolean; reason?: string; }>; protected doDispose(): Promise<{ cancelled: boolean; reason?: string; }>; protected doDestroy(): Promise<{ cancelled: boolean; reason?: string; }>; /** * Sets up an event listener to handle external theme change events. * When an external theme change event is triggered, this method updates the control state. * @private */ private setupCrossInstanceSync; /** * Handles an external theme change event by updating the state and the DOM * if the root elements of the event and the component share roots. * * Implementation note: for performance reasons, DOM is only updated when the state is updated. * @private * @param e - The external theme change event */ private readonly handleExternalThemeChange; /** * Syncs the state of the dark mode toggle by updating the DOM and persisting the current theme to storage. * @private */ private syncState; toggle(silent?: boolean): void; light(silent?: boolean): void; dark(silent?: boolean): void; setStorageType(type: StorageType): void; /** * Triggers the events if silent is false. * The events are triggered with the current state of the dark mode toggle. * Emits the typed event via Component.emit and dispatches the legacy event manually. * @private * @param {boolean} silent - Whether to trigger the event. */ private trigger; /** * Persist the current theme to the storage. * @private */ private persistTheme; /** * Applies the preferred color scheme based on cookies or system preference * @returns a boolean indicating whether a preference was applied (true) or not (false) * @throws Error on storage provider failure */ private applyPreferredScheme; /** * Applies the color scheme based on stored preference if available * @returns a boolean indicating whether a preference was applied (true) or not (false) * @throws Error on storage provider failure */ private applyStoredPreference; /** * Applies the color scheme based on system preferences if available * @returns a boolean indicating whether a preference was applied (true) or not (false) */ private applySystemPreference; /** * Gets the system color scheme preference if available * @returns color scheme preference as `ColorModes` */ private getSystemPreference; /** * Checks if the bs-darkmode-toggle instance has been destroyed. * If it has, throws an error indicating that the instance is no longer usable. * This is a safety measure to prevent accessing methods of a destroyed instance. * @throws {Error} If the instance has been destroyed. */ private ensureNotDestroyed; } /** * Custom monitor for darkmode component that logs custom events. * * Extends the base Monitor to add logging for darkmode:change events at DEBUG level. * All other log levels (DEBUG, WARN, ERROR) are handled by the base implementation. * * @example * ```typescript * // Start monitoring with INFO level * window.Darkmode.MONITOR.start('INFO'); * * // Enable full debugging (lifecycle + custom events) * window.Darkmode.MONITOR.start('DEBUG'); * * // Stop monitoring * window.Darkmode.MONITOR.stop(); * ``` */ declare class DarkModeMonitor extends Monitor<"darkmode"> { private static instance; private constructor(); /** * Gets the singleton instance of DarkModeMonitor. * @returns The DarkModeMonitor instance. */ static getInstance(): DarkModeMonitor; /** * Sets up DEBUG level logging. * * Extends base DEBUG behavior by adding logging for darkmode:change events. * Preserves all base functionality by calling super.setupDebug() first. */ protected setupDebug(): void; } declare enum Methods { LIGHT = "LIGHT", DARK = "DARK", TOGGLE = "TOGGLE", SET_STORAGE = "SET_STORAGE", DESTROY = "DESTROY" } declare enum ColorModes { DARK = "dark", LIGHT = "light", NONE = "none" } export { DarkModeMonitor, DarkModeToggle, ColorModes as DarkModeToggleColorModes, Methods as DarkModeToggleMethods, Layout, StorageType }; export type { DarkModeOptions, DarkModeToggleEventDetail, ToggleStyle };