-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(docs): remove the theme provider (#2137)
- Loading branch information
1 parent
32d0b9a
commit cfb7c5b
Showing
14 changed files
with
346 additions
and
50 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@kadena/react-ui": minor | ||
--- | ||
|
||
Added a hook to toggle and keep theme state, removing the necessity of a theme provider context |
23 changes: 6 additions & 17 deletions
23
packages/apps/docs/src/components/Layout/components/DocsLogo/DocsLogo.tsx
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
18 changes: 9 additions & 9 deletions
18
packages/apps/docs/src/components/Layout/components/Header/ThemeToggle.tsx
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
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
103 changes: 103 additions & 0 deletions
103
packages/libs/react-ui/src/utils/__tests__/useTheme.test.ts
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,103 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { useTheme } from '../useTheme'; | ||
import { Themes, storageKey } from '../useTheme/utils/constants'; | ||
|
||
const mocks = vi.hoisted(() => { | ||
return { | ||
getSystemTheme: vi.fn(), | ||
}; | ||
}); | ||
describe('useTheme', () => { | ||
beforeEach(() => { | ||
vi.mock('../useTheme/utils/getSystemTheme', () => ({ | ||
getSystemTheme: mocks.getSystemTheme, | ||
})); | ||
}); | ||
|
||
afterEach(() => { | ||
window.localStorage.removeItem(storageKey); | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it('should set the state of the system (dark) when there is no localstorage set', () => { | ||
mocks.getSystemTheme.mockReturnValue(Themes.dark); | ||
const { result } = renderHook(() => useTheme()); | ||
expect(result.current.theme).toEqual(Themes.dark); | ||
}); | ||
|
||
it('should set the state of the system (light) when there is no localstorage set', () => { | ||
mocks.getSystemTheme.mockReturnValue(Themes.light); | ||
const { result } = renderHook(() => useTheme()); | ||
expect(result.current.theme).toEqual(Themes.light); | ||
}); | ||
|
||
it('should set the state of the localstorage value (light), if available', () => { | ||
window.localStorage.setItem(storageKey, Themes.light); | ||
|
||
const { result } = renderHook(() => useTheme()); | ||
expect(result.current.theme).toEqual(Themes.light); | ||
}); | ||
|
||
it('should set the state of the localstorage value (light), if available', () => { | ||
window.localStorage.setItem(storageKey, Themes.dark); | ||
|
||
const { result } = renderHook(() => useTheme()); | ||
expect(result.current.theme).toEqual(Themes.dark); | ||
}); | ||
|
||
it('should lock the theme when it is given as a prop (dark)', () => { | ||
window.localStorage.setItem(storageKey, Themes.light); | ||
|
||
expect(localStorage.getItem(storageKey)).toEqual(Themes.light); | ||
|
||
const { result } = renderHook(() => useTheme({ lockedTheme: Themes.dark })); | ||
expect(result.current.theme).toEqual(Themes.dark); | ||
expect(localStorage.getItem(storageKey)).toEqual(Themes.dark); | ||
}); | ||
|
||
it('should overwrite the theme when given as prop', () => { | ||
window.localStorage.setItem(storageKey, Themes.light); | ||
|
||
const { result } = renderHook(() => useTheme()); | ||
|
||
expect(result.current.theme).toEqual(Themes.light); | ||
|
||
const { result: result2 } = renderHook(() => | ||
useTheme({ overwriteTheme: Themes.dark }), | ||
); | ||
|
||
expect(result2.current.theme).toEqual(Themes.dark); | ||
}); | ||
it('should overwrite the theme when given as prop and the overwrite is set as well', () => { | ||
expect(localStorage.getItem(storageKey)).toEqual(null); | ||
|
||
const { result } = renderHook(() => | ||
useTheme({ overwriteTheme: Themes.light, lockedTheme: Themes.dark }), | ||
); | ||
|
||
expect(localStorage.getItem(storageKey)).toEqual(Themes.dark); | ||
expect(result.current.theme).toEqual(Themes.light); | ||
}); | ||
it('should switch state when the setTheme is called', () => { | ||
window.localStorage.setItem(storageKey, Themes.light); | ||
const { result, rerender } = renderHook(() => useTheme()); | ||
expect(result.current.theme).toEqual(Themes.light); | ||
|
||
result.current.setTheme(Themes.dark); | ||
rerender(); | ||
expect(result.current.theme).toEqual(Themes.dark); | ||
}); | ||
|
||
it('should switch state when the setTheme is called, but the overwrite should still hold', () => { | ||
window.localStorage.setItem(storageKey, Themes.light); | ||
const { result, rerender } = renderHook(() => | ||
useTheme({ overwriteTheme: Themes.light }), | ||
); | ||
expect(result.current.theme).toEqual(Themes.light); | ||
|
||
result.current.setTheme(Themes.dark); | ||
rerender(); | ||
expect(result.current.theme).toEqual(Themes.light); | ||
}); | ||
}); |
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
Oops, something went wrong.