-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4888435
commit 9147a4e
Showing
8 changed files
with
165 additions
and
68 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
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,58 @@ | ||
import React, { | ||
createContext, | ||
FC, | ||
useContext, | ||
useEffect, | ||
useState | ||
} from 'react' | ||
import { getLuminance } from 'color2k' | ||
|
||
type GlobalContextValue = { | ||
isApiInit: boolean | ||
isDarkTheme: boolean | ||
} | ||
|
||
const GlobalContext = createContext<GlobalContextValue | null>(null) | ||
|
||
export const useGlobalContext = () => { | ||
const context = useContext(GlobalContext) | ||
if (!context) { | ||
throw new Error( | ||
'useGlobalContext must be used within a GlobalContextProvider' | ||
) | ||
} | ||
return context | ||
} | ||
|
||
export const GlobalContextProvider: FC< | ||
Omit<GlobalContextValue, 'isDarkTheme'> & { children: React.ReactNode } | ||
> = ({ isApiInit, children }) => { | ||
const [isDarkTheme, setIsDarkTheme] = useState(true) | ||
|
||
useEffect(() => { | ||
const updateTheme = () => { | ||
const backgroundColor = getComputedStyle(document.documentElement) | ||
.getPropertyValue('--vscode-sideBarTitle-background') | ||
.trim() | ||
setIsDarkTheme(getLuminance(backgroundColor) < 0.5) | ||
} | ||
|
||
// Initial theme check | ||
updateTheme() | ||
|
||
// Watch for theme changes | ||
const observer = new MutationObserver(updateTheme) | ||
observer.observe(document.documentElement, { | ||
attributes: true, | ||
attributeFilter: ['style'] | ||
}) | ||
|
||
return () => observer.disconnect() | ||
}, []) | ||
|
||
return ( | ||
<GlobalContext.Provider value={{ isApiInit, isDarkTheme }}> | ||
{children} | ||
</GlobalContext.Provider> | ||
) | ||
} |
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