Giter Site home page Giter Site logo

jh3y / use-caret-position Goto Github PK

View Code? Open in Web Editor NEW
34.0 3.0 3.0 102 KB

A custom React hook for grabbing the caret position in a text input.

Home Page: https://use-caret-position.netlify.app/

JavaScript 81.94% HTML 1.96% CSS 16.10%
hacktoberfest react reactjs hooks hooks-api-react caret dom dom-manipulation ui ui-components

use-caret-position's Introduction

use-caret-position

A custom React hook for grabbing the caret position in an input field.

Install

npm i use-caret-position
yarn add use-caret-position

Usage

Refer to src/app/index.js for common usage examples.

At a high-level, you pass the hook an input ref.

import useCaretPosition from 'use-caret-position'
const { x, y, getPosition, getSelection } = useCaretPosition(inputRef)
<input type="text" ref="inputRef" onInput{() => getPosition(inputRef)} />
<span style={{ "--x": x, "--y": y }}>I'm the marker</span>

You get an x and y coordinate that you can use to display a marker with. I'd recommend using CSS variables for this. This is what the demos do. getPosition and getSelection are functions you can trigger whenever you need to update the x and y positions.

getPosition returns the position at selectionStart for an input. getSelection returns a midpoint between selectionStart and selectionEnd for an input. It's tricky to provide an exact accurate x and y for the caret in all scenarios so often you might need to adjust the positioning slightly with CSS transforms.

Development

Package is built with parcel-bundler. To get to work, use yarn dev.

To work with the test app, you'll also need to run yarn and yarn dev from within src/app.

To work with the published node module directly, use npm link in the root of the repo.

Then use npm link use-caret-position within src/app.


Made in haste by @jh3y 2020 ๐Ÿ˜…

use-caret-position's People

Contributors

jh3y avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.