|
| 1 | +import { EventListenerManager } from '@/core/eventListenerManager'; |
| 2 | +import { defaultOptions } from '@/core/defaultOptions'; |
| 3 | +import { isBrowser } from '@/utils'; |
| 4 | +import type { DarkifyPlugin, Options } from '@/types'; |
| 5 | + |
| 6 | +export class Darkify { |
| 7 | + private static readonly storageKey: string = 'theme'; |
| 8 | + public readonly options: Options = defaultOptions; |
| 9 | + private plugins: DarkifyPlugin[] = []; |
| 10 | + public theme: string = 'light'; |
| 11 | + private _elm!: EventListenerManager; |
| 12 | + private _meta!: HTMLMetaElement; |
| 13 | + private _style!: HTMLStyleElement; |
| 14 | + |
| 15 | + /** |
| 16 | + * Creates a new Darkify instance with default options |
| 17 | + * @param element - Button ID (recommended) or HTML element selector |
| 18 | + */ |
| 19 | + constructor(element: string); |
| 20 | + |
| 21 | + /** |
| 22 | + * Creates a new Darkify instance with custom options only |
| 23 | + * @param options - Options |
| 24 | + */ |
| 25 | + constructor(options: Partial<Options>); |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates a new Darkify instance for managing dark/light theme |
| 29 | + * @param element - Button ID (recommended) or HTML element selector |
| 30 | + * @param options - Options |
| 31 | + * @see {@link https://github.com/emrocode/darkify-js/wiki|Documentation} |
| 32 | + */ |
| 33 | + constructor(element: string, options: Partial<Options>); |
| 34 | + |
| 35 | + constructor(element?: string | Partial<Options>, options?: Partial<Options>) { |
| 36 | + if (!isBrowser) return; |
| 37 | + |
| 38 | + this._elm = new EventListenerManager(); |
| 39 | + |
| 40 | + const el = typeof element === 'string' ? element : undefined; |
| 41 | + const inputOpts = |
| 42 | + element && typeof element === 'object' ? (element as Partial<Options>) : options; |
| 43 | + const opts: Options = { ...defaultOptions, ...inputOpts }; |
| 44 | + |
| 45 | + this.options = opts; |
| 46 | + this.theme = this.getOsPreference(); |
| 47 | + this._style = document.createElement('style'); |
| 48 | + this._meta = document.createElement('meta'); |
| 49 | + |
| 50 | + this.createAttribute(); |
| 51 | + this.init(el); |
| 52 | + this.syncThemeBetweenTabs(); |
| 53 | + } |
| 54 | + |
| 55 | + private init(element?: string): void { |
| 56 | + this._elm.addListener( |
| 57 | + window.matchMedia('(prefers-color-scheme: dark)'), |
| 58 | + 'change', |
| 59 | + ({ matches: isDark }: MediaQueryListEvent) => { |
| 60 | + this.theme = isDark ? 'dark' : 'light'; |
| 61 | + this.createAttribute(); |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + const setup = () => { |
| 66 | + this.initPlugins(); |
| 67 | + const hasWidget = this.plugins.some(p => p.el !== undefined); |
| 68 | + if (element && !hasWidget) { |
| 69 | + const htmlElement = document.querySelector<HTMLElement>(element); |
| 70 | + if (htmlElement) { |
| 71 | + this._elm.addListener(htmlElement, 'click', () => this.toggleTheme()); |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + if (document.readyState !== 'loading') { |
| 77 | + return setup(); |
| 78 | + } |
| 79 | + |
| 80 | + this._elm.addListener(document, 'DOMContentLoaded', setup); |
| 81 | + } |
| 82 | + |
| 83 | + private initPlugins(): void { |
| 84 | + this.options.usePlugins?.forEach(p => { |
| 85 | + const [Plugin, pluginOptions] = Array.isArray(p) ? p : [p, undefined]; |
| 86 | + const plugin = new Plugin(this, pluginOptions); |
| 87 | + |
| 88 | + const renderedNode = plugin.render(); |
| 89 | + if (renderedNode) { |
| 90 | + plugin.el = renderedNode; |
| 91 | + } |
| 92 | + |
| 93 | + this.plugins.push(plugin); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + private notifyPlugins(theme: string) { |
| 98 | + this.plugins.forEach(plugin => { |
| 99 | + plugin.onThemeChange?.(theme); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private getStorage(): Storage | undefined { |
| 104 | + const { useStorage } = this.options; |
| 105 | + if (useStorage === 'none') return; |
| 106 | + return useStorage === 'local' ? window.localStorage : window.sessionStorage; |
| 107 | + } |
| 108 | + |
| 109 | + private getOsPreference(): string { |
| 110 | + const storage = this.getStorage(); |
| 111 | + |
| 112 | + if (storage) { |
| 113 | + const stored = storage.getItem(Darkify.storageKey); |
| 114 | + if (stored) return stored; |
| 115 | + } |
| 116 | + |
| 117 | + if (this.options.autoMatchTheme) { |
| 118 | + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
| 119 | + } |
| 120 | + |
| 121 | + return 'light'; |
| 122 | + } |
| 123 | + |
| 124 | + private createAttribute(): void { |
| 125 | + const dataTheme = document.documentElement; |
| 126 | + const { useColorScheme } = this.options; |
| 127 | + |
| 128 | + const css = `/**! Darkify / A simple dark mode toggle library **/\n:root:where([data-theme="${this.theme}"]),[data-theme="${this.theme}"]{color-scheme:${this.theme}}`; |
| 129 | + |
| 130 | + dataTheme.dataset.theme = this.theme; |
| 131 | + |
| 132 | + this.updateTags(css, useColorScheme); |
| 133 | + this.savePreference(); |
| 134 | + } |
| 135 | + |
| 136 | + private updateTags(css: string, useColorScheme: Options['useColorScheme']) { |
| 137 | + const [lightColor, darkColor] = useColorScheme; |
| 138 | + |
| 139 | + this._meta.name = 'theme-color'; |
| 140 | + this._meta.media = `(prefers-color-scheme: ${this.theme})`; |
| 141 | + this._meta.content = this.theme === 'light' ? lightColor : (darkColor ?? lightColor); |
| 142 | + this._style.innerHTML = css; |
| 143 | + |
| 144 | + const head = document.head; |
| 145 | + |
| 146 | + // avoid tags duplication |
| 147 | + if (!this._meta.parentNode) head.appendChild(this._meta); |
| 148 | + if (!this._style.parentNode) head.appendChild(this._style); |
| 149 | + } |
| 150 | + |
| 151 | + private savePreference(): void { |
| 152 | + const { useStorage } = this.options; |
| 153 | + if (useStorage === 'none') return; |
| 154 | + const storage = useStorage === 'local'; |
| 155 | + |
| 156 | + const STO = storage ? window.localStorage : window.sessionStorage; |
| 157 | + const OTS = storage ? window.sessionStorage : window.localStorage; |
| 158 | + |
| 159 | + OTS.removeItem(Darkify.storageKey); |
| 160 | + STO.setItem(Darkify.storageKey, this.theme); |
| 161 | + } |
| 162 | + |
| 163 | + private syncThemeBetweenTabs(): void { |
| 164 | + this._elm.addListener(window, 'storage', (e: StorageEvent) => { |
| 165 | + if (e.key === Darkify.storageKey && e.newValue) { |
| 166 | + this.theme = e.newValue; |
| 167 | + this.createAttribute(); |
| 168 | + this.notifyPlugins(e.newValue); |
| 169 | + } |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + private setTheme(newTheme: 'light' | 'dark'): void { |
| 174 | + this.theme = newTheme; |
| 175 | + this.createAttribute(); |
| 176 | + this.notifyPlugins(newTheme); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Toggles the theme between light and dark modes |
| 181 | + */ |
| 182 | + toggleTheme(): void { |
| 183 | + this.setTheme(this.theme === 'light' ? 'dark' : 'light'); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Retrieves the currently active theme |
| 188 | + * @returns The current theme name ('light' or 'dark') |
| 189 | + */ |
| 190 | + getCurrentTheme(): string { |
| 191 | + return this.theme; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Destroys the Darkify instance and cleans up all resources |
| 196 | + * |
| 197 | + * Removes all event listeners (system theme changes, click handlers, storage events), |
| 198 | + * destroys all active plugins, removes injected DOM elements (<style> and <meta> tags), |
| 199 | + * and frees associated resources. |
| 200 | + * Call this method when the instance is no longer needed to prevent memory leaks. |
| 201 | + */ |
| 202 | + destroy(): void { |
| 203 | + this._elm.clearListeners(); |
| 204 | + |
| 205 | + // remove injected DOM elements |
| 206 | + this._style?.parentNode?.removeChild(this._style); |
| 207 | + this._meta?.parentNode?.removeChild(this._meta); |
| 208 | + |
| 209 | + // destroy plugins |
| 210 | + if (this.plugins.length > 0) { |
| 211 | + this.plugins.forEach(plugin => { |
| 212 | + plugin.onDestroy?.(); |
| 213 | + }); |
| 214 | + |
| 215 | + this.plugins = []; |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments