21
loading...
This website collects cookies to deliver better user experience
styled-components
or explaining deeply functions like React.createContext.Saves battery
Dark mode has been proven to reduce up to 58% of the power drain from the battery compared to the normal mode.
Easy on the eyes
It may not be confirmed if dark mode can help the eyes but it for sure can reduce the strain on the eyes. It's also more comfortable to read at night.
Looks amazing 😍
Dark mode simply makes your app look cooler and giving users the feeling of using a new app when trying out the dark mode for the first time.
eg --main-background
, these variables can be accessed using the CSS command var() and an example of the usage would be background:var(--main-background)
.npx create-react-app darkmode
cd darkmode
npm start
cd src && touch globalstyles.js
export const lightTheme = {
navTextColor: "#000",
background: "#fff",
};
export const darkTheme = {
navTextColor: "#fff",
background: "#000"
};
setProperty
an example would bedocument.body.style.setProperty(`--background-name`, `pink`);
import React from "react";
import { darkTheme, lightTheme } from "./globalstyles";
const themeColours = {
light: lightTheme,
dark: darkTheme,
};
const ThemeContext = React.createContext();
export const ThemeProvider = ({ children }) => {
const [themeName, setThemeName] = React.useState("light");
///adding code here
return (
<ThemeContext.Provider value={{ theme: themeName, setTheme }}>
{children}
</ThemeContext.Provider>
);
};
export const useTheme = () => React.useContext(ThemeContext);
import React from "react";
import { darkTheme, lightTheme } from "./globalstyles";
const ThemeContext = React.createContext();
const [themeName, setThemeName] = React.useState("light");
return (
<ThemeContext.Provider value={{ theme: themeName, setTheme }}>
{children}
</ThemeContext.Provider>
);
import React from "react";
import { darkTheme, lightTheme } from "./theme";
const themeColours = {
light: lightTheme,
dark: darkTheme,
};
const ThemeContext = React.createContext();
export const ThemeProvider = ({ children }) => {
const [themeName, setThemeName] = React.useState("light");
///get the defined mode from the browser
React.useEffect(() => {
const darkOS = window.matchMedia("(prefers-color-scheme: dark)").matches;
setTheme(darkOS ? "dark" : "light");
}, []);
const setTheme = (name) => {
///turn my configurations to css variables
const keys = Object.keys(themeColours[name])
keys.map((key)=>{
const constructVar = `--${key}`
document.body.style.setProperty(constructVar, themeColours[name][key]);
return false /// cuz eslint just wants me to return something
})
setThemeName(name);
};
return (
<ThemeContext.Provider value={{ theme: themeName, setTheme }}>
{children}
</ThemeContext.Provider>
);
};
export const useTheme = () => React.useContext(ThemeContext);
///get the defined mode from the browser
React.useEffect(() => {
const darkOS = window.matchMedia("(prefers-color-scheme: dark)").matches;
setTheme(darkOS ? "dark" : "light");
}, []);
const setTheme = (name) => {
///turn my configurations to css variables
const keys = Object.keys(themeColours[name])
keys.map((key)=>{
const constructVar = `--${key}`
document.body.style.setProperty(constructVar, themeColours[name][key]);
return false /// cuz eslint just wants me to return something
})
setThemeName(name);
};
touch index.css
body{
background:var(--background);
color:var(--navTextColor);
text-align: center;
}
button{
background:var(--background);
color:var(--navTextColor);
}
import { useState } from "react";
import { useTheme } from "./utils/themeContext";
import "./index.css";
const App = () => {
const { setTheme, themeName } = useTheme();
const [mood, setMood] = useState(themeName === "dark");
return (
<div>
<button
className="fab"
type="primary"
onClick={(e) => {
setTheme(mood ? "dark" : "light");
setMood(!mood);
}}
>
{" Toggle d theme"} <p>{mood ? "dark" : "light"}</p>
</button>
</div>
);
};
App.prototype = {};
export default App;
const { setTheme, themeName } = useTheme();
const [mood, setMood] = useState(themeName === "dark");
<button
className="fab"
type="primary"
onClick={(e) => {
setTheme(mood ? "dark" : "light");
setMood(!mood);
}}
>
{" Toggle d theme"} <p>{mood ? "dark" : "light"}</p>
</button>