New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-dropdown-aria

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-dropdown-aria

Simple and accessible React dropdown component

  • 2.0.0
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-99.87%
Maintainers
1
Weekly downloads
 
Created
Source

CircleCI npm npm bundle size npm

react-dropdown-aria

Simple, lightweight, and accessible React dropdown component.

Purpose

This component was created to be a light weight and fully accessible dropdown component for React. For a more feature heavy and powerful dropdown look to the react-select package. (This dropdown is inspired by react-select)

Features

  1. Fully customizable styling
  2. Grouped options
  3. Accessible
  4. Type to find item
  5. Custom render function props
  6. Typescript typings included

Demo And Examples

For demo and examples see https://jfangrad.github.io/react-dropdown-aria/

Installation

Simply use npm or yarn to install the package.

// Yarn
$ yarn add react-dropdown-aria
// npm
$ npm install --save react-dropdown-aria

You can then include it in your project as needed:

import Dropdown from 'react-dropdown-aria';

Usage

Options should be provided to the Dropdown as an array of objects.

Simple Example:

<Dropdown
  ...
  options: {[
    { value: 'one' },
    { value: 'two' },
    { value: 'three' }
  ]}
/>

Each option object in the array of options can have the following keys:

keyTypeDefaultDescription
ariaLabelstringnullAria Label to be applied to the option
classNamestringnullCSS class to be applied to this option
titlestringnullHTML title to be used for the option
valuestringnullThe value to be displayed in the dropdown (Required)

Groups Example

Groups of items can also be implemented by pasing an array of objects of the following form to the dropdown options prop:

<Dropdown
  ...
  options: {[
    { label: 'Group 1', groupOptions: optionsGroup1 },
    { label: 'Group 2', groupOptions: optionsGroup2 },
  ]}
/>

Where the groupOptions is an array of options as described above, and the label is the string to display above the group in the dropdown.

Styling

Custom styling can be applied to the dropdown through 2 ways:

  1. CSS className props (As seen in props table below)
  2. Emotion JavaScript Objects passed to style prop

The suggested method is by using the style prop as shown in the Custom Styling Example. The following object keys are supported to apply style to the corresponding part of the dropdown:

Arrow, DropdownButton, DisplayedValue, DropdownWrapper, GroupContainer, GroupHeading, GroupDivider, OptionContainer, OptionItem

The style prop should be passed an object containing one or more of the above properties. Each of those properties should be a function that returns an object. An example is shown below:

const style = {
  OptionItem: (base, state, { selected }) => ({
    ...base,
    fontSize: '0.95em',
    color: selected ? 'white' : 'black',
    backgroundColor: selected ? '#00A3EF' : 'white',
    '&:hover': {
      backgroundColor: selected ? '#0092d6' : '#e0f5ff',
    },
    '&:focus': {
      backgroundColor: selected ? '#0092d6' : '#e0f5ff',
    },
  }),
  ...
}

As shown above, each function will be called with base and state at minimum and some will be provided a third extraState parameter which should be an object, as shown with optionStyle where the extraState contains whether an option is currently selected or not.

base - is the default styles object. It can either be spread out to extend the default styles (as shown above) or ommitted to ignore all default styles

state - is the internal state of the dropdown

Dropdown Props

PropertyTypeDefaultDescription
ariaDescribedBystringnullID of element that should be used to describe the dropdown
ariaLabelstringnullAria Label to be applied to the main dropdown button
ariaLabelledBystringnullID of element that should be used as the label for the dropdown
arrowRendererfunctionundefinedCustom function to render the arrow for the dropdown
centerTextbooleanfalseWhether main dropdown button text should be centered or not
classNamestringundefinedCSS class to be applied to the drodown
contentClassNamestringundefinedCSS class to be applied to the dropdown option ul container
defaultOpenbooleanfalseWhether the dropdown should be open by default
disabledbooleanfalseWhether the dropdown should be disabled or not
heightnumbernullUse to set the dropdown height manually
hideArrowbooleanfalseControls whether dropdown component has the arrow or not
idstringundefinedid to be passed to the main dropdown button
maxContentHeightnumbernullControls the max height of the dropdown area that contains all options
openUpbooleanfalseWhether dropdown should open up or not
optionRendererfunctionundefinedCustom function to render the options displayed in the dropdown
optionsarray[]Array of option objects
optionClassNamestringundefinedCSS class to be applied to all options in the dropdown
pageKeyTraverseSizenumber10Number of options page up or page down will move the current focused by
placeholderstringSelect...Placeholder value for dropdown
valuestringnullThe value of the option that is currently selected (as set by setSelected).
selectedValueClassNamestringundefinedCSS class to be applied to main drodown button text
searchablebooleantrueWhether or not the keyboard can be used to quickly navigate to an item through typing
onChangefunctionundefinedFunction called when the selected value changes
styleobject{}Use to change the style of the dropdown through js instead of css (see styling section)
widthnumbernullUse to set the dropdown width manually

Developing

Clone the repo

git clone https://github.com/jfangrad/react-dropdown-aria.git

Install node modules

yarn install

Then in two seperate consoles start the library and demo

yarn lib:dev
yarn demo:dev

Keywords

FAQs

Package last updated on 13 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc