Skip to content
Snippets Groups Projects
Commit 18852b38 authored by Jose Manuel Serrano Amaut's avatar Jose Manuel Serrano Amaut
Browse files

[WIP][FEAT]: Add darkmode in the rest of pages with context, also keeping...

[WIP][FEAT]: Add darkmode in the rest of pages with context, also keeping track of the darkmode selection. Missing to refactor some custom components to work well in darkmode
parent 9bddc303
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ import { IconButton, Tooltip } from "@mui/material"; ...@@ -5,6 +5,7 @@ import { IconButton, Tooltip } from "@mui/material";
//import { useNavigate } from "react-router"; //import { useNavigate } from "react-router";
import logo_text_blue from "../../static/files-ui-logo-text-med.png"; import logo_text_blue from "../../static/files-ui-logo-text-med.png";
import logo_text_blue_dark from "../../static/files-ui-logo-text-med-dark.png"; import logo_text_blue_dark from "../../static/files-ui-logo-text-med-dark.png";
import AnchorToTab from "../util-components/AnchorToTab";
const MainNavBar = ({ const MainNavBar = ({
darkModeOn, darkModeOn,
...@@ -12,9 +13,9 @@ const MainNavBar = ({ ...@@ -12,9 +13,9 @@ const MainNavBar = ({
logo_blue_dark, logo_blue_dark,
handleDarkMode, handleDarkMode,
}) => { }) => {
const handleGoGitRepo = () => { /* const handleGoGitRepo = () => {
window.open("https://github.com/files-ui", "_blank"); window.open("https://github.com/files-ui", "_blank");
}; }; */
return ( return (
<nav className="filesui-nav"> <nav className="filesui-nav">
...@@ -38,15 +39,17 @@ const MainNavBar = ({ ...@@ -38,15 +39,17 @@ const MainNavBar = ({
<div className="right-part"> <div className="right-part">
<Tooltip title="Go to Files-ui GitHub repo"> <Tooltip title="Go to Files-ui GitHub repo">
<AnchorToTab href="https://github.com/files-ui">
<IconButton <IconButton
style={{ borderRadius: "8px", border: "0.5px solid #eaeef3" }} style={{ borderRadius: "8px", border: "0.5px solid #eaeef3" }}
onClick={handleGoGitRepo} //onClick={handleGoGitRepo}
color="secondary" color="secondary"
aria-label="upload picture" aria-label="upload picture"
component="label" component="label"
> >
<GitHubIcon /* htmlColor="white" */ /> <GitHubIcon /* htmlColor="white" */ />
</IconButton> </IconButton>
</AnchorToTab>
</Tooltip> </Tooltip>
<DarkModeLightModeButton <DarkModeLightModeButton
darkModeOn={darkModeOn} darkModeOn={darkModeOn}
......
import * as React from "react";
import { initialUserValue } from "../initialValues/userInitialValue";
import { UserFilesUi } from "../types/UserFilesUi";
export const UserContext=
React.createContext({});
import { UserFilesUi } from "../types/UserFilesUi";
export const initialUserValue:UserFilesUi={
id:undefined,
darkMode:false,
}
import { UserContext } from "../contexts/UserContext";
import * as React from "react";
import { UserFilesUi } from "../types/UserFilesUi";
import { FilesUIAction, userInitializer, userReducer } from "../reducers/userReducer";
import { ThemeProvider } from "@emotion/react";
import { MUItheme } from "../../theme/mainTheme";
export const UserProvider = (props: {
children: React.ReactNode;
valorInicial: UserFilesUi;
}) => {
const { children, valorInicial } = props;
const [usuario, dispatch]: [UserFilesUi, React.Dispatch<FilesUIAction>] =
React.useReducer(userReducer, valorInicial, userInitializer);
React.useEffect(() => {
localStorage.setItem("filesuiuser", JSON.stringify(usuario));
console.log("filesuiuser", usuario);
}, [usuario]);
return (
<UserContext.Provider value={[usuario, dispatch]}>
<ThemeProvider theme={MUItheme(usuario.darkMode ? "dark" : "light")}>
{children}
</ThemeProvider>
</UserContext.Provider>
);
};
import { UserFilesUi } from "../types/UserFilesUi";
export interface FilesUIAction {
type?: string; payload?: UserFilesUi;
}
export const userReducer = (state: UserFilesUi, action: FilesUIAction): UserFilesUi => {
const { type = "", payload = {} } = action;
console.log("userReducer", state, action);
switch (type) {
case "INICIARSESION":
return { ...state, ...payload };
case "TURNONLIGHT":
return { ...state, darkMode: false };
case "TURNOFFLIGHT":
return { ...state, darkMode: true };
default:
return state;
}
};
export const userInitializer = () => {
const usuarioEncontrado = localStorage.getItem("filesuiuser");
if (usuarioEncontrado !== "udefined" && usuarioEncontrado !== null) {
return JSON.parse(usuarioEncontrado);
} else {
return {};
}
};
export type UserFilesUi={
id?:string;
darkMode?:boolean,
}
\ No newline at end of file
...@@ -3,18 +3,19 @@ import ReactDOM from "react-dom/client"; ...@@ -3,18 +3,19 @@ import ReactDOM from "react-dom/client";
import "./index.css"; import "./index.css";
import reportWebVitals from "./reportWebVitals"; import reportWebVitals from "./reportWebVitals";
//import MainPage from "./pages/MainPage"; //import MainPage from "./pages/MainPage";
import { ThemeProvider } from "@mui/material/styles"; /* import { ThemeProvider } from "@mui/material/styles";
import { MUItheme } from "./theme/mainTheme"; import { MUItheme } from "./theme/mainTheme"; */
import MainRouter from "./routes/MainRouter"; import MainRouter from "./routes/MainRouter";
import { UserProvider } from "./globals/providers/UserProvider";
import { initialUserValue } from "./globals/initialValues/userInitialValue";
const root = ReactDOM.createRoot(document.getElementById("root")); const root = ReactDOM.createRoot(document.getElementById("root"));
root.render( root.render(
<ThemeProvider theme={MUItheme}> <UserProvider valorInicial={initialUserValue}>
<MainRouter /> <MainRouter />
</ThemeProvider> </UserProvider>
); );
// If you want to start measuring performance in your app, pass a function // If you want to start measuring performance in your app, pass a function
......
...@@ -12,12 +12,19 @@ import { Divider } from "@mui/material"; ...@@ -12,12 +12,19 @@ import { Divider } from "@mui/material";
import ExtraComponentsMainPage from "../components/MainPage/SecondaryRight/ExtraComponentsMainPage"; import ExtraComponentsMainPage from "../components/MainPage/SecondaryRight/ExtraComponentsMainPage";
import ExtraComponentsMainPageInputButton from "../components/MainPage/SecondaryRight/ExtraComponentsMainPageInputButton"; import ExtraComponentsMainPageInputButton from "../components/MainPage/SecondaryRight/ExtraComponentsMainPageInputButton";
import ExtraComponentsMainPageAvatar from "../components/MainPage/SecondaryRight/ExtraComponentsMainPageAvatar"; import ExtraComponentsMainPageAvatar from "../components/MainPage/SecondaryRight/ExtraComponentsMainPageAvatar";
import { UserContext } from "../globals/contexts/UserContext";
const MainPage = ({ darkMode }) => { const MainPage = ({ darkMode }) => {
const [darkModeOn, setDarkModeOn] = React.useState(false); const [usuario, dispatch] = React.useContext(UserContext);
// const [darkModeOn, setDarkModeOn] = React.useState(false);
const darkModeOn = usuario.darkMode;
console.log("userReducer darkModeOn", darkModeOn);
const handleDarkMode = () => { const handleDarkMode = () => {
setDarkModeOn((darkModeOn) => !darkModeOn); // setDarkModeOn((darkModeOn) => !darkModeOn);
if (!darkModeOn) dispatch({ type: "TURNOFFLIGHT" });
else dispatch({ type: "TURNONLIGHT" });
}; };
return ( return (
......
...@@ -15,6 +15,8 @@ import DarkModeLightModeButton from "../components/MainPage/DarkModeLightModeBut ...@@ -15,6 +15,8 @@ import DarkModeLightModeButton from "../components/MainPage/DarkModeLightModeBut
import MainMenuSideBar from "../components/MainMenu/MainMenuSideBar"; import MainMenuSideBar from "../components/MainMenu/MainMenuSideBar";
import logo_text_blue from "../static/files-ui-logo-text-med.png"; import logo_text_blue from "../static/files-ui-logo-text-med.png";
import logo_text_blue_dark from "../static/files-ui-logo-text-med-dark.png"; import logo_text_blue_dark from "../static/files-ui-logo-text-med-dark.png";
import { UserContext } from "../globals/contexts/UserContext";
import AnchorToTab from "../components/util-components/AnchorToTab";
const drawerWidth = 280; const drawerWidth = 280;
const StyledImage = styled("img")(({ theme }) => ({ const StyledImage = styled("img")(({ theme }) => ({
...@@ -27,23 +29,34 @@ const StyledImage = styled("img")(({ theme }) => ({ ...@@ -27,23 +29,34 @@ const StyledImage = styled("img")(({ theme }) => ({
}, },
})); }));
function NavBarTemplate(props) { function NavBarTemplate(props) {
const [usuario, dispatch] = React.useContext(UserContext);
const darkModeOn = usuario.darkMode;
//const navigate = useNavigateToTop(); //const navigate = useNavigateToTop();
const { window, children, darkModeOn, handleDarkMode, selectedIndex } = props; const {
window,
children,
/* darkModeOn, */ /* handleDarkMode, */ selectedIndex,
} = props;
const [mobileOpen, setMobileOpen] = React.useState(false); const [mobileOpen, setMobileOpen] = React.useState(false);
//const [selectedIndex, setSelectedIndex] = React.useState(0); //const [selectedIndex, setSelectedIndex] = React.useState(0);
const handleDarkMode = () => {
// setDarkModeOn((darkModeOn) => !darkModeOn);
if (!darkModeOn) dispatch({ type: "TURNOFFLIGHT" });
else dispatch({ type: "TURNONLIGHT" });
};
const handleGoGitRepo = () => { const handleGoGitRepo = () => {
alert("HAAA");
window.open("https://github.com/files-ui", "_blank"); window.open("https://github.com/files-ui", "_blank");
}; };
const handleDrawerToggle = () => { const handleDrawerToggle = () => {
setMobileOpen(!mobileOpen); setMobileOpen(!mobileOpen);
}; };
const drawer = ( const drawer = (
<div <div>
>
<Toolbar> <Toolbar>
<a href="/" style={{ textDecoration: "none" }}> <a href="/" style={{ textDecoration: "none" }}>
<Stack <Stack
...@@ -126,10 +139,11 @@ function NavBarTemplate(props) { ...@@ -126,10 +139,11 @@ function NavBarTemplate(props) {
</Typography> </Typography>
<Box style={{ flexGrow: 1 }} /> <Box style={{ flexGrow: 1 }} />
<Stack direction="row" spacing={1}> <Stack direction="row" spacing={1}>
<AnchorToTab href="https://github.com/files-ui">
<Tooltip title="Go to Files-ui GitHub repo"> <Tooltip title="Go to Files-ui GitHub repo">
<IconButton <IconButton
style={{ borderRadius: "8px", border: "0.5px solid #eaeef3" }} style={{ borderRadius: "8px", border: "0.5px solid #eaeef3" }}
onClick={handleGoGitRepo} //onClick={handleGoGitRepo}
color="secondary" color="secondary"
aria-label="upload picture" aria-label="upload picture"
component="label" component="label"
...@@ -137,6 +151,7 @@ function NavBarTemplate(props) { ...@@ -137,6 +151,7 @@ function NavBarTemplate(props) {
<GitHubIcon /* htmlColor="white" */ /> <GitHubIcon /* htmlColor="white" */ />
</IconButton> </IconButton>
</Tooltip> </Tooltip>
</AnchorToTab>
<DarkModeLightModeButton <DarkModeLightModeButton
darkModeOn={darkModeOn} darkModeOn={darkModeOn}
onChangeDarkMode={handleDarkMode} onChangeDarkMode={handleDarkMode}
......
import { createTheme } from "@mui/material/styles"; import { createTheme } from "@mui/material/styles";
export const MUItheme = createTheme({ export const MUItheme = (modeLight = "light") =>
createTheme({
palette: { palette: {
mode: modeLight,
primary: { primary: {
// light: will be calculated from palette.primary.main, // light: will be calculated from palette.primary.main,
main: "#042354", main: "#042354",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment