You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

react-scroll-to-bottom

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-scroll-to-bottom

React container that will auto scroll to bottom

4.0.0
Source
npmnpm
Version published
Weekly downloads
49K
-8.75%
Maintainers
1
Weekly downloads
 
Created
Source

react-scroll-to-bottom

npm version Node.js CI

React container that will auto scroll to bottom or top if new content is added and viewport is at the bottom, similar to tail -f. Otherwise, a "jump to bottom" button will be shown to allow user to quickly jump to bottom.

Demo

Try out the demo at https://compulim.github.io/react-scroll-to-bottom/.

Demo

Breaking changes

[3.0.0] - 2020-06-21

  • scrollToBottom/scrollToEnd/scrollToStart/scrollToTop now accept an option { behavior: 'auto' | 'smooth' }
    • Without the option, it is by default to artificial smooth scrolling (smooth), to keep existing behavior
    • This behavior may change in the future, by defaulting to discrete scrolling (auto), to better align with HTML DOMElement.scrollIntoView standard
    • During the transition, please always pass { behavior: 'smooth' } to keep existing behavior

[2.0.0] - 2020-05-07

  • Starting from react-scroll-to-bottom@2, we requires React 16.8.6 or above. This enable developers to use React Hooks to add features to the scroll view.

Sample code

import { css } from 'emotion';
import ScrollToBottom from 'react-scroll-to-bottom';

const ROOT_CSS = css({
  height: 600,
  width: 400
});

export default props => (
  <ScrollToBottom className={ROOT_CSS}>
    <p>
      Nostrud nisi duis veniam ex esse laboris consectetur officia et. Velit cillum est veniam culpa magna sit
      exercitation excepteur consectetur ea proident. Minim pariatur nisi dolore Lorem ipsum adipisicing do. Ea
      cupidatat Lorem sunt fugiat. Irure est sunt nostrud commodo sint.
    </p>
    <p>
      Duis consectetur ad in fugiat et aliquip esse adipisicing occaecat et sunt ea occaecat ad. Tempor anim consequat
      commodo veniam nostrud sunt deserunt adipisicing Lorem Lorem magna irure. Eu ut ipsum magna nulla sunt duis Lorem
      officia pariatur. Nostrud nisi anim nostrud ea est do nostrud cupidatat occaecat dolor labore do anim. Laborum
      quis veniam ipsum ullamco voluptate sit ea qui adipisicing aliqua sunt dolor nulla. Nulla consequat sunt qui amet.
      Pariatur esse pariatur veniam non fugiat laboris eu nulla incididunt.
    </p>
    <p>
      Laboris duis do consectetur aliquip non aliquip ad ad quis minim. Aute magna tempor occaecat magna fugiat culpa.
      Commodo id eiusmod ea pariatur consequat fugiat minim est anim. Ipsum amet ipsum eu nisi. Exercitation minim amet
      incididunt tempor do ut id in officia eu sit est. Dolor qui laboris laboris tempor sunt velit eiusmod non ipsum
      exercitation ut sint ipsum officia.
    </p>
  </ScrollToBottom>
);

We use glamor for component styles. It is not required, but we don't support style props for performance reason.

Props

NameTypeDefaultDescription
checkIntervalnumber150Recurring interval of stickiness check, in milliseconds (minimum is 17 ms)
classNamestringSet the class name for the root element
debouncenumber17Set the debounce for tracking the onScroll event
followButtonClassNamestringSet the class name for the follow button
modestring"bottom"Set it to "bottom" for scroll-to-bottom, "top" for scroll-to-top
noncestringSet the nonce for Content Security Policy
scrollViewClassNamestringSet the class name for the container element that house all props.children

Hooks

You can use React Hooks to perform various operations and signal state changes. The component which use the hook must stay under <ScrollToBottom> or <Composer>.

CategoryNameTypeDescription
FunctionuseScrollTo() => (scrollTop: number | '100%') => voidScroll panel to specified position
FunctionuseScrollToBottom() => () => voidScroll panel to bottom
FunctionuseScrollToEnd() => () => voidScroll panel to end (depends on mode)
FunctionuseScrollToStart() => () => voidScroll panel to start (depends on mode)
FunctionuseScrollToTop() => () => voidScroll panel to top
ObserveruseObserveScrollPosition(observer: (({ scrollTop: number }) => void) | false) => voidObserve scroll position change by passing a callback function
StateuseAnimating() => [boolean]true if the panel is animating scroll effect
StateuseAnimatingToEndbooleantrue if the panel is animating scroll effect and towards the end (depends on mode)
StateuseAtBottom() => [boolean]true if the panel is currently near bottom
StateuseAtEnd() => [boolean]true if the panel is currently near the end (depends on mode)
StateuseAtStart() => [boolean]true if the panel is currently near the start (depends on mode)
StateuseAtTop() => [boolean]true if the panel is currently near top
StateuseMode() => [string]"bottom" for scroll-to-bottom, "top" for scroll-to-top
StateuseSticky() => [boolean]true if the panel is sticking to the end

Callback function passed to useObserveScrollPosition will be called rapidly during scrolling. To unsubscribe, pass a falsy value.

Sample code

The following sample code will put a button inside the content view only if the view is not at the bottom. When the button is clicked, it will scroll the view to the bottom.

Note: useScrollToBottom can only be called inside components hosted under <ScrollToBottom>.

import ScrollToBottom, { useScrollToBottom, useSticky } from 'react-scroll-to-bottom';

const Content = () => {
  const scrollToBottom = useScrollToBottom();
  const [sticky] = useSticky();

  return (
    <React.Fragment>
      <p>
        Labore commodo consectetur commodo et Lorem mollit voluptate velit adipisicing proident sit. Dolor consequat
        nostrud aliquip ea anim enim. Culpa quis tempor et quis esse proident cupidatat reprehenderit laborum ullamco.
      </p>
      <p>
        Incididunt labore nulla cupidatat occaecat elit esse occaecat culpa irure et nisi excepteur. Duis Lorem labore
        consectetur nostrud et voluptate culpa consequat enim reprehenderit. Id voluptate occaecat anim consequat id ea
        eiusmod laborum proident irure veniam esse. Aliquip nostrud culpa nostrud laborum cillum adipisicing dolore. Est
        tempor labore Lorem ad cupidatat reprehenderit exercitation pariatur officia ex adipisicing cupidatat
        exercitation.
      </p>
      <p>
        Est labore cupidatat exercitation est laboris et tempor Lorem irure velit ea commodo sint officia. Ullamco
        exercitation cillum est fugiat do. Enim qui eu veniam nostrud tempor elit. Duis elit mollit ut reprehenderit sit
        adipisicing proident culpa veniam sint veniam consectetur fugiat Lorem. Sint dolor proident commodo proident non
        cupidatat labore.
      </p>
      {!sticky && <button onClick={scrollToBottom}>Click me to scroll to bottom</button>}
    </React.Fragment>
  );
};

export default () => (
  <ScrollToBottom>
    <Content />
  </ScrollToBottom>
);

Context

Starting with React Hooks, we are deprecating the React Context. New functions may not be added to context.

We use 2 different contexts with different performance characteristics to provide better overall performance. Function context contains immutable functions. State context may change when the user scroll the panel.

Function context

This context contains functions used to manipulate the container. And will not update throughout the lifetime of the composer.

NameTypeDescription
scrollTo(scrollTop: number | '100%') => voidScroll panel to specified position
scrollToBottom() => voidScroll panel to bottom
scrollToEnd() => voidScroll panel to end (depends on mode)
scrollToStart() => voidScroll panel to start (depends on mode)
scrollToTop() => voidScroll panel to top

State context

This context contains state of the container.

NameTypeDescription
animatingbooleantrue if the panel is animating scroll effect
animatingToEndbooleantrue if the panel is animating scroll effect and towards the end (depends on mode)
atBottombooleantrue if the panel is currently near bottom
atEndbooleantrue if the panel is currently near the end (depends on mode)
atStartbooleantrue if the panel is currently near the start (depends on mode)
atTopbooleantrue if the panel is currently near top
modestring"bottom" for scroll-to-bottom, "top" for scroll-to-top
stickybooleantrue if the panel is sticking to the end

atEnd and sticky are slightly different. During scroll animation, the panel is not at the end yet, but it is still sticky.

Sample code

The following sample code will put a button inside the content view only if the view is not at the bottom. When the button is clicked, it will scroll the view to the bottom.

import ScrollToBottom from 'react-scroll-to-bottom';

const Content = ({ scrollToBottom, sticky }) => {
  return (
    <React.Fragment>
      <p>
        Labore commodo consectetur commodo et Lorem mollit voluptate velit adipisicing proident sit. Dolor consequat
        nostrud aliquip ea anim enim. Culpa quis tempor et quis esse proident cupidatat reprehenderit laborum ullamco.
      </p>
      <p>
        Incididunt labore nulla cupidatat occaecat elit esse occaecat culpa irure et nisi excepteur. Duis Lorem labore
        consectetur nostrud et voluptate culpa consequat enim reprehenderit. Id voluptate occaecat anim consequat id ea
        eiusmod laborum proident irure veniam esse. Aliquip nostrud culpa nostrud laborum cillum adipisicing dolore. Est
        tempor labore Lorem ad cupidatat reprehenderit exercitation pariatur officia ex adipisicing cupidatat
        exercitation.
      </p>
      <p>
        Est labore cupidatat exercitation est laboris et tempor Lorem irure velit ea commodo sint officia. Ullamco
        exercitation cillum est fugiat do. Enim qui eu veniam nostrud tempor elit. Duis elit mollit ut reprehenderit sit
        adipisicing proident culpa veniam sint veniam consectetur fugiat Lorem. Sint dolor proident commodo proident non
        cupidatat labore.
      </p>
      {!sticky && <button onClick={scrollToBottom}>Click me to scroll to bottom</button>}
    </React.Fragment>
  );
};

export default () => (
  <ScrollToBottom>
    <FunctionContext.Consumer>
      {({ scrollToBottom }) => (
        <StateContext.Consumer>
          {({ sticky }) => <Content scrollToBottom={scrollToBottom} sticky={sticky} />}
        </StateContext.Consumer>
      )}
    </FunctionContext.Consumer>
  </ScrollToBottom>
);

Security

We support nonce-based Content Security Policy. To enable, the following directive is required:

Road map

  • Easier customization for "scroll to bottom" button
  • Debounce on showing "scroll to bottom" button
  • Investigate using scroll for scrolling and polyfill IE11

Contributions

Like us? Star us.

Want to make it better? File us an issue.

Don't like something you see? Submit a pull request.

Keywords

react

FAQs

Package last updated on 01 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