Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-scroll-to-bottom

compulim247kMIT4.2.0TypeScript support: definitely-typed

React container that will auto scroll to bottom

react, follow, jump, jump to bottom, jump to top, scroll, scroll to bottom, scroll to top, sticky, stick to bottom, stick to top, tail

readme

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

Name Type Default Description
checkInterval number 150 Recurring interval of stickiness check, in milliseconds (minimum is 17 ms)
className string | Set the class name for the root element
debounce number 17 Set the debounce for tracking the onScroll event
debug bool NODE_ENV === 'development' Show debug information in console
followButtonClassName string | Set the class name for the follow button
initialScrollBehavior string smooth Set the initial scroll behavior, either "auto" (discrete scrolling) or "smooth"
mode string "bottom" Set it to "bottom" for scroll-to-bottom, "top" for scroll-to-top
nonce string | Set the nonce for Content Security Policy
scroller function () => Infinity A function to determine how far should scroll when scroll is needed
scrollViewClassName string | Set the class name for the container element that house all props.children
styleOptions.stylesRoot Node undefined Set the container node for component styles to be placed into. When set to undefined, will use document.head

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>.

Category Name Type Description
Function useScrollTo () => (scrollTop: number | '100%') => void Scroll panel to specified position
Function useScrollToBottom () => () => void Scroll panel to bottom
Function useScrollToEnd () => () => void Scroll panel to end (depends on mode)
Function useScrollToStart () => () => void Scroll panel to start (depends on mode)
Function useScrollToTop () => () => void Scroll panel to top
Observer useObserveScrollPosition (observer: (({ scrollTop: number }) => void) | false) => void Observe scroll position change by passing a callback function
State useAnimating () => [boolean] true if the panel is animating scroll effect
State useAnimatingToEnd boolean true if the panel is animating scroll effect and towards the end (depends on mode)
State useAtBottom () => [boolean] true if the panel is currently near bottom
State useAtEnd () => [boolean] true if the panel is currently near the end (depends on mode)
State useAtStart () => [boolean] true if the panel is currently near the start (depends on mode)
State useAtTop () => [boolean] true if the panel is currently near top
State useMode () => [string] "bottom" for scroll-to-bottom, "top" for scroll-to-top
State useSticky () => [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.

Name Type Description
scrollTo (scrollTop: number | '100%') => void Scroll panel to specified position
scrollToBottom () => void Scroll panel to bottom
scrollToEnd () => void Scroll panel to end (depends on mode)
scrollToStart () => void Scroll panel to start (depends on mode)
scrollToTop () => void Scroll panel to top

State context

This context contains state of the container.

Name Type Description
animating boolean true if the panel is animating scroll effect
animatingToEnd boolean true if the panel is animating scroll effect and towards the end (depends on mode)
atBottom boolean true if the panel is currently near bottom
atEnd boolean true if the panel is currently near the end (depends on mode)
atStart boolean true if the panel is currently near the start (depends on mode)
atTop boolean true if the panel is currently near top
mode string "bottom" for scroll-to-bottom, "top" for scroll-to-top
sticky boolean true 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>
);

Observing scroll position

You can use useObserveScrollPosition to listen to scroll change.

// This is the content rendered inside the scrollable container
const ScrollContent = () => {
  const observer = useCallback(({ scrollTop }) => {
    console.log(scrollTop);
  }, []);

  useObserveScrollPosition(observer);

  return <div>Hello, World!</div>;
};

If you want to turn off the hook, in the render call, pass a falsy value, e.g. useObserveScrollPosition(false).

Please note that the observer will called very frequently, it is recommended:

  • Only observe the scroll position when needed
  • Don't put too much logic inside the callback function
  • If logic is needed, consider deferring handling using setTimeout or similar functions
  • Make sure the callback function passed on each render call is memoized appropriately, e.g. useCallback

For best practices on handling scroll event, please read this article.

Programmatically pausing scroll

This only works when mode prop is set to bottom (default).

You can pass a function to the scroller prop to customize how far the scrollable should animate/scroll (in pixel) when its content changed. The signature of the scroller function is:

scroller({ maxValue, minValue, offsetHeight, scrollHeight, scrollTop }) => number;
Argument Type Description
maxValue number Maximum distance (in pixel) to scroll
minValue number Minimum distance (in pixel) to scroll, see notes below
offsetHeight number View height of the scrollable container
scrollHeight number Total height of the content in the container, must be equal or greater than offsetHeight
scrollTop number Current scroll position (in pixel)

Note: the scroller function will get called when the scrollable is sticky and the content size change. If the scrollable is not sticky, the function will not be called as animation is not needed.

When the scrollable is animating, if there are new contents added to the scrollable, the scroller function will get called again with minValue set to the current position. The minValue means how far the animation has already scrolled.

By default, the scroller function will returns Infinity. When new content is added, it will scroll all the way to the bottom.

You can return a different value (in number) to indicates how far you want to scroll when the content has changed. If you return 0, the scrollable will stop scrolling for any new content. Returning any values less than maxValue will make the scrollable to lose its stickiness after animation. After the scrollable lose its stickiness, the scroller function will not be called again for any future content change, until the scrollable regains its stickiness.

Security

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

Road map

  • <input checked="" disabled="" type="checkbox"> Easier customization for "scroll to bottom" button
  • <input disabled="" type="checkbox"> Debounce on showing "scroll to bottom" button
  • <input disabled="" type="checkbox"> 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.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[Unreleased]

Changed

  • Added styleOptions.stylesRoot property which allows to specify a container node component styles will be placed into, by @OEvgeny in PR #137

[4.2.0] - 2021-10-14

Changed

[4.1.2] - 2021-05-17

Changed

Fixed

  • Fixed #90. Added @babel/runtime-corejs3 and core-js@3 for transforming for Internet Explorer 11, by @compulim, in PR #93.

[4.1.1] - 2021-05-13

Added

  • Added a test harness, in PR #85

Fixed

  • Fixed #75. If debug is set, it will show debug in console log. If not specified, it will fallback to NODE_ENV === 'production', in PR #77.
  • Fixed #84. Fixed a race condition: while under heavy load, sticky, and at the end, calling useScrollTo() to any positions, the scroll view may scroll back to the bottom immediately, in PR #85

[4.1.0] - 2021-01-03

Added

  • Added scroller prop for limiting scroll distance when mode is set to bottom, in PR #73
  • Added initialScrollBehavior prop for first scroll behavior. When set to "auto" (discrete scrolling), it will jump to end on initialization. in PR #73
  • Added debug prop for dumping debug log to console, in PR #73
  • Improved performance by separating StateContext into 2 tiers, in PR #73

Fixed

  • Emptying container should regain stickiness, in PR #73

[4.0.0] - 2020-09-01

Added

Changed

[3.0.0] - 2020-06-21

Breaking changes

  • 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

Changed

Added

  • Added version number to <meta name="react-scroll-to-bottom:version"> for diagnostic purpose, in PR #51
  • Added useAnimatingToEnd getter to indicate if it is animating towards to the end, in PR #49
    • The existing useAnimating getter only indicate if it is animating to any scroll positions
  • Added scrollTo function to scroll to a specific scrollTop value, this is similar to DOMElement.scrollIntoView(), in PR #49
    • The signature is scrollTo(scrollTop: number, options: { behavior: 'auto' | 'smooth' })
    • Pass { behavior: 'smooth' } for synthetic smooth scrolling
  • Added useObserveScrollTop hook to observe scroll event, in PR #49
    • This effect function will be called rapidly on scroll, please avoid expensive code such as calling setter of useState and any code that would cause re-render

Fixed

  • Cancel scroll animation on mouse wheel or touch gesture, in PR #49
  • Calling scrollTo should cancel any existing scroll animation, in PR #49

[2.0.0] - 2020-05-07

Breaking changes

  • We moved to React Hooks and it requires React 16.8.6 or up
    • Hooks will allow us to write simpler and more maintainable code
    • Developers can use our React Hooks to perform various operations

Changed

Added

Fixed

  • Fix atStart was not reporting correctly, in PR #31
  • Chrome: Fix scroll to bottom button should hide when using <kbd>TAB</kbd> to scroll the bottommost button into view, in PR #46

[1.3.2] - 2019-06-20

Changed

Fixed

  • Composer: fix #22, synthetic scroll events crafted by Chrome should not cause stickiness to lose, in PR #23

[1.3.1] - 2019-02-13

Changed

  • Composer: fix #13, user scrolling in Firefox may have the scroll position locked occasionally, in PR #12
  • SpineTo: fix #10, set stopping threshold from 0.5 to 1.5, in PR #14
  • Composer: fix #15, set near-end threshold from 0 to (less than) 1, in PR #16

[1.3.0] - 2019-01-21

Changed

  • Playground: bumped to `react@16.6.0,react-dom@16.6.0, andreact-scripts@2.1.6`
  • *: Update algorithm, instead of using componentDidUpdate, we now use setInterval to check if the panel is sticky or not, this help to track content update that happen outside of React lifecycle, for example, HTMLImageElement.onload event
  • Composer: scrollTo() now accepts "100%" instead of "bottom"

Removed

  • Removed threshold props because the algorithm is now more robust

[1.2.0] - 2018-10-28

Added

  • AutoHideFollowButton: will now hide when it start animating scroll position
  • BasicScrollToBottom: will now pass debounce and threshold to Composer, fix #2
  • Composer: debounce prop to control debouncing on onScroll event, default to 17
  • FunctionContext: scrollTo now support "bottom", in addition to a number, fix #1
    • This will help when animating scroll position while new content was added to the panel
  • FunctionContext: scrollToStart function to scroll to the start, depends on mode
  • StateContext: animating returns true if the scroll position is being animated
  • StateContext: atStart indicates if the scroll position is at the start or not, depend on mode

Changed

  • Performance improvements
    • Context is now separated into FunctionContext, InternalContext and StateContext for better performance and reduce exposure
      • FunctionContext is static and only hold functions for manipulating the panel
      • InternalContext is static and for internal use (to overcome shortcomings of React.createRef)
      • StateContext is dynamic and change when scroll position change
    • StateContext will now only update if there are any meaningful changes
  • Added displayName to context
  • lerna bumped from 2.11.0 to 3.4.3

[1.1.0] - 2018-06-22

Added

  • Container class name

[1.0.0] - 2018-06-17

Added

  • Initial release