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

@jonny1994/react-sticky

v1.0.0

Published

Sticky component for React

Downloads

4

Readme

react-sticky

Modified from react-sticky repository, refactored with typescript and react hooks.

Make your React components sticky!

CSS

There's a CSS alternative to react-sticky: the position: sticky feature. However it currently does not have full browser support, specifically a lack of IE11 support and some bugs with table elements. Before using react-sticky, check to see if the browser support and restrictions prevent you from using position: sticky, as CSS will always be faster and more durable than a JS implementation.

position: -webkit-sticky;
position: sticky;
top: 0;

Installation

npm install @jonny1994/react-sticky

# or

yarn add @jonny1994/react-sticky

Overview & Basic Example

The goal of react-sticky is make it easier for developers to build UIs that have sticky elements. Some examples include a sticky navbar, or a two-column layout where the left side sticks while the right side scrolls.

react-sticky works by calculating the position of a <Sticky> component relative to a <StickyContainer> component. If it would be outside the viewport, the styles required to affix it to the top of the screen are passed as an argument to a render callback, a function passed as a child.

<StickyContainer>
  <Sticky>{({ style }) => <h1 style={style}>Sticky element</h1>}</Sticky>
</StickyContainer>

The majority of use cases will only need the style to pass to the DOM, but some other properties are passed for advanced use cases:

  • style (object) - modifiable style attributes to optionally be passed to the element returned by this function. For many uses, this will be the only attribute needed.
  • isSticky (boolean) - is the element sticky as a result of the current event?
  • wasSticky (boolean) - was the element sticky prior to the current event?
  • distanceFromTop (number) - number of pixels from the top of the Sticky to the nearest StickyContainer's top
  • distanceFromBottom (number) - number of pixels from the bottom of the Sticky to the nearest StickyContainer's bottom
  • calculatedHeight (number) - height of the element returned by this function

The Sticky's child function will be called when events occur in the parent StickyContainer, and will serve as the callback to apply your own logic and customizations, with sane style attributes to get you up and running quickly.

Full Example

Here's an example of all of those pieces together:

app.js

import React from 'react';
import { StickyContainer, Sticky } from '@jonny1994/react-sticky';
// ...

class App extends React.Component {
  render() {
    return (
      <StickyContainer>
        {/* Other elements can be in between `StickyContainer` and `Sticky`,
        but certain styles can break the positioning logic used. */}
        <Sticky>
          {({
            style,

            // the following are also available but unused in this example
            isSticky,
            wasSticky,
            distanceFromTop,
            distanceFromBottom,
            calculatedHeight
          }) => (
            <header style={style}>
              {/* ... */}
            </header>
          )}
        </Sticky>
        {/* ... */}
      </StickyContainer>
    );
  },
};

When the "stickiness" becomes activated, the arguments to the sticky function are modified. Similarly, when deactivated, the arguments will update accordingly.

<StickyContainer /> Props

<StickyContainer /> supports all valid <div /> props.

<Sticky /> Props

relative (default: false)

Set relative to true if the <Sticky /> element will be rendered within an overflowing <StickyContainer /> (e.g. style={{ overflowY: 'auto' }}) and you want the <Sticky /> behavior to react to events only within that container.

When in relative mode, window events will not trigger sticky state changes. Only scrolling within the nearest StickyContainer can trigger sticky state changes.

topOffset (default: 0)

Sticky state will be triggered when the top of the element is topOffset pixels from the top of the closest <StickyContainer />. Positive numbers give the impression of a lazy sticky state, whereas negative numbers are more eager in their attachment.

app.js

<StickyContainer>
  ...
  <Sticky topOffset={80}>
    { props => (...) }
  </Sticky>
  ...
</StickyContainer>

The above would result in an element that becomes sticky once its top is greater than or equal to 80px away from the top of the <StickyContainer />.

bottomOffset (default: 0)

Sticky state will be triggered when the bottom of the element is bottomOffset pixels from the bottom of the closest <StickyContainer />.

app.js

<StickyContainer>
  ...
  <Sticky bottomOffset={80}>
    { props => (...) }
  </Sticky>
  ...
</StickyContainer>

The above would result in an element that ceases to be sticky once its bottom is 80px away from the bottom of the <StickyContainer />.

disableCompensation (default: false)

Set disableCompensation to true if you do not want your <Sticky /> to apply padding to a hidden placeholder <div /> to correct "jumpiness" as attachment changes from position:fixed and back.

app.js

<StickyContainer>
  ...
  <Sticky disableCompensation>
    { props => (...) }
  </Sticky>
  ...
</StickyContainer>

disableHardwareAcceleration (default: false)

When disableHardwareAcceleration is set to true, the <Sticky /> element will not use hardware acceleration (e.g. transform: translateZ(0)). This setting is not recommended as it negatively impacts the mobile experience, and can usually be avoided by improving the structure of your DOM.

app.js

<StickyContainer>
  ...
  <Sticky disableHardwareAcceleration>
    { props => (...) }
  </Sticky>
  ...
</StickyContainer>

FAQ

I get errors while using React.Fragments

React.Fragments does not correspond to an actual DOM node, so react-sticky can not calculate its position. Because of this, React.Fragments is not supported.