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-use-popper

v0.4.1

Published

React Hook to add tooltip to any element

Downloads

6

Readme

React Popper Hook

React Hook to add tooltip to any element

NPM JavaScript Style Guide

React Popper is a low level react hook (along with a provider) to add all kinds of absolutely positioned poppers which are added to the target element via React.createPortal.

It identifies the target element (the one to which popper gets appended to), via the event.currentTarget (in case, it's not provided in the options to the usePopper) and calculates the positioning of the popper internally using a few inline styles, hence, does not have any dependency on any other styling library.

UsePopper Hook exposes togglePopper, showPopper and hidePopper callbacks to the consume/client which can be called in response to different MouseEvents. The Hook manages the open/close state of the popper internally, and therefore, togglePopper is the recommend way to use, and should be enough for most of the use cases. But if the consumer really wants to manage the open/close manually, then he may use a combination of showPopper and hidePopper callbacks.

Demo

Install

npm install --save react-use-popper

Setup

Wrap your root component with PopperProvider

// app.jsx

import { PopperProvider } from 'react-use-popper'

const App = () => {
  return (
    <PopperProvider>
      <RootComponent />
    </PopperProvider>
  )
}

Basic Usage

import React, { Component } from 'react'
import { usePopper } from 'react-use-popper'

const ChidlComponent = () => {
  const { togglePopper } = usePopper('Popper 1')

  const handleClick = (e: React.MouseEvent) => {
    togglePopper(e)
  }

  return (
    <>
      <button onClick={handleClick}>Button</button>
    </>
  )
}

Multiple Poppers

If you want to have multiple poppers open at same time, it's recommended to use multiple hooks, e.g:

import React, { Component } from 'react'
import { usePopper } from 'react-use-popper'

const ChidlComponent = () => {
  const popper1 = usePopper('Popper 1')
  const popper2 = usePopper('Popper 2')

  const handleClick = (e: React.MouseEvent) => {
    popper1.togglePopper(e)
  }
  const handleAnotherClick = (e: React.MouseEvent) => {
    popper2.togglePopper(e)
  }

  return (
    <>
      <button onClick={handleClick}>Button</button>
      <button onClick={handleAnotherClick}>Another Button</button>
    </>
  )
}

API Documentation

usePopper Hook

arguements

defaultContent

((popper: Popper) => React.ReactNode) | React.ReactNode | optional | default value: "Default Content"

The react element you want to render in the popper

defaultOptions
id

string | optional

An ID to avoid duplicate poppers. generated randomly by default, if no id is provided

appendTo

Element | optional

A DOM node in which to render the popper gets extracted from the event.currentTarget, in case not provided.

onClose

() => void | optional

A callback that's fired when the popper gets closed.

returns

togglePopper

Easiest and Recommended to use, manages popper's open/close state internally.

args

event: React.MouseEvent | required

content : ((popper: Popper) => React.ReactNode) | React.ReactNode | optional | defaults to usePopper's defaultContent

options: PopperOptions | optional | defaults to usePopper's defaultContent

showPopper

Useful in case you want to manage popper's state manually, takes similar args as togglePopper returns, Popper object containing id which will be needed to pass to hidePopper

args

event: React.MouseEvent | required

content : ((popper: Popper) => React.ReactNode) | React.ReactNode | optional | defaults to usePopper's defaultContent

options: PopperOptions | optional | defaults to usePopper's defaultContent

returns

popper: PrivatePopper

hidePopper

Closes Popper by ID.

args

popperId: string | required

Interfaces

Popper Args

interface PopperArgs {
  /**
   * The react element you want to render in the popper
   */
  defaultContent?: ((popper: Popper) => React.ReactNode) | React.ReactNode
  defaultOptions?: PopperOptions
}

Popper Options

interface PopperOptions {
  /**
   * An ID to avoid duplicate poppers
   * generates randomly by default, if no id is provided
   */
  id?: string
  /**
   * A DOM node in which to render the popper
   * gets extracted by the `event`, in case not provided
   */
  appendTo?: Element
  /**
   * A callback that is fired when the popper closes
   */
  onClose?: () => void
}

PrivatePopper

interface PrivatePopper {
  /**
   * Popper Content Element
   */
  element: React.ReactNode
  /**
   * Popper ID, need to pass this id while closing
   */
  id: string
  /**
   * Popper Container Element to which Popper is appended to
   */
  appendTo: Element
}

License

MIT © WaleedAli070