Skip to content

Commit

Permalink
fix(ThemeProvider): fix rootClassName update (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorko authored Dec 27, 2023
1 parent 3180060 commit 4f0f099
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/theme/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ export function ThemeProvider({
) as RealTheme;
const themeValue = theme === 'system' ? systemTheme : theme;

const prevRootClassName = React.useRef('');

React.useEffect(() => {
if (!scoped) {
updateBodyClassName(themeValue, {'native-scrollbar': nativeScrollbar}, rootClassName);
updateBodyClassName(
themeValue,
{'native-scrollbar': nativeScrollbar},
rootClassName,
prevRootClassName.current,
);
prevRootClassName.current = rootClassName;
}
}, [nativeScrollbar, themeValue, scoped, rootClassName]);

Expand Down
10 changes: 10 additions & 0 deletions src/components/theme/updateBodyClassName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function updateBodyClassName(
newTheme: RealTheme,
modifiers?: Partial<BodyClassNameModifiers>,
customRootClassName?: string,
prevCustomRootClassName?: string,
) {
const bodyEl = document.body;

Expand All @@ -29,6 +30,15 @@ export function updateBodyClassName(
bodyEl.classList.add(rootNewClassName);
}

if (prevCustomRootClassName) {
const parsedPrevCustomRootClassNames = prevCustomRootClassName.split(' ');
parsedPrevCustomRootClassNames.forEach((cls) => {
if (cls) {
bodyEl.classList.remove(cls);
}
});
}

if (customRootClassName) {
const parsedCustomRootClassNames = customRootClassName.split(' ');
parsedCustomRootClassNames.forEach((cls) => {
Expand Down

0 comments on commit 4f0f099

Please sign in to comment.