3.1.6 • Published 3 years ago

use-scroll-to-element v3.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

use-scroll-to-element

✨ React hook for using scroll to an element before it been rendered

✨ Scroll to an element gives the ability to scroll to any element in the app from a place far ahead without knowing on the Component anything (if it has been rendered or not)

useScrollToElement HOOK - a tiny hook that useLayoutEffect, ensuring the element is on Dom. let you the ability to dispatch an action before the component has rendered and the scroll will happen right after it appears.

Install

npm install --save use-scroll-to-element

Usage

import { ScrollToElement } from "use-scroll-to-element";

export const {
  useScroll: useScrollToImg,
  scroll: scrollToImg
} = new ScrollToElement();
  • ScrollToElement can apply just on "real" elements that is on the DOM - will not work on virtual lists etc.
  • @scroll - promise function once solved will scroll to the element by the id was generated from useScroll.
  • @useScroll - should be placed in the Component element, returning the id to attach to the desired scrolled element.
  • @scroll - promise function once solved will scroll to the element by the id was generated from useScroll.
  • @useScroll - should be placed in the Component element, returning the id to attach to the desired scrolled element.

./components/Main.js

export const Main = () => {
  const id = useScrollToImg();

  return (
          <>
            <div>Hello World</div>
            {...lotsOfElements}
            <img id={id} src="./image.jpg" />
          </>
  );
};

./modals/GoToImage.js

export const GoToImage = () => <button onClick={scrollToImg}>Show Image</button>;

#Live Example

https://codesandbox.io/s/use-scroll-to-element-3mc2p?file=/src/indexScroll.js:0-229

3.1.3

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.6

3 years ago

3.1.5

3 years ago

3.1.4

3 years ago

3.1.0

3 years ago

3.0.9

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.0

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

2.0.0

3 years ago

0.0.1

3 years ago