Last active
August 22, 2019 02:48
-
-
Save dance2die/b334739cabccfc716d1b177f303af947 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
// 1️⃣ | |
function useSentinelOffsets(topSentinelRef) { | |
const { stickyRefs } = useStickyState(); | |
const [bottomSentinelHeight, setBottomSentinelHeight] = useState(""); | |
const [topSentinelMarginTop, setTopSentinelMarginTop] = useState(""); | |
// 2️⃣ | |
useEffect(() => { | |
// 3️⃣ | |
const stickyNode = stickyRefs.get(topSentinelRef.current); | |
// 4️⃣ | |
const topStyle = window.getComputedStyle(stickyNode); | |
const getProp = name => topStyle.getPropertyValue(name); | |
const paddingtop = getProp("padding-top"); | |
const paddingBottom = getProp("padding-bottom"); | |
const height = getProp("height"); | |
const marginTop = getProp("margin-top"); | |
// 5️⃣ | |
const bottomSentinelHeight = `calc(${marginTop} + | |
${paddingtop} + | |
${height} + | |
${paddingBottom})`; | |
setBottomSentinelHeight(bottomSentinelHeight); | |
setTopSentinelMarginTop(marginTop); | |
}, [stickyRefs, topSentinelRef]); | |
// 6️⃣ | |
return { bottomSentinelHeight, topSentinelMarginTop }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment