From bcd1c41aa1d359b97f5404b9be176011fe69b377 Mon Sep 17 00:00:00 2001 From: Tomek Date: Thu, 3 Sep 2026 11:35:07 +0200 Subject: [PATCH] Scroll to top when selecting current page in search (#4578) Co-authored-by: Claude Haiku 4.5 --- .changeset/rnd-12673-search-current-page.md | 5 +++++ .../src/components/Search/SearchResults.tsx | 21 +++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .changeset/rnd-12673-search-current-page.md diff --git a/.changeset/rnd-12673-search-current-page.md b/.changeset/rnd-12673-search-current-page.md new file mode 100644 index 000000000..6ae529ef7 --- /dev/null +++ b/.changeset/rnd-12673-search-current-page.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Scroll to the top when selecting a search result for the page already being viewed. diff --git a/packages/gitbook/src/components/Search/SearchResults.tsx b/packages/gitbook/src/components/Search/SearchResults.tsx index 453a3c167..b7520a4ec 100644 --- a/packages/gitbook/src/components/Search/SearchResults.tsx +++ b/packages/gitbook/src/components/Search/SearchResults.tsx @@ -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 + ) => { + 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'); }