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

@cycle9898/react-custom-dropdown-component

v0.3.6

Published

This library provides a custom drop-down list React component that can be imported into any other React component.

Downloads

7

Readme

React Custom Dropdown Component

This library provides a custom drop-down list React component that can be imported into any other React component.

Dropdown image 1

Dropdown image 2

Dropdown image 3

The component respects the common accessibility rules (WAI-ARIA).

Users can interact with the dropdown list with a mouse (clicks) and/or a keyboard :

  • Tab OR Down Arrow: Choose the next option and validate it
  • Shift + Tab OR Up Arrow: Choose the previous option and validate it
  • Enter OR Space Bar: Open/close the dropdown list or choose the focused option
  • Escape: Close the dropdown list
  • Home: Automatically choose the first option in the dropdown list
  • End: Automatically choose the last option in the dropdown list

Getting Started

Prerequisites

Node v18.17.0 is required because a package manager (npm or yarn, for instance) is needed to install this library.

The project also need the React library to work.

React V18.2.0 and React-dom V18.2.0 are required.

React's docs advise to use controlled components, so this custom component also need a React state variable and its setState method to work (see Props in Component customization section).

Instructions

  1. Add the library to the project with a package manager (yarn for exemple):
yarn add @cycle9898/react-custom-dropdown-component
  1. Import the React component in a .jsx or .tsx file (types definitions are included):
import {Dropdown } from "@cycle9898/react-custom-dropdown-component";
  1. Use the component:
function ExempleComponent() {
   return (
      <form>
         <Dropdown displayedValue={displayedValue}
                    setDisplayedValue={setDisplayedValue}
                    optionArray={optionList} />
      </form>
   );
}

Component customization

Props

This component have 3 main props:

  • DisplayedValue:

It is a state variable, from the parent component's local state or the app's global state for exemple.

Additionally, it appears in the closed drop-down list when no options have been chosen.

If "displayedValue" is an empty string, it will be updated, if possible, with the first option's value from "optionArray".

Afterwards, it will store the value from the chosen option from the list.

  • setDisplayedValue:

It is a setState method, from the parent component's local state or the app's global state for exemple, that will update the displayedValue state variable.

  • optionArray:

It is an array of objects, with an id (type: string) and a value (type: string) properties, that represent all the available options in the dropdown list.

The ID's must be unique, otherwise the component will throw an error.

  • ariaLabelById:

This prop is optional.

It is a string that represents the ID of a HTML element that label the dropdown list, it is used for the "aria-labelledBy" attribute.

If this prop is not used, the attribute "aria-label" with the string "Choose an option from the dropdown list" will be set instead.

Modify the style with CSS

There are some HTML classes that can be used to change the style of dropdown list's elements:

  • rcdc-dropdown-wrapper class:

This class is applied to the dropdown list container.

  • .rcdc-dropdown-value-container class:

This class is applied to displayed value container.

The border color or border radius can be changed with the corresponding CSS properties and this class.

With the ":focus" pseudo-class, the outline style can also be modified.

  • .rcdc-dropdown-value-text class:

This class is applied to the span tag of .rcdc-dropdown-value-container element and contain the displayedValue string.

  • .rcdc-dropdown-value-logo class:

This class is applied to the arrow icons. Its style can be changed like any Font Awesome icons.

The "color" and "font-size" CSS properties to change its color and size, for exemple.

  • .rcdc-dropdown-list class:

This class is applied to the "ul" HTML tag that contains all the options.

The border color or border radius can be changed with the corresponding CSS properties and this class.

  • .rcdc-dropdown-option class:

This class is applied to the "li" HTML tag that represent an option.

With ":hover" and ":focus" pseudo-classes and "background-color" or "color" CSS properties, the background color or text color can be changed when an option is hovered or focused.