feat: support follow system appearance (#670)

pull/671/head
boojack 3 years ago committed by GitHub
parent 1ea74dfd0d
commit 4767ee3293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,7 +35,7 @@ func (key UserSettingKey) String() string {
var ( var (
UserSettingLocaleValue = []string{"en", "zh", "vi", "fr", "sv"} UserSettingLocaleValue = []string{"en", "zh", "vi", "fr", "sv"}
UserSettingAppearanceValue = []string{"light", "dark"} UserSettingAppearanceValue = []string{"system", "light", "dark"}
UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public} UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public}
UserSettingMemoDisplayTsOptionKeyValue = []string{"created_ts", "updated_ts"} UserSettingMemoDisplayTsOptionKeyValue = []string{"created_ts", "updated_ts"}
) )

@ -1,17 +1,18 @@
import { useColorScheme } from "@mui/joy";
import { useEffect, Suspense } from "react"; import { useEffect, Suspense } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { RouterProvider } from "react-router-dom"; import { RouterProvider } from "react-router-dom";
import { locationService } from "./services"; import { globalService, locationService } from "./services";
import { useAppSelector } from "./store"; import { useAppSelector } from "./store";
import Loading from "./pages/Loading"; import Loading from "./pages/Loading";
import router from "./router"; import router from "./router";
import * as storage from "./helpers/storage"; import * as storage from "./helpers/storage";
import { useColorScheme } from "@mui/joy"; import { getSystemColorScheme } from "./helpers/utils";
function App() { function App() {
const { i18n } = useTranslation(); const { i18n } = useTranslation();
const { appearance, locale, systemStatus } = useAppSelector((state) => state.global); const { appearance, locale, systemStatus } = useAppSelector((state) => state.global);
const { setMode } = useColorScheme(); const { mode, setMode } = useColorScheme();
useEffect(() => { useEffect(() => {
locationService.updateStateWithLocation(); locationService.updateStateWithLocation();
@ -20,6 +21,15 @@ function App() {
}; };
}, []); }, []);
useEffect(() => {
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
if (globalService.getState().appearance === "system") {
const mode = e.matches ? "dark" : "light";
setMode(mode);
}
});
}, []);
// Inject additional style and script codes. // Inject additional style and script codes.
useEffect(() => { useEffect(() => {
if (systemStatus.additionalStyle) { if (systemStatus.additionalStyle) {
@ -43,18 +53,27 @@ function App() {
}, [locale]); }, [locale]);
useEffect(() => { useEffect(() => {
const root = document.documentElement;
if (appearance === "light") {
root.classList.remove("dark");
} else if (appearance === "dark") {
root.classList.add("dark");
}
setMode(appearance);
storage.set({ storage.set({
appearance: appearance, appearance: appearance,
}); });
let currentAppearance = appearance;
if (appearance === "system") {
currentAppearance = getSystemColorScheme();
}
setMode(currentAppearance);
}, [appearance]); }, [appearance]);
useEffect(() => {
const root = document.documentElement;
if (mode === "light") {
root.classList.remove("dark");
} else {
root.classList.add("dark");
}
}, [mode]);
return ( return (
<Suspense fallback={<Loading />}> <Suspense fallback={<Loading />}>
<RouterProvider router={router} /> <RouterProvider router={router} />

@ -4,7 +4,7 @@ import { globalService, userService } from "../services";
import { useAppSelector } from "../store"; import { useAppSelector } from "../store";
import Icon from "./Icon"; import Icon from "./Icon";
const appearanceList = ["light", "dark"]; const appearanceList = ["system", "light", "dark"];
const AppearanceSelect = () => { const AppearanceSelect = () => {
const user = useAppSelector((state) => state.user.user); const user = useAppSelector((state) => state.user.user);
@ -17,6 +17,8 @@ const AppearanceSelect = () => {
return <Icon.Sun className={className} />; return <Icon.Sun className={className} />;
} else if (apperance === "dark") { } else if (apperance === "dark") {
return <Icon.Moon className={className} />; return <Icon.Moon className={className} />;
} else {
return <Icon.Smile className={className} />;
} }
}; };

@ -168,9 +168,9 @@
"additional-script-placeholder": "Additional JavaScript codes" "additional-script-placeholder": "Additional JavaScript codes"
}, },
"apperance-option": { "apperance-option": {
"system": "Follow system",
"light": "Always light", "light": "Always light",
"dark": "Always dark", "dark": "Always dark"
"system": "Follow system"
} }
}, },
"amount-text": { "amount-text": {

@ -168,9 +168,9 @@
"additional-script-placeholder": "Ytterligare JavaScript kod" "additional-script-placeholder": "Ytterligare JavaScript kod"
}, },
"apperance-option": { "apperance-option": {
"system": "Follow system",
"light": "Alltid ljus", "light": "Alltid ljus",
"dark": "Alltid mörk", "dark": "Alltid mörk"
"system": "Följ systeminställningarna"
} }
}, },
"amount-text": { "amount-text": {
@ -210,4 +210,4 @@
"not-allow-space": "Tillåt inte mellanslag", "not-allow-space": "Tillåt inte mellanslag",
"not-allow-chinese": "Tillåt inte kinesiska" "not-allow-chinese": "Tillåt inte kinesiska"
} }
} }

@ -168,9 +168,9 @@
"additional-script-placeholder": "自定义 JavaScript 代码" "additional-script-placeholder": "自定义 JavaScript 代码"
}, },
"apperance-option": { "apperance-option": {
"system": "跟随系统",
"light": "总是浅色", "light": "总是浅色",
"dark": "总是深色", "dark": "总是深色"
"system": "跟随系统"
} }
}, },
"amount-text": { "amount-text": {

@ -11,7 +11,7 @@ const globalService = {
initialState: async () => { initialState: async () => {
const defaultGlobalState = { const defaultGlobalState = {
locale: "en" as Locale, locale: "en" as Locale,
appearance: "light" as Appearance, appearance: "system" as Appearance,
systemStatus: { systemStatus: {
allowSignUp: false, allowSignUp: false,
additionalStyle: "", additionalStyle: "",

@ -10,7 +10,7 @@ const globalSlice = createSlice({
name: "global", name: "global",
initialState: { initialState: {
locale: "en", locale: "en",
appearance: "light", appearance: "system",
systemStatus: { systemStatus: {
host: undefined, host: undefined,
profile: { profile: {

@ -1,4 +1,4 @@
type Appearance = "light" | "dark"; type Appearance = "system" | "light" | "dark";
interface Setting { interface Setting {
locale: Locale; locale: Locale;

Loading…
Cancel
Save