1- import './darkmode.css'
21
32const LOCAL_STORAGE_KEY = 'nightowl-color-scheme'
43const LIGHT = 'light' ;
54const DARK = 'dark' ;
65
7- let store : Storage | null = null ;
6+ let store : Storage | null = null ;
87let persistPreference = true ;
98let mode = LIGHT ;
109
@@ -14,15 +13,58 @@ try {
1413 // Do nothing. The user probably blocks cookies.
1514}
1615
16+ function loadCss ( ) {
17+ // fix css loading via vite
18+ // https://github.com/vitejs/vite/issues/8976
19+ const css = document . createElement ( 'style' ) ;
20+ css . innerHTML = `
21+ /* Prevent inconsistencies for positioning */
22+ .nightowl-light body{
23+ filter: invert(0%);
24+ }
25+
26+ .nightowl-dark {
27+ /* Firefox fallback. */
28+ background-color: #111;
29+ }
30+
31+ .nightowl-dark body {
32+ filter: invert(100%) hue-rotate(180deg);
33+ }
34+
35+ /* Do not invert media (revert the invert). */
36+ .nightowl-dark img, .nightowl-dark video, .nightowl-dark iframe {
37+ filter: invert(100%) hue-rotate(180deg);
38+ }
39+
40+ /* Improve contrast on icons. */
41+ .nightowl-dark .icon {
42+ filter: invert(15%) hue-rotate(180deg);
43+ }
44+
45+ /* Re-enable code block backgrounds. */
46+ .nightowl-dark pre {
47+ filter: invert(6%);
48+ }
49+
50+ /* Improve contrast on list item markers. */
51+ .nightowl-dark li::marker {
52+ color: #666;
53+ }
54+ ` ;
55+ document . head . appendChild ( css ) ;
56+ }
57+
1758window . addEventListener ( 'load' , ( ) => {
18- initializeNightowl ( ) ;
19- initializeSwitcher ( ) ;
59+ loadCss ( ) ;
60+ initializeNightowl ( ) ;
61+ initializeSwitcher ( ) ;
2062} ) ;
2163
2264function enableDarkMode ( ) {
2365 mode = DARK ;
2466 const htmlElement = document . querySelector ( 'html' ) ;
25- if ( htmlElement ) {
67+ if ( htmlElement ) {
2668 htmlElement . classList . remove ( 'nightowl-light' ) ;
2769 htmlElement . classList . add ( 'nightowl-dark' ) ;
2870 }
@@ -31,7 +73,7 @@ function enableDarkMode() {
3173function enableLightMode ( ) {
3274 mode = LIGHT ;
3375 const htmlElement = document . querySelector ( 'html' ) ;
34- if ( htmlElement ) {
76+ if ( htmlElement ) {
3577 htmlElement . classList . remove ( 'nightowl-dark' ) ;
3678 htmlElement . classList . add ( 'nightowl-light' ) ;
3779 }
@@ -43,19 +85,18 @@ function toggleMode() {
4385}
4486
4587function updateMode ( ) {
46- if ( mode === DARK ) {
88+ if ( mode === DARK ) {
4789 enableDarkMode ( ) ;
48- }
49- else {
90+ } else {
5091 enableLightMode ( ) ;
5192 }
5293 setSwitcherIcon ( ) ;
5394}
5495
5596function setSwitcherIcon ( ) {
5697 const switcher = document . getElementById ( 'nightowl-switcher-default' ) ;
57- if ( switcher ) {
58- const lightIcon = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="width: 25px; height:25px;">\n' +
98+ if ( switcher ) {
99+ const lightIcon = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="width: 25px; height:25px;">\n' +
59100 ' <path stroke-linecap="round" stroke-linejoin="round" d="M12 3v2.25m6.364.386l-1.591 1.591M21 12h-2.25m-.386 6.364l-1.591-1.591M12 18.75V21m-4.773-4.227l-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0z" />\n' +
60101 '</svg>' ;
61102 const darkIcon = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="width: 25px; height:25px;">\n' +
@@ -67,7 +108,7 @@ function setSwitcherIcon() {
67108
68109}
69110
70- function initializeSwitcher ( ) {
111+ function initializeSwitcher ( ) {
71112 const switcher = document . createElement ( 'div' ) ;
72113 console . log ( window . innerWidth ) ;
73114 switcher . id = 'nightowl-switcher-default' ;
@@ -108,7 +149,7 @@ function checkForRememberedValue() {
108149
109150 let rememberedValue = null ;
110151 try {
111- if ( store ) {
152+ if ( store ) {
112153 rememberedValue = store . getItem ( LOCAL_STORAGE_KEY ) ;
113154 }
114155 } catch ( err ) {
@@ -134,9 +175,9 @@ function initializeNightowl() {
134175}
135176
136177function storeModeInLocalStorage ( ) {
137- if ( persistPreference && mode !== null ) {
178+ if ( persistPreference && mode !== null ) {
138179 try {
139- if ( store ) {
180+ if ( store ) {
140181 store . setItem ( LOCAL_STORAGE_KEY , mode ) ;
141182 }
142183 } catch ( err ) {
0 commit comments