From 00d3b41c9f44f3d397b0461559d705732b10e17f Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 20 Nov 2024 10:14:50 +0100 Subject: [PATCH] make it work on nav --- packages/html-reporter/src/links.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/html-reporter/src/links.tsx b/packages/html-reporter/src/links.tsx index 14078aa625..24b548cc1b 100644 --- a/packages/html-reporter/src/links.tsx +++ b/packages/html-reporter/src/links.tsx @@ -134,13 +134,14 @@ export function useAnchor(id: AnchorID, onReveal: () => void) { const listener = () => { const params = new URLSearchParams(window.location.hash.slice(1)); - if (!params.has('anchor')) - return; - const anchor = params.get('anchor'); - if (matchesAnchor(id, anchor!)) + if (params.has('anchor') && matchesAnchor(id, params.get('anchor')!)) onReveal(); }; window.addEventListener('popstate', listener); + + // check if we're already on the anchor. required to make it work on page load. + listener(); + return () => window.removeEventListener('popstate', listener); }, [id, onReveal]); }