Skip to content

Instantly share code, notes, and snippets.

@dance2die
Last active August 22, 2019 02:48
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 dance2die/b334739cabccfc716d1b177f303af947 to your computer and use it in GitHub Desktop.
Save dance2die/b334739cabccfc716d1b177f303af947 to your computer and use it in GitHub Desktop.
// 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