Skip to content

Commit b6fb051

Browse files
committed
Minor changes
1 parent 492d53c commit b6fb051

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

src/darkify.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { type Options } from '../types';
55
export class Darkify {
66
private static readonly storageKey = 'theme';
77
readonly options: Options = {};
8-
theme!: { value: string };
9-
_cssTag!: HTMLStyleElement;
10-
_metaTag!: HTMLMetaElement;
8+
theme: string = 'light';
9+
_style!: HTMLStyleElement;
10+
_meta!: HTMLMetaElement;
1111

1212
/**
1313
* @param {string} element Button ID ( recommended ) or HTML element
@@ -26,9 +26,9 @@ export class Darkify {
2626
this.options = options;
2727

2828
this.init(element);
29-
this.theme = { value: this.getOsPreference(options) };
30-
this._cssTag = document.createElement('style');
31-
this._metaTag = document.createElement('meta');
29+
this.theme = this.getOsPreference(options);
30+
this._style = document.createElement('style');
31+
this._meta = document.createElement('meta');
3232
this.createAttribute();
3333
this.syncThemeBetweenTabs();
3434
}
@@ -37,59 +37,56 @@ export class Darkify {
3737
window
3838
.matchMedia('(prefers-color-scheme: dark)')
3939
.addEventListener('change', ({ matches: isDark }) => {
40-
this.theme.value = isDark ? 'dark' : 'light';
40+
this.theme = isDark ? 'dark' : 'light';
4141
this.savePreference();
4242
});
4343

4444
document.addEventListener('DOMContentLoaded', () => {
45-
this.createAttribute();
4645
const htmlElement = document.querySelector<HTMLElement>(element);
4746
htmlElement?.addEventListener('click', () => this.toggleTheme());
4847
});
4948
}
5049

5150
// get os color preference
52-
getOsPreference(options: Options): string | 'light' {
51+
getOsPreference(options: Options): string {
5352
const { autoMatchTheme, useLocalStorage, useSessionStorage } = options;
5453
const STO =
5554
(useLocalStorage && window.localStorage.getItem(Darkify.storageKey)) ||
56-
(useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey));
57-
58-
return (
59-
STO ||
55+
(useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey)) ||
6056
(autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
6157
? 'dark'
62-
: 'light')
63-
);
58+
: 'light');
59+
60+
return STO;
6461
}
6562

6663
createAttribute() {
6764
const dataTheme = document.getElementsByTagName('html')[0];
6865
const { useColorScheme } = this.options;
69-
let css = `/**! Darkify / A simple dark mode toggle library **/\n:root:is([data-theme="${this.theme.value}"]),[data-theme="${this.theme.value}"]{color-scheme:${this.theme.value}}`;
66+
let css = `/**! Darkify / A simple dark mode toggle library **/\n:root:is([data-theme="${this.theme}"]),[data-theme="${this.theme}"]{color-scheme:${this.theme}}`;
7067

71-
dataTheme.setAttribute('data-theme', this.theme.value);
68+
dataTheme.setAttribute('data-theme', this.theme);
7269

73-
this.updateTags(css, useColorScheme || []);
70+
this.updateTags(css, useColorScheme ?? []);
7471
this.savePreference();
7572
}
7673

7774
updateTags(css: string, useColorScheme: string[]) {
7875
const head = document.head || document.getElementsByTagName('head')[0];
7976

8077
// update theme-color meta tag
81-
this._metaTag.setAttribute('name', 'theme-color');
82-
this._metaTag.setAttribute(
78+
this._meta.setAttribute('name', 'theme-color');
79+
this._meta.setAttribute(
8380
'content',
84-
this.theme.value === 'light' ? useColorScheme[0] : useColorScheme[1]
81+
this.theme === 'light' ? useColorScheme[0] : useColorScheme[1]
8582
);
8683

8784
// update style tag
88-
this._cssTag.setAttribute('type', 'text/css');
89-
this._cssTag.innerHTML = css;
85+
this._style.setAttribute('type', 'text/css');
86+
this._style.innerHTML = css;
9087

91-
head.appendChild(this._metaTag);
92-
head.appendChild(this._cssTag);
88+
head.appendChild(this._meta);
89+
head.appendChild(this._style);
9390
}
9491

9592
// save to local or session storage
@@ -99,13 +96,13 @@ export class Darkify {
9996
const OTS = useLocalStorage ? window.sessionStorage : window.localStorage;
10097

10198
OTS.removeItem(Darkify.storageKey);
102-
STO.setItem(Darkify.storageKey, this.theme.value);
99+
STO.setItem(Darkify.storageKey, this.theme);
103100
}
104101

105102
syncThemeBetweenTabs() {
106103
window.addEventListener('storage', e => {
107104
if (e.key === Darkify.storageKey && e.newValue) {
108-
this.theme.value = e.newValue;
105+
this.theme = e.newValue;
109106
this.createAttribute();
110107
}
111108
});
@@ -116,7 +113,7 @@ export class Darkify {
116113
* @returns {void}
117114
*/
118115
toggleTheme(): void {
119-
this.theme.value = this.theme.value === 'light' ? 'dark' : 'light';
116+
this.theme = this.theme === 'light' ? 'dark' : 'light';
120117
this.createAttribute();
121118
}
122119

@@ -125,6 +122,6 @@ export class Darkify {
125122
* @returns {string}
126123
*/
127124
getCurrentTheme(): string {
128-
return this.theme.value;
125+
return this.theme;
129126
}
130127
}

0 commit comments

Comments
 (0)