Last active
August 24, 2019 19:32
-
-
Save dance2die/d2c410470a75b612ec38e94af3011126 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 StickyViewport({ children, as = "div", ...rest }) { | |
return ( | |
// 1️⃣ To provide context for Sticky component tree | |
<StickyProvider> | |
2️⃣ Real work starts here | |
<StickyRoot as={as} {...rest}> | |
{children} | |
</StickyRoot> | |
</StickyProvider> | |
); | |
} | |
function StickyRoot({ children, as: Component = "div", ...rest }) { | |
// 3️⃣ Get actions provided in `StickyViewport` via `StickyProvider` | |
const dispatch = useStickyActions(); | |
// 4️⃣ Add the viewport reference to be used later in child component | |
to be set as `root` option of IntersectionObserver | |
const addContainerRef = containerRef => { | |
dispatch.setContainerRef(containerRef); | |
}; | |
return ( | |
<Component ref={addContainerRef} {...rest}> | |
{children} | |
</Component> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment