diff --git a/packages/bumbag/src/ColorMode/ColorModeContext.tsx b/packages/bumbag/src/ColorMode/ColorModeContext.tsx index 2d1889f2b..c0931796f 100644 --- a/packages/bumbag/src/ColorMode/ColorModeContext.tsx +++ b/packages/bumbag/src/ColorMode/ColorModeContext.tsx @@ -3,9 +3,9 @@ import ConditionalWrap from 'conditional-wrap'; import { useLocalStorage } from '../utils/useLocalStorage'; import { useTheme } from '../utils/useTheme'; -import { addColorModeBodyClassName, getDefaultColorMode } from './utils'; +import { addColorModeRootElementClassName, getDefaultColorMode } from './utils'; -export const ColorModeContext = React.createContext({ setColorMode: () => {}, colorMode: 'default' }); +export const ColorModeContext = React.createContext({ setColorMode: () => { }, colorMode: 'default' }); type Props = { children: React.ReactNode; @@ -34,7 +34,7 @@ export function ColorModeProvider(props: Props) { //////////////////////////////////// React.useEffect(() => { - addColorModeBodyClassName(defaultMode); + addColorModeRootElementClassName(defaultMode); localStorage.set('mode', defaultMode); }, [defaultMode]); // eslint-disable-line @@ -42,7 +42,7 @@ export function ColorModeProvider(props: Props) { const setColorMode = React.useCallback( (colorMode) => { - addColorModeBodyClassName(colorMode, mode); + addColorModeRootElementClassName(colorMode, mode); localStorage.set('mode', colorMode); setMode(colorMode); }, diff --git a/packages/bumbag/src/ColorMode/InitializeColorMode.tsx b/packages/bumbag/src/ColorMode/InitializeColorMode.tsx index bd84a6757..1592d65e5 100644 --- a/packages/bumbag/src/ColorMode/InitializeColorMode.tsx +++ b/packages/bumbag/src/ColorMode/InitializeColorMode.tsx @@ -4,7 +4,7 @@ import * as React from 'react'; const noflash = `(function() { try { var mode = localStorage.getItem('bb.mode'); if (!mode) return - document.body.classList.add('bb-mode-' + mode); + document.getElementsByTagName('html')[0].classList.add('bb-mode-' + mode); } catch (e) {} })();`; export function InitializeColorMode() { diff --git a/packages/bumbag/src/ColorMode/utils.ts b/packages/bumbag/src/ColorMode/utils.ts index ddac50311..414a6d15e 100644 --- a/packages/bumbag/src/ColorMode/utils.ts +++ b/packages/bumbag/src/ColorMode/utils.ts @@ -1,6 +1,6 @@ import { omit } from '../utils/omit'; -const bodyClassPrefix = 'bb-mode'; +const rootElementClassPrefix = 'bb-mode'; const cssVariablePrefix = '--bb'; const palettePrefix = `${cssVariablePrefix}-palette`; @@ -23,7 +23,7 @@ export function getColorModesCSSVariables(theme) { cssVariables = Object.entries(theme.palette.modes || {}).reduce((cssVariables, [modeKey, value]) => { return { ...cssVariables, - [`&.${bodyClassPrefix}-${modeKey}`]: mapCSSVariables(value), + [`&.${rootElementClassPrefix}-${modeKey}`]: mapCSSVariables(value), }; }, cssVariables); return { @@ -37,12 +37,13 @@ export function getColorFromCSSVariable(selector, fallback) { return `var(${palettePrefix}-${selector}, ${fallback})`; } -export function addColorModeBodyClassName(nextMode: string, prevMode?: string) { - if (typeof window !== 'undefined' && window.document && window.document.body) { +export function addColorModeRootElementClassName(nextMode: string, prevMode?: string) { + const rootElement = window?.document?.getElementsByTagName?.('html')?.[0] ?? window?.document?.body; + if (rootElement) { if (prevMode) { - document.body.classList.remove(`${bodyClassPrefix}-${prevMode}`); + rootElement.classList.remove(`${rootElementClassPrefix}-${prevMode}`); } - document.body.classList.add(`${bodyClassPrefix}-${nextMode}`); + rootElement.classList.add(`${rootElementClassPrefix}-${nextMode}`); } } diff --git a/packages/bumbag/src/theme/index.ts b/packages/bumbag/src/theme/index.ts index 9b9e32514..cc84e8c4d 100644 --- a/packages/bumbag/src/theme/index.ts +++ b/packages/bumbag/src/theme/index.ts @@ -23,7 +23,7 @@ import Toast from './Toast'; export default (overrides: ThemeConfig = {}) => ({ name: 'default', useCSSVariables: true, - cssVariablesSelector: 'html,body', + cssVariablesSelector: 'html', ...overrides, altitudes: altitudes(overrides.altitudes || {}), borders: borders(overrides.borders || {}),