selectText.tsx 453 B

123456789101112131415161718
  1. /**
  2. * Select the text of the provided HTML Element
  3. */
  4. export function selectText(node: HTMLElement): void {
  5. if (node instanceof HTMLInputElement && node.type === 'text') {
  6. node.select();
  7. return;
  8. }
  9. if (node instanceof Node && window.getSelection) {
  10. const range = document.createRange();
  11. range.selectNode(node);
  12. const selection = window.getSelection();
  13. selection?.removeAllRanges();
  14. selection?.addRange(range);
  15. }
  16. }