Skip to content

Commit 7b0b7e9

Browse files
author
Dimitri POSTOLOV
authoredJul 20, 2024··
[v3] fix page scroll jump while entering characters in the search input (#2984)
aa
1 parent 97c59d5 commit 7b0b7e9

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed
 

‎.changeset/hot-readers-sell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"nextra-theme-docs": patch
3+
---
4+
5+
fix page scroll jump while entering characters in the search input

‎.eslintrc.cjs

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ module.exports = {
6262
'prefer-const': ['error', { destructuring: 'all' }],
6363
'unicorn/prefer-array-index-of': 'error',
6464
'sonarjs/no-unused-collection': 'error',
65+
'unicorn/catch-error-name': 'error',
66+
'unicorn/prefer-optional-catch-binding': 'error',
6567
// todo: enable
6668
'@typescript-eslint/no-explicit-any': 'off',
6769
'@typescript-eslint/no-non-null-assertion': 'off',

‎packages/nextra-theme-docs/src/components/flexsearch.tsx

+12-27
Original file line numberDiff line numberDiff line change
@@ -244,35 +244,20 @@ export function Flexsearch({
244244
)
245245
}
246246

247-
const preload = useCallback(
248-
async (active: boolean) => {
249-
if (active && !indexes[locale]) {
250-
setLoading(true)
251-
try {
252-
await loadIndexes(basePath, locale)
253-
} catch (e) {
254-
setError(true)
255-
}
256-
setLoading(false)
257-
}
258-
},
259-
[locale, basePath]
260-
)
247+
const preload = useCallback(async () => {
248+
if (indexes[locale]) return
249+
setLoading(true)
250+
try {
251+
await loadIndexes(basePath, locale)
252+
} catch {
253+
setError(true)
254+
}
255+
setLoading(false)
256+
}, [locale, basePath])
261257

262-
const handleChange = async (value: string) => {
258+
const handleChange = (value: string) => {
263259
setSearch(value)
264-
if (loading) {
265-
return
266-
}
267-
if (!indexes[locale]) {
268-
setLoading(true)
269-
try {
270-
await loadIndexes(basePath, locale)
271-
} catch (e) {
272-
setError(true)
273-
}
274-
setLoading(false)
275-
}
260+
if (loading) return
276261
doSearch(value)
277262
}
278263

‎packages/nextra-theme-docs/src/components/search.tsx

+21-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import NextLink from 'next/link'
55
import { useRouter } from 'next/router'
66
import { useMounted } from 'nextra/hooks'
77
import { InformationCircleIcon, SpinnerIcon } from 'nextra/icons'
8-
import type { CompositionEvent, KeyboardEvent, ReactElement } from 'react'
8+
import type {
9+
CompositionEvent,
10+
FocusEventHandler,
11+
KeyboardEvent,
12+
ReactElement
13+
} from 'react'
914
import { Fragment, useCallback, useEffect, useRef, useState } from 'react'
1015
import { useMenu, useThemeConfig } from '../contexts'
1116
import type { SearchResult } from '../types'
@@ -17,7 +22,7 @@ type SearchProps = {
1722
overlayClassName?: string
1823
value: string
1924
onChange: (newValue: string) => void
20-
onActive?: (active: boolean) => void
25+
onActive?: () => void
2126
loading?: boolean
2227
error?: boolean
2328
results: SearchResult[]
@@ -193,6 +198,18 @@ export function Search({
193198
[]
194199
)
195200

201+
const handleFocus = useCallback<FocusEventHandler>(
202+
event => {
203+
const isFocus = event.type === 'focus'
204+
const htmlStyle = document.documentElement.style
205+
// Fixes page scroll jump https://github.com/shuding/nextra/issues/2840
206+
htmlStyle.scrollPaddingTop = isFocus ? '0' : 'var(--nextra-navbar-height)'
207+
if (isFocus) onActive?.()
208+
setFocused(isFocus)
209+
},
210+
[onActive]
211+
)
212+
196213
return (
197214
<div className={cn('nextra-search _relative md:_w-64', className)}>
198215
{renderList && (
@@ -207,13 +224,8 @@ export function Search({
207224
onChange(value)
208225
setShow(Boolean(value))
209226
}}
210-
onFocus={() => {
211-
onActive?.(true)
212-
setFocused(true)
213-
}}
214-
onBlur={() => {
215-
setFocused(false)
216-
}}
227+
onFocus={handleFocus}
228+
onBlur={handleFocus}
217229
onCompositionStart={handleComposition}
218230
onCompositionEnd={handleComposition}
219231
type="search"

‎packages/nextra/src/server/compile.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ export async function compileMdx(
200200
...(searchIndexKey !== null && { searchIndexKey, structurizedData }),
201201
frontMatter
202202
}
203-
} catch (err) {
203+
} catch (error) {
204204
console.error(`[nextra] Error compiling ${filePath}.`)
205-
throw err
205+
throw error
206206
}
207207

208208
function createCompiler(): Processor {

0 commit comments

Comments
 (0)
Please sign in to comment.