Created
February 6, 2020 20:11
-
-
Save subtleGradient/d95ed780a9fcf5afe6dc3272f9827e67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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