Last active
February 6, 2020 20:31
-
-
Save subtleGradient/cea26454f8abf18368560332f8db94f9 to your computer and use it in GitHub Desktop.
useRootLayerDomNode RootLayer
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, calculateNewCount: (count: number) => number) => { | |
const oldCount = mountCountMap.get(className) || 0; | |
const count = calculateNewCount(oldCount); | |
mountCountMap.set(className, count); | |
if (!document.documentElement) return; | |
if (count === 0) document.documentElement.classList.remove(className); | |
else if (count > 0) document.documentElement.classList.add(className); | |
}; | |
export function useRootClassList(className: string, disabled?: boolean = false) { | |
useLayoutEffect(() => { | |
if (disabled) { | |
return; | |
} | |
updateRootClassNameUsageCount(className, count => count + 1); | |
return () => updateRootClassNameUsageCount(className, count => count - 1); | |
}, [className, disabled]); | |
} | |
export function UseRootClassListView({ className, disabled }: { className: string, disabled: boolean }) { | |
useRootClassList(className, disabled); | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment