2023-06-16 12:43:34 +00:00
|
|
|
export class ThemeManager {
|
2021-01-05 18:16:23 +00:00
|
|
|
|
2023-06-16 12:43:34 +00:00
|
|
|
private _themeButton: HTMLElement = null;
|
setup: flex-ify, light/dark, keep page position on reload
got rid of a bunch of m[l/r/x/y]-x tailwind classes and used more
flex-[row/col] gap-2's. UI should be more consistent in general, and
with the admin UI.
The page you were on is actually read from the URL on reload, however
does not keep settings (implemented just for ease of UI editing,
really).
`missing-colors.js` preprocessor script now applies dark prefixes for
<section>s, but like with cards, does not apply a default ~neutral to
those without, so that <section class=""> looks different to <section
class="~neutral">.
Light/dark selector added to setup too, and the actual mode given to the
browser through CSS `color-scheme` is correct, meaning things like textareas, checkboxes and
controls are now colored according to the theme.
2024-08-21 15:13:17 +00:00
|
|
|
private _metaTag: HTMLMetaElement;
|
2023-06-16 12:43:34 +00:00
|
|
|
|
|
|
|
private _beforeTransition = () => {
|
2021-12-30 23:52:53 +00:00
|
|
|
const doc = document.documentElement;
|
|
|
|
const onTransitionDone = () => {
|
|
|
|
doc.classList.remove('nightwind');
|
|
|
|
doc.removeEventListener('transitionend', onTransitionDone);
|
|
|
|
}
|
|
|
|
doc.addEventListener('transitionend', onTransitionDone);
|
|
|
|
if (!doc.classList.contains('nightwind')) {
|
|
|
|
doc.classList.add('nightwind');
|
|
|
|
}
|
|
|
|
};
|
2023-06-16 12:43:34 +00:00
|
|
|
|
|
|
|
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) {
|
setup: flex-ify, light/dark, keep page position on reload
got rid of a bunch of m[l/r/x/y]-x tailwind classes and used more
flex-[row/col] gap-2's. UI should be more consistent in general, and
with the admin UI.
The page you were on is actually read from the URL on reload, however
does not keep settings (implemented just for ease of UI editing,
really).
`missing-colors.js` preprocessor script now applies dark prefixes for
<section>s, but like with cards, does not apply a default ~neutral to
those without, so that <section class=""> looks different to <section
class="~neutral">.
Light/dark selector added to setup too, and the actual mode given to the
browser through CSS `color-scheme` is correct, meaning things like textareas, checkboxes and
controls are now colored according to the theme.
2024-08-21 15:13:17 +00:00
|
|
|
this._metaTag = document.querySelector("meta[name=color-scheme]") as HTMLMetaElement;
|
2021-12-30 23:52:53 +00:00
|
|
|
const theme = localStorage.getItem("theme");
|
|
|
|
if (theme == "dark") {
|
2023-06-16 12:43:34 +00:00
|
|
|
this._enable(true);
|
2021-12-30 23:52:53 +00:00
|
|
|
} else if (theme == "light") {
|
2023-06-16 12:43:34 +00:00
|
|
|
this._enable(false);
|
2021-12-30 23:52:53 +00:00
|
|
|
} else if (window.matchMedia('(prefers-color-scheme: dark)').media !== 'not all') {
|
2023-06-16 12:43:34 +00:00
|
|
|
this._enable(true);
|
2021-12-30 23:52:53 +00:00
|
|
|
}
|
2023-06-16 12:43:34 +00:00
|
|
|
|
setup: flex-ify, light/dark, keep page position on reload
got rid of a bunch of m[l/r/x/y]-x tailwind classes and used more
flex-[row/col] gap-2's. UI should be more consistent in general, and
with the admin UI.
The page you were on is actually read from the URL on reload, however
does not keep settings (implemented just for ease of UI editing,
really).
`missing-colors.js` preprocessor script now applies dark prefixes for
<section>s, but like with cards, does not apply a default ~neutral to
those without, so that <section class=""> looks different to <section
class="~neutral">.
Light/dark selector added to setup too, and the actual mode given to the
browser through CSS `color-scheme` is correct, meaning things like textareas, checkboxes and
controls are now colored according to the theme.
2024-08-21 15:13:17 +00:00
|
|
|
if (button)
|
2023-06-16 12:43:34 +00:00
|
|
|
this.bindButton(button);
|
2021-12-30 23:52:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 12:43:34 +00:00
|
|
|
private _toggle = () => {
|
setup: flex-ify, light/dark, keep page position on reload
got rid of a bunch of m[l/r/x/y]-x tailwind classes and used more
flex-[row/col] gap-2's. UI should be more consistent in general, and
with the admin UI.
The page you were on is actually read from the URL on reload, however
does not keep settings (implemented just for ease of UI editing,
really).
`missing-colors.js` preprocessor script now applies dark prefixes for
<section>s, but like with cards, does not apply a default ~neutral to
those without, so that <section class=""> looks different to <section
class="~neutral">.
Light/dark selector added to setup too, and the actual mode given to the
browser through CSS `color-scheme` is correct, meaning things like textareas, checkboxes and
controls are now colored according to the theme.
2024-08-21 15:13:17 +00:00
|
|
|
let metaValue = "light dark";
|
2023-06-16 12:43:34 +00:00
|
|
|
this._beforeTransition();
|
2021-12-30 23:52:53 +00:00
|
|
|
if (!document.documentElement.classList.contains('dark')) {
|
|
|
|
document.documentElement.classList.add('dark');
|
setup: flex-ify, light/dark, keep page position on reload
got rid of a bunch of m[l/r/x/y]-x tailwind classes and used more
flex-[row/col] gap-2's. UI should be more consistent in general, and
with the admin UI.
The page you were on is actually read from the URL on reload, however
does not keep settings (implemented just for ease of UI editing,
really).
`missing-colors.js` preprocessor script now applies dark prefixes for
<section>s, but like with cards, does not apply a default ~neutral to
those without, so that <section class=""> looks different to <section
class="~neutral">.
Light/dark selector added to setup too, and the actual mode given to the
browser through CSS `color-scheme` is correct, meaning things like textareas, checkboxes and
controls are now colored according to the theme.
2024-08-21 15:13:17 +00:00
|
|
|
metaValue = "dark light";
|
2021-12-30 23:52:53 +00:00
|
|
|
} else {
|
|
|
|
document.documentElement.classList.remove('dark');
|
|
|
|
}
|
|
|
|
localStorage.setItem('theme', document.documentElement.classList.contains('dark') ? "dark" : "light");
|
setup: flex-ify, light/dark, keep page position on reload
got rid of a bunch of m[l/r/x/y]-x tailwind classes and used more
flex-[row/col] gap-2's. UI should be more consistent in general, and
with the admin UI.
The page you were on is actually read from the URL on reload, however
does not keep settings (implemented just for ease of UI editing,
really).
`missing-colors.js` preprocessor script now applies dark prefixes for
<section>s, but like with cards, does not apply a default ~neutral to
those without, so that <section class=""> looks different to <section
class="~neutral">.
Light/dark selector added to setup too, and the actual mode given to the
browser through CSS `color-scheme` is correct, meaning things like textareas, checkboxes and
controls are now colored according to the theme.
2024-08-21 15:13:17 +00:00
|
|
|
|
|
|
|
// this._metaTag.setAttribute("content", metaValue);
|
2021-12-30 23:52:53 +00:00
|
|
|
};
|
|
|
|
|
2023-06-16 12:43:34 +00:00
|
|
|
private _enable = (dark: boolean) => {
|
2021-12-30 23:52:53 +00:00
|
|
|
const mode = dark ? "dark" : "light";
|
|
|
|
const opposite = dark ? "light" : "dark";
|
|
|
|
|
|
|
|
localStorage.setItem('theme', dark ? "dark" : "light");
|
|
|
|
|
2023-06-16 12:43:34 +00:00
|
|
|
this._beforeTransition();
|
2021-12-30 23:52:53 +00:00
|
|
|
|
|
|
|
if (document.documentElement.classList.contains(opposite)) {
|
|
|
|
document.documentElement.classList.remove(opposite);
|
|
|
|
}
|
|
|
|
document.documentElement.classList.add(mode);
|
setup: flex-ify, light/dark, keep page position on reload
got rid of a bunch of m[l/r/x/y]-x tailwind classes and used more
flex-[row/col] gap-2's. UI should be more consistent in general, and
with the admin UI.
The page you were on is actually read from the URL on reload, however
does not keep settings (implemented just for ease of UI editing,
really).
`missing-colors.js` preprocessor script now applies dark prefixes for
<section>s, but like with cards, does not apply a default ~neutral to
those without, so that <section class=""> looks different to <section
class="~neutral">.
Light/dark selector added to setup too, and the actual mode given to the
browser through CSS `color-scheme` is correct, meaning things like textareas, checkboxes and
controls are now colored according to the theme.
2024-08-21 15:13:17 +00:00
|
|
|
|
|
|
|
// this._metaTag.setAttribute("content", `${mode} ${opposite}`);
|
2021-12-30 23:52:53 +00:00
|
|
|
};
|
2023-06-16 12:43:34 +00:00
|
|
|
|
|
|
|
enable = (dark: boolean) => {
|
|
|
|
this._enable(dark);
|
|
|
|
if (this._themeButton != null) {
|
|
|
|
this._updateThemeIcon();
|
|
|
|
}
|
|
|
|
};
|
2021-12-30 23:52:53 +00:00
|
|
|
}
|