Skip to main content

selectionapi

Read, clear, and set text selection via the Selection API.

Import: @rtorcato/browser-common/selectionapi

📖 MDN: Selection

Example

import {
isSelectionApiAvailable,
getSelectedText,
clearSelection,
selectElementText,
} from '@rtorcato/browser-common/selectionapi'

if (isSelectionApiAvailable()) {
const text = getSelectedText()
clearSelection()
selectElementText(document.querySelector('#code')!)
}

Exports

  • isSelectionApiAvailable() — feature check
  • getSelection() — the current Selection object, or null
  • getSelectedText() — the currently selected text, or ''
  • clearSelection() — clears the current selection
  • selectElementText(element) — selects the text content of an element

See the API reference for full signatures.