useScrollToBlock

Returns a function that scrolls the viewport to a block, such as jumping to a search hit or a form field.

const scrollToBlock = useScrollToBlock()

scrollToBlock(blockId, options?)
  • blockId is a block id from the SDK, such as a widget id from useWidgets. An unknown or deleted id throws InvalidInputError; with no document loaded, the call throws DocumentNotLoadedError.
  • options are the same alignment options as in useScrollToPage, applied to the block's bounds instead of the page.

Example

import { useScrollToBlock, useWidgets } from '@sdk-pdf/react'

function useScrollToFirstEmptyField() {
  const { widgets } = useWidgets()
  const scrollToBlock = useScrollToBlock()

  return (fieldName: string) => {
    const widget = widgets.find((widget) => widget.fieldName === fieldName)
    if (widget) scrollToBlock(widget.id, { behavior: 'smooth', vertical: 'center' })
  }
}

On this page