2.4.2 • Published 2 years ago

use-keyboard-list-navigation v2.4.2

Weekly downloads
132
License
MIT
Repository
github
Last release
2 years ago

use-keyboard-list-navigation

semantic-release npm Build Status

What is this?

A React hook to navigate through lists with your keyboard.

Installation

yarn add use-keyboard-list-navigation

Usage

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 (
    <pre>
      <code>{JSON.stringify({ index, cursor, interactive, selected })}</code>
    </pre>
  );
};

Interface

type UseKeyboardListNavigationProps<T> = {
  ref?: React.MutableRefObject<any> | 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: <T>({
  ref,
  list,
  waitForInteractive,
  defaultValue,
  onEnter,
  extractValue,
}: UseKeyboardListNavigationProps<T>) => {
  index: number;
  selected: T;
  cursor: number;
  length: number;
  interactive: boolean;
};
2.4.2

2 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago