GithubHelp home page GithubHelp logo

juliankrispel / use-text-selection Goto Github PK

View Code? Open in Web Editor NEW
35.0 2.0 6.0 734 KB

React hook for tracking text selection

Home Page: https://juliankrispel.github.io/use-text-selection/

License: MIT License

CSS 1.06% HTML 5.47% TypeScript 93.47%
react editor wysiwyg-editor text-editor slatejs draft-js ui hooks

use-text-selection's Introduction

use-text-selection

React hook 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

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:

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 to get in touch about a project.

use-text-selection's People

Contributors

juliankrispel 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  avatar  avatar

Watchers

 avatar  avatar

use-text-selection's Issues

Parcel as non-dev dependency

In this commit 683fea2 Parcel moved from a dev dependency to a normal one. The commit message says it was an upgrade but the version didn't change. Mistake?

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.