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: a11y, enabled clear indicator to be accessible via keyboard #5850

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/poor-walls-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-select': patch
---

Optional props to enable clear indicator to be keyboard accessible
16 changes: 12 additions & 4 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
@@ -176,6 +176,8 @@ export interface Props<
instanceId?: number | string;
/** Is the select value clearable */
isClearable?: boolean;
/** enabled clear indicator to accessible via keyboard and screen-reader */
enableAccessibleClearIndicator?: boolean;
/** Is the select disabled */
isDisabled: boolean;
/** Is the select in a state of loading (async) */
@@ -734,7 +736,6 @@ export default class Select<
`${instancePrefix}-option`
)
: [];

const focusedValue = clearFocusValueOnUpdate
? getNextFocusedValue(state, selectValue)
: null;
@@ -743,7 +744,6 @@ export default class Select<
focusableOptionsWithIds,
focusedOption
);

newMenuOptionsState = {
selectValue,
focusedOption,
@@ -764,7 +764,8 @@ export default class Select<

let newAriaSelection = ariaSelection;

let hasKeptFocus = isFocused && prevWasFocused;
let hasKeptFocus =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that since we are setting focus back to select input after clearing the value, initial-input-focus is not called again.

isFocused && (prevWasFocused || ariaSelection?.action === 'clear');

if (isFocused && !hasKeptFocus) {
// If `value` or `defaultValue` props are not empty then announce them
@@ -1860,7 +1861,8 @@ export default class Select<
renderClearIndicator() {
const { ClearIndicator } = this.getComponents();
const { commonProps } = this;
const { isDisabled, isLoading } = this.props;
const { isDisabled, isLoading, enableAccessibleClearIndicator } =
this.props;
const { isFocused } = this.state;

if (
@@ -1884,6 +1886,12 @@ export default class Select<
{...commonProps}
innerProps={innerProps}
isFocused={isFocused}
enableAccessibleClearIndicator={enableAccessibleClearIndicator}
handleClearingValue={() => {
this.openAfterFocus = false;
this.focusInput();
this.clearValue();
}}
/>
);
}
403 changes: 276 additions & 127 deletions packages/react-select/src/__tests__/Select.test.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -146,13 +146,21 @@ exports[`defaults - snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}
.emotion-9:hover {
color: hsl(0, 0%, 60%);
}
.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}
.emotion-10 {
display: inline-block;
fill: currentColor;
Original file line number Diff line number Diff line change
@@ -146,13 +146,21 @@ exports[`defaults - snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}
.emotion-9:hover {
color: hsl(0, 0%, 60%);
}
.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}
.emotion-10 {
display: inline-block;
fill: currentColor;
Original file line number Diff line number Diff line change
@@ -146,13 +146,21 @@ exports[`defaults - snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}
.emotion-9:hover {
color: hsl(0, 0%, 60%);
}
.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}
.emotion-10 {
display: inline-block;
fill: currentColor;
Original file line number Diff line number Diff line change
@@ -146,13 +146,21 @@ exports[`snapshot - defaults 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}
.emotion-9:hover {
color: hsl(0, 0%, 60%);
}
.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}
.emotion-10 {
display: inline-block;
fill: currentColor;
Original file line number Diff line number Diff line change
@@ -146,13 +146,21 @@ exports[`defaults > snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}
.emotion-9:hover {
color: hsl(0, 0%, 60%);
}
.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}
.emotion-10 {
display: inline-block;
fill: currentColor;
1 change: 0 additions & 1 deletion packages/react-select/src/components/LiveRegion.tsx
Original file line number Diff line number Diff line change
@@ -204,7 +204,6 @@ const LiveRegion = <
<span id="aria-guidance">{ariaGuidance}</span>
</Fragment>
);

return (
<Fragment>
{/* We use 'aria-describedby' linked to this component for the initial focus */}
50 changes: 42 additions & 8 deletions packages/react-select/src/components/indicators.tsx
Original file line number Diff line number Diff line change
@@ -92,10 +92,18 @@ const baseCSS = <
':hover': {
color: isFocused ? colors.neutral80 : colors.neutral40,
},
border: 'none',
background: 'none',
':focus': {
borderColor: !isFocused ? colors.primary : 'none',
outline: 'unset',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some focus css, this can be updated or removed

boxShadow: !isFocused ? `0 0 0 2px ${colors.primary} inset` : 'unset',
},
}),
});

export const dropdownIndicatorCSS = baseCSS;

export const DropdownIndicator = <
Option,
IsMulti extends boolean,
@@ -128,6 +136,10 @@ export interface ClearIndicatorProps<
innerProps: JSX.IntrinsicElements['div'];
/** The focused state of the select. */
isFocused: boolean;
/** Handle clearing the value and focus*/
handleClearingValue: () => void;
/** Enabled clear indictor to be acessible via keyboard and screen-reader */
enableAccessibleClearIndicator?: boolean;
}

export const clearIndicatorCSS = baseCSS;
@@ -138,15 +150,37 @@ export const ClearIndicator = <
>(
props: ClearIndicatorProps<Option, IsMulti, Group>
) => {
const { children, innerProps } = props;
const {
children,
innerProps,
enableAccessibleClearIndicator,
handleClearingValue: onClearValue,
} = props;

const clearIndicatorStyle = getStyleProps(props, 'clearIndicator', {
indicator: true,
'clear-indicator': true,
});

if (enableAccessibleClearIndicator) {
return (
<div {...innerProps} aria-hidden={false}>
<button
aria-label="clear"
{...clearIndicatorStyle}
onClick={(e) => {
e.preventDefault();
onClearValue();
}}
>
{children || <CrossIcon />}
</button>
</div>
);
}

return (
<div
{...getStyleProps(props, 'clearIndicator', {
indicator: true,
'clear-indicator': true,
})}
{...innerProps}
>
<div {...clearIndicatorStyle} {...innerProps}>
{children || <CrossIcon />}
</div>
);