Skip to content

Commit

Permalink
Add unlink URL in buttons block (#23445)
Browse files Browse the repository at this point in the history
* Deactivate link in buttons

* bind unlink shortcut
  • Loading branch information
ntsekouras authored Jun 25, 2020
1 parent 8c2cdfd commit 6c878a8
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
__experimentalLinkControl as LinkControl,
} from '@wordpress/block-editor';
import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { link } from '@wordpress/icons';
import { link, linkOff } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -70,13 +70,21 @@ function URLPicker( {
onToggleOpenInNewTab,
} ) {
const [ isURLPickerOpen, setIsURLPickerOpen ] = useState( false );
const urlIsSet = !! url;
const urlIsSetandSelected = urlIsSet && isSelected;
const openLinkControl = () => {
setIsURLPickerOpen( true );

// prevents default behaviour for event
return false;
return false; // prevents default behaviour for event
};
const unlinkButton = () => {
setAttributes( {
url: undefined,
linkTarget: undefined,
rel: undefined,
} );
setIsURLPickerOpen( false );
};
const linkControl = isURLPickerOpen && (
const linkControl = ( isURLPickerOpen || urlIsSetandSelected ) && (
<Popover
position="bottom center"
onClose={ () => setIsURLPickerOpen( false ) }
Expand All @@ -101,20 +109,33 @@ function URLPicker( {
<>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
name="link"
icon={ link }
title={ __( 'Link' ) }
shortcut={ displayShortcut.primary( 'k' ) }
onClick={ openLinkControl }
/>
{ ! urlIsSet && (
<ToolbarButton
name="link"
icon={ link }
title={ __( 'Link' ) }
shortcut={ displayShortcut.primary( 'k' ) }
onClick={ openLinkControl }
/>
) }
{ urlIsSetandSelected && (
<ToolbarButton
name="link"
icon={ linkOff }
title={ __( 'Unlink' ) }
shortcut={ displayShortcut.primaryShift( 'k' ) }
onClick={ unlinkButton }
isActive={ true }
/>
) }
</ToolbarGroup>
</BlockControls>
{ isSelected && (
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 'k' ) ]: openLinkControl,
[ rawShortcut.primaryShift( 'k' ) ]: unlinkButton,
} }
/>
) }
Expand Down

0 comments on commit 6c878a8

Please sign in to comment.