From d60ce2a7a5dd307098db02b33e2c2becbf741856 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Tue, 14 Dec 2021 10:06:56 +0800 Subject: [PATCH] Don't refocus if the activeElement is still on the page --- .../components/routes/use-reset-focus-on-route-change.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/routes/use-reset-focus-on-route-change.js b/packages/edit-site/src/components/routes/use-reset-focus-on-route-change.js index aba2ab5efc723..15a134bd4ba8d 100644 --- a/packages/edit-site/src/components/routes/use-reset-focus-on-route-change.js +++ b/packages/edit-site/src/components/routes/use-reset-focus-on-route-change.js @@ -35,6 +35,11 @@ export default function useResetFocusOnRouteChange( targetRef ) { } } - targetRef.current?.focus(); + const activeElement = targetRef.current?.ownerDocument.activeElement; + + // Don't refocus if the activeElement is still on the page (like NavLink). + if ( ! activeElement || activeElement === document.body ) { + targetRef.current?.focus(); + } }, [ location, targetRef, history ] ); }