diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro
index f564166..6cb5e21 100644
--- a/src/layouts/Layout.astro
+++ b/src/layouts/Layout.astro
@@ -21,115 +21,8 @@ const base = import.meta.env.BASE_URL;
{title}
diff --git a/src/utils/theme.ts b/src/utils/theme.ts
new file mode 100644
index 0000000..15e2fe6
--- /dev/null
+++ b/src/utils/theme.ts
@@ -0,0 +1,108 @@
+function hasLocalStorage(): boolean {
+ try {
+ const ls: Storage = localStorage;
+ const key: string = "__test";
+ ls.setItem(key, key);
+ ls.removeItem(key);
+ return true;
+ } catch (e) {
+ return (
+ e instanceof DOMException &&
+ e.name === "QuotaExceededError" &&
+ localStorage &&
+ localStorage.length !== 0
+ );
+ }
+}
+
+class ThemeToggler {
+ elements: NodeListOf;
+ btnThemeState: string = 'light';
+ exclude: string[] = ['df'];
+ ls: boolean = false;
+
+ constructor() {
+ this.ls = hasLocalStorage();
+ this.toggleThemeClass();
+ if (this.ls && ('theme' in localStorage && localStorage.theme !== 'undefined')) {
+ this.btnThemeState = localStorage.theme;
+ } else {
+ this.btnThemeState = 'system';
+ }
+ this.elements = document.querySelectorAll('[data-toggle-theme]');
+ this.elements.forEach((e) => {
+ this.setThemeState(e, this.btnThemeState);
+ e.addEventListener('click', this);
+ });
+ }
+
+ handleEvent() {
+ const state = localStorage.theme;
+ this.elements.forEach((e) => {
+ this.setNextTheme(e, state);
+ const s = this.getThemeState(e);
+ if (s != localStorage.theme) {
+ localStorage.theme = this.getThemeState(e);
+ }
+ });
+ this.toggleThemeClass();
+ }
+
+ getThemeState(element: Element): string {
+ const attr = element.getAttribute('data-toggle-theme');
+ return attr != null ? attr : 'light';
+ }
+
+ getSysTheme(): string {
+ return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
+ }
+
+ setThemeState(element: Element, state: string) {
+ element.setAttribute('data-toggle-theme', state);
+ }
+
+ setNextTheme(element: Element, state: string) {
+ const states = ['light', 'dark', 'system'];
+ let next = states.indexOf(state) + 1;
+ if (next === states.length) {
+ next = 0;
+ }
+ element.setAttribute('data-toggle-theme', states[next]);
+ }
+
+ toggleThemeClass() {
+ const ele = document.documentElement;
+ for (const e of this.exclude) {
+ if (ele.classList.contains(e)) {
+ return;
+ }
+ }
+ if (localStorage.af_enabled === '1') {
+ if (localStorage.af_mode === 'df') {
+ ele.classList.remove('dark');
+ ele.classList.remove('hc');
+ ele.classList.add('df');
+ }
+ if (localStorage.af_mode === 'hc') {
+ ele.classList.remove('dark');
+ ele.classList.remove('df');
+ ele.classList.add('hc');
+ }
+ return;
+ }
+ switch (localStorage.theme) {
+ case 'light':
+ ele.classList.remove('dark');
+ break;
+ case 'dark':
+ ele.classList.add('dark');
+ break;
+ default:
+ ele.classList.toggle('dark', this.getSysTheme() === 'dark');
+ }
+ }
+}
+
+export const theme = () => {
+ new ThemeToggler();
+}
\ No newline at end of file