JedWatson / JedWatson/react-select

[Accessibility] Unable to select an option using Windows Narrator

Open
#6,080 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

Hey,

I tested react-select using Windows Narrator, and I was unable to select any options. I open the select, and when I try to use the Arrow Down key to move to another option, the select closes.

I noticed that the onKeyDown function does not receive the Arrow Down event in the dropdown when Narrator is on.

I created a hook to handle this, but I’d appreciate it if react-select supported this natively.

Here you can check the bug:

https://github.com/user-attachments/assets/945b2efa-424f-4afe-a78b-e0596e93f5c8

The hook:

import {useCallback, useEffect, useRef, useState} from 'react';

interface KeyboardNavigation {
  initialValue?: any;
  onValueChange?: any;
}

export const useSelectKeyboardNavigation = ({
  initialValue,
  onValueChange,
}: KeyboardNavigation) => {
  const [value, setValue] = useState<any>(initialValue);
  const [isMenuOpen, setIsMenuOpen] = useState(false);

  const selectRef = useRef<any>(null);
  const preventCloseRef = useRef(false);

  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (selectRef.current?.controlRef) {
        if (!selectRef.current.controlRef.contains(event.target as Node)) {
          const menu = document.querySelector('[role="listbox"]');
          if (menu && !menu.contains(event.target as Node)) {
            preventCloseRef.current = false;
            setIsMenuOpen(false);
          }
        }
      }
    };

    document.addEventListener('mousedown', handleClickOutside);

    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, []);

  const handleChange = useCallback(
    (newValue: any) => {
      setValue(newValue);

      preventCloseRef.current = false;
      setIsMenuOpen(false);

      setTimeout(() => {
        const firstOption = document.querySelector('[role="option"]') as HTMLElement;
        if (firstOption) {
          firstOption.setAttribute('tabindex', '0');
          firstOption.focus();
        }
      }, 100);

      onValueChange?.(newValue);
    },
    [onValueChange]
  );

  const handleMenuClose = useCallback(() => {
    if (preventCloseRef.current) {
      return;
    }

    setIsMenuOpen(false);
  }, []);

  const handleMenuOpen = useCallback(() => {
    preventCloseRef.current = true;
    setIsMenuOpen(true);

    setTimeout(() => {
      const firstOption = document.querySelector('[role="option"]') as HTMLElement;
      if (firstOption) {
        firstOption.setAttribute('tabindex', '0');
        firstOption.focus();
      }
    }, 100);
  }, []);

  const handleKeyDown = useCallback(
    (event: React.KeyboardEvent) => {
      if (event.key === 'Escape') {
        preventCloseRef.current = false;
        setIsMenuOpen(false);
      }
    },
    []
  );

  return {
    value,
    isMenuOpen:isMenuOpen,
    selectRef,
    handleChange,
    handleMenuClose,
    handleKeyDown,
    handleMenuOpen,
  };
};

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the select behavior with Windows Narrator and Arrow Down, then trace the onKeyDown handling and the menu-close path in react-select. The fix is complete when Narrator users can open the select, move between options, and select an option without the menu closing unexpectedly.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
accessibility, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.