Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xranx OpenContext ThemeContext

From Leeroopedia
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

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>
  );
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment