From c34be01150d86aa697c09be58ab2bf24aa13af7b Mon Sep 17 00:00:00 2001 From: retrofox Date: Tue, 5 Nov 2019 11:34:25 -0300 Subject: [PATCH] navigation-menu-item: cleanup timer once unmount --- .../block-library/src/navigation-menu-item/edit.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation-menu-item/edit.js b/packages/block-library/src/navigation-menu-item/edit.js index a69333ff3eb1a..9a0273c5de3c5 100644 --- a/packages/block-library/src/navigation-menu-item/edit.js +++ b/packages/block-library/src/navigation-menu-item/edit.js @@ -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. @@ -89,6 +91,13 @@ function NavigationMenuItemEdit( { if ( ! isSelected ) { setIsLinkOpen( false ); } + + return () => { + // Clear LinkControl.OnClose timeout. + if ( onCloseTimerId ) { + clearTimeout( onCloseTimerId ); + } + }; }, [ isSelected ] ); /** @@ -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 ) } />