@ -1,25 +1,8 @@
export function toggleTheme() {
export class ThemeManager {
document . documentElement . classList . toggle ( 'dark' ) ;
document . documentElement . classList . toggle ( 'light' ) ;
localStorage . setItem ( 'theme' , document . documentElement . classList . contains ( 'dark' ) ? "dark" : "light" ) ;
}
export function loadTheme() {
private _themeButton : HTMLElement = null ;
const theme = localStorage . getItem ( "theme" ) ;
if ( theme == "dark" ) {
document . documentElement . classList . add ( 'dark' ) ;
document . documentElement . classList . remove ( 'light' ) ;
} else if ( theme == "light" ) {
document . documentElement . classList . add ( 'light' ) ;
document . documentElement . classList . remove ( 'dark' ) ;
} else if ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . media !== 'not all' ) {
document . documentElement . classList . add ( 'dark' ) ;
document . documentElement . classList . remove ( 'light' ) ;
}
}
export class nightwind {
private _beforeTransition = ( ) = > {
beforeTransition = ( ) = > {
const doc = document . documentElement ;
const doc = document . documentElement ;
const onTransitionDone = ( ) = > {
const onTransitionDone = ( ) = > {
doc . classList . remove ( 'nightwind' ) ;
doc . classList . remove ( 'nightwind' ) ;
@ -30,19 +13,53 @@ export class nightwind {
doc . classList . add ( 'nightwind' ) ;
doc . classList . add ( 'nightwind' ) ;
}
}
} ;
} ;
constructor ( ) {
private _updateThemeIcon = ( ) = > {
const icon = this . _themeButton . childNodes [ 0 ] as HTMLElement ;
if ( document . documentElement . classList . contains ( "dark" ) ) {
icon . classList . add ( "ri-sun-line" ) ;
icon . classList . remove ( "ri-moon-line" ) ;
this . _themeButton . classList . add ( "~warning" ) ;
this . _themeButton . classList . remove ( "~neutral" ) ;
this . _themeButton . classList . remove ( "@high" ) ;
} else {
icon . classList . add ( "ri-moon-line" ) ;
icon . classList . remove ( "ri-sun-line" ) ;
this . _themeButton . classList . add ( "@high" ) ;
this . _themeButton . classList . add ( "~neutral" ) ;
this . _themeButton . classList . remove ( "~warning" ) ;
}
} ;
bindButton = ( button : HTMLElement ) = > {
this . _themeButton = button ;
this . _themeButton . onclick = this . toggle ;
this . _updateThemeIcon ( ) ;
}
toggle = ( ) = > {
this . _toggle ( ) ;
if ( this . _themeButton ) {
this . _updateThemeIcon ( ) ;
}
}
constructor ( button? : HTMLElement ) {
const theme = localStorage . getItem ( "theme" ) ;
const theme = localStorage . getItem ( "theme" ) ;
if ( theme == "dark" ) {
if ( theme == "dark" ) {
this . enable ( true ) ;
this . _ enable( true ) ;
} else if ( theme == "light" ) {
} else if ( theme == "light" ) {
this . enable ( false ) ;
this . _ enable( false ) ;
} else if ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . media !== 'not all' ) {
} else if ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . media !== 'not all' ) {
this . enable ( true ) ;
this . _ enable( true ) ;
}
}
if ( arguments . length == 1 )
this . bindButton ( button ) ;
}
}
toggle = ( ) = > {
private _ toggle = ( ) = > {
this . beforeTransition ( ) ;
this . _ beforeTransition( ) ;
if ( ! document . documentElement . classList . contains ( 'dark' ) ) {
if ( ! document . documentElement . classList . contains ( 'dark' ) ) {
document . documentElement . classList . add ( 'dark' ) ;
document . documentElement . classList . add ( 'dark' ) ;
} else {
} else {
@ -51,17 +68,24 @@ export class nightwind {
localStorage . setItem ( 'theme' , document . documentElement . classList . contains ( 'dark' ) ? "dark" : "light" ) ;
localStorage . setItem ( 'theme' , document . documentElement . classList . contains ( 'dark' ) ? "dark" : "light" ) ;
} ;
} ;
enable = ( dark : boolean ) = > {
private _ enable = ( dark : boolean ) = > {
const mode = dark ? "dark" : "light" ;
const mode = dark ? "dark" : "light" ;
const opposite = dark ? "light" : "dark" ;
const opposite = dark ? "light" : "dark" ;
localStorage . setItem ( 'theme' , dark ? "dark" : "light" ) ;
localStorage . setItem ( 'theme' , dark ? "dark" : "light" ) ;
this . beforeTransition( ) ;
this . _ beforeTransition( ) ;
if ( document . documentElement . classList . contains ( opposite ) ) {
if ( document . documentElement . classList . contains ( opposite ) ) {
document . documentElement . classList . remove ( opposite ) ;
document . documentElement . classList . remove ( opposite ) ;
}
}
document . documentElement . classList . add ( mode ) ;
document . documentElement . classList . add ( mode ) ;
} ;
} ;
enable = ( dark : boolean ) = > {
this . _enable ( dark ) ;
if ( this . _themeButton != null ) {
this . _updateThemeIcon ( ) ;
}
} ;
}
}