Skip to content

Commit

Permalink
simplified userdarkmode hook, added check if user prefers dark theme …
Browse files Browse the repository at this point in the history
…based on OS settings
  • Loading branch information
Michal Bartosik committed Jun 4, 2021
1 parent 09fa077 commit e3a3994
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ npm test
- React Router Dom
- Downshift
- Axios
- React Paginate
- react-paginate
- react-media-hook
- Mobile-first workflow

### What I learned
Expand All @@ -74,13 +75,9 @@ Making accessible UI components like custom select is difficult, we all know tha
Also, this is my first React project with TypeScript. At the beginning it was tough, there was a lot of googling for sure, but on the second day I started to see the benefits that static typing provides.
Since I'm still learning, I didn't want TypeScript to slow me down, so to make progress with the project I used 'any' in some places and moved on.

To be added:

1. Checking if user prefers dark mode based on OS settings.

### Useful resources

- [Dark mode in react](https://www.smashingmagazine.com/2020/04/dark-mode-react-apps-styled-components/) - This helped me implement my custom hook, but I had to adjust it for my app, since I'm not using ThemeProvider
- [Dark mode in react](https://www.npmjs.com/package/react-media-hook/) - great package that you can use to check whether user prefers dark or light mode based on their OS settings

## Author

Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-icons": "^4.2.0",
"react-media-hook": "^0.4.9",
"react-paginate": "^7.1.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
Expand All @@ -37,12 +38,6 @@
"resolutions": {
"babel-loader": "8.1.0"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"lint-staged": {
"src/**/*.{ts,tsx,js,jsx,json}": [
"prettier --write"
Expand Down
23 changes: 10 additions & 13 deletions src/hooks/UseDarkMode.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { useEffect, useState, useCallback } from 'react';
import { useEffect, useState } from 'react';
import Theme from 'enums/Theme';
import { useMediaPredicate } from 'react-media-hook';

const useDarkMode = (): [string, () => void] => {
const [theme, setTheme] = useState<string>(Theme.Light);

const setMode = useCallback((mode: string) => {
window.localStorage.setItem('theme', mode);
setTheme(mode);
}, []);
const preferredTheme = useMediaPredicate('(prefers-color-scheme: dark)')
? Theme.Dark
: Theme.Light;
const [theme, setTheme] = useState<string>(
localStorage.getItem('theme') || preferredTheme
);

const themeToggler = (): void => {
theme === Theme.Light ? setMode(Theme.Dark) : setMode(Theme.Light);
theme === Theme.Light ? setTheme(Theme.Dark) : setTheme(Theme.Light);
};

useEffect(() => {
const localTheme = window.localStorage.getItem('theme');
localTheme ? setTheme(localTheme) : setMode(Theme.Light);
}, [setMode]);

useEffect(() => {
document.body.className = theme;
window.localStorage.setItem('theme', theme);
}, [theme]);

return [theme, themeToggler];
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9528,6 +9528,11 @@ react-is@^17.0.1, react-is@^17.0.2:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react-media-hook@^0.4.9:
version "0.4.9"
resolved "https://registry.yarnpkg.com/react-media-hook/-/react-media-hook-0.4.9.tgz#7e57092f5800c53787dc9e912ad09e749dfb7bc7"
integrity sha512-FZr/2xA1+23vDJ1IZ794yLqMRRkBoCNOiJATdtTfB5GyVc5djf8FL2qEB/68pSkiNgHdHsmKknMSDr0sC4zBKQ==

react-paginate@^7.1.2:
version "7.1.2"
resolved "https://registry.yarnpkg.com/react-paginate/-/react-paginate-7.1.2.tgz#3a94f36a774f9d2e82b877e30474a16088b00a4d"
Expand Down

1 comment on commit e3a3994

@vercel
Copy link

@vercel vercel bot commented on e3a3994 Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.