# react-nested-dropdown

> Dropdown with submenu for React

Latest version **0.0.12** (published 2023-04-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-nested-dropdown
pnpm add react-nested-dropdown
yarn add react-nested-dropdown
bun add react-nested-dropdown
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2023-04-05 |
| First published | 2023-01-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 75.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | German Makarov |
| Maintainers | g-makarov |
| Keywords | react, dropdown, nested, menu, component, context, submenu, multi-level, ui |

## Links

- npm: https://www.npmjs.com/package/react-nested-dropdown
- Repository: https://github.com/g-makarov/react-nested-dropdown
- Issues: https://github.com/g-makarov/react-nested-dropdown/issues
- npm.io page: https://npm.io/package/react-nested-dropdown

## Dependencies (1)

- [clsx](https://npm.io/package/clsx.md) ^1.2.1

## Alternatives

- [@progress/kendo-ooxml](https://npm.io/package/@progress/kendo-ooxml.md) — 152.1K weekly downloads
- [@progress/kendo-react-ripple](https://npm.io/package/@progress/kendo-react-ripple.md) — 8.0K weekly downloads
- [@progress/kendo-react-orgchart](https://npm.io/package/@progress/kendo-react-orgchart.md) — 4.3K weekly downloads
- [@praxisui/dynamic-fields](https://npm.io/package/@praxisui/dynamic-fields.md) — 2.4K weekly downloads
- [@mesalvo/react-ui](https://npm.io/package/@mesalvo/react-ui.md) — 1.7K weekly downloads

## Recent versions

- 0.0.12 (latest) — 2023-04-05
- 0.0.11 — 2023-02-07
- 0.0.10 — 2023-01-23
- 0.0.9 — 2023-01-23
- 0.0.8 — 2023-01-09
- 0.0.7 — 2023-01-09
- 0.0.6 — 2023-01-09
- 0.0.5 — 2023-01-09
- 0.0.4 — 2023-01-07
- 0.0.3 — 2023-01-05
- 0.0.2 — 2023-01-04
- 0.0.1 — 2023-01-02

## README

# react-nested-dropdown

A simple and customizable nested dropdown component for React.

<a href="https://www.npmjs.com/package/react-nested-dropdown">
  <img alt="npm version" src="https://img.shields.io/npm/v/react-nested-dropdown.svg?style=flat-square" />
</a>
<a href="https://www.npmjs.com/package/react-nested-dropdown">
  <img alt="npm downloads" src="https://img.shields.io/npm/dm/react-nested-dropdown.svg?style=flat-square" />
</a>
<a href="https://bundlephobia.com/package/react-nested-dropdown">
  <img alt="npm minified bundle size" src="https://img.shields.io/bundlephobia/min/react-nested-dropdown?style=flat-square">
</a>
<a href="https://bundlephobia.com/package/react-nested-dropdown">
  <img alt="npm gzip minified bundle size" src="https://img.shields.io/bundlephobia/minzip/react-nested-dropdown?style=flat-square">
</a>

<br>
<br>
<img src="https://raw.githubusercontent.com/g-makarov/react-nested-dropdown/main/screenshots/1.png" width="966">

## Features

- Custom trigger element
- Dropdown item with submenu
- Multi level submenu support
- Specific props to each dropdown item
- Auto positioning of dropdown menu
- Written in TypeScript 🤙

## Installation

```bash
# using npm
npm install react-nested-dropdown
# using pnpm
pnpm install react-nested-dropdown
# using yarn
yarn add react-nested-dropdown
```

## Basic usage

```tsx
import React from 'react';

import { Dropdown } from 'react-nested-dropdown';
import 'react-nested-dropdown/dist/styles.css';

const items = [
  {
    label: 'Option 1',
    onSelect: () => console.log('Option 1 selected'),
  },
  {
    label: 'Option 2',
    items: [
      {
        label: 'Option 2.1',
        onSelect: () => console.log('Option 2.1 selected'),
      },
      {
        label: 'Option 2.2',
        onSelect: () => console.log('Option 2.2 selected'),
      },
    ],
  },
];

export const App = () => {
  return (
    <Dropdown items={items} containerWidth="300px">
      {({ isOpen, onClick }) => (
        <button type="button" onClick={onClick}>
          {isOpen ? 'Close dropdown' : 'Open dropdown'}
        </button>
      )}
    </Dropdown>
  );
};
```

## `Dropdown` props

| Prop             | Type                                                                       | Default | Description                                                                                                                                                                                                                                                      |
| ---------------- | -------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `items`          | `DropdownItem[]`                                                           | `[]`    | An array of dropdown items to render in the menu.                                                                                                                                                                                                                |
| `containerWidth` | `number` or `string`                                                       | `300`   | The width of the dropdown menu container. Can be a number for pixels or a string for any valid CSS width value.                                                                                                                                                  |
| `onSelect`       | `(value: any, option: DropdownItem) => void`                               | `null`  | A callback function that is called when an option is selected. It is passed the value of the selected option and the option object itself.                                                                                                                       |
| `children`       | `(params: { onClick: () => void, isOpen: boolean }) => React.ReactElement` | `null`  | A function that returns a React element to be used as the trigger for the dropdown menu. The function is passed an object with an `onClick` function to open and close the dropdown, and an `isOpen` boolean to indicate the current open state of the dropdown. |
| `className`      | `string`                                                                   | `null`  | A custom class name to be applied to the root element of the component.                                                                                                                                                                                          |
| `renderOption`   | `(option: DropdownItem) => React.ReactElement`                             | `null`  | A function that returns a React element for rendering each option in the dropdown menu. It is passed the option object for the current item.                                                                                                                     |
| `closeOnScroll`  | `boolean`                                                                  | `true`  | If set to `true`, the dropdown menu will close when the page is scrolled.                                                                                                                                                                                        |

## `DropdownItem` interface

| Prop                  | Type                 | Default     | Description                                                                                                      |
| --------------------- | -------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------- |
| `label`               | `string`             | `''`        | The label to display for the item.                                                                               |
| `iconBefore`          | `React.ReactNode`    | `null`      | An optional icon to display before the label.                                                                    |
| `iconAfter`           | `React.ReactNode`    | `null`      | An optional icon to display after the label.                                                                     |
| `items`               | `DropdownItem[]`     | `null`      | An optional array of nested items to create a submenu.                                                           |
| `itemsContainerWidth` | `number` or `string` | `150`       | The width of the sub items menu container. Can be a number for pixels or a string for any valid CSS width value. |
| `value`               | `any`                | `undefined` | An optional value for the item. This value will be in `Dropdown`'s callback `onSelect` as first argument.        |
| `onSelect`            | `() => void`         | `null`      | An optional callback function to be called when the item is selected.                                            |
| `disabled`            | `boolean`            | `false`     | Whether the item should be disabled and unable to be selected.                                                   |
| `className`           | `string`             | `null`      | An optional class name to be applied to the item element.                                                        |

---
_Source: https://npm.io/package/react-nested-dropdown · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
