Skip to content

Instantly share code, notes, and snippets.

@dance2die
Last active August 24, 2019 19:32
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/d2c410470a75b612ec38e94af3011126 to your computer and use it in GitHub Desktop.
Save dance2die/d2c410470a75b612ec38e94af3011126 to your computer and use it in GitHub Desktop.
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