Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created February 6, 2020 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subtleGradient/d95ed780a9fcf5afe6dc3272f9827e67 to your computer and use it in GitHub Desktop.
Save subtleGradient/d95ed780a9fcf5afe6dc3272f9827e67 to your computer and use it in GitHub Desktop.
// @flow
import { useLayoutEffect } from 'react';
const mountCountMap = new Map<string, number>();
const updateRootClassNameUsageCount = (className, setter: (count: number) => number) => {
const oldCount = mountCountMap.get(className) || 0;
const count = setter(oldCount);
mountCountMap.set(className, count);
if (!document.documentElement) return;
if (count === 0) document.documentElement.classList.remove(className);
if (count > 0) document.documentElement.classList.add(className);
};
export function useRootClassList(className: string = '') {
useLayoutEffect(() => {
updateRootClassNameUsageCount(className, count => count + 1);
return () => updateRootClassNameUsageCount(className, count => count - 1);
}, [className]);
}
export function UseRootClassListView({ className }: { className: string }) {
useRootClassList(className);
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment