# use-text-selection

> A hook for getting the state of your current text selection

Latest version **1.1.5** (published 2021-11-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-text-selection
pnpm add use-text-selection
yarn add use-text-selection
bun add use-text-selection
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.5 |
| Published | 2021-11-09 |
| First published | 2021-01-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 40 |
| Author | Julian Krispel-Samsel |
| Maintainers | juliankrispel |

## Links

- npm: https://www.npmjs.com/package/use-text-selection
- Repository: https://github.com/juliankrispel/use-text-selection
- Homepage: https://juliankrispel.github.io/use-text-selection/
- Issues: https://github.com/juliankrispel/use-text-selection/issues
- npm.io page: https://npm.io/package/use-text-selection

## Dependencies (1)

- [parcel](https://npm.io/package/parcel.md) ^2.0.0-beta.2

## Recent versions

- 1.1.5 (latest) — 2021-11-09
- 1.1.4 — 2021-11-09
- 1.1.3 — 2021-02-05
- 1.1.2 — 2021-02-02
- 1.1.1 — 2021-02-02
- 1.1.0 — 2021-02-02
- 1.0.0 — 2021-01-31

## README

# use-text-selection

[React hook](https://reactjs.org/docs/hooks-reference.html) for tracking the state of the current text selection.

Useful when building any kind of UI that is displayed in relation to your text-selection. For example:
- Floating toolbars for richt text editing or content sharing.
- Autocomplete/suggestion featues in editors.

## Basic usage

```tsx
import { useRef } from 'react'
import { useTextSelection } from 'use-text-selection'

export const AutoComplete = () => {
  const { clientRect, isCollapsed } = useTextSelection()
  // to constrain text selection events to an element
  // just add the element as a an argument like `useTextSelection(myElement)`

  if (clientRect == null) return null

  return (
    <div
      style={{
        left: clientRect.x,
        top: clientRect.y + clientRect.height,
        position: 'absolute',
      }}
    >
      Autocomplete
    </div>
  );
}}
```

### Constraining text selection events to an element

`useTextSelection` takes one argument called, which would be a dom element which constrains updates to inside the dom element.

Retrieve a reference to this dom element with the `querySelector` api or with React refs (recommended).

Here's an example:

```tsx
const MyTextSelectionComponent = () => {
  const [ref, setRef] = useRef()
  const { clientRect, isCollapsed } = useTextSelection(ref.current)

  if (clientRect == null) return null

  return (
    <>
      <div ref={(el) => setRef(el)}>
        <MyOtherComponent>
      </div>

      <div
        style={{
          left: clientRect.x,
          top: clientRect.y + clientRect.height,
          position: 'absolute',
        }}
      >
        Autocomplete
      </div>
    </>
  );
}
```

## Work with me?

I build editors for companies, or help their teams do so. Hit me up on [my website](http://jkrsp.com) to get in touch about a project.

---
_Source: https://npm.io/package/use-text-selection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
