Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
Try to apply mode styles to root html element before body (#162)
Browse files Browse the repository at this point in the history
* Try to apply mode to root html element before body

* Make safe access ever more safe

* Update index.ts

Co-authored-by: Jake Moxey <jakemoxey@gmail.com>
  • Loading branch information
daveols and jxom authored Sep 20, 2021
1 parent ec9842f commit f91986a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/bumbag/src/ColorMode/ColorModeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>({ setColorMode: () => {}, colorMode: 'default' });
export const ColorModeContext = React.createContext<any>({ setColorMode: () => { }, colorMode: 'default' });

type Props = {
children: React.ReactNode;
Expand Down Expand Up @@ -34,15 +34,15 @@ export function ColorModeProvider(props: Props) {
////////////////////////////////////

React.useEffect(() => {
addColorModeBodyClassName(defaultMode);
addColorModeRootElementClassName(defaultMode);
localStorage.set('mode', defaultMode);
}, [defaultMode]); // eslint-disable-line

////////////////////////////////////

const setColorMode = React.useCallback(
(colorMode) => {
addColorModeBodyClassName(colorMode, mode);
addColorModeRootElementClassName(colorMode, mode);
localStorage.set('mode', colorMode);
setMode(colorMode);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/bumbag/src/ColorMode/InitializeColorMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 7 additions & 6 deletions packages/bumbag/src/ColorMode/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { omit } from '../utils/omit';

const bodyClassPrefix = 'bb-mode';
const rootElementClassPrefix = 'bb-mode';
const cssVariablePrefix = '--bb';
const palettePrefix = `${cssVariablePrefix}-palette`;

Expand All @@ -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 {
Expand All @@ -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}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/bumbag/src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}),
Expand Down

1 comment on commit f91986a

@vercel
Copy link

@vercel vercel bot commented on f91986a Sep 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.