Skip to content

Commit 268ad4c

Browse files
committed
Button block: hide link popover if not in visual edit mode
1 parent da9b4ae commit 268ad4c

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

  • packages/block-library/src/button

‎packages/block-library/src/button/edit.js‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import classnames from 'classnames';
88
*/
99
import { __ } from '@wordpress/i18n';
1010
import { useCallback, useState, useRef } from '@wordpress/element';
11+
import { useSelect } from '@wordpress/data';
1112
import {
1213
Button,
1314
ButtonGroup,
@@ -23,6 +24,7 @@ import {
2324
InspectorControls,
2425
InspectorAdvancedControls,
2526
RichText,
27+
store as blockEditorStore,
2628
useBlockProps,
2729
__experimentalLinkControl as LinkControl,
2830
__experimentalUseEditorFeature as useEditorFeature,
@@ -100,7 +102,7 @@ function WidthPanel( { selectedWidth, setAttributes } ) {
100102
}
101103

102104
function URLPicker( {
103-
isSelected,
105+
mayOpen,
104106
url,
105107
setAttributes,
106108
opensInNewTab,
@@ -109,7 +111,6 @@ function URLPicker( {
109111
} ) {
110112
const [ isURLPickerOpen, setIsURLPickerOpen ] = useState( false );
111113
const urlIsSet = !! url;
112-
const urlIsSetandSelected = urlIsSet && isSelected;
113114
const openLinkControl = () => {
114115
setIsURLPickerOpen( true );
115116
return false; // prevents default behaviour for event
@@ -122,7 +123,7 @@ function URLPicker( {
122123
} );
123124
setIsURLPickerOpen( false );
124125
};
125-
const linkControl = ( isURLPickerOpen || urlIsSetandSelected ) && (
126+
const linkControl = ( isURLPickerOpen || urlIsSet ) && mayOpen && (
126127
<Popover
127128
position="bottom center"
128129
onClose={ () => setIsURLPickerOpen( false ) }
@@ -156,7 +157,7 @@ function URLPicker( {
156157
onClick={ openLinkControl }
157158
/>
158159
) }
159-
{ urlIsSetandSelected && (
160+
{ urlIsSet && (
160161
<ToolbarButton
161162
name="link"
162163
icon={ linkOff }
@@ -167,7 +168,7 @@ function URLPicker( {
167168
/>
168169
) }
169170
</BlockControls>
170-
{ isSelected && (
171+
{ mayOpen && (
171172
<KeyboardShortcuts
172173
bindGlobal
173174
shortcuts={ {
@@ -186,6 +187,7 @@ function ButtonEdit( props ) {
186187
attributes,
187188
setAttributes,
188189
className,
190+
clientId,
189191
isSelected,
190192
onReplace,
191193
mergeBlocks,
@@ -231,6 +233,14 @@ function ButtonEdit( props ) {
231233
setAttributes( { text: newText.replace( /<\/?a[^>]*>/g, '' ) } );
232234
};
233235

236+
const isVisualEditMode = useSelect( ( select ) => {
237+
const store = select( blockEditorStore );
238+
return (
239+
'visual' === store.getBlockMode( clientId ) &&
240+
! store.isNavigationMode()
241+
);
242+
}, [] );
243+
234244
const colorProps = getColorAndStyleProps( attributes, colors, true );
235245
const ref = useRef();
236246
const blockProps = useBlockProps( { ref } );
@@ -277,7 +287,7 @@ function ButtonEdit( props ) {
277287
<URLPicker
278288
url={ url }
279289
setAttributes={ setAttributes }
280-
isSelected={ isSelected }
290+
mayOpen={ isVisualEditMode && isSelected }
281291
opensInNewTab={ linkTarget === '_blank' }
282292
onToggleOpenInNewTab={ onToggleOpenInNewTab }
283293
anchorRef={ ref }

0 commit comments

Comments
 (0)