Skip to content

Commit

Permalink
fix: Avoid unsupported Gallery MenuGroup usage (#63953)
Browse files Browse the repository at this point in the history
The `MenuGroup` and `MenuItem` components are currently undefined for
the native mobile editor. Therefore, we cannot render them in code
shared between web and native without platform conditionals. Ideally, we
add proper support for the native platform to avoid these conditionals,
and their complexity and bundle size increase.
  • Loading branch information
dcalhoun authored Jul 26, 2024
1 parent 31dcc28 commit 0bf1cac
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 40 deletions.
88 changes: 51 additions & 37 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,17 @@ function GalleryEdit( props ) {
size="__unstable-large"
/>
) }
{ Platform.isNative ? (
<SelectControl
__nextHasNoMarginBottom
label={ __( 'Link to' ) }
value={ linkTo }
onChange={ setLinkTo }
options={ linkOptions }
hideCancelButton
size="__unstable-large"
/>
) : null }
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Crop images to fit' ) }
Expand Down Expand Up @@ -615,43 +626,46 @@ function GalleryEdit( props ) {
) }
</PanelBody>
</InspectorControls>
<BlockControls group="block">
<ToolbarDropdownMenu
icon={ linkIcon }
label={ __( 'Link To' ) }
>
{ ( { onClose } ) => (
<MenuGroup className="blocks-gallery__link-to-control">
{ linkOptions.map( ( linkItem ) => {
const isOptionSelected =
linkTo === linkItem.value;
return (
<MenuItem
key={ linkItem.value }
isSelected={ isOptionSelected }
className={ clsx(
'components-dropdown-menu__menu-item',
{
'is-active': isOptionSelected,
}
) }
iconPosition="left"
icon={ linkItem.icon }
onClick={ () => {
setLinkTo( linkItem.value );
onClose();
} }
role="menuitemradio"
info={ linkItem.infoText ?? false }
>
{ linkItem.label }
</MenuItem>
);
} ) }
</MenuGroup>
) }
</ToolbarDropdownMenu>
</BlockControls>
{ Platform.isWeb ? (
<BlockControls group="block">
<ToolbarDropdownMenu
icon={ linkIcon }
label={ __( 'Link To' ) }
>
{ ( { onClose } ) => (
<MenuGroup className="blocks-gallery__link-to-control">
{ linkOptions.map( ( linkItem ) => {
const isOptionSelected =
linkTo === linkItem.value;
return (
<MenuItem
key={ linkItem.value }
isSelected={ isOptionSelected }
className={ clsx(
'components-dropdown-menu__menu-item',
{
'is-active':
isOptionSelected,
}
) }
iconPosition="left"
icon={ linkItem.icon }
onClick={ () => {
setLinkTo( linkItem.value );
onClose();
} }
role="menuitemradio"
info={ linkItem.infoText ?? false }
>
{ linkItem.label }
</MenuItem>
);
} ) }
</MenuGroup>
) }
</ToolbarDropdownMenu>
</BlockControls>
) : null }
{ Platform.isWeb && (
<>
{ ! multiGallerySelection && (
Expand Down
7 changes: 4 additions & 3 deletions packages/block-library/src/gallery/test/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,11 @@ describe( 'Gallery block', () => {
<!-- /wp:gallery -->`,
numberOfItems: 2,
} );
const { getByLabelText, getByText } = screen;
const { getByText } = screen;

fireEvent.press( getBlock( screen, 'Gallery' ) );
fireEvent.press( getByLabelText( 'Link To' ) );
// Set "Link to" setting via Gallery block settings
await openBlockSettings( screen );
fireEvent.press( getByText( 'Link to' ) );
fireEvent.press( getByText( 'Link images to media files' ) );

expect( getEditorHtml() ).toMatchSnapshot();
Expand Down

0 comments on commit 0bf1cac

Please sign in to comment.