npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-nested-dropdown

v0.0.12

Published

Dropdown with submenu for React

Downloads

2,964

Readme

react-nested-dropdown

A simple and customizable nested dropdown component for React.

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

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

Basic usage

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. |