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

Components: Add a WAI-ARIA compliant Combobox. #19657

Merged
merged 3 commits into from
Jun 26, 2020
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,12 @@
"markdown_source": "../packages/components/src/color-picker/README.md",
"parent": "components"
},
{
"title": "ComboboxControl",
"slug": "combobox-control",
"markdown_source": "../packages/components/src/combobox-control/README.md",
"parent": "components"
},
{
"title": "CustomSelectControl",
"slug": "custom-select-control",
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@wordpress/warning": "file:../warning",
"classnames": "^2.2.5",
"dom-scroll-into-view": "^1.2.1",
"downshift": "^4.0.5",
"downshift": "^5.4.0",
"gradient-parser": "^0.1.5",
"lodash": "^4.17.15",
"memize": "^1.1.0",
Expand Down
146 changes: 146 additions & 0 deletions packages/components/src/combobox-control/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# ComboboxControl

`ComboboxControl` is an enhanced version of a [`CustomSelectControl`](/packages/components/src/custom-select-control/readme.md), with the addition of being able to search for options using a search input.

## Table of contents

1. [Design guidelines](#design-guidelines)
2. [Development guidelines](#development-guidelines)
3. [Related components](#related-components)

## Design guidelines

These are the same as [the ones for `CustomSelectControl`s](/packages/components/src/select-control/readme.md#design-guidelines), but this component is better suited for when there are too many items to scroll through or load at once so you need to filter them based on user input.
epiqueras marked this conversation as resolved.
Show resolved Hide resolved

## Development guidelines

### Usage

```jsx
/**
* WordPress dependencies
*/
import { ComboboxControl } from "@wordpress/components";
import { useState } from "@wordpress/compose";

const options = [
{
key: "small",
name: "Small",
style: { fontSize: "50%" }
},
{
key: "normal",
name: "Normal",
style: { fontSize: "100%" }
},
{
key: "large",
name: "Large",
style: { fontSize: "200%" }
},
{
key: "huge",
name: "Huge",
style: { fontSize: "300%" }
}
];

function MyComboboxControl() {
const [, setFontSize] = useState();
const [filteredOptions, setFilteredOptions] = useState(options);
return (
<ComboboxControl
label="Font Size"
options={filteredOptions}
onInputValueChange={({ inputValue }) =>
setFilteredOptions(
options.filter(option =>
option.name.toLowerCase().startsWith(inputValue.toLowerCase())
)
)
}
onChange={({ selectedItem }) => setFontSize(selectedItem)}
/>
);
}

function MyControlledComboboxControl() {
const [fontSize, setFontSize] = useState(options[0]);
const [filteredOptions, setFilteredOptions] = useState(options);
return (
<ComboboxControl
label="Font Size"
options={filteredOptions}
onInputValueChange={({ inputValue }) =>
setFilteredOptions(
options.filter(option =>
option.name.toLowerCase().startsWith(inputValue.toLowerCase())
)
)
}
onChange={({ selectedItem }) => setFontSize(selectedItem)}
value={options.find(option => option.key === fontSize.key)}
/>
);
}
```

### Props

#### className

A custom class name to append to the outer `<div>`.

- Type: `String`
- Required: No

#### hideLabelFromVision

Used to visually hide the label. It will always be visible to screen readers.

- Type: `Boolean`
- Required: No

#### label

The label for the control.

- Type: `String`
- Required: Yes

#### options

The options that can be chosen from.

- Type: `Array<{ key: String, name: String, style: ?{}, ...rest }>`
- Required: Yes

#### onInputValueChange

Function called with the control's search input value changes. The `inputValue` property contains the next input value.

- Type: `Function`
- Required: No

#### onChange

Function called with the control's internal state changes. The `selectedItem` property contains the next selected item.

- Type: `Function`
- Required: No

#### value

Can be used to externally control the value of the control, like in the `MyControlledComboboxControl` example above.

- Type: `Object`
- Required: No

## Related components

- Like this component, but without a search input, the `CustomSelectControl` component.

- To select one option from a set, when you want to show all the available options at once, use the `Radio` component.
- To select one or more items from a set, use the `CheckboxControl` component.
- To toggle a single setting on or off, use the `ToggleControl` component.
126 changes: 126 additions & 0 deletions packages/components/src/combobox-control/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* External dependencies
*/
import { useCombobox } from 'downshift';
import classnames from 'classnames';

/**
* Internal dependencies
*/
import { Button, Dashicon } from '../';

const itemToString = ( item ) => item && item.name;
export default function ComboboxControl( {
className,
hideLabelFromVision,
label,
options: items,
onInputValueChange: onInputValueChange,

Choose a reason for hiding this comment

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

no change here right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope

onChange: onSelectedItemChange,
value: _selectedItem,
} ) {
const {
getLabelProps,
getToggleButtonProps,
getComboboxProps,
getInputProps,
getMenuProps,
getItemProps,
isOpen,
highlightedIndex,
selectedItem,
} = useCombobox( {
initialSelectedItem: items[ 0 ],
items,
itemToString,
onInputValueChange,
onSelectedItemChange,
selectedItem: _selectedItem,
} );
const menuProps = getMenuProps( {
className: 'components-combobox-control__menu',
} );
// We need this here, because the null active descendant is not
// fully ARIA compliant.
if (
menuProps[ 'aria-activedescendant' ] &&
menuProps[ 'aria-activedescendant' ].slice(
0,
'downshift-null'.length
) === 'downshift-null'
) {
delete menuProps[ 'aria-activedescendant' ];

Choose a reason for hiding this comment

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

this looks like it should be a downshift bug.

Choose a reason for hiding this comment

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

@epiqueras can you point out to me the use case when this happens? I want to fix it in v5. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When mounting in uncontrolled mode.

Choose a reason for hiding this comment

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

I am confused, sorry :)). Do we have an example somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

@epiqueras downshift-js/downshift#945 is this fixing your use case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should, yeah.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this code if it's fixed in Downshift?

}
return (
<div
className={ classnames( 'components-combobox-control', className ) }
>
{ /* eslint-disable-next-line jsx-a11y/label-has-associated-control, jsx-a11y/label-has-for */ }
<label
{ ...getLabelProps( {
className: classnames(
'components-combobox-control__label',
{
'screen-reader-text': hideLabelFromVision,
}
),
} ) }
>
{ label }
</label>
<div
{ ...getComboboxProps( {
className: 'components-combobox-control__button',
} ) }
>
<input
{ ...getInputProps( {
className: 'components-combobox-control__button-input',
} ) }
/>
<Button
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, if we're following Example 1, I don't think we need a button here at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's valuable to have a button on the right to open the list without typing, isn't it?

It's what Downshift recommends too.

cc @silviuavram

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm going by the WAI-ARIA patterns linked to above, where the only example with a button is the inline autocomplete one. That's not the one we're implementing here, so thought it would be good to stay consistent with their recommendations.

Choose a reason for hiding this comment

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

Yes, there are versions with the toggle and versions without. The toggle is helpful to see the options before typing anything, but if that is not part of the requirement then it can be ommited. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we keep it? It looks quite nice.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep it for now, shouldn't do any harm 🙂

{ ...getToggleButtonProps( {
// This is needed because some speech recognition software don't support `aria-labelledby`.
'aria-label': label,
'aria-labelledby': undefined,
className: 'components-combobox-control__button-button',
} ) }
>
<Dashicon
icon="arrow-down-alt2"
className="components-combobox-control__button-icon"
/>
</Button>
</div>
<ul { ...menuProps }>
{ isOpen &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we check that the input has a non-empty value before rendering the items? I'm still seeing the bug where, after typing in a letter and then deleting it so the input becomes empty, the whole list shows. Or perhaps this should be fixed in Downshift instead? Cc @silviuaavram

Choose a reason for hiding this comment

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

isOpen does not take into account the value in inputValue, so you can have the menu open without having anything in the input. Is there an ARIA/general agreed behavior for this? I am not aware of it.

To customise you can probably use stateReducer and only make isOpen true if inputValue has content. If you consider the default behavior as a bug, can you open an issue in Downshift and have it documented? Our hooks are very easy to customise behavior, but we should have the standard actions on par with ARIA guidelines.

Thanks!

Copy link
Contributor

@tellthemachines tellthemachines May 20, 2020

Choose a reason for hiding this comment

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

Is there an ARIA/general agreed behavior for this? I am not aware of it.

Yup, that's the behaviour in the official ARIA combobox examples. Check the third example, that has the button just like ours. The whole menu is only visible on click of the button, or on Arrow Down keypress. But if you type in the box and then clear, the dropdown disappears.

items.map( ( item, index ) => (
// eslint-disable-next-line react/jsx-key
<li
{ ...getItemProps( {
item,
index,
key: item.key,
className: classnames(
'components-combobox-control__item',
{
'is-highlighted':
index === highlightedIndex,
}
),
style: item.style,
} ) }
>
{ item === selectedItem && (
<Dashicon
icon="saved"
className="components-combobox-control__item-icon"
/>
) }
{ item.name }
</li>
) ) }
</ul>
</div>
);
}
Loading