All files / webview ThemeContext.tsx

100% Statements 11/11
100% Branches 4/4
100% Functions 6/6
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28    2x     9x 6x       9x 6x 8x         6x 6x     9x       9x    
import { createContext, useContext, useState, useEffect, type ReactNode } from 'react';
 
const ThemeContext = createContext(false);
 
export function ThemeProvider({ children }: { children: ReactNode }) {
  const [isDark, setIsDark] = useState(() =>
    document.body.classList.contains('vscode-dark') ||
    document.body.classList.contains('vscode-high-contrast')
  );
 
  useEffect(() => {
    const observer = new MutationObserver(() => {
      setIsDark(
        document.body.classList.contains('vscode-dark') ||
        document.body.classList.contains('vscode-high-contrast')
      );
    });
    observer.observe(document.body, { attributes: true, attributeFilter: ['class'] });
    return () => observer.disconnect();
  }, []);
 
  return <ThemeContext.Provider value={isDark}>{children}</ThemeContext.Provider>;
}
 
export function useIsDark(): boolean {
  return useContext(ThemeContext);
}