Skip to content

Commit

Permalink
[WNMGDS-2779] Replace dropdown label with div (#3099)
Browse files Browse the repository at this point in the history
* replace dropdown label with div.

* remove redundant prop on dropdown label

* re-implement the id prop.

* change root id name to exclude word button.
  • Loading branch information
zarahzachz authored and pwolfert committed Jun 3, 2024
1 parent 66e2f09 commit 6c052ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
13 changes: 8 additions & 5 deletions packages/design-system/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import mergeRefs from '../utilities/mergeRefs';
import useClickOutsideHandler from '../utilities/useClickOutsideHandler';
import useId from '../utilities/useId';
import useAutofocus from '../utilities/useAutoFocus';
import { Label } from '../Label';
import { SvgIcon } from '../Icons';
import { getFirstOptionValue, isOptGroup, parseChildren, validateProps } from './utils';
import { Item, Section, useSelectState } from '../react-aria'; // from react-stately
Expand Down Expand Up @@ -139,7 +138,7 @@ export type DropdownProps = BaseDropdownProps &
export const Dropdown: React.FC<DropdownProps> = (props: DropdownProps) => {
validateProps(props);

const id = useId('dropdown__button--', props.id);
const id = useId('dropdown--', props.id);
const buttonContentId = `${id}__button-content`;
const menuId = `${id}__menu`;

Expand Down Expand Up @@ -246,8 +245,11 @@ export const Dropdown: React.FC<DropdownProps> = (props: DropdownProps) => {

const labelProps = {
...useSelectProps.labelProps,
...useLabelProps({ ...props, id }),
fieldId: id,
...useLabelProps({
...props,
id,
labelClassName: classNames('ds-c-label', 'ds-c-dropdown__label', props.labelClassName),
}),
};

const buttonProps = {
Expand Down Expand Up @@ -286,7 +288,8 @@ export const Dropdown: React.FC<DropdownProps> = (props: DropdownProps) => {
className={classNames('ds-c-dropdown', className, state.isOpen && 'ds-c-dropdown--open')}
ref={wrapperRef}
>
<Label {...labelProps} />
{/* `<div>` is used instead of `<label>` to satisfy a11y issue. Because dropdown is a `<button>`, a `<label>` is inappropriate to use with it. */}
<div {...labelProps} />
{hintElement}
{topError}
<HiddenSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ exports[`Dropdown dropdown matches snapshot 1`] = `
<div
class="ds-c-dropdown"
>
<label
class="ds-c-label"
<div
class="ds-c-label ds-c-dropdown__label"
id="static-id__label"
>
Select an option
</label>
</div>
<div
aria-hidden="true"
data-a11y-ignore="aria-hidden-focus"
Expand Down
7 changes: 7 additions & 0 deletions packages/design-system/src/styles/components/_Dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
}
}

// This class is applied to a `<div>` that acts as a label
// Because the dropdown is a `<button>` element, a `<label>` element
// breaks for some a11y checkers. Using a `<div>` is correct.
.ds-c-dropdown__label {
margin-block: 1.5em 0;
}

.ds-c-dropdown__label-text {
max-width: 100%;
overflow: hidden;
Expand Down

0 comments on commit 6c052ad

Please sign in to comment.