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-hook-sticky

v0.4.0

Published

React hook sticky

Downloads

30

Readme

React hook sticky

Top performant lightweight solution for sticky components. Start to collaborate and share your sticky plugin.

Provide your ideas, feel free to contribute.

Demo

sticky

react-hook-sticky-playground

Roadmap

Would you like to contribute? Here are some nice to have ideas:

  • [ ] Add plugins.
  • [ ] Storybook and examples.
  • [ ] Add development runtime invalidations for warning and error check.
  • [ ] Create Wiki Page.
  • [ ] Add unit test.
  • [ ] Add E2E tests for IE and Chrome. (Probably Puppeter)
  • [ ] Add E2E perfomance painting tests
  • [ ] Configure CI (Probably Travis)
  • [ ] Add code coverage and bundle size check.
  • [ ] Add support for Horizontal scrolling / resizing

Install

yarn add react-hook-sticky --exact

Quickstart

Basic

// sticky-component.js
import React from 'react';
// 1 - Import
import { useSticky, plugins } from 'react-hook-sticky';

const stickyConfig = {
  // Define the context for this sticky, you may have many sticky elements.
  context: 'my-sticky-1',
  // 2 - Make use of builtin plugins
  plugin: plugins.fillBetween, // you may combine with throttling or wrap as your needs throttle(plugins.fillBetween, 10),

  // Optional - Specify the proper event listener strategy for better performance over any browser.
  // default { passive: true } https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
  eventListenerOptions: Modernizr.passiveeventlisteners
    ? { passive: true }
    : false,
};

export const StickyComponent = props => {
  const { createBoundary } = useSticky(stickyConfig);

  return (
    <div className="sticky-outer" ref={createBoundary('outer')}>
      {/* 3 - Register the target to become sticky */}
      <div className="sticky" ref={createBoundary('sticky')}>
        I am sticky between outer height or screen height
      </div>
    </div>
  );
};

Optional define specific boundaries

export const StickyComponent = props => {
  const { createBoundary } = useSticky(stickyConfig);

  return (
    <div className="sticky-outer">
      <h1 ref={createBoundary('top')}>Top boundary</h1>
      {/* 3 - Register the target to become sticky */}
      <div className="sticky" ref={createBoundary('sticky')}>
        I am sticky between top and bottom boundaries or screen height
      </div>
      <div ref={createBoundary('bottom')}>Bottom boundary</div>
    </div>
  );
};

Development

Contribute and create your own plug-in right away.

Quick

  • Fork demo https://codesandbox.io/s/react-hook-sticky-ts0rb
  • Create your sticky logic
// You may reuse common code and tools
import {
  useMomentum,
  getClampArea,
  createStyle,
  setInlineStyle,
} from '../commons';

// Explode your creativity, create your new module sticky to later being integrated
export const bunnyJump = (context, event) => {
  const { cacheStyles, boundaries } = context;
  const stickyBoundary = boundaries.sticky;
  const clampArea = getClampArea(boundaries);

  if (!stickyBoundary || !clampArea.height) {
    return;
  }

  const nextPosition = {
    position: 'absolute',
    maxHeight: clampArea.height,
  };
  const { isGoingDown } = useMomentum(event);

  if (isGoingDown) {
    nextPosition.top = clampArea.top;
  } else {
    nextPosition.bottom = clampArea.bottom;
  }

  setInlineStyle(stickyBoundary, createStyle(nextPosition), cacheStyles);
};
  • Implement in real time in your sandbox
// 1 - Import your snippet
import { bunnyJump } from './sticky-jump-draft';

import { useSticky } from 'react-hook-sticky';

const stickyConfig = {
  context: 'stickyJump',

  // 2 - Register it!
  plugin: bunnyJump,
};

export const StickyComponent = props => {
  const { createBoundary } = useSticky(stickyConfig);

  return (
    <section ref={createBoundary('top')}>
      <div className="ad" ref={createBoundary('sticky')} />
      {/* Registering boundaries all in one collection under stickyJump context */}
      <div className="block" ref={createBoundary(['jumpMe'])} />
      <div className="block" ref={createBoundary(['jumpMe'])} />
      <div className="block" ref={createBoundary('bottom')} />
    </section>
  );
};

How to develop local

Setup

  git clone [email protected]:Ariel-Rodriguez/react-hook-sticky.git
  yarn
  # Link library
  yarn link
  # install and link library into example
  # any change done in library will be hor reloaded in example page.
  cd example && yarn && yarn link react-hook-sticky

Run examples

  yarn start

Share or contribute!