Giter Site home page Giter Site logo

react-use-sticky's Introduction

React Sticky Hook

A react hook for observing/watching position: sticky state on refs

Installation

npm i react-use-sticky

Usage

useSticky returns a pair of values, the ref to observe/watch and the current sticky state of the ref.

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

function HeaderBar() {
  const [headerBarRef, sticky] = useSticky();
  const style = {
    position: 'sticky',
    top: 0,
    background: sticky ? 'green' : 'red',
  };

  return (
    <nav ref={headerBarRef} style={style}>
      HeaderBar
    </nav>
  );
}

export default HeaderBar;

Typescript with generic

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

function HeaderBar() {
  const [headerBarRef, sticky] = useSticky<HTMLDivElement>();
  const style = {
    position: 'sticky',
    top: 0,
    background: sticky ? 'green' : 'red',
  } as const;

  return (
    <div ref={headerBarRef} style={style}>
      HeaderBar
    </div>
  );
}

export default HeaderBar;

Build

npm run build

react-use-sticky's People

Contributors

robinjonsson avatar robindeseniogroup avatar wesbos avatar

Stargazers

YC.W avatar Vitaly Chernov avatar William Bout avatar zengkun avatar Alexandru Ţurcanu avatar Vinicius De Antoni avatar Henri Nikka avatar John avatar Andrey avatar Guilherme Oderdenge avatar Tao avatar Richard Scarrott avatar Airat Zhanshuakov avatar Andréas Hanss avatar Edgar Zamora  avatar Andres Fernando avatar Alejandro avatar Giulia Nicole Pernice avatar Wesley Cheung avatar Mike Stecker avatar Sofyan Setiawan avatar Greg Deane avatar atrekkingturtle avatar Muhammad Shahzad Ali avatar Fabian Lee avatar Christian Gaetano avatar AgedCoffee avatar Viktor Vasilev avatar Mohammad Kermani avatar Donga Kasi Pavan Kumar avatar Atila Murat avatar david harvey avatar Muhammad Farid Zia avatar Abhishek Uniyal avatar Muhammad Bhaska avatar André Iván avatar Noval avatar Guilherme Mota avatar Breno Polanski avatar Yasin ATEŞ avatar  avatar Atanas Atanasov avatar Siv Ram Shastri Jonnalagadda avatar Igor Conde avatar Ryota Murakami avatar Nicolas Santos avatar

Watchers

 avatar James Cloos avatar

Forkers

mczenith wesbos

react-use-sticky's Issues

Problems with Mobile Safari

Thanks for the hook, really useful.

I'm seeing issues on iOS where the reported value of stickyRef.current.getBoundingClientRect().top is often incorrect by quite some margin.

I see you're handling cases where it's negative due to rubber banding, but I've also observed incorrect positive values; I think it might have something to do with when the Mobile Safari browser UI grows / shrinks. Not sure if you've run into this / know of a fix?

There's a bug filed for the same problematic behaviour but with position fixed here https://openradar.appspot.com/radar?id=6668472289329152

I modified their jsbin slightly to dump out every value reported https://output.jsbin.com/divoyud

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.