Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@designbycosmic/cosmic-react-scroll-hook

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@designbycosmic/cosmic-react-scroll-hook

React hook for listening to window scroll position

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

cosmic-react-scroll-hook

React hook for listening to window scroll position

Typical use: getting scroll position of window

// from any functional component

import { useScrollPosition } from "cosmic-react-scroll-hook";

const ScrollWatcher = () => {
  const { scrollX, scrollY } = useScrollPosition();

  return (
    <p>The current scroll position is {`x: ${scrollX}, y: ${scrollY}`}</p>
  );
}

export default ScrollWatcher;

You can also get the scroll position of scrollable elements other than the window

// from any functional component

import { useScrollPosition } from "cosmic-react-scroll-hook";

const ScrollWatcher = () => {
  const scrollableDiv = useRef({}); // Make sure to initialize ref with empty object
  const { scrollX, scrollY } = useScrollPosition(scrollableDiv.current);

  return (
    <div ref={scrollableDiv}>
      ...
    </div>
  );
}

export default ScrollWatcher;

You can also get direction of the scroll

// from any functional component

import { useScrollPosition } from "cosmic-react-scroll-hook";

const ScrollWatcher = () => {
  const scrollableDiv = useRef();
  const { scrollingUp, scrollingDown, scrollingRight, scrollingLeft } = useScrollPosition();

  ...
}

export default ScrollWatcher;

FAQs

Package last updated on 15 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc