Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Dropdown screen reader problems #714

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/core/Dropdown/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const { Provider: DropdownProvider, Consumer: DropdownConsumer } =
interface DropdownState {
selectedValue: string | undefined | null;
selectedValueText: string | undefined | null;
ariaExpanded: boolean;
showPopover: boolean;
focusedDescendantId: string | null | undefined;
}
Expand Down Expand Up @@ -166,6 +167,7 @@ class BaseDropdown extends Component<DropdownProps> {
: undefined,
this.props.children,
),
ariaExpanded: false,
showPopover: false,
focusedDescendantId:
'value' in this.props && !!this.props.value
Expand Down Expand Up @@ -241,9 +243,14 @@ class BaseDropdown extends Component<DropdownProps> {
this.props.children,
),
focusedDescendantId: itemValue,
ariaExpanded: false,
});
this.buttonRef.current?.focus();
this.setState({ showPopover: false });
setTimeout(() => {
// NVDA with Firefox requires small timeout before focus to read updated value
this.buttonRef.current?.focus();
// Set showPopover separately to prevent NVDA focus from going to body
this.setState({ showPopover: false });
}, 10);
}

private handleSpaceAndEnter = (
Expand All @@ -270,7 +277,7 @@ class BaseDropdown extends Component<DropdownProps> {
};

private handleKeyDown = (event: React.KeyboardEvent) => {
const { focusedDescendantId, showPopover } = this.state;
const { focusedDescendantId, ariaExpanded, showPopover } = this.state;
const popoverItems = Array.isArray(this.props.children)
? this.props.children
: this.props.children !== undefined
Expand Down Expand Up @@ -322,7 +329,10 @@ class BaseDropdown extends Component<DropdownProps> {

case ' ': {
event.preventDefault();
this.setState({ showPopover: !showPopover });
this.setState({
showPopover: !showPopover,
ariaExpanded: !ariaExpanded,
});
this.handleSpaceAndEnter(popoverItems, getNextItem);
break;
}
Expand Down Expand Up @@ -370,7 +380,10 @@ class BaseDropdown extends Component<DropdownProps> {
);
const focusInDropdown = focusInPopover || focusInToggleButton;
if (!focusInDropdown) {
this.setState({ showPopover: focusInDropdown });
this.setState({
showPopover: focusInDropdown,
ariaExpanded: focusInDropdown,
});
}
});
};
Expand All @@ -394,12 +407,13 @@ class BaseDropdown extends Component<DropdownProps> {
}

private focusToButtonAndClosePopover() {
this.setState({ ariaExpanded: false });
this.buttonRef.current?.focus();
this.setState({ showPopover: false });
}

private openPopover() {
this.setState({ showPopover: true });
this.setState({ showPopover: true, ariaExpanded: true });
/**
* Timeout is used here to ensure the popover
* exists when setting focus
Expand Down Expand Up @@ -441,19 +455,15 @@ class BaseDropdown extends Component<DropdownProps> {
return null;
}

const {
selectedValue,
selectedValueText,
showPopover,
focusedDescendantId,
} = this.state;
const { selectedValue, ariaExpanded, showPopover, focusedDescendantId } =
this.state;

const labelId = `${id}-label`;
const buttonId = `${id}_button`;
const popoverItemListId = `${id}-popover`;
const hintTextId = hintText ? `${id}-hintText` : undefined;
const statusTextId = statusText ? `${id}-statusText` : undefined;
const displayValueId = selectedValueText ? `${id}-displayValue` : undefined;
const displayValueId = `${id}-displayValue`;

const ariaActiveDescendant = focusedDescendantId
? `${id}-${focusedDescendantId}`
Expand Down Expand Up @@ -512,7 +522,7 @@ class BaseDropdown extends Component<DropdownProps> {
hintTextId,
])}
aria-owns={popoverItemListId}
aria-expanded={showPopover}
aria-expanded={ariaExpanded}
onClick={() => {
if (!showPopover) {
this.openPopover();
Expand All @@ -534,6 +544,7 @@ class BaseDropdown extends Component<DropdownProps> {
value={selectedValue || ''}
/>
</HtmlButton>
{/* NVDA, Chrome does not read displayValue, if within HtmlButton */}
<VisuallyHidden id={displayValueId}>
{dropdownDisplayValue}
</VisuallyHidden>
Expand All @@ -555,6 +566,7 @@ class BaseDropdown extends Component<DropdownProps> {
if (!this.isOutsideClick(event)) {
this.setState({
showPopover: false,
ariaExpanded: false,
});
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ exports[`Basic dropdown should match snapshot 1`] = `
<button
aria-expanded="true"
aria-haspopup="listbox"
aria-labelledby="test-id-label"
aria-labelledby="test-id-displayValue test-id-label"
aria-owns="test-id-popover"
class="c5 fi-dropdown_button"
id="test-id_button"
Expand All @@ -630,6 +630,7 @@ exports[`Basic dropdown should match snapshot 1`] = `
</button>
<span
class="c4 c7 fi-visually-hidden"
id="test-id-displayValue"
>
Dropdown
</span>
Expand Down Expand Up @@ -666,7 +667,7 @@ exports[`Basic dropdown should match snapshot 1`] = `
class="c11 c12 fi-dropdown_item fi-dropdown_item--hasKeyboardFocus"
id="test-id-item-1"
role="option"
tabindex="-1"
tabindex="0"
>
Item 1
</li>
Expand Down Expand Up @@ -1389,7 +1390,7 @@ exports[`Controlled Dropdown should match snapshot 1`] = `
class="c11 c12 fi-dropdown_item fi-dropdown_item--hasKeyboardFocus fi-dropdown_item--selected"
id="test-id-item-2"
role="option"
tabindex="-1"
tabindex="0"
>
Item 2
<svg
Expand Down Expand Up @@ -2024,7 +2025,7 @@ exports[`Dropdown with additional aria-label should match snapshot 1`] = `
<button
aria-expanded="true"
aria-haspopup="listbox"
aria-labelledby="additional-label-id test-id-label"
aria-labelledby="test-id-displayValue additional-label-id test-id-label"
aria-owns="test-id-popover"
class="c5 fi-dropdown_button"
id="test-id_button"
Expand All @@ -2046,6 +2047,7 @@ exports[`Dropdown with additional aria-label should match snapshot 1`] = `
</button>
<span
class="c4 c7 fi-visually-hidden"
id="test-id-displayValue"
>
Dropdown
</span>
Expand Down Expand Up @@ -2082,7 +2084,7 @@ exports[`Dropdown with additional aria-label should match snapshot 1`] = `
class="c11 c12 fi-dropdown_item fi-dropdown_item--hasKeyboardFocus"
id="test-id-item-1"
role="option"
tabindex="-1"
tabindex="0"
>
Item 1
</li>
Expand Down Expand Up @@ -2550,7 +2552,7 @@ exports[`Dropdown with hidden label should match snapshot 1`] = `
<button
aria-expanded="false"
aria-haspopup="listbox"
aria-labelledby="test-id-label"
aria-labelledby="test-id-displayValue test-id-label"
aria-owns="test-id-popover"
class="c5 fi-dropdown_button"
id="test-id_button"
Expand All @@ -2572,6 +2574,7 @@ exports[`Dropdown with hidden label should match snapshot 1`] = `
</button>
<span
class="c6 c4 fi-visually-hidden"
id="test-id-displayValue"
>
Dropdown
</span>
Expand Down
21 changes: 19 additions & 2 deletions src/core/Dropdown/DropdownItem/DropdownItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactNode } from 'react';
import React, { ReactNode, useLayoutEffect, useRef } from 'react';
import { default as styled } from 'styled-components';
import { baseStyles } from './DropdownItem.basestyles';
import {
Expand All @@ -10,6 +10,7 @@ import classnames from 'classnames';
import { SuomifiThemeProp, SuomifiThemeConsumer } from '../../theme';
import { HtmlLi } from '../../../reset';
import { Icon } from '../../Icon/Icon';
import { getOwnerDocument } from '../../../utils/common';

export interface DropdownItemProps {
/** Item value */
Expand Down Expand Up @@ -51,6 +52,15 @@ const BaseDropdownItem = (props: BaseDropdownItemProps & SuomifiThemeProp) => {

const listElementId = `${consumer.id}-${value}`;

const listElementRef = useRef<HTMLLIElement>(null);

useLayoutEffect(() => {
const ownerDocument = getOwnerDocument(listElementRef);
if (ownerDocument.activeElement?.id !== listElementId && hasKeyboardFocus) {
listElementRef.current?.focus({ preventScroll: true });
}
}, [consumer.focusedItemValue]);

const handleClick = () => {
if (!disabled) {
consumer.onItemClick(value);
Expand All @@ -65,12 +75,19 @@ const BaseDropdownItem = (props: BaseDropdownItemProps & SuomifiThemeProp) => {
[dropdownItemClassNames.disabled]: disabled,
[dropdownItemClassNames.noSelectedStyles]: consumer.noSelectedStyles,
})}
tabIndex={-1}
tabIndex={hasKeyboardFocus ? 0 : -1}
role="option"
aria-disabled={disabled}
aria-selected={selected}
id={listElementId}
onClick={handleClick}
forwardedRef={listElementRef}
onKeyDown={(event: React.KeyboardEvent) => {
if (event.key === 'Tab') {
event.preventDefault();
consumer.onItemTabPress();
}
}}
{...passProps}
>
{children}
Expand Down