{"id":24941429,"url":"https://github.com/melanieseltzer/react-use-caret-position","last_synced_at":"2025-06-29T23:34:26.334Z","repository":{"id":35076152,"uuid":"203710526","full_name":"melanieseltzer/react-use-caret-position","owner":"melanieseltzer","description":"Custom React hook to keep track of the caret position in an input","archived":false,"fork":false,"pushed_at":"2022-10-24T21:14:22.000Z","size":604,"stargazers_count":21,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T11:47:50.043Z","etag":null,"topics":["hacktoberfest","hooks","react","react-hooks","reactjs","typescript"],"latest_commit_sha":null,"homepage":"https://react-use-caret-position.netlify.app","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/melanieseltzer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-22T03:52:24.000Z","updated_at":"2025-01-05T00:51:57.000Z","dependencies_parsed_at":"2023-01-15T13:30:20.776Z","dependency_job_id":null,"html_url":"https://github.com/melanieseltzer/react-use-caret-position","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melanieseltzer%2Freact-use-caret-position","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melanieseltzer%2Freact-use-caret-position/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melanieseltzer%2Freact-use-caret-position/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melanieseltzer%2Freact-use-caret-position/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/melanieseltzer","download_url":"https://codeload.github.com/melanieseltzer/react-use-caret-position/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248148224,"owners_count":21055547,"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":["hacktoberfest","hooks","react","react-hooks","reactjs","typescript"],"created_at":"2025-02-02T18:22:50.419Z","updated_at":"2025-04-10T03:04:09.312Z","avatar_url":"https://github.com/melanieseltzer.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  🎣\n  \u003cbr /\u003e\n  react-use-caret-position\n\u003c/h1\u003e\n\n[![npm (scoped)](https://img.shields.io/npm/v/react-use-caret-position.svg)](https://www.npmjs.com/package/react-use-caret-position) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)\n\nLive demo: https://react-use-caret-position.netlify.app\n\n## Motivation\n\nIf there is some side effect/async code in an input's `onChange` event handler (e.g. dispatching a Redux action without doing anything else, format text in some way like uppercase, etc), the caret will annoyingly be booted to the end of the input after each keystroke.\n\n`react-use-caret-position` fixes this undesirable behavior by keeping track of the caret position and setting it on render.\n\n## Install\n\nWith npm...\n\n```bash\nnpm install react-use-caret-position\n```\n\nWith yarn...\n\n```bash\nyarn add react-use-caret-position\n```\n\n## Usage\n\n\u003c!-- prettier-ignore --\u003e\n```js\nimport React, { useState } from 'react';\nimport { useCaretPosition } from 'react-use-caret-position';\n\nconst Input = () =\u003e {\n  const [text, setText] = useState('hello world');\n\n  const { ref, updateCaret } = useCaretPosition();\n\n  const handleChange = e =\u003e {\n    // Some side effect in the `onChange` handler (could be anything)\n    const inputToUpperCase = e.target.value.toUpperCase();\n\n    setText(inputToUpperCase);\n\n    // Track the caret position with the hook\n    updateCaret();\n  };\n\n  return (\n    \u003cinput \n      ref={ref}\n      value={value} \n      onChange={handleChange} \n    /\u003e\n  );\n};\n\nexport default Input;\n```\n\n## API\n\n\u003c!-- prettier-ignore --\u003e\n```js\nconst { ref, start, end, updateCaret } = useCaretPosition();\n```\n\n### Return Object\n\n| Key           | Type              | Description                                  |\n| :------------ | :---------------- | :------------------------------------------- |\n| `ref`         | `React.RefObject` | Ref that should be assigned to your input    |\n| `start`       | `number`          | Current start position of the text selection |\n| `end`         | `number`          | Current end position of the text selection   |\n| `updateCaret` | `() =\u003e void`      | Function to set the caret position           |\n\n## License\n\nMIT © [melanieseltzer](https://github.com/melanieseltzer)\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\"\u003e\u003ca href=\"https://aaronross.tech\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/18172185?v=4?s=100\" width=\"100px;\" alt=\"Aaron Ross\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAaron Ross\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/melanieseltzer/react-use-caret-position/commits?author=superhawk610\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/melanieseltzer/react-use-caret-position/commits?author=superhawk610\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/bin1357\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/6586467?v=4?s=100\" width=\"100px;\" alt=\"Евгений\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eЕвгений\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/melanieseltzer/react-use-caret-position/commits?author=bin1357\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-restore --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelanieseltzer%2Freact-use-caret-position","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmelanieseltzer%2Freact-use-caret-position","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelanieseltzer%2Freact-use-caret-position/lists"}