@codecademy/gamut

74.0.174.0.2-alpha.b4821a.0
dist/Form/SelectDropdown/elements/containers.js
~dist/Form/SelectDropdown/elements/containers.jsModified
+119−38
Index: package/dist/Form/SelectDropdown/elements/containers.js
===================================================================
--- package/dist/Form/SelectDropdown/elements/containers.js
+++ package/dist/Form/SelectDropdown/elements/containers.js
@@ -1,5 +1,5 @@
-import { createContext, useEffect, useLayoutEffect } from 'react';
+import { createContext, useContext, useEffect, useLayoutEffect } from 'react';
 import ReactSelect, { components as SelectDropdownElements } from 'react-select';
 import CreatableSelect from 'react-select/creatable';
 import { DEFAULT_SELECT_DROPDOWN_TRANSLATIONS } from '../core/translations';
 import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
@@ -59,58 +59,139 @@
     })]
   });
 };
 
+/*
+ * With isSearchable={false}, react-select renders a hidden <input role="combobox">.
+ * Screen readers announce it as editable ("type text", "auto complete"), which is
+ * wrong for a control that takes no typing. Instead, the ValueContainer is the
+ * combobox (ARIA APG select-only pattern) and takes focus. react-select's keydown
+ * handling still applies because keydown bubbles up to its container.
+ */
+const SELECT_ONLY_ARIA_PROPS = ['aria-describedby', 'aria-errormessage', 'aria-invalid', 'aria-label', 'aria-labelledby', 'aria-required'];
+
+/* Methods on react-select's Select instance, reached through the selectRef. */
+
+const useSelectOnlyInput = (inputId, isSearchable) => {
+  useLayoutEffect(() => {
+    if (isSearchable !== false || !inputId) return;
+
+    /* Look the trigger up from the hidden input: the public id is shared with other elements. */
+    const input = document.getElementById(inputId);
+    /* Start at the parent: closest() includes the input itself, which still has role="combobox" here. */
+    const trigger = input?.parentElement?.closest('[role="combobox"]');
+    if (!input || !trigger) return;
+
+    /*
+     * A disabled input can't take focus, so react-select's focusInput() calls
+     * become no-ops and focus stays on the trigger.
+     */
+    input.setAttribute('disabled', '');
+    input.setAttribute('aria-hidden', 'true');
+    input.removeAttribute('role');
+
+    /*
+     * react-select only sets aria-activedescendant on the hidden input off Apple devices
+     * (its isAppleDevice check), so VoiceOver on macOS gets nothing from it. Derive the
+     * active option from the rendered menu instead: Gamut sets aria-selected on the
+     * focused option. The container is SelectContainer > Control > ValueContainer.
+     */
+    const container = trigger.parentElement?.parentElement ?? trigger;
+    const syncActiveOption = () => {
+      const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
+      const listbox = container.querySelector('[role="listbox"]');
+      const activeOption = container.querySelector('[role="option"][aria-selected="true"]');
+      if (listbox?.id && isExpanded) {
+        trigger.setAttribute('aria-controls', listbox.id);
+      } else {
+        trigger.removeAttribute('aria-controls');
+      }
+      if (activeOption?.id && isExpanded) {
+        trigger.setAttribute('aria-activedescendant', activeOption.id);
+      } else {
+        trigger.removeAttribute('aria-activedescendant');
+      }
+    };
+
+    /*
+     * aria-expanded is set on the trigger in a later React commit than the listbox
+     * mounts, so it has to be observed too, or the sync can run while it's still "false".
+     */
+    const observer = new MutationObserver(syncActiveOption);
+    observer.observe(container, {
+      attributeFilter: ['aria-expanded', 'aria-selected'],
+      attributes: true,
+      childList: true,
+      subtree: true
+    });
+    syncActiveOption();
+    return () => observer.disconnect();
+  }, [inputId, isSearchable]);
+};
+
 /**
- * Custom ValueContainer component that applies combobox props when isSearchable is false.
- * When isSearchable is false, react-select renders a dummy input (div) that doesn't accept
- * custom props, so we apply the combobox data attributes to the ValueContainer's innerProps.
+ * Custom ValueContainer component. When isSearchable is false, it becomes the
+ * select-only combobox trigger (see `useSelectOnlyInput`). Otherwise it renders
+ * react-select's default ValueContainer.
  */
 export const CustomValueContainer = ({
+  innerProps,
   ...rest
 }) => {
   const {
+    selectInputRef,
+    triggerId
+  } = useContext(SelectDropdownContext);
+  const {
+    inputId,
     inputProps,
-    isSearchable
+    isDisabled,
+    isSearchable,
+    menuIsOpen
   } = rest.selectProps;
   const comboboxProps = inputProps?.combobox || {};
-  useLayoutEffect(() => {
-    if (isSearchable === false && Object.keys(comboboxProps).length > 0) {
-      // Find the input element by inputId since we can't use a ref on ValueContainer
-      const {
-        inputId
-      } = rest.selectProps;
-      if (inputId) {
-        const inputElement = document.getElementById(inputId);
-        if (inputElement?.getAttribute('role') === 'combobox') {
-          Object.entries(comboboxProps).forEach(([key, value]) => {
-            inputElement.setAttribute(key, value);
-          });
-        }
-      }
-    }
-    // We only want to run this effect when isSearchable or comboboxProps change
-    // eslint-disable-next-line react-hooks/exhaustive-deps
-  }, [isSearchable, comboboxProps, rest.selectProps.inputId]);
-
-  // When isSearchable is false, apply combobox props to ValueContainer's innerProps
-  // since the dummy input doesn't accept custom props
-  if (isSearchable === false && Object.keys(comboboxProps).length > 0) {
-    const {
-      innerProps,
-      ...otherProps
-    } = rest;
-    const mergedInnerProps = {
-      ...innerProps,
-      ...comboboxProps
-    };
+  useSelectOnlyInput(inputId, isSearchable);
+  if (isSearchable !== false || !triggerId) {
     return /*#__PURE__*/_jsx(SelectDropdownElements.ValueContainer, {
-      ...otherProps,
-      innerProps: mergedInnerProps
+      ...rest
     });
   }
+  const ariaProps = Object.fromEntries(Object.entries(rest.selectProps).filter(([key, value]) => SELECT_ONLY_ARIA_PROPS.includes(key) && value !== undefined));
+
+  /* Read the ref inside handlers: it is still null during render on the first pass. */
+  const getInstance = () => selectInputRef?.current;
+  const handleMouseDown = event => {
+    if (event.button !== 0 || isDisabled) return;
+
+    // Stops react-select from focusing its hidden input on mousedown.
+    event.preventDefault();
+    event.currentTarget.focus();
+    if (menuIsOpen) getInstance()?.onMenuClose();else getInstance()?.openMenu('first');
+  };
+
+  /* Close only when focus leaves the whole select; focus moving to its own menu or hidden input is not a dismissal. */
+  const handleBlur = event => {
+    const selectContainer = event.currentTarget.parentElement?.parentElement;
+    const nextFocus = event.relatedTarget;
+    if (nextFocus && selectContainer?.contains(nextFocus)) return;
+    if (menuIsOpen) getInstance()?.onMenuClose();
+  };
   return /*#__PURE__*/_jsx(SelectDropdownElements.ValueContainer, {
-    ...rest
+    ...rest,
+    innerProps: {
+      ...innerProps,
+      ...ariaProps,
+      'aria-disabled': isDisabled || undefined,
+      'aria-expanded': Boolean(menuIsOpen),
+      'aria-haspopup': 'listbox',
+      id: triggerId,
+      onBlur: handleBlur,
+      onMouseDown: handleMouseDown,
+      role: 'combobox',
+      tabIndex: isDisabled ? -1 : 0,
+      /* Last, so consumer data- and aria- props can override the defaults above, as before this change. */
+      ...comboboxProps
+    }
   });
 };
 
 /**