Skip to content

Commit

Permalink
Alt approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Mar 30, 2021
1 parent 33e8edc commit 7f8cc7e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
19 changes: 14 additions & 5 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ function URLPicker( {
onToggleOpenInNewTab,
anchorRef,
} ) {
const toggleButtonRef = useRef();
const [ isURLPickerOpen, setIsURLPickerOpen ] = useState( false );
const urlIsSet = !! url;
const urlIsSetandSelected = urlIsSet && isSelected;
const openLinkControl = () => {
setIsURLPickerOpen( true );
const toggleLinkControl = () => {
setIsURLPickerOpen( ( value ) => ! value );
return false; // prevents default behaviour for event
};
const unlinkButton = () => {
Expand All @@ -125,7 +126,14 @@ function URLPicker( {
const linkControl = ( isURLPickerOpen || urlIsSetandSelected ) && (
<Popover
position="bottom center"
onClose={ () => setIsURLPickerOpen( false ) }
onClose={ () => {
if (
toggleButtonRef.current !==
toggleButtonRef.current.ownerDocument.activeElement
) {
setIsURLPickerOpen( false );
}
} }
anchorRef={ anchorRef?.current }
>
<LinkControl
Expand Down Expand Up @@ -153,7 +161,8 @@ function URLPicker( {
icon={ link }
title={ __( 'Link' ) }
shortcut={ displayShortcut.primary( 'k' ) }
onClick={ openLinkControl }
onClick={ toggleLinkControl }
ref={ toggleButtonRef }
/>
) }
{ urlIsSetandSelected && (
Expand All @@ -171,7 +180,7 @@ function URLPicker( {
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 'k' ) ]: openLinkControl,
[ rawShortcut.primary( 'k' ) ]: toggleLinkControl,
[ rawShortcut.primaryShift( 'k' ) ]: unlinkButton,
} }
/>
Expand Down
16 changes: 13 additions & 3 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export default function NavigationLinkEdit( {
const isDraggingWithin = useIsDraggingWithin( listItemRef );
const itemLabelPlaceholder = __( 'Add link…' );
const ref = useRef();
const toggleButtonRef = useRef();

const {
isParentOfSelectedBlock,
Expand Down Expand Up @@ -370,15 +371,16 @@ export default function NavigationLinkEdit( {
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 'k' ) ]: () =>
setIsLinkOpen( true ),
setIsLinkOpen( ( value ) => ! value ),
} }
/>
<ToolbarButton
name="link"
icon={ linkIcon }
title={ __( 'Link' ) }
shortcut={ displayShortcut.primary( 'k' ) }
onClick={ () => setIsLinkOpen( true ) }
onClick={ () => setIsLinkOpen( ( value ) => ! value ) }
ref={ toggleButtonRef }
/>
<ToolbarButton
name="submenu"
Expand Down Expand Up @@ -468,7 +470,15 @@ export default function NavigationLinkEdit( {
{ isLinkOpen && (
<Popover
position="bottom center"
onClose={ () => setIsLinkOpen( false ) }
onClose={ () => {
if (
toggleButtonRef.current !==
toggleButtonRef.current.ownerDocument
.activeElement
) {
setIsLinkOpen( false );
}
} }
anchorRef={ listItemRef.current }
>
<KeyboardShortcuts
Expand Down
14 changes: 12 additions & 2 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { useState } from '@wordpress/element';
import { useState, useRef } from '@wordpress/element';
import { __experimentalGetSettings, dateI18n } from '@wordpress/date';
import {
AlignmentToolbar,
Expand Down Expand Up @@ -37,6 +37,7 @@ export default function PostDateEdit( { attributes, context, setAttributes } ) {
'date',
postId
);
const toggleButtonRef = useRef();
const [ isPickerOpen, setIsPickerOpen ] = useState( false );
const settings = __experimentalGetSettings();
// To know if the current time format is a 12 hour time, look for "a".
Expand Down Expand Up @@ -77,6 +78,7 @@ export default function PostDateEdit( { attributes, context, setAttributes } ) {
<ToolbarButton
icon={ edit }
title={ __( 'Change Date' ) }
dateToggleButton={ toggleButtonRef }
onClick={ () =>
setIsPickerOpen(
( _isPickerOpen ) => ! _isPickerOpen
Expand Down Expand Up @@ -112,7 +114,15 @@ export default function PostDateEdit( { attributes, context, setAttributes } ) {

{ isPickerOpen && (
<Popover
onClose={ setIsPickerOpen.bind( null, false ) }
onClose={ () => {
if (
toggleButtonRef.current !==
toggleButtonRef.current.ownerDocument
.activeElement
) {
setIsPickerOpen( false );
}
} }
>
<DateTimePicker
currentDate={ date }
Expand Down

0 comments on commit 7f8cc7e

Please sign in to comment.