Implementation:0xranx OpenContext ThemeContext
| Knowledge Sources | |
|---|---|
| Domains | Theming, React |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
React context provider for theme management, supporting system/light/dark preferences with localStorage persistence, system media query detection, and Tauri window theme sync.
Description
The ThemeContext.jsx module in src/ui/src/context/ exports ThemeProvider and useTheme. The provider stores the user's theme preference in localStorage under oc.theme, listens to prefers-color-scheme: dark media query for system theme changes, applies the resolved theme to document.documentElement via useLayoutEffect, and synchronizes the Tauri window theme via dynamic import of @tauri-apps/api/window. The useTheme hook returns { preference, resolvedTheme, setPreference }.
Usage
Wrap the app root in ThemeProvider and call useTheme() in any component that needs the current theme.
Code Reference
Source Location
- Repository: 0xranx_OpenContext
- File: src/ui/src/context/ThemeContext.jsx
- Lines: 1-95
Signature
export function ThemeProvider({ children })
export function useTheme() // → { preference, resolvedTheme, setPreference }
Import
import { ThemeProvider, useTheme } from './context/ThemeContext';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| children | ReactNode | Yes | Child components |
Outputs
| Name | Type | Description |
|---|---|---|
| preference | string | 'light' | 'dark' |
| resolvedTheme | string | 'dark' (resolved actual) |
| setPreference | Function | Update theme preference |
Usage Examples
Setup and Usage
import { ThemeProvider, useTheme } from './context/ThemeContext';
// App root
function App() {
return <ThemeProvider><MainUI /></ThemeProvider>;
}
// In a component
function ThemeToggle() {
const { preference, setPreference } = useTheme();
return (
<button onClick={() => setPreference(preference === 'dark' ? 'light' : 'dark')}>
Toggle {preference}
</button>
);
}