From 10471090f5e70e003ac5aa2c368536df6451d080 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Thu, 7 Sep 2023 12:00:13 +0800 Subject: [PATCH] More accurate isActive --- src/components/link.jsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/components/link.jsx b/src/components/link.jsx index 04793831..ce82fec0 100644 --- a/src/components/link.jsx +++ b/src/components/link.jsx @@ -19,7 +19,18 @@ const Link = forwardRef((props, ref) => { let hash = (location.hash || '').replace(/^#/, '').trim(); if (hash === '') hash = '/'; const { to, ...restProps } = props; - // TODO: maybe better pass hash into URL to deconstruct the pathname and search, then decodeURIComponent them + + // Handle encodeURIComponent of searchParams values + if (!!hash && hash !== '/') { + const parsedHash = new URL(hash, location.origin); // Fake base URL + if (parsedHash.searchParams.size) { + const searchParamsStr = Array.from(parsedHash.searchParams.entries()) + .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) + .join('&'); + hash = parsedHash.pathname + '?' + searchParamsStr; + } + } + const isActive = hash === to || decodeURIComponent(hash) === to; return (