Last active
August 22, 2019 19:24
-
-
Save dance2die/4483589c5ff5ed9cc9a6fc9ecd2588be 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
function Sticky({ children, as = "div", className = "", ...rest }) { | |
// 1️⃣ Get TOP & BOTTOM sentinels to associate with | |
const { topSentinelRef, bottomSentinelRef } = useContext( | |
StickySectionContext | |
); | |
const dispatch = useStickyActions(); | |
// 2️⃣ Associate the current element with TOP & BOTTOM sentinels | |
// so that we can retrieve correct child target element | |
// from either a top sentinel or a bottom sentinel | |
const addStickyRef = stickyRef => { | |
dispatch.addStickyRef(topSentinelRef, bottomSentinelRef, stickyRef); | |
}; | |
const Component = as; | |
// 3️⃣ No magic - Just apply the "sticky" position using class | |
return ( | |
<Component | |
ref={addStickyRef} | |
className={styles.sticky + className || ` ${className}`} | |
{...rest} | |
> | |
{children} | |
</Component> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment