1- import { EventListenerManager } from '@/eventListenerManager' ;
2- import { defaultOptions } from '@/defaultOptions' ;
3- import { isBrowser } from '@/isBrowser ' ;
1+ import { EventListenerManager } from '@/core/ eventListenerManager' ;
2+ import { defaultOptions } from '@/core/ defaultOptions' ;
3+ import { isBrowser } from '@/utils ' ;
44import type { DarkifyPlugin , Options } from '@/types' ;
55
66export class Darkify {
@@ -12,27 +12,43 @@ export class Darkify {
1212 private _meta ! : HTMLMetaElement ;
1313 private _style ! : HTMLStyleElement ;
1414
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+
1527 /**
1628 * Creates a new Darkify instance for managing dark/light theme
1729 * @param element - Button ID (recommended) or HTML element selector
18- * @param options - Configuration options for customizing behavior
30+ * @param options - Options
1931 * @see {@link https://github.com/emrocode/darkify-js/wiki|Documentation }
2032 */
21- constructor ( element : string , options : Partial < Options > ) {
33+ constructor ( element : string , options : Partial < Options > ) ;
34+
35+ constructor ( element ?: string | Partial < Options > , options ?: Partial < Options > ) {
2236 if ( ! isBrowser ) return ;
2337
2438 this . _elm = new EventListenerManager ( ) ;
2539
26- // merge defaults with user options
27- const opts = { ...defaultOptions , ...options } as Options ;
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 } ;
2844
2945 this . options = opts ;
3046 this . theme = this . getOsPreference ( ) ;
3147 this . _style = document . createElement ( 'style' ) ;
3248 this . _meta = document . createElement ( 'meta' ) ;
3349
34- this . init ( element ) ;
3550 this . createAttribute ( ) ;
51+ this . init ( el ) ;
3652 this . syncThemeBetweenTabs ( ) ;
3753 }
3854
@@ -46,18 +62,22 @@ export class Darkify {
4662 }
4763 ) ;
4864
49- this . initPlugins ( ) ;
50-
51- const hasWidget = this . plugins . some ( p => p . el !== undefined ) ;
52-
53- if ( element && ! hasWidget ) {
54- this . _elm . addListener ( document , 'DOMContentLoaded' , ( ) => {
65+ const setup = ( ) => {
66+ this . initPlugins ( ) ;
67+ const hasWidget = this . plugins . some ( p => p . el !== undefined ) ;
68+ if ( element && ! hasWidget ) {
5569 const htmlElement = document . querySelector < HTMLElement > ( element ) ;
5670 if ( htmlElement ) {
5771 this . _elm . addListener ( htmlElement , 'click' , ( ) => this . toggleTheme ( ) ) ;
5872 }
59- } ) ;
73+ }
74+ } ;
75+
76+ if ( document . readyState !== 'loading' ) {
77+ return setup ( ) ;
6078 }
79+
80+ this . _elm . addListener ( document , 'DOMContentLoaded' , setup ) ;
6181 }
6282
6383 private initPlugins ( ) : void {
@@ -66,7 +86,7 @@ export class Darkify {
6686 const plugin = new Plugin ( this , pluginOptions ) ;
6787
6888 const renderedNode = plugin . render ( ) ;
69- if ( renderedNode instanceof HTMLElement || renderedNode instanceof ShadowRoot ) {
89+ if ( renderedNode ) {
7090 plugin . el = renderedNode ;
7191 }
7292
@@ -189,10 +209,6 @@ export class Darkify {
189209 // destroy plugins
190210 if ( this . plugins . length > 0 ) {
191211 this . plugins . forEach ( plugin => {
192- if ( plugin . el ) {
193- ( plugin . el instanceof ShadowRoot ? plugin . el . host : plugin . el ) . remove ( ) ;
194- }
195-
196212 plugin . onDestroy ?.( ) ;
197213 } ) ;
198214
0 commit comments