-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: dark mode strategy through context api
- Loading branch information
1 parent
023b791
commit c705c8d
Showing
7 changed files
with
72 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { initializeDarkThemeStrategy } from "./src/business/dark-mode-strategy" | ||
import "prismjs/themes/prism-tomorrow.css" | ||
import React from "react" | ||
import { wrapWithDarkThemeProvider } from "./src/contexts/dark-theme-context" | ||
|
||
initializeDarkThemeStrategy() | ||
export const wrapRootElement = wrapWithDarkThemeProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { wrapWithDarkThemeProvider } from "./src/contexts/dark-theme-context" | ||
|
||
export const wrapRootElement = wrapWithDarkThemeProvider |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from "react" | ||
|
||
export const paletteTypeLight = "light" | ||
export const paletteTypeDark = "dark" | ||
export const paletteTypeLocalStorageKey = "custom-palette-type" | ||
const mediaQueryColorTheme = `(prefers-color-scheme: ${paletteTypeDark})` | ||
|
||
const DarkThemeContext = React.createContext(null) | ||
const useDarkThemeContext = () => React.useContext(DarkThemeContext) | ||
|
||
function currentTheme() { | ||
const prefersDark = typeof window !== "undefined" && window.matchMedia(mediaQueryColorTheme).matches | ||
|
||
try { | ||
const themeInWindow = typeof window !== "undefined" && localStorage.getItem(paletteTypeLocalStorageKey) | ||
if (themeInWindow) { | ||
return localStorage.getItem(paletteTypeLocalStorageKey) | ||
} | ||
} catch (exceptionToBeIgnored) {} | ||
|
||
return prefersDark ? paletteTypeDark : paletteTypeLight | ||
} | ||
|
||
function DarkThemeProvider({ children }) { | ||
const themeToUse = currentTheme() | ||
const [paletteType, setPaletteType] = React.useState(themeToUse) | ||
const properties = { paletteType, setPaletteType } | ||
|
||
return <DarkThemeContext.Provider value={properties}>{children}</DarkThemeContext.Provider> | ||
} | ||
|
||
function wrapWithDarkThemeProvider({ element }) { | ||
return <DarkThemeProvider>{element}</DarkThemeProvider> | ||
} | ||
|
||
export { useDarkThemeContext, wrapWithDarkThemeProvider } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters