Skip to content
Snippets Groups Projects
Commit 01e6cd0f authored by danieldeng2's avatar danieldeng2
Browse files

Defaults theme to follow system

parent 7b967e46
No related branches found
No related tags found
No related merge requests found
...@@ -38,8 +38,12 @@ class App extends React.Component<{}, AppState> { ...@@ -38,8 +38,12 @@ class App extends React.Component<{}, AppState> {
}%`; }%`;
const currentTheme = localStorage.getItem("theme"); const currentTheme = localStorage.getItem("theme");
if (currentTheme) { if (currentTheme == "light" || currentTheme == "dark") {
document.documentElement.setAttribute("data-theme", currentTheme); document.documentElement.setAttribute("data-theme", currentTheme);
} else {
let mq = window.matchMedia("(prefers-color-scheme: dark)");
mq.addListener((mq) => this.setDarkTheme(mq.matches));
this.setDarkTheme(mq.matches);
} }
window.addEventListener("resize", () => { window.addEventListener("resize", () => {
...@@ -51,6 +55,13 @@ class App extends React.Component<{}, AppState> { ...@@ -51,6 +55,13 @@ class App extends React.Component<{}, AppState> {
this.showOrHideSideBars(); this.showOrHideSideBars();
} }
setDarkTheme(toSet: boolean) {
document.documentElement.setAttribute(
"data-theme",
toSet ? "dark" : "light"
);
}
toggleLeftBar() { toggleLeftBar() {
if (window.innerWidth <= 1024) { if (window.innerWidth <= 1024) {
this.setState({ this.setState({
...@@ -108,6 +119,7 @@ class App extends React.Component<{}, AppState> { ...@@ -108,6 +119,7 @@ class App extends React.Component<{}, AppState> {
fileView={this.state.fileView} fileView={this.state.fileView}
onCardViewClick={() => this.setFileView("card")} onCardViewClick={() => this.setFileView("card")}
onListViewClick={() => this.setFileView("list")} onListViewClick={() => this.setFileView("list")}
setDarkTheme={(b) => this.setDarkTheme(b)}
/> />
<Switch> <Switch>
......
...@@ -13,6 +13,7 @@ interface Props { ...@@ -13,6 +13,7 @@ interface Props {
show: boolean; show: boolean;
onHide: any; onHide: any;
fileView: string; fileView: string;
setDarkTheme: (toSet: boolean) => any;
onCardViewClick: (event: React.MouseEvent) => void; onCardViewClick: (event: React.MouseEvent) => void;
onListViewClick: (event: React.MouseEvent) => void; onListViewClick: (event: React.MouseEvent) => void;
} }
...@@ -21,6 +22,7 @@ const SettingsModal: React.FC<Props> = ({ ...@@ -21,6 +22,7 @@ const SettingsModal: React.FC<Props> = ({
show, show,
onHide, onHide,
fileView, fileView,
setDarkTheme,
onCardViewClick, onCardViewClick,
onListViewClick, onListViewClick,
}) => { }) => {
...@@ -29,7 +31,7 @@ const SettingsModal: React.FC<Props> = ({ ...@@ -29,7 +31,7 @@ const SettingsModal: React.FC<Props> = ({
"90" "90"
); );
const [theme, setTheme] = useLocalStorage("theme", "light"); const [theme, setTheme] = useLocalStorage("theme", "default");
return ( return (
<Modal <Modal
...@@ -72,11 +74,24 @@ const SettingsModal: React.FC<Props> = ({ ...@@ -72,11 +74,24 @@ const SettingsModal: React.FC<Props> = ({
<Form.Group style={{ alignItems: "center" }}> <Form.Group style={{ alignItems: "center" }}>
<Form.Label>Theme</Form.Label> <Form.Label>Theme</Form.Label>
<ButtonGroup style={{ float: "right" }}> <ButtonGroup style={{ float: "right" }}>
<Button
className={styles.modalToggleButton}
active={theme === "default"}
onClick={() => {
let mq = window.matchMedia("(prefers-color-scheme: dark)");
mq.addListener((mq) => setDarkTheme(mq.matches));
setDarkTheme(mq.matches);
setTheme("default");
}}
variant="secondary"
>
Default
</Button>
<Button <Button
className={styles.modalToggleButton} className={styles.modalToggleButton}
active={theme === "light"} active={theme === "light"}
onClick={() => { onClick={() => {
document.documentElement.setAttribute("data-theme", "light"); setDarkTheme(false);
setTheme("light"); setTheme("light");
}} }}
variant="secondary" variant="secondary"
...@@ -87,7 +102,7 @@ const SettingsModal: React.FC<Props> = ({ ...@@ -87,7 +102,7 @@ const SettingsModal: React.FC<Props> = ({
className={styles.modalToggleButton} className={styles.modalToggleButton}
active={theme === "dark"} active={theme === "dark"}
onClick={() => { onClick={() => {
document.documentElement.setAttribute("data-theme", "dark"); setDarkTheme(true);
setTheme("dark"); setTheme("dark");
}} }}
variant="secondary" variant="secondary"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment