{"id":16534106,"url":"https://github.com/juliankrispel/use-text-selection","last_synced_at":"2025-07-10T00:02:08.967Z","repository":{"id":46126817,"uuid":"334641809","full_name":"juliankrispel/use-text-selection","owner":"juliankrispel","description":"React hook for tracking text selection","archived":false,"fork":false,"pushed_at":"2023-10-04T06:03:47.000Z","size":734,"stargazers_count":39,"open_issues_count":3,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-02T00:47:42.230Z","etag":null,"topics":["draft-js","editor","hooks","react","slatejs","text-editor","ui","wysiwyg-editor"],"latest_commit_sha":null,"homepage":"https://juliankrispel.github.io/use-text-selection/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juliankrispel.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-31T11:44:43.000Z","updated_at":"2025-02-20T06:46:29.000Z","dependencies_parsed_at":"2024-06-18T22:58:36.362Z","dependency_job_id":"b36c1006-56dc-4fec-8283-700661f35396","html_url":"https://github.com/juliankrispel/use-text-selection","commit_stats":{"total_commits":14,"total_committers":2,"mean_commits":7.0,"dds":0.1428571428571429,"last_synced_commit":"4662c3a5e416f4f7a5faf3b9c7e53d332189d2ca"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/juliankrispel/use-text-selection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliankrispel%2Fuse-text-selection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliankrispel%2Fuse-text-selection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliankrispel%2Fuse-text-selection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliankrispel%2Fuse-text-selection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juliankrispel","download_url":"https://codeload.github.com/juliankrispel/use-text-selection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliankrispel%2Fuse-text-selection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264505733,"owners_count":23618962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["draft-js","editor","hooks","react","slatejs","text-editor","ui","wysiwyg-editor"],"created_at":"2024-10-11T18:16:52.340Z","updated_at":"2025-07-10T00:02:08.919Z","avatar_url":"https://github.com/juliankrispel.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-text-selection\n\n[React hook](https://reactjs.org/docs/hooks-reference.html) for tracking the state of the current text selection.\n\nUseful when building any kind of UI that is displayed in relation to your text-selection. For example:\n- Floating toolbars for richt text editing or content sharing.\n- Autocomplete/suggestion featues in editors.\n\n## Basic usage\n\n```tsx\nimport { useRef } from 'react'\nimport { useTextSelection } from 'use-text-selection'\n\nexport const AutoComplete = () =\u003e {\n  const { clientRect, isCollapsed } = useTextSelection()\n  // to constrain text selection events to an element\n  // just add the element as a an argument like `useTextSelection(myElement)`\n\n  if (clientRect == null) return null\n\n  return (\n    \u003cdiv\n      style={{\n        left: clientRect.x,\n        top: clientRect.y + clientRect.height,\n        position: 'absolute',\n      }}\n    \u003e\n      Autocomplete\n    \u003c/div\u003e\n  );\n}}\n```\n\n### Constraining text selection events to an element\n\n`useTextSelection` takes one argument called, which would be a dom element which constrains updates to inside the dom element.\n\nRetrieve a reference to this dom element with the `querySelector` api or with React refs (recommended).\n\nHere's an example:\n\n```tsx\nconst MyTextSelectionComponent = () =\u003e {\n  const [ref, setRef] = useRef()\n  const { clientRect, isCollapsed } = useTextSelection(ref.current)\n\n  if (clientRect == null) return null\n\n  return (\n    \u003c\u003e\n      \u003cdiv ref={(el) =\u003e setRef(el)}\u003e\n        \u003cMyOtherComponent\u003e\n      \u003c/div\u003e\n\n      \u003cdiv\n        style={{\n          left: clientRect.x,\n          top: clientRect.y + clientRect.height,\n          position: 'absolute',\n        }}\n      \u003e\n        Autocomplete\n      \u003c/div\u003e\n    \u003c/\u003e\n  );\n}\n```\n\n## Work with me?\n\nI 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliankrispel%2Fuse-text-selection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliankrispel%2Fuse-text-selection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliankrispel%2Fuse-text-selection/lists"}