Skip to content

Commit 76332cd

Browse files
authored
refactor(theme): the theme method of initialization (#33)
1 parent 47efb9a commit 76332cd

4 files changed

Lines changed: 7 additions & 13 deletions

File tree

public/theme-init.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
document.head.appendChild(style);
2020
}
2121

22-
function readThemeCookie() {
22+
function isDarkTheme() {
2323
var themeCookie = document.cookie
2424
.split("; ")
2525
.find(function (row) {
@@ -39,7 +39,7 @@
3939
}
4040

4141
function initTheme() {
42-
var dark = readThemeCookie();
42+
var dark = isDarkTheme();
4343
syncThemeDom(dark);
4444
return dark;
4545
}
@@ -55,7 +55,7 @@
5555
}
5656

5757
window.__NODEGET_THEME__ = {
58-
readThemeCookie: readThemeCookie,
58+
isDarkTheme: isDarkTheme,
5959
syncThemeDom: syncThemeDom,
6060
initTheme: initTheme,
6161
applyTheme: applyTheme,

src/main.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { createPersistedState } from "pinia-plugin-persistedstate";
44
import { createI18n } from "vue-i18n";
55
import App from "./App.vue";
66
import router from "./router";
7-
import { useThemeStore } from "./stores/theme";
87
import routePrefetchPlugin from "./router/prefetchPlugin";
98
import "./style/app.css";
109
import en from "./locales/en";
@@ -32,7 +31,6 @@ const i18n = createI18n({
3231
});
3332

3433
pinia.use(createPersistedState());
35-
useThemeStore(pinia).init();
3634

3735
app.use(pinia);
3836
app.use(routePrefetchPlugin(router));

src/stores/theme.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { ref } from "vue";
33
import { applyTheme, initTheme } from "@/theme/dom";
44

55
export const useThemeStore = defineStore("theme", () => {
6-
const isDark = ref(false);
7-
8-
const init = () => {
9-
isDark.value = initTheme();
10-
};
6+
const isDark = ref(!!initTheme());
117

128
const setTheme = (dark: boolean) => {
139
isDark.value = dark;
@@ -18,5 +14,5 @@ export const useThemeStore = defineStore("theme", () => {
1814
setTheme(!isDark.value);
1915
};
2016

21-
return { isDark, init, setTheme, toggle };
17+
return { isDark, setTheme, toggle };
2218
});

src/theme/dom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type ThemeApi = {
2-
readThemeCookie: () => boolean;
2+
isDarkTheme: () => boolean;
33
syncThemeDom: (dark: boolean) => void;
44
initTheme: () => boolean;
55
applyTheme: (dark: boolean) => void;
@@ -21,7 +21,7 @@ const getThemeApi = () => {
2121
return api;
2222
};
2323

24-
export const readThemeCookie = () => getThemeApi().readThemeCookie();
24+
export const isDarkTheme = () => getThemeApi().isDarkTheme();
2525

2626
export const syncThemeDom = (dark: boolean) => getThemeApi().syncThemeDom(dark);
2727

0 commit comments

Comments
 (0)