MCPcopy Create free account
hub / github.com/codeaashu/claude-code / ThemeProvider

Function ThemeProvider

src/components/design-system/ThemeProvider.tsx:43–116  ·  view source on GitHub ↗
({
  children,
  initialState,
  onThemeSave = defaultSaveTheme
}: Props)

Source from the content-addressed store, hash-verified

41 }));
42}
43export function ThemeProvider({
44 children,
45 initialState,
46 onThemeSave = defaultSaveTheme
47}: Props) {
48 const [themeSetting, setThemeSetting] = useState(initialState ?? defaultInitialTheme);
49 const [previewTheme, setPreviewTheme] = useState<ThemeSetting | null>(null);
50
51 // Track terminal theme for 'auto' resolution. Seeds from $COLORFGBG (or
52 // 'dark' if unset); the OSC 11 watcher corrects it on first poll.
53 const [systemTheme, setSystemTheme] = useState<SystemTheme>(() => (initialState ?? themeSetting) === 'auto' ? getSystemThemeName() : 'dark');
54
55 // The setting currently in effect (preview wins while picker is open)
56 const activeSetting = previewTheme ?? themeSetting;
57 const {
58 internal_querier
59 } = useStdin();
60
61 // Watch for live terminal theme changes while 'auto' is active.
62 // Positive feature() pattern so the watcher import is dead-code-eliminated
63 // in external builds.
64 useEffect(() => {
65 if (feature('AUTO_THEME')) {
66 if (activeSetting !== 'auto' || !internal_querier) return;
67 let cleanup: (() => void) | undefined;
68 let cancelled = false;
69 void import('../../utils/systemThemeWatcher.js').then(({
70 watchSystemTheme
71 }) => {
72 if (cancelled) return;
73 cleanup = watchSystemTheme(internal_querier, setSystemTheme);
74 });
75 return () => {
76 cancelled = true;
77 cleanup?.();
78 };
79 }
80 }, [activeSetting, internal_querier]);
81 const currentTheme: ThemeName = activeSetting === 'auto' ? systemTheme : activeSetting;
82 const value = useMemo<ThemeContextValue>(() => ({
83 themeSetting,
84 setThemeSetting: (newSetting: ThemeSetting) => {
85 setThemeSetting(newSetting);
86 setPreviewTheme(null);
87 // Switching to 'auto' restarts the watcher (activeSetting dep), whose
88 // first poll fires immediately. Seed from the cache so the OSC
89 // round-trip doesn't flash the wrong palette.
90 if (newSetting === 'auto') {
91 setSystemTheme(getSystemThemeName());
92 }
93 onThemeSave?.(newSetting);
94 },
95 setPreviewTheme: (newSetting_0: ThemeSetting) => {
96 setPreviewTheme(newSetting_0);
97 if (newSetting_0 === 'auto') {
98 setSystemTheme(getSystemThemeName());
99 }
100 },

Callers

nothing calls this directly

Calls 4

getSystemThemeNameFunction · 0.85
useStdinFunction · 0.85
featureFunction · 0.85
cleanupFunction · 0.50

Tested by

no test coverage detected