🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@react-aria/select

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
r

@react-aria/select

Spectrum UI components in React

3.15.4
latest
99

Supply Chain Security

100

Vulnerability

69

Quality

97

Maintenance

100

License

Version published
Weekly downloads
999K
11.63%
Maintainers
2
Weekly downloads
 
Created
Issues
721

What is @react-aria/select?

@react-aria/select is a React library that provides accessible components for building custom select menus. It is part of the React Aria collection, which focuses on providing high-quality, accessible UI components.

What are @react-aria/select's main functionalities?

Basic Select

This code demonstrates a basic select component using @react-aria/select. It includes a label, a button to trigger the select menu, and a list of items to choose from.

import { useSelectState } from '@react-stately/select';
import { useSelect } from '@react-aria/select';
import { Item } from '@react-stately/collections';

function SelectExample() {
  let state = useSelectState({
    items: [
      { key: 'red', name: 'Red' },
      { key: 'green', name: 'Green' },
      { key: 'blue', name: 'Blue' }
    ]
  });

  let ref = React.useRef();
  let { labelProps, triggerProps, valueProps, menuProps } = useSelect({
    label: 'Favorite Color',
    items: state.collection,
    selectedKey: state.selectedKey,
    onSelectionChange: state.setSelectedKey
  }, state, ref);

  return (
    <div>
      <label {...labelProps}>Favorite Color</label>
      <button {...triggerProps} ref={ref}>
        <span {...valueProps}>{state.selectedItem ? state.selectedItem.name : 'Select an option'}</span>
      </button>
      {state.isOpen && (
        <ul {...menuProps}>
          {[...state.collection].map(item => (
            <Item key={item.key}>{item.name}</Item>
          ))}
        </ul>
      )}
    </div>
  );
}

Custom Styling

This example shows how to apply custom styling to the select component using CSS classes. The custom styles are defined in an external CSS file.

import { useSelectState } from '@react-stately/select';
import { useSelect } from '@react-aria/select';
import { Item } from '@react-stately/collections';
import './customStyles.css';

function CustomStyledSelect() {
  let state = useSelectState({
    items: [
      { key: 'apple', name: 'Apple' },
      { key: 'banana', name: 'Banana' },
      { key: 'cherry', name: 'Cherry' }
    ]
  });

  let ref = React.useRef();
  let { labelProps, triggerProps, valueProps, menuProps } = useSelect({
    label: 'Favorite Fruit',
    items: state.collection,
    selectedKey: state.selectedKey,
    onSelectionChange: state.setSelectedKey
  }, state, ref);

  return (
    <div className="custom-select">
      <label {...labelProps}>Favorite Fruit</label>
      <button {...triggerProps} ref={ref} className="custom-trigger">
        <span {...valueProps}>{state.selectedItem ? state.selectedItem.name : 'Select an option'}</span>
      </button>
      {state.isOpen && (
        <ul {...menuProps} className="custom-menu">
          {[...state.collection].map(item => (
            <Item key={item.key} className="custom-item">{item.name}</Item>
          ))}
        </ul>
      )}
    </div>
  );
}

Other packages similar to @react-aria/select

FAQs

Package last updated on 11 Apr 2025

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