0.1.6 ā€¢ Published 1 year ago

react-use-navigate-list v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

react-use-navigate-list hook

Demo: Sandbox

šŸŽ‰ react-use-navigate-list hook makes the list interactive using arrow buttons. It is optimized to reduce unnecessary re-renders

Installation

$ npm install react-use-navigate-list 
$ yarn add react-use-navigate-list 

Features

  • Typescript support
  • Navigate through the list using ArrowUp and ArrowDown buttons
  • Select item by pressing Enter
  • Supports vertical and horizontal navigation

Props

NameTypeDefaultDescription
listArrayrequired. Array of items
onSelectFunctionrequired. Callback function called on item click or on Enter press.
indexPathString"id"optional. Custom path to the item index
verticalBooleantrueoptional. Switches between horizontal and vertical navigation

Example

import React from "react";
import useNavigateList from "react-use-navigate-list";

const itemList = [
  { id: 1, name: "Banana" },
  { id: 2, name: "Pineapple" },
  { id: 3, name: "Blueberry" },
];

function App() {
  const { activeIndex, itemProps } = useNavigateList({
    list: itemList,
    onSelect: (item) => {
      console.log(item);
    },
  });

  return itemList.map((item, index) => (
    <div
      {...itemProps(item)}
      key={item.id}
      className={activeIndex === index ? "active-className" : ""}
    >
      {item.name}
    </div>
  ));
}

License

Licensed under MIT

0.1.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago