npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-popover-selector

v1.0.0

Published

A popover component positioned according to the current selection in a contenteditable element

Downloads

1

Readme

react-text-selection-popover

lil-demo

This module provides a way to render a popover/tooltip style component when text (inside a contenteditable element) is selected.

You'll need React.16.3 or higher to use this btw. If you strongly disagree please open an issue

NPM JavaScript Style Guide

Install

npm install --save react-text-selection-popover

Usage

The simplest way to use react-text-selection-popover is with 0 props just like this:

<Popover><MySweetComponent /></Popover>

This will display the popover when any text on the page is selected.

Restricting selection to a component

By default, any text within document (or in other words - the entire page) will trigger the Popover. To restrict it you can define the selectionRef prop. Just pass a React ref to it:

constructor(props) {
  super(props)
  this.ref = React.createRef()
}

render() {
  return <div>
    <p ref={this.ref}>This text will trigger the popover</p>
    <Popover selectionRef={this.ref}>Hello there</Popover>
  </div>
}

Positioning the Popover

Since 1.0, positioning is completely configurable via the placementStrategy prop. By default, the popover gets centered above or below the text selection (useful for inline toolbars in text editors like Medium) but you're not tied in to this behaviour.

Alternative strategies are in the lib folder of the package.

Here's how you use a placement strategy from the lib folder:

import Popover from 'react-text-selection-popover';
import placeRightBelow from 'react-text-selection-popover/lib/placeRightBelow'

<Popover
  placementStrategy={placeRightBelow}
>
  <!-- my awesome popover content here -->
</Popover

Writing your own placement strategy

You can also write your very own placement strategy. A placement strategy takes as an argument an object containg all the properties you need to compute the position of the popover and returns a style object as a result. See below for the exact shape of the Object

If you'd like to look at an example, just have a look at the default positioning strategy

Btw: If you have written a placement strategy that you want to share because you think it might be useful to others :), please open a pr and add it to the lib folder. I'd be more than happy to review it and help get it merged if there's a significant usecase.

Managing Popover display

By default, the popover automatically opens when text is selected and closes when text is unselected. However, on some occasions you might want to control this yourself.

To control whether the Popover is shown or not all you need to define is the isOpen prop. isOpen={true} will show the popover, isOpen={false} will hide it.

<Popover isOpen={this.state.isOpen}>Hey there</Popover>

You might still want to use selection events to control whether the Popover is shown or hidden. To do so - use the onTextSelect and onTextUnSelect prop.

<Popover
  isOpen={this.state.isOpen}
  onTextSelect={() => this.setState({ isOpen: true })}
  onTextUnselect={() => this.setState({ isOpen: false })}
>Hey there</Popover>

Using Popover inside a scrollable element

Some applications have scrollable elements inside them other than <body>. By default Popover repositions itself when the viewport scrolls (window that is). If you're using the Popover inside a scrollable element you need to define the scrollRef prop like so:

class MyApp extends Component {
  constructor() {
    this.scrollRef = React.createRef()
  }

  render() {
    return <div>
      <main ref={this.scrollRef}>
      </main>
      <Popover scrollRef={this.scrollRef}>My popover</Popover>
    </div>
  }
}

For more info on how to use the Popover component, please see below :)

<Popover /> Props

| Property | Type | required? | Description | | - | - | - | - | | selectionRef | React.ref | optional | Set this to constrain selection events to a dom element and its children. You need this especially if you use more than one Popover component (defaults to document) | | scrollRef | React.ref | optional | By default Popover repositions itself when there's a scroll event for document.body. Set this to reposition the Popover upon scrolling of a given element | | isOpen | Boolean | optional | Is the Popover visible or not (defaults to true) | | onTextSelect | Function | optional | Callback for when text is selected (typically used for setting state that opens the modal) | | onTextUnSelect | Function | optional | Callback for when selection collapses (typically used for setting state that closes the modal) | | className | String | optional | CSS class name for Popover container. | | gap | Number | optional | Pixel gap between text selection and popover - (defaults to 5) | | placementStrategy | ({ gap, windowWidth, windowHeight, topOffset, leftOffset, lineHeight, boxWidth, boxHeight }) => StyleObject | optional | A method that produces a style object to position the popover |

License

MIT © juliankrispel

Wanna collaborate?

Just ask me, I'll add you as collaborator! Any help I can get to make better software together I'll take it!

Shoutouts

This was originally written during some freelance work for Spectrum. Shoutout to their awesomeness for letting me do all my work for them in the open!

TODO:

write test for all states:

  • Test pointerEvents are correct
  • Test isOpen overrides anything internal
  • Test placement strategies
  • Test refs
  • Test scroll events
  • Test resize events
  • Test events being only inside selection ref