All Projects → stevensacks → react-smart-scroll

stevensacks / react-smart-scroll

Licence: other
Efficient rendering of long lists in React

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to react-smart-scroll

vanillajs-scrollspy
ScrollSpy in pure JavaScript
Stars: ✭ 47 (+74.07%)
Mutual labels:  scroll, scrollable
Scroll Snap
↯ Snap page when user stops scrolling, with a customizable configuration and a consistent cross browser behaviour
Stars: ✭ 187 (+592.59%)
Mutual labels:  scrolling, scroll
Horizontal Scroll
Horizontal scroll with inertia
Stars: ✭ 175 (+548.15%)
Mutual labels:  scrolling, scroll
Ngx Ui Scroll
Infinite/virtual scroll for Angular
Stars: ✭ 145 (+437.04%)
Mutual labels:  scrolling, scroll
Moveto
A lightweight scroll animation javascript library without any dependency
Stars: ✭ 2,746 (+10070.37%)
Mutual labels:  scrolling, scroll
NoobScroll
A lightweight jQuery Plugin that add some cool function to make scrolling more fun [Archive]
Stars: ✭ 43 (+59.26%)
Mutual labels:  scrolling, scroll
Infinitescroll
Infinite Scroll (Endless Scrolling) for RecyclerView in Android
Stars: ✭ 183 (+577.78%)
Mutual labels:  scrolling, scroll
Pd Select
vue components ,like ios 3D picker style,vue 3d 选择器组件,3D滚轮
Stars: ✭ 101 (+274.07%)
Mutual labels:  scrolling, scroll
V Bar
The virtual responsive crossbrowser scrollbar component for VueJS 2x
Stars: ✭ 216 (+700%)
Mutual labels:  scrolling, scroll
Stickyfill
Polyfill for CSS `position: sticky`
Stars: ✭ 2,252 (+8240.74%)
Mutual labels:  scrolling, scroll
React Virtual
⚛️ Hooks for virtualizing scrollable elements in React
Stars: ✭ 2,369 (+8674.07%)
Mutual labels:  scrolling, virtualized
scroll-sync-react
A scroll syncing library for react that is up to date
Stars: ✭ 49 (+81.48%)
Mutual labels:  scrolling, scroll
Jquery Scrolllock
Locks mouse wheel scroll inside container, preventing it from propagating to parent element
Stars: ✭ 109 (+303.7%)
Mutual labels:  scrolling, scroll
react-scroll-trigger
📜 React component that monitors scroll events to trigger callbacks when it enters, exits and progresses through the viewport. All callback include the progress and velocity of the scrolling, in the event you want to manipulate stuff based on those values.
Stars: ✭ 126 (+366.67%)
Mutual labels:  scrolling, scroll
Prognroll
A tiny, lightweight jQuery plugin that creates scroll progress bar on the page.
Stars: ✭ 108 (+300%)
Mutual labels:  scrolling, scroll
Scroll Js
Light cross-browser scroller that uses native javascript
Stars: ✭ 179 (+562.96%)
Mutual labels:  scrolling, scroll
Android scroll endless
Scroll endless for Android recyclerview
Stars: ✭ 12 (-55.56%)
Mutual labels:  scrolling, scroll
Dragscroll
micro library for drag-n-drop scrolling style
Stars: ✭ 989 (+3562.96%)
Mutual labels:  scrolling, scroll
React Indiana Drag Scroll
React component which implements scrolling via holding the mouse button or touch
Stars: ✭ 190 (+603.7%)
Mutual labels:  scrolling, scroll
react-is-scrolling
Simply detect if users are scrolling in your components in a declarative API
Stars: ✭ 17 (-37.04%)
Mutual labels:  scrolling, scroll

react-smart-scroll

npm version

ReactSmartScroll is a lightweight, high-performance, easy-to-use way to render long lists efficiently in React. It only renders the visible rows, with a few buffer rows above and below.

ReactSmartScroll is (mostly) un-opinionated, with minimum configuration. It has automatic support for variable height rows, even if they change height at runtime due to resizing, expanding, etc.

Installation

// npm
npm i -S react-smart-scroll

// yarn
yarn add react-smart-scroll

Super Simple Sample:

import ReactSmartScroll from 'react-smart-scroll';

const data = [
    {id: 1, text: 'Hello'},
    {id: 2, text: 'World'},
];

const TestRow = ({data, rowRef}) => (
    <div ref={rowRef}>
        {data.text}
    </div>
);

export default () => (
    <ReactSmartScroll data={data} row={TestRow} />
);

TypeScript

Typing for your row components is included as a named export.

import {ReactSmartScrollRow} from 'react-smart-scroll';

const TypedTestRow: ReactSmartScrollRow = ({data, rowRef}) => (
    <div ref={rowRef}>
        {data.text}
    </div>
);

Working Demo with variable height rows:

This component uses React Hooks, so it requires React 16.8.x or above.

How To Use

ReactSmartScroll is designed to be simple to use. You can check the demo folder for an example, as well.

At its most basic, you pass an array of data and a row component to render the items in the array.

Props

  • className - A css className that will be applied to the component.
  • data - An array of items to be passed to your row component.
  • overflow - You can set this to auto or scroll ("auto" is the default).
  • row - Your row component.
  • rowHeight - Starting row height (100 by default) - it starts with this as an estimate for all rows, and then measures and caches the actual height of each row is as you scroll.
  • startAt - You can pass an index to start the list at (0 by default).
  • style - A style object applied to the component if you prefer using inline styling instead of css.
  • ...rowProps - Any additional props you pass will be applied to your row components, for example onClick.

All of the props are optional. If you don't provide any data or a row component, it won't render anything, but it won't cause any errors, either.

You need to apply height, either in the css class or the style object. You can use a specific height, or calc().

Due to a CSS limitation with how overflow works with padding, height: 100% does not work.

The overflow default value of auto is applied via style. If you want to use a css class to control it, you need to pass null, undefined, or an empty string to overflow. If you use a style object, you can set it there, since the default auto is applied before your style object and thus can be overwritten that way.

import ReactSmartScroll, {ReactSmartScrollRow} from 'react-smart-scroll';

<ReactSmartScroll 
    className="demo-smart-scroll" 
    data={data} 
    overflow="scroll"
    row={TestRow}
    startAt={5}
    onClick={() => console.log('Hello!')} // passed to row components
    label="My text is: " // passed to row components
/>

const TestRow: ReactSmartScrollRow = ({data, height, label, onClick, rowIndex, rowRef}) => (
    <div ref={rowRef} className="test-row" onClick={() => onClick(rowIndex)}>
        <strong>[{data.id}]</strong>: {height}px<br/>
        <label>{label}</label>:<p>{data.text}</p>
    </div>
);

The data array can be made up of objects or strings. ReactSmartScroll passes the items in the array directly to your row component so you have complete freedom in how the rows render. If you pass an object with an id property, it will use the id as the key for each row, which can be more efficient than using the row index.

Your row component receives the following props:

  • data - The item in the data array that corresponds to this row.
  • height - The current measured height of the row. It's provided for debugging or if you have a use for it.
  • rowIndex - The index of the item in the data array. Also provided for debugging, use if you like.
  • rowRef - required You need to include ref={rowRef} prop on your row component's container element.
  • ...rowProps - Any additional props you passed to the ReactSmartScroll component will be available on every row.

How It Works

ReactSmartScroll renders enough rows to fill the visible space of the scroll area, plus an extra one above and below. It measures the height of each of the visible row and caches that height as you scroll (or resize) so that the next render of the same row is more efficient. It simulates the total height of all the items by adjusting the padding top and bottom of the div that contains the rows as you scroll.

Unlike other virtual scrolling components/libraries, there are no unnecessary wrapper divs for your rows, it doesn't use absolute positioning, and doesn't require any polyfills.

Firefox Note

The latest version of Firefox doesn't seem to keep up with React's rendering speed with useLayoutEffect() or useEffect(), so if you grab the scrollbar and scroll up and down really fast, you'll see blank space momentarily. Other browsers don't appear to have this problem. Using the mouse wheel to scroll looks good in all browsers.

rowRef Note

Why does React Smart Scroll pass rowRef instead of just passing ref?

This removes the burden of having to wrap functional row components with React.forwardRef().

IMO, it's easier and more flexible for you to use rowRef.

Closing Note

I hope you enjoy using this component as much as I enjoyed making it!

Follow me:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].