Skip to content

Commit

Permalink
fix css for real (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
chennisden authored Jun 18, 2024
1 parent 92bd77c commit dfe06f1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
11 changes: 10 additions & 1 deletion frontend/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ div.content {

html,
body {
transition: color 0.1s ease;
word-wrap: break-word;
}

Expand Down Expand Up @@ -47,6 +46,11 @@ p.card-text {
border-color: #0d6efd;
}

.btn-primary:hover {
background-color: #0d6efd;
color: white;
}

body.dark-mode .btn-primary {
background-color: black;
color: #8ab9fe;
Expand All @@ -64,6 +68,11 @@ body.dark-mode .btn-primary:hover {
border-color: #dc3545;
}

.btn-danger:hover {
background-color: #dc3545;
color: white;
}

body.dark-mode .btn-danger {
background-color: black;
color: #f0cbce;
Expand Down
47 changes: 45 additions & 2 deletions frontend/src/hooks/theme.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
import { createContext, ReactNode, useContext, useState } from "react";
import {
createContext,
ReactNode,
useContext,
useEffect,
useState,
} from "react";

export type Theme = "light" | "dark";

const THEME_KEY = "__THEME";

interface ThemeColors {
backgroundColor: string;
color: string;
buttonBorder: string;
buttonHover: string;
text_color: string;
}

const COLORS: { [key in Theme]: ThemeColors } = {
light: {
backgroundColor: "#ffffff",
color: "#201a42",
buttonBorder: "#0D6EFD",
buttonHover: "#0D6EFD",
text_color: "#f5f2ef",
},
dark: {
backgroundColor: "#000000",
color: "#f5f2ef",
buttonBorder: "#8AB9FE",
buttonHover: "#0D6EFD",
text_color: "#201a42",
},
};

const getThemeFromLocalStorage = (): Theme => {
const theme = localStorage.getItem(THEME_KEY);
if (theme === null) {
Expand All @@ -19,6 +50,7 @@ const setThemeWithLocalStorage = (theme: Theme) => {
interface ThemeContextProps {
theme: Theme;
setTheme: (theme: Theme) => void;
colors: ThemeColors;
}

const ThemeContext = createContext<ThemeContextProps | undefined>(undefined);
Expand All @@ -36,8 +68,19 @@ export const ThemeProvider = (props: ThemeProviderProps) => {
setTheme(theme);
setThemeWithLocalStorage(theme);
};

useEffect(() => {
document.body.setAttribute("data-bs-theme", theme);
document.body.classList.toggle("dark-mode", theme === "dark");
document.body.classList.toggle("light-mode", theme === "light");
document.body.style.backgroundColor = COLORS[theme].backgroundColor;
document.body.style.color = COLORS[theme].color;
}, [theme, COLORS]);

return (
<ThemeContext.Provider value={{ theme, setTheme: setThemeWrapper }}>
<ThemeContext.Provider
value={{ theme, setTheme: setThemeWrapper, colors: COLORS[theme] }}
>
{children}
</ThemeContext.Provider>
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const Home: React.FC = () => {
<Row className="row-two">
<Col md={6} sm={12}>
<Button
variant="outline-primary"
variant="primary"
size="lg"
style={{
width: "100%",
Expand All @@ -82,7 +82,7 @@ const Home: React.FC = () => {
</Col>
<Col md={6} sm={12}>
<Button
variant="outline-primary"
variant="primary"
size="lg"
style={{
width: "100%",
Expand Down

0 comments on commit dfe06f1

Please sign in to comment.