npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-on-sticky

v0.0.9

Published

A React implementation for position:sticky events

Downloads

7

Readme

react-on-sticky

A React implementation of "An event for CSS position:sticky". Uses Intersection Obverser to trigger when an element is stickied. Please refer to the link for more details.

DEMO

Note

This is a pre-release, put this on npm for now so I can use it already for a personal project (unfortunately, i don't pay for npm private packages sob someone donate)

Getting started

yarn add react-on-sticky

The package consists of three components:

  • useStickyRoot (hook)
  • useOnSticky (hook)
  • StickyContainer

Usage

useOnStickyRoot

Use this hook within parent element/component of the targeted element which has position: sticky. Pass the ref provided by the hook into your parent element's ref.

import { useOnStickyRoot } from 'react-on-sticky';

function MyScrollableParentElement() {
  const [ stickyRootRef ] = useOnStickyRoot();

  return (
    <div
      ref={stickyRootRef}
      style={{
        position: 'absolute';
        overflowY: 'auto';
      }}>
      { children }
    </div>
  );
}

StickyContainer

The sticky container must wrap the target element with position: sticky. This component will inject invisibble header/footer sentinels which the IntersectionObserver will keep track of. Please see the reference article above in the introduction to learn more about the mechanics of this. Play around with the height/offset of the sentinels to get the exact trigger you're looking for.

| prop | Description | | -------------- | ------------------------------------------------------------------------------------------- | | component | string | JSX.Element - the dom element as string to render or a JSX Element as the wrapper | | headerHeight | string | the height of the top sentinel | | headerPosition | string | the position of the top sentinel relative to the top of the parent | | footerHeight | string | the height of the bottom sentinel | | footerPosition | string | the position of the bottom sentinel relative to the bottom of the parent | | isDebug | boolean | optional, render a visible border around the sentinels |

import { useOnStickyRoot, StickyContainer } from 'react-on-sticky';

function MyScrollableParentElement() {
  const [ stickyRootRef ] = useOnStickyRoot();

  return (
    <div
      ref={stickyRootRef}
      style={{
        position: 'absolute';
        overflowY: 'auto';
      }}>
      <StickyContainer
        component="div" // string or a e.g. StyledComponent
        headerHeight="100px"
        headerPosition="0px" // from top
        footerHeight="2rem"
        footerPosition="4rem" // from bottom
      >
      </StickyContainer>
    </div>
  );
}

useOnSticky

The hook that will receive the on sticky event. In the reference article, it toggles a class. This does not do that, but it exposes a boolean isStuck value which you can then do anything under the sun with (including toggling classNames).

Important note: Make sure that the element that has position:sticky has a className sticky. Otherwise this won't work.

import { useOnStickyRoot, StickyContainer, useOnSticky } from 'react-on-sticky';

function MyScrollableParentElement() {
  const [ stickyRootRef ] = useOnStickyRoot();

  return (
    <div
      ref={stickyRootRef}
      style={{
        position: 'absolute';
        overflowY: 'auto';
      }}>
      <StickyContainer
        component="div" // string or a e.g. StyledComponent
        headerHeight="100px"
        headerPosition="0px" // from top
        footerHeight="2rem"
        footerPosition="4rem" // from bottom
      >
      </StickyContainer>
    </div>
  );
}

function MyStickyElement() {
  const id = "someId";
  // important: you need to pass in a unique id
  const [ isStuck ] = useOnSticky(id);

  useEffect(() => {
    console.log(isStuck);
  }, [isStuck]);

  return (
    <div
      className="sticky"
      id={id} // important: you need to pass in a unique id
      style={{
        position: 'sticky',
        top: '10px',
      }}>
      I'm stuck!
    </div>
  );
}