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

https://github.com/brnrdog/listboxkit

Light and flexible React hooks written in ReScript for building accessible listbox composed components, like menus, dropdown select and others.
https://github.com/brnrdog/listboxkit

bucklescript listbox react reason-react rescript

Last synced: 9 months ago
JSON representation

Light and flexible React hooks written in ReScript for building accessible listbox composed components, like menus, dropdown select and others.

Awesome Lists containing this project

README

          

# listboxkit

![GitHub Workflow Status](https://img.shields.io/github/workflow/status/brnrdog/listbox/Release?style=flat-square)
[![npm](https://img.shields.io/npm/v/listboxkit?style=flat-square)](https://www.npmjs.com/package/listboxkit)
[![npm bundle size](https://img.shields.io/bundlephobia/min/listboxkit?style=flat-square)](https://bundlephobia.com/result?p=listboxkit)
![Codecov](https://img.shields.io/codecov/c/github/brnrdog/listboxkit?style=flat-square)

Light and flexible React hooks written in ReScript for building accessible listbox composed components, like menus, dropdown select and others.

## Installation

Install it using the package manager of your preference:

```bash
npm install --save listboxkit
```

Or if your project uses yarn:

```bash
yarn add listboxkit
```

For **ReScript** projects, add `listboxkit` as a dependency in your `bsconfig.json` file:

```json
{
"bs-dependencies": ["listboxkit"]
}
```

## Usage

The main React hook for building listbox components is the `useListbox`. Given a list of options, this hook will provide the state and the necessary event handlers for building your listbox.

### Using in JavaScript/TypeScript projects:

```js
const options = ["Red", "Green", "Blue"];

function ColorSelect() {
const {
highlightedIndex,
getOptionProps,
getContainerProps,
selectedIndexes
} = useListbox(options);

const selectedColors = selectedIndexes.map(i => options[i]).join(",")

return (


Selected color:{" "}
{selectedColors.length === 0 ? "no selected color" : selectedColors}.

    {options.map((option, index) => {
    const highlighted = highlightedIndex === index;

    return (


  • {highlighted ? `> ${option}` : option}

  • );
    })}


);
```

### Using in ReScript projects:

```rescript
module ColorSelect {
let options = ["Red", "Green", "Blue"]

@react.component
let make = () => {
let {
highlightedIndex,
getOptionProps,
getContainerProps,
selectedIndexes
}: Listbox.listbox = Listbox.useListbox(options)

let { role, tabIndex, onKeyDown, onFocus, onBlur } = getContainerProps()

let selectedOption = selectedIndexes
-> Belt.Array.map(i => options -> Belt.Array.get(i))
-> Belt.Array.get(0)
-> Belt.getWithDefault("no selected color.")

let renderColorOption = (index, option) => {
let {
ariaSelected,
onClick,
role,
}: Listbox.optionProps = getOptionProps(index)
let highlighted = highlightedIndex == index


  • {(highlighted ? `> ${option}` : option) |> React.string}

  • }


    {React.string("Selected color :" ++ selectedOption)}

      {options
      -> Belt.Array.mapWithIndex(renderOption)
      -> React.array}


    }
    }
    ```