An open API service indexing awesome lists of open source software.

https://github.com/dzucconi/use-keyboard-list-navigation

A React hook to navigate through lists with your keyboard
https://github.com/dzucconi/use-keyboard-list-navigation

keyboard react react-hook react-hooks typescript

Last synced: 6 months ago
JSON representation

A React hook to navigate through lists with your keyboard

Awesome Lists containing this project

README

          

# use-keyboard-list-navigation

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![npm](https://img.shields.io/npm/v/use-keyboard-list-navigation)](https://www.npmjs.com/package/use-keyboard-list-navigation) [![Build Status](https://travis-ci.com/dzucconi/use-keyboard-list-navigation.svg?branch=master)](https://app.travis-ci.com/github/dzucconi/use-keyboard-list-navigation)

## What is this?

A React hook to navigate through lists with your keyboard.

## Installation

```bash
yarn add use-keyboard-list-navigation
```

## Usage

```javascript
import React from "react";
import { useKeyboardListNavigation } from "use-keyboard-list-navigation";

const App: React.FC = () => {
const list = ["one", "two", "three"];

const { index, cursor, interactive, selected } = useKeyboardListNavigation({
list,
onEnter: console.log.bind(console),
});

return (


{JSON.stringify({ index, cursor, interactive, selected })}

);
};
```

## Interface

```typescript
type UseKeyboardListNavigationProps = {
ref?: React.MutableRefObject | undefined;
list: T[];
waitForInteractive?: boolean | undefined;
defaultValue?: T | undefined;
bindAxis?: "vertical" | "horizontal" | "both" | undefined;
onEnter({
event,
element,
state,
index,
}: {
event: KeyboardEvent;
element: T;
state: UseKeyboardListNavigationState;
index: number;
}): void;
extractValue?(item: T): string;
};

const useKeyboardListNavigation: ({
ref,
list,
waitForInteractive,
defaultValue,
onEnter,
extractValue,
}: UseKeyboardListNavigationProps) => {
index: number;
selected: T;
cursor: number;
length: number;
interactive: boolean;
};
```