-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
9 changed files
with
142 additions
and
21 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
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,8 @@ | ||
import { useCallback, useRef } from 'react' | ||
|
||
export function useEventCallback<T extends (...args: any[]) => any>(fn: T) { | ||
const ref = useRef<T>(fn) | ||
ref.current = fn | ||
|
||
return useCallback((...args: any[]) => ref.current(...args), []) as T | ||
} |
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,11 @@ | ||
import { useRef } from 'react' | ||
|
||
// @see https://react.dev/reference/react/useRef#avoiding-recreating-the-ref-contents | ||
export function useRefValue<T>(value: () => T): T { | ||
const ref = useRef<T>() | ||
|
||
if (!ref.current) | ||
ref.current = value() | ||
|
||
return ref.current! | ||
} |
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 { createStore } from 'jotai/vanilla' | ||
|
||
export const jotaiStore = createStore() |
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,67 @@ | ||
import type { PropsWithChildren } from 'react' | ||
import { createContext, useContext, useEffect } from 'react' | ||
import { atomWithStorage } from 'jotai/utils' | ||
import { useAtomValue } from 'jotai' | ||
import { jotaiStore } from '../lib/store' | ||
|
||
const darkModeAtom = atomWithStorage('dark-mode', false) | ||
|
||
const actions = { | ||
// TODO | ||
toggle: () => jotaiStore.set(darkModeAtom, prev => !prev), | ||
} | ||
|
||
const DarkModeActionContext = createContext< | ||
typeof actions | ||
>(null!) | ||
const COLOR_SCHEME_QUERY = '(prefers-color-scheme: dark)' | ||
|
||
function getMatches(query: string): boolean { | ||
// Prevents SSR issues | ||
if (typeof window !== 'undefined') | ||
return window.matchMedia(query).matches | ||
return false | ||
} | ||
|
||
export function DarkModeProvider(props: PropsWithChildren) { | ||
// const isOSDark = useMediaQuery(COLOR_SCHEME_QUERY) | ||
|
||
useEffect(() => { | ||
const matchMedia = window.matchMedia(COLOR_SCHEME_QUERY) | ||
// Triggered at the first client-side load and if query changes | ||
const handleChange = () => { | ||
jotaiStore.set(darkModeAtom, getMatches(COLOR_SCHEME_QUERY)) | ||
} | ||
// Listen matchMedia | ||
matchMedia.addEventListener('change', handleChange) | ||
return () => { | ||
matchMedia.removeEventListener('change', handleChange) | ||
} | ||
}, []) | ||
return <DarkModeActionContext.Provider value={actions}> | ||
{props.children} | ||
<ThemeObserver /> | ||
</DarkModeActionContext.Provider> | ||
} | ||
|
||
function ThemeObserver() { | ||
const isDarkMode = useAtomValue(darkModeAtom) | ||
useEffect(() => { | ||
if (isDarkMode) { | ||
document.documentElement.dataset.theme = 'business' | ||
document.documentElement.classList.toggle('dark', true) | ||
} | ||
else { | ||
document.documentElement.dataset.theme = 'bumblebee' | ||
document.documentElement.classList.toggle('dark', false) | ||
} | ||
}, [isDarkMode]) | ||
|
||
return null | ||
} | ||
|
||
export function useThemeActions() { | ||
return useContext(DarkModeActionContext) | ||
} | ||
|
||
export const useIsDark = () => useAtomValue(darkModeAtom) |
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,21 @@ | ||
'use client' | ||
|
||
import type { FC, PropsWithChildren } from 'react' | ||
import React from 'react' | ||
import { useRefValue } from '../hooks/useRefValue' | ||
import { JotaiStoreProvider } from './jotai' | ||
import { DarkModeProvider } from './dark-mode' | ||
|
||
const ProviderComposer: FC<{ | ||
contexts: JSX.Element[] | ||
} & PropsWithChildren> = ({ contexts, children }) => { | ||
return contexts.reduceRight((kids: any, parent: any) => { | ||
return React.cloneElement(parent, { children: kids }) | ||
}, children) | ||
} | ||
|
||
export function Providers(props: PropsWithChildren) { | ||
return <ProviderComposer contexts={useRefValue(() => [<JotaiStoreProvider key={'JotaiStoreProvider'} />, <DarkModeProvider key={'DarkModeProvider'} />])}> | ||
{props.children} | ||
</ProviderComposer> | ||
} |
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 @@ | ||
import { Provider } from 'jotai' | ||
import type { PropsWithChildren } from 'react' | ||
import { jotaiStore } from '../lib/store' | ||
|
||
export const JotaiStoreProvider: React.FC<PropsWithChildren> = ({ children }) => { | ||
return <Provider store={jotaiStore}> | ||
{children} | ||
</Provider> | ||
} |