Scroll to top when selecting current page in search (#4578)

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Tomek
2026-09-03 11:35:07 +02:00
committed by GitHub
parent 77058e3f17
commit bcd1c41aa1
2 changed files with 20 additions and 6 deletions
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Scroll to the top when selecting a search result for the page already being viewed.
@@ -198,14 +198,23 @@ export const SearchResults = React.forwardRef(function SearchResults(
const itemKey = getResultKey(item);
const shouldAnimateItem =
shouldAnimateResults || !seenResultKeys.current.has(itemKey);
const handleResultSelect = () => {
const handleResultSelect = (
event: React.MouseEvent<HTMLAnchorElement>
) => {
const isPageResult =
item.type === 'local-page' ||
item.type === 'page' ||
item.type === 'record';
if (
query &&
siteSpaceId &&
(item.type === 'local-page' ||
item.type === 'page' ||
item.type === 'record')
isPageResult &&
!event.currentTarget.hash &&
event.currentTarget.pathname === window.location.pathname
) {
window.scrollTo({ top: 0, behavior: 'smooth' });
}
if (query && siteSpaceId && isPageResult) {
addRecentSearchQuery(siteSpaceId, query, 'search');
}