useSelectedBlocksGetter

Returns a function that reads the currently selected blocks as snapshots, without subscribing the component to changes. Use it in event handlers, such as a toolbar button or a hotkey; to render the selection, use useSelectedBlocks.

const getSelectedBlocks = useSelectedBlocksGetter()

const selectedBlocks = getSelectedBlocks() // Block[]
  • The entries are the same Block snapshots as in useSelectedBlocks, read at the moment of the call.
  • With no document loaded, the function returns an empty array.

Example

import { useSelectedBlocksGetter, useSetWidgetProps } from '@sdk-pdf/react'

function LockSelectedFieldsButton() {
  const getSelectedBlocks = useSelectedBlocksGetter()
  const setWidgetProps = useSetWidgetProps()

  const handleClick = () => {
    const widgetIds = getSelectedBlocks()
      .filter((block) => block.type === 'annotation' && block.subtype === 'Widget')
      .map((block) => block.id)

    void setWidgetProps(widgetIds, { isReadOnly: true })
  }

  return <button onClick={handleClick}>Lock selected fields</button>
}

On this page