useScrollToPage

Returns a function that scrolls the viewport to a page.

const scrollToPage = useScrollToPage()

scrollToPage(pageId, options?)
  • pageId is an id obtained from usePages. An unknown or deleted id throws InvalidInputError; with no document loaded, the call throws DocumentNotLoadedError.
  • options controls where the page lands in the viewport:
OptionValuesDefaultWhat it does
behavior'auto' | 'smooth' | 'instant''auto'The scroll animation, as in native scrolling.
vertical'top' | 'center' | 'bottom' | 'nearest''top'Which vertical viewport edge the page aligns to.
horizontal'left' | 'center' | 'right' | 'nearest''nearest'Which horizontal viewport edge the page aligns to.

The edges are physical: they don't change with the writing mode or text direction.

Example

const scrollToPage = useScrollToPage()

scrollToPage(page.id, { behavior: 'smooth', vertical: 'center' })

On this page