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

CustomSelectControl: Update to use a Popover component for rendering the internals #37272

Closed
wants to merge 13 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

### Bug Fix

- CustomSelectControl: Update to use a Popover component for rendering the menu ([#37272](https://github.com/WordPress/gutenberg/pull/37272)).
- Fixed spacing between `BaseControl` fields and help text within the `ToolsPanel` ([#36334](https://github.com/WordPress/gutenberg/pull/36334))
- Replaced hardcoded blue in `ColorPicker` with UI theme color ([#36153](https://github.com/WordPress/gutenberg/pull/36153)).
- Fixed empty `ToolsPanel` height by correcting menu button line-height ([#36895](https://github.com/WordPress/gutenberg/pull/36895)).
Expand Down
131 changes: 84 additions & 47 deletions packages/components/src/custom-select-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,32 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useResizeObserver } from '@wordpress/compose';
import { useRef } from '@wordpress/element';
import { Icon, check, chevronDown } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { Button, VisuallyHidden } from '../';
import { Button, Popover, VisuallyHidden } from '../';

const OptionList = ( { anchorRef, isOpen, children } ) => {
if ( ! isOpen ) {
return children;
}

return (
<Popover
anchorRef={ anchorRef?.current }
className="components-custom-select-control__popover"
isAlternate={ true }
position={ 'bottom center' }
shouldAnchorIncludePadding
>
{ children }
</Popover>
);
};

const itemToString = ( item ) => item?.name;
// This is needed so that in Windows, where
Expand Down Expand Up @@ -81,6 +101,14 @@ export default function CustomSelectControl( {
stateReducer,
} );

const anchorRef = useRef();

// Calculate Popover width based on the size of the component's container.
const [
containerResizeListener,
{ width: containerWidth },
] = useResizeObserver();

function getDescribedBy() {
if ( describedBy ) {
return describedBy;
Expand All @@ -97,20 +125,36 @@ export default function CustomSelectControl( {
const menuProps = getMenuProps( {
className: 'components-custom-select-control__menu',
'aria-hidden': ! isOpen,
style: {
width: containerWidth,
},
} );

// We need this here, because the null active descendant is not fully ARIA compliant.
if (
menuProps[ 'aria-activedescendant' ]?.startsWith( 'downshift-null' )
) {
delete menuProps[ 'aria-activedescendant' ];
}

const toggleButtonProps = getToggleButtonProps( {
// This is needed because some speech recognition software don't support `aria-labelledby`.
'aria-label': label,
'aria-labelledby': undefined,
className: 'components-custom-select-control__button',
isSmall: true,
describedBy: getDescribedBy(),
} );

return (
<div
className={ classnames(
'components-custom-select-control',
className
) }
ref={ anchorRef }
>
{ containerResizeListener }
{ hideLabelFromVision ? (
<VisuallyHidden as="label" { ...getLabelProps() }>
{ label }
Expand All @@ -125,58 +169,51 @@ export default function CustomSelectControl( {
{ label }
</label>
) }
<Button
{ ...getToggleButtonProps( {
// This is needed because some speech recognition software don't support `aria-labelledby`.
'aria-label': label,
'aria-labelledby': undefined,
className: 'components-custom-select-control__button',
isSmall: true,
describedBy: getDescribedBy(),
} ) }
>
<Button { ...toggleButtonProps }>
{ itemToString( selectedItem ) }
<Icon
icon={ chevronDown }
className="components-custom-select-control__button-icon"
/>
</Button>
<ul { ...menuProps }>
{ isOpen &&
items.map( ( item, index ) => (
// eslint-disable-next-line react/jsx-key
<li
{ ...getItemProps( {
item,
index,
key: item.key,
className: classnames(
item.className,
'components-custom-select-control__item',
{
'is-highlighted':
index === highlightedIndex,
'has-hint': !! item.__experimentalHint,
}
),
style: item.style,
} ) }
>
{ item.name }
{ item.__experimentalHint && (
<span className="components-custom-select-control__item-hint">
{ item.__experimentalHint }
</span>
) }
{ item === selectedItem && (
<Icon
icon={ check }
className="components-custom-select-control__item-icon"
/>
) }
</li>
) ) }
</ul>
<OptionList isOpen={ isOpen } anchorRef={ anchorRef }>
<ul { ...menuProps }>
{ isOpen &&
Comment on lines +180 to +181
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if the ul should instead be wrapped around OptionList. That would also avoid the isOpen && check

i.e

<ul { ...menuProps }>
  <OptionList isOpen={ isOpen } anchorRef={ anchorRef }>
    { items.map( ... ) }
  </OptionList>
</ul>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The isOpen check is slightly odd, but unfortunately I don't think we can move OptionList to be a child of the ul, as then the Popover divs would be direct descendants of the ul element (where only li / script tags are allowed).

items.map( ( item, index ) => (
// eslint-disable-next-line react/jsx-key
<li
{ ...getItemProps( {
item,
index,
key: item.key,
className: classnames(
item.className,
'components-custom-select-control__item',
{
'is-highlighted':
index === highlightedIndex,
'has-hint': !! item.__experimentalHint,
}
),
style: item.style,
} ) }
>
{ item.name }
{ item.__experimentalHint && (
<span className="components-custom-select-control__item-hint">
{ item.__experimentalHint }
</span>
) }
{ item === selectedItem && (
<Icon
icon={ check }
className="components-custom-select-control__item-icon"
/>
) }
</li>
) ) }
</ul>
</OptionList>
</div>
);
}
18 changes: 11 additions & 7 deletions packages/components/src/custom-select-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,12 @@
display: none;
}

// Block UI appearance.
border: $border-width solid $gray-900;
background-color: $white;
border-radius: $radius-block-ui;
outline: none;
transition: none;

max-height: 400px;
min-width: 100%;
overflow: auto;
padding: 0;
position: absolute;
margin: 0 (-$border-width); // Allow width value to include the Popover border width.
z-index: z-index(".components-popover");
}

Expand Down Expand Up @@ -84,3 +78,13 @@
margin-bottom: 0;
}
}

.components-custom-select-control__popover {
&.components-popover {
.components-popover__content {
// Prevent the Popover scrollbars so that
// the scrollbars are only handled by the Menu.
overflow: hidden;
}
}
}