-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Brad Garropy <[email protected]>
- Loading branch information
1 parent
90bfc60
commit 3f0a985
Showing
9 changed files
with
91 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Empty file.
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,9 @@ | ||
const localTheme = window.localStorage.getItem("theme") | ||
|
||
if (localTheme === "light") { | ||
document.documentElement.classList.remove("dark") | ||
} | ||
|
||
if (localTheme === "dark") { | ||
document.documentElement.classList.add("dark") | ||
} |
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
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,42 +1,80 @@ | ||
import {act, renderHook} from "@testing-library/react" | ||
import {useTheme} from "hooks" | ||
|
||
test("returns default theme", () => { | ||
const mockGetItem = jest.fn() | ||
const mockSetItem = jest.fn() | ||
|
||
beforeEach(() => { | ||
global.Storage.prototype.getItem = mockGetItem | ||
global.Storage.prototype.setItem = mockSetItem | ||
}) | ||
|
||
afterEach(() => { | ||
mockGetItem.mockReset() | ||
mockGetItem.mockReset() | ||
}) | ||
|
||
test("defaults to light theme", () => { | ||
mockGetItem.mockReturnValue(null) | ||
|
||
const {result} = renderHook(() => useTheme()) | ||
const [theme] = result.current | ||
expect(theme).toEqual("light") | ||
|
||
expect(result.current.theme).toEqual("light") | ||
expect(mockSetItem).toHaveBeenCalledTimes(1) | ||
expect(mockSetItem).toHaveBeenCalledWith("theme", "light") | ||
expect(document.documentElement.classList.contains("dark")).toBeFalsy() | ||
}) | ||
|
||
test("returns light theme", () => { | ||
const {result} = renderHook(() => useTheme("light")) | ||
const [theme] = result.current | ||
expect(theme).toEqual("light") | ||
test("uses localstorage light theme", () => { | ||
mockGetItem.mockReturnValue("light") | ||
|
||
const {result} = renderHook(() => useTheme()) | ||
|
||
expect(result.current.theme).toEqual("light") | ||
expect(mockSetItem).toHaveBeenCalledTimes(1) | ||
expect(mockSetItem).toHaveBeenCalledWith("theme", "light") | ||
expect(document.documentElement.classList.contains("dark")).toBeFalsy() | ||
}) | ||
|
||
test("returns dark theme", () => { | ||
const {result} = renderHook(() => useTheme("dark")) | ||
const [theme] = result.current | ||
expect(theme).toEqual("dark") | ||
test("uses localstorage dark theme", () => { | ||
mockGetItem.mockReturnValue("dark") | ||
|
||
const {result} = renderHook(() => useTheme()) | ||
|
||
expect(result.current.theme).toEqual("dark") | ||
expect(mockSetItem).toHaveBeenCalledTimes(1) | ||
expect(mockSetItem).toHaveBeenCalledWith("theme", "dark") | ||
expect(document.documentElement.classList.contains("dark")).toBeTruthy() | ||
}) | ||
|
||
test("sets dark theme", () => { | ||
const {result} = renderHook(() => useTheme("light")) | ||
expect(result.current[0]).toEqual("light") | ||
test("switches to light theme", () => { | ||
mockGetItem.mockReturnValue("dark") | ||
|
||
const {result} = renderHook(() => useTheme()) | ||
expect(result.current.theme).toEqual("dark") | ||
|
||
act(() => { | ||
result.current[1]("dark") | ||
result.current.setTheme("light") | ||
}) | ||
|
||
expect(result.current[0]).toEqual("dark") | ||
expect(result.current.theme).toEqual("light") | ||
expect(mockSetItem).toHaveBeenCalledTimes(2) | ||
expect(mockSetItem).toHaveBeenLastCalledWith("theme", "light") | ||
expect(document.documentElement.classList.contains("dark")).toBeFalsy() | ||
}) | ||
|
||
test("sets light theme", () => { | ||
const {result} = renderHook(() => useTheme("dark")) | ||
expect(result.current[0]).toEqual("dark") | ||
test("switches to dark theme", () => { | ||
mockGetItem.mockReturnValue("light") | ||
|
||
const {result} = renderHook(() => useTheme()) | ||
expect(result.current.theme).toEqual("light") | ||
|
||
act(() => { | ||
result.current[1]("light") | ||
result.current.setTheme("dark") | ||
}) | ||
|
||
expect(result.current[0]).toEqual("light") | ||
expect(result.current.theme).toEqual("dark") | ||
expect(mockSetItem).toHaveBeenCalledTimes(2) | ||
expect(mockSetItem).toHaveBeenLastCalledWith("theme", "dark") | ||
expect(document.documentElement.classList.contains("dark")).toBeTruthy() | ||
}) |
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,27 +1,33 @@ | ||
import {Dispatch, SetStateAction, useEffect, useRef, useState} from "react" | ||
import {Dispatch, SetStateAction, useEffect, useState} from "react" | ||
|
||
type Theme = "light" | "dark" | ||
|
||
const useTheme = ( | ||
defaultTheme: Theme = "light", | ||
): [Theme, Dispatch<SetStateAction<Theme>>] => { | ||
const initialRender = useRef(true) | ||
const [theme, setTheme] = useState<Theme>(defaultTheme) | ||
const useTheme = (): { | ||
theme: Theme | ||
setTheme: Dispatch<SetStateAction<Theme>> | ||
} => { | ||
const [theme, setTheme] = useState<Theme>() | ||
|
||
useEffect(() => { | ||
if (initialRender.current) { | ||
initialRender.current = false | ||
const localTheme = window.localStorage.getItem("theme") as Theme | ||
setTheme(localTheme ?? "light") | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (!theme) { | ||
return | ||
} | ||
|
||
if (theme === "dark") { | ||
window.localStorage.setItem("theme", "dark") | ||
document.documentElement.classList.add("dark") | ||
} else { | ||
window.localStorage.setItem("theme", "light") | ||
document.documentElement.classList.remove("dark") | ||
} | ||
}, [theme]) | ||
|
||
return [theme, setTheme] | ||
return {theme, setTheme} | ||
} | ||
|
||
export default useTheme |
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
3f0a985
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bradgarropy β ./
bradgarropy-git-master-bradgarropy.vercel.app
bradgarropy.com
bradgarropy-bradgarropy.vercel.app
www.bradgarropy.com
bradgarropy.vercel.app