7.0.0 • Published 23 days ago

react-horizontal-scrolling-menu v7.0.0

Weekly downloads
16,855
License
MIT
Repository
github
Last release
23 days ago

For hire

React horizontal scrolling menu

example

npm Tests Codacy Badge Codacy Badge Commitizen friendly npm bundle size (minified + gzip) Donate Bitcoin

Stand With Ukraine

Poll what you like/dislike/need from this library

Proud corner

performance-dashboard-on-aws | React status code

Storybook (Faster and more convinient, new examples will be here)

Codesandbox Examples (Deprecated)

Basic example

Hidden scrollbar and arrows on bottom

Select item

Drag by mouse

Click and select multiple items

Scroll by 1 item

Center items

Dynamically add items when last is visible

apiRef - controling component outside

Add item and scroll to it

RTL

Loop scroll

One centered item

Custom transition/animation

Swipe on mobile devices(need to run locally, codesandbox has issues)

Previous version V1

This is a highly customizable horizontal scrolling menu component for React. Can also use it for Amazon like items block or a Gallery. Menu component is responsive, just set width for parent container. Items width will be determined from CSS styles.

For navigation, you can use scrollbar, native touch scroll, mouse wheel or drag by mouse.

Component provide context with visible items and helpers.

Possible set default position on initialization.

Check out examples on Storybook or codesandbox.

:star: if you like the project :)

NextJS issues

Cannot use import statement outside a module

Quick start

npm install --save react-horizontal-scrolling-menu

In project:

import React from 'react';
import { ScrollMenu, VisibilityContext } from 'react-horizontal-scrolling-menu';
import 'react-horizontal-scrolling-menu/dist/styles.css';

const getItems = () =>
  Array(20)
    .fill(0)
    .map((_, ind) => ({ id: `element-${ind}` }));

function App() {
  const [items, setItems] = React.useState(getItems);
  const [selected, setSelected] = React.useState([]);

  const isItemSelected = (id) => !!selected.find((el) => el === id);

  const handleClick =
    (id) =>
    ({ getItemById, scrollToItem }) => {
      const itemSelected = isItemSelected(id);

      setSelected((currentSelected) =>
        itemSelected
          ? currentSelected.filter((el) => el !== id)
          : currentSelected.concat(id),
      );
    };

  return (
    <ScrollMenu LeftArrow={LeftArrow} RightArrow={RightArrow}>
      {items.map(({ id }) => (
        <Card
          itemId={id} // NOTE: itemId is required for track items
          title={id}
          key={id}
          onClick={handleClick(id)}
          selected={isItemSelected(id)}
        />
      ))}
    </ScrollMenu>
  );
}

const LeftArrow = () => {
  const visibility = React.useContext < publicApiType > VisibilityContext;
  const isFirstItemVisible = visibility.useIsVisible('first', true);
  return (
    <Arrow
      disabled={isFirstItemVisible}
      onClick={visibility.scrollPrev}
      className="left"
    >
      Left
    </Arrow>
  );
};

const RightArrow = () => {
  const visibility = React.useContext < publicApiType > VisibilityContext;
  const isLastItemVisible = visibility.useIsVisible('last', false);
  return (
    <Arrow
      disabled={isLastItemVisible}
      onClick={visibility.scrollNext}
      className="right"
    >
      Right
    </Arrow>
  );
};

function Card({ onClick, selected, title, itemId }) {
  const visibility = React.useContext < publicApiType > VisibilityContext;
  const visible = visibility.useIsVisible(itemId, true);

  return (
    <div
      onClick={() => onClick(visibility)}
      style={{
        width: '160px',
      }}
      tabIndex={0}
    >
      <div className="card">
        <div>{title}</div>
        <div>visible: {JSON.stringify(visible)}</div>
        <div>selected: {JSON.stringify(!!selected)}</div>
      </div>
      <div
        style={{
          height: '200px',
        }}
      />
    </div>
  );
}

export default App;

Check out Example in example-nextjs folder for info how to implement more features like mouse drag or disable body scroll.

Example

You can clone repository and run demo project.

git clone https://github.com/asmyshlyaev177/react-horizontal-scrolling-menu
npm run setup
npm run demo

Storybook

Can clone repo and run storybook

git clone https://github.com/asmyshlyaev177/react-horizontal-scrolling-menu
npm run setup
npm run storybook

Helpers and api

Children of main ScrollMenu component(arrows, fotter, items) can use VisibilityContext to access state and callbacks. Function callbacks also pass context, eg onWheel, onScroll etc.

Properties and callbacks

PropSignature
LeftArrowReact component for left arrow
RightArrowReact component for right arrow
HeaderReact component Header
FooterReact component Footer
onWheel(VisibilityContext, event) => void
onScroll(VisibilityContext, event) => void, will fire before scroll
onInit(VisibilityContext) => void
apiRefReact.RefObject | React.RefCallback
containerRefReact.RefObject | React.RefCallback
onUpdate(VisibilityContext) => void
onMouseDown(VisibilityContext) => (React.MouseEventHandler) => void
onMouseUp(VisibilityContext) => (React.MouseEventHandler) => void
onMouseMove(VisibilityContext) => (React.MouseEventHandler) => void
onTouchMove(VisibilityContext) => (React.TouchEventHandler) => void
onTouchStart(VisibilityContext) => (React.TouchEventHandler) => void
onTouchEnd(VisibilityContext) => (React.TouchEventHandler) => void
itemClassNameClassName of Item
scrollContainerClassNameClassName of scrollContainer
transitionDurationDuration of transitions in ms, default 500
transitionBehavior'smooth' |'auto' | customFunction
wrapperClassNameClassName of the outer-most div
RTLEnable Right to left direction
noPolyfillDon't use polyfill for scroll, no transitions

VisibilityContext

PropSignature
useIsVisible(itemId: string, defaultValue?: false) => boolean
getItemByIditemId => IOItem | undefined
getItemElementByIditemId => DOM Element | null
getItemByIndexindex => IOItem | undefined
getItemElementByIndexindex => DOM Element | null
getNextElement() => IOItem | undefined
getPrevElement() => IOItem | undefined
isFirstItemVisibleboolean
isItemVisibleitemId => boolean
isLastItemboolean
isLastItemVisibleboolean
scrollNext(behavior, inline, block, ScrollOptions) => void
scrollPrev(behavior, inline, block, ScrollOptions) => void
scrollToItem(item, behavior, inline, block, ScrollOptions) => void
itemsItemsMap class instance
scrollContainerRef

items class instance

ItemsMap class store info about all items and has methods to get currently visible items, prev/next item. Also, can subscribe to updates.

Prop/methodDescription
subscribesubscribe for events for itemId or first, last, onInit, onUpdate, eg. items.subscribe('item5', (item) => setVisible(item.visible))
unsubscribeuse in useEffect to cleanup, pass same cb instance
getVisiblereturn only visible items
toItemsreturn ids for all items
toArrreturn all items
firstreturn first item
lastreturn last item
prev(itemId | Item) => previous item | undefined
next(itemId | Item) => next item | undefined

Transition/Animation

NOTE: won't work with RTL prop

Can use transitionDuration, and transitionBehavior See example

ScrollOptions for scrollToItem, scrollPrev, scrollNext

Will override transition* options passed to ScrollMenu

{
  // target,
  behavior, // 'smooth', 'auto' or custom function
    // inline,
    // block,
    {
      duration: number, // number in milliseconds
    };
}

Other helpers

slidingWindow

Can get previous or next visible group of items with slidingWindow(allItems: string[], visibleItems: string[]) helper, e.g

slidingWindow(allItems, visibleItems)
.prev()
//.next()

getItemsPos

Can get first, center and last items, e.g.

const prevGroup = slidingWindow(allItems, visibleItems).prev()
const { first, center: centerItem, last } = getItemsPos(prevGroup)

// and scroll to center item of previous group of items
scrollToItem(getItemById(centerItem, 'smooth', 'center'))

Check out examples

apiRef

Can pass Ref object to Menu, current value will assigned as VisibilityContext. But some other values can be staled, so better use it only for firing functions like scrollToItem.

For scrolling use apiRef.scrollToItem(apiRef.getItemElementById) instead of apiRef.scrollToItem(apiRef.getItemById).

Can get item outside of context via apiRef.getItemElementById(id) or directly via document.querySelector(`[data-key='${itemId}']`). See apiRef example and Add item and scroll to it

Browser support

  • Browser must support IntersectionObserver API and requestAnimationFrame or use polyfills.
  • Only modern browsers, no IE or smart toasters

About

My first npm project. Sorry for my english.

Any contribution and correction appreciated. Just fork repo, commit and make PR, don't forget about tests.

Contributing

Changelog

7.0.0

23 days ago

6.1.0

26 days ago

6.0.2

2 months ago

6.0.1

2 months ago

6.0.0

2 months ago

5.0.2

2 months ago

5.0.1

2 months ago

5.0.0

2 months ago

5.0.0-beta.2

2 months ago

5.0.0-beta.3

2 months ago

5.0.0-beta.0

2 months ago

5.0.0-beta.1

2 months ago

4.1.4

3 months ago

4.1.2

3 months ago

4.1.1

7 months ago

4.1.0

10 months ago

3.2.5

1 year ago

3.2.4

1 year ago

4.0.4

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.0.3

1 year ago

4.0.2

1 year ago

3.2.4-0

1 year ago

4.0.4-beta.0

1 year ago

4.0.2-beta.0

1 year ago

4.0.4-beta.1

1 year ago

3.2.2

1 year ago

3.2.1

1 year ago

3.2.3

1 year ago

2.8.1

2 years ago

2.8.0

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

2.8.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.7.2

2 years ago

2.7.1

2 years ago

2.7.0

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.4.3

3 years ago

2.4.2

3 years ago

2.4.4

3 years ago

2.5.0

3 years ago

2.3.3

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.2.1

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.10

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.5

3 years ago

2.0.6

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-beta.8

3 years ago

2.0.0-beta.7

3 years ago

2.0.0-beta.6

3 years ago

2.0.0-beta.5

3 years ago

2.0.0-beta.4

3 years ago

2.0.0-beta.2

3 years ago

2.0.0-beta.1

3 years ago

2.0.0-beta.3

3 years ago

0.7.10

3 years ago

0.7.9

3 years ago

0.7.8

4 years ago

0.7.7

4 years ago

0.7.6

4 years ago

0.7.5

4 years ago

0.7.4

4 years ago

0.7.3

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.13

5 years ago

0.6.12

5 years ago

0.6.11

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.91

5 years ago

0.5.9

5 years ago

0.5.8

5 years ago

0.5.7

5 years ago

0.5.7-0

5 years ago

0.5.6

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago