mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-28 01:08:47 +00:00
Projects (#192)
* Add file save UI and drafts project * Fix build * Create SearchList component as abstraction * Add project page and file tree view * Create Locale context * Create Theme context * Create Version context * Create Title context * Create Project context * Store current file in project context * Fix issues when renaming file and implement deleting * Style improvements * Make all project strings translatable * Fix z-index
This commit is contained in:
41
src/app/contexts/Theme.tsx
Normal file
41
src/app/contexts/Theme.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { ComponentChildren } from 'preact'
|
||||
import { createContext } from 'preact'
|
||||
import { useCallback, useContext, useEffect, useState } from 'preact/hooks'
|
||||
import { Analytics } from '../Analytics'
|
||||
import { Store } from '../Store'
|
||||
|
||||
interface Theme {
|
||||
theme: string,
|
||||
changeTheme: (theme: string) => unknown,
|
||||
}
|
||||
const Theme = createContext<Theme>({
|
||||
theme: 'dark',
|
||||
changeTheme: () => {},
|
||||
})
|
||||
|
||||
export function useTheme() {
|
||||
return useContext(Theme)
|
||||
}
|
||||
|
||||
export function ThemeProvider({ children }: { children: ComponentChildren }) {
|
||||
const [theme, setTheme] = useState(Store.getTheme())
|
||||
|
||||
useEffect(() => {
|
||||
document.documentElement.setAttribute('data-theme', theme)
|
||||
}, [theme])
|
||||
|
||||
const changeTheme = useCallback((theme: string) => {
|
||||
Analytics.setTheme(theme)
|
||||
Store.setTheme(theme)
|
||||
setTheme(theme)
|
||||
}, [])
|
||||
|
||||
const value: Theme = {
|
||||
theme,
|
||||
changeTheme,
|
||||
}
|
||||
|
||||
return <Theme.Provider value={value}>
|
||||
{children}
|
||||
</Theme.Provider>
|
||||
}
|
||||
Reference in New Issue
Block a user