Skip to content

Commit

Permalink
navigation-menu-item: cleanup timer once unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
retrofox authored and talldan committed Nov 6, 2019
1 parent f60ae05 commit c34be01
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/block-library/src/navigation-menu-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ function NavigationMenuItemEdit( {
const link = title ? { title, url } : null;
const [ isLinkOpen, setIsLinkOpen ] = useState( ! label && isSelected );

let onCloseTimerId = null;

/**
* It's a kind of hack to handle closing the LinkControl popover
* clicking on the ToolbarButton link.
Expand All @@ -89,6 +91,13 @@ function NavigationMenuItemEdit( {
if ( ! isSelected ) {
setIsLinkOpen( false );
}

return () => {
// Clear LinkControl.OnClose timeout.
if ( onCloseTimerId ) {
clearTimeout( onCloseTimerId );
}
};
}, [ isSelected ] );

/**
Expand Down Expand Up @@ -205,7 +214,9 @@ function NavigationMenuItemEdit( {
onKeyPress={ ( event ) => event.stopPropagation() }
currentLink={ link }
onLinkChange={ updateLink( setAttributes, label ) }
onClose={ () => setTimeout( () => setIsLinkOpen( false ), 100 ) }
onClose={ () => {
onCloseTimerId = setTimeout( () => setIsLinkOpen( false ), 100 );
} }
currentSettings={ { 'new-tab': opensInNewTab } }
onSettingsChange={ updateLinkSetting( setAttributes ) }
/>
Expand Down

0 comments on commit c34be01

Please sign in to comment.