useFocusSelection

Returns a function that puts the focus back into the selection. Clicking a toolbar button moves the browser focus out of the text being edited; call this after acting on the selection so the user can continue typing.

const focusSelection = useFocusSelection()

focusSelection()

When the selection is not an on-screen text selection, the function blurs the active element instead, which closes open toolbar dropdowns.

Example

import { useFocusSelection, useSelectionTextStyle } from '@sdk-pdf/react'

function BoldButton() {
  const { isBold } = useSelectionTextStyle()
  const focusSelection = useFocusSelection()

  return (
    <button
      aria-pressed={isBold.value}
      onClick={() => {
        isBold.set(!isBold.value)
        // Return the focus so the user keeps typing in the paragraph.
        focusSelection()
      }}
    >
      Bold
    </button>
  )
}

On this page