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

use-chat-scroll

v1.0.3

Published

React hook for chat-like scroll behavior

Downloads

69

Readme

use-chat-scroll

by Vytenis Urbonavičius

use-chat-scroll is a React hook for chat-like scroll behavior of HTML elements.

Main features:

  • When scroll is at the bottom of container, it would stay there whenever new data is added.
  • When scrolling up, it provides infinite scroll behavior.

You can use useChatScroll for getting both features or useStickyScroll to only keep scroll at the bottom when new data is added to scrollable HTML element.


Installation

npm install --save use-chat-scroll

Usage

Below examples use TypeScript. However, you can use plain JavaScript as well by skipping type declarations and writing everything else same as in examples.

Usage example of full chat scroll behavior with React hooks for data state management:

import {useChatScroll, useDataLoader} from 'use-chat-scroll'

const loadAdditionalData = () => [ /* Additional data */ ]

const YourFunctionalReactComponent: React.FC = () => {
  const [data, setData] = useState<any[]>([])
  const containerRef = useRef<React.MutableRefObject<HTMLDivElement>>()
  const loader = useDataLoader(loadAdditionalData, data, setData)
  useChatScroll(containerRef, data, loader)

  return (
    <div ref={containerRef} style={{height: "400px", width: "100%", overflow: 'auto'}}>
      {data.map(item => (
        // ...
      ))}
    </div>
  )
}

In case you would notice that scroll is not properly adjusted when loading additional data, make sure that setting scrollTop attribute to scrollable HTML element works. Sometimes browsers have issues when height is set using percentage. This is not considered to be a hook-related issue.

Note that even if loadAdditionalData would be defined inside a functional component, it would not be able to properly use state variables such as data. If such data would need to be passed, use optional fourth argument of useDataLoader hook. Array passed there would become accessible as arguments in loadAdditionalData.

If you would be using a state management library such as Redux with thunk actions, you would not need to be using useDataLoader. In such case your action should accept beforeRender callback and would become a loader itself. beforeRender callback should be invoked immediately after gathering data but before updating state.

If you only need to keep scroll at the bottom without infinite scroll behavior:

import {useStickyScroll} from 'use-chat-scroll'

const YourFunctionalReactComponent: React.FC = (data: any[]) => {
  const containerRef = useRef<React.MutableRefObject<HTMLDivElement>>()
  useStickyScroll(containerRef, data)

  return (
    <div ref={containerRef} style={{height: "400px", width: "100%", overflow: 'auto'}}>
      {data.map(item => (
        // ...
      ))}
    </div>
  )
}

Supported Configuration

It is possible to customize behavior of useChatScroll. This is done by passing additional object argument when calling the hook:

useChatScroll(ref, data, loadCb, options)

Options is an object with the following structure (below example contains default values). All keys are optional.

const options = {

  /**
   * Options for reverse infinite scroll behavior.
   */
  reverseInfiniteScroll: {

    /**
     * Defines how close to the top user needs to scroll in order to invoke gathering of additional data.
     */
    scrollThreshold: {

      /**
       * Defines how threshold is calculated.
       */
      type: EScrollThresholdType.fraction,

      /**
       * Threshold value.
       */
      value: 0.3,

    },

    /**
     * Defines whether infinite scroll behavior is enabled initially.
     */
    enabled: true,

  },

  /**
   * Options for sticky scroll behavior.
   */
  stickyScroll: {

    /**
     * Defines whether sticky scroll behavior is enabled initially.
     */
    enabled: true.

  },
}

Additional Documentation

You can find more details about the hook in a generated documentation under "./docs" folder of the node module.