Package detail

@react-bootstrap/react-popper

souporserious27.7kMIT1.2.1

React wrapper around Popper.js

react, react-popper, popperjs, component

readme

React Popper

Build Status npm version npm downloads Dependency Status code style: prettier Get support or discuss

React wrapper around Popper.js.

important note: popper.js is not a tooltip library, it's a positioning engine to be used to build features such as (but not restricted to) tooltips.

Install

Via package managers:

npm install react-popper --save
# or
yarn add react-popper

Via script tag (UMD library exposed as ReactPopper):

<script src="https://unpkg.com/react-popper/dist/index.umd.js"></script>

Usage

Using react-popper@0.x? You can find its documentation clicking here

Example:

import { Manager, Reference, Popper } from 'react-popper';

const Example = () => (
  <Manager>
    <Reference>
      {({ ref }) => (
        <button type="button" ref={ref}>
          Reference element
        </button>
      )}
    </Reference>
    <Popper placement="right">
      {({ ref, style, placement, arrowProps }) => (
        <div ref={ref} style={style} data-placement={placement}>
          Popper element
          <div ref={arrowProps.ref} style={arrowProps.style} />
        </div>
      )}
    </Popper>
  </Manager>
);

react-popper makes use of a React pattern called "render prop", if you are not familiar with it, please read more on the official React documentation.

Using React <=15 or Preact? The components created with them don't support to return fragments, this means that you will need to wrap <Reference /> and <Popper /> into a single, common, <div /> to make react-popper work.

API documentation

The Manager component is a simple wrapper that needs to surround all the other react-popper components in order to make them communicate with each others.

The Popper component accepts the properties children, placement, modifiers, eventsEnabled and positionFixed.

<Popper
  innerRef={(node) => this.popperNode = node}
  placement="right"
  modifiers={{ preventOverflow: { enabled: false } }}
  eventsEnabled={true}
  positionFixed={false}
>
    { props => [...] }
</Popper>
children
children: ({|
  ref: (?HTMLElement) => void,
  style: { [string]: string | number },
  placement: ?Placement,
  outOfBoundaries: ?boolean,
  scheduleUpdate: () => void,
  arrowProps: {
    ref: (?HTMLElement) => void,
    style: { [string]: string | number },
  },
|}) => Node

A function (render prop) that takes as argument an object containing the properties ref, style, placement, andarrowProps.

The first 3 properties are the ref property that is going to be used to retrieve the React refs of the popper element, the style property, which contains the CSS styles (React CSS properties) computed by Popper.js and needed to style the popper element so that it gets positioned in the desired way.
These styles should be applied to your React component using the style prop or with any CSS-in-JS library of your choice.

The placement property describes the placement of your popper after Popper.js has applied all the modifiers that may have flipped or altered the originally provided placement property. You can use this to alter the style of the popper and or of the arrow according to the definitive placement. For instance, you can use this property to orient the arrow to the right direction.

scheduleUpdate is a function you can call to schedule a Popper.js position update. It will directly call the Popper#scheduleUpdate method.

The arrowProps argument is an object, containing a style and ref properties that are identical to the ones provided as first and second argument of children, but are relative to the arrow element rather than the popper. Use them to, accordingly, retrieve the ref of the arrow element and style it.

innerRef
innerRef?: (?HTMLElement) => void

Function that can be used to obtain popper reference

placement
placement?: PopperJS$Placement;

One of the accepted placement values listed in the Popper.js documentation.
Your popper is going to be placed according to the value of this property.
Defaults to bottom.

outOfBoundaries: ?boolean;

A boolean that can be used to hide the popper element in case it's overflowing from its boundaries. Read more.

eventsEnabled
eventsEnabled?: boolean;

Tells react-popper to enable or disable the Popper.js event listeners. true by default.

positionFixed

Set this property to true to tell Popper.js to use the position: fixed strategy to position the popper element. By default it's false, meaning that it will use the position: absolute strategy.

modifiers
modifiers?: PopperJS$Modifiers;

An object containing custom settings for the Popper.js modifiers.
You can use this property to override their settings or to inject your custom ones.

Usage with ReactDOM.createPortal

Popper.js is smart enough to work even if the popper and reference elements aren't in the same DOM context.
This means that you can use ReactDOM.createPortal (or any pre React 16 alternative) to move the popper component somewhere else in the DOM.

This can be useful if you want to position a tooltip inside an overflow: hidden container that you want to make overflow. Please note that you can also try the positionFixed strategy to obtain a similar effect with less hassle.

import { Manager, Reference, Popper } from 'react-popper';

const Example = () => (
  <Manager>
    <Reference>
      {({ ref }) => (
        <button type="button" ref={ref}>
          Reference
        </button>
      )}
    </Reference>
    {ReactDOM.createPortal(
      <Popper>
        {({ placement, ref, style }) => (
          <div ref={ref} style={style} data-placement={placement}>
            Popper
          </div>
        )}
      </Popper>,
      document.querySelector('#destination')
    )}
  </Manager>
);

Usage without a reference HTMLElement

Whenever you need to position a popper based on some arbitrary coordinates, you can provide Popper with a referenceElement property that is going to be used in place of the referenceProps.getRef React ref.

The referenceElement property must be an object with an interface compatible with an HTMLElement as described in the Popper.js referenceObject documentation, this implies that you may also provide a real HTMLElement if needed.

If referenceElement is defined, it will take precedence over any referenceProps.ref provided refs.

import { Popper } from 'react-popper';

class VirtualReference {
  getBoundingClientRect() {
    return {
      top: 10,
      left: 10,
      bottom: 20,
      right: 100,
      width: 90,
      height: 10,
    };
  }

  get clientWidth() {
    return this.getBoundingClientRect().width;
  }

  get clientHeight() {
    return this.getBoundingClientRect().height;
  }
}

// This is going to create a virtual reference element
// positioned 10px from top and left of the document
// 90px wide and 10px high
const virtualReferenceElement = new VirtualReference();

// This popper will be positioned relatively to the
// virtual reference element defined above
const Example = () => (
  <Popper referenceElement={virtualReferenceElement}>
    {({ ref, style, placement, arrowProps }) => (
      <div ref={ref} style={style} data-placement={placement}>
        Popper element
        <div ref={arrowProps.ref} style={arrowProps.style} />
      </div>
    )}
  </Popper>
);

Flow and TypeScript types

This library is built with Flow but it supports TypeScript as well.

You can find the exported Flow types in src/index.js, and the TypeScript definitions in typings/react-popper.d.ts.

Running Locally

clone repo

git clone git@github.com:FezVrasta/react-popper.git

move into folder

cd ~/react-popper

install dependencies

npm install or yarn

run dev mode

npm run demo:dev or yarn demo:dev

open your browser and visit:

http://localhost:1234/

changelog

CHANGELOG

Here are listed the changelogs until 0.8.2, if you are looking for more recent releases changelog please refer to the dedicated GitHub releases page, where you will find all the releases plus the changelog for each of them.


0.8.2

fix es5 build #90

0.8.1

Move back to controlling DOM updates through React

Update & clean dependencies #89

0.8.0

Upgrade PopperJS dependency to 1.12.9

Fix Popper ref getting called too many times #81

Let PopperJS style DOM for better performance as described in vjeux's talk.

0.7.5

Fix PopperJS instantiation #77

0.7.4

Allow React 16 as a peerDependency #59

Updates TypeScript definition for IPopperChildrenProps #61

Made scripts platform independent #63

0.7.3

Upgraded dependencies #44

Fix missing data-x-out-of-boundaries attribute #45

Update how react-popper.d.ts imports PopperJS #51

0.7.2

Fix top and left arrow calculation. Disregard the note below about changing the CSS positioning, this was an error on react-popper's part.

0.7.1

Support top and left arrow offsets together, be aware this most likely broke any prior CSS positioning your arrows #37

Fix scheduleUpdate call if this._popper does not exist #38

Add typescript definitions #40

Upgrade to Popper.js 1.10.8

0.7.0

Change Target, Popper, and Arrow component's tag prop to component to allow custom components to be passed in.

Upgrade PopperJS 1.10.2

0.6.6

Upgrade PopperJS to 1.9.9

0.6.5

Call innerRef in all component child functions

0.6.4

Call innerRef in child function as well

0.6.3

Upgrade PopperJS to 1.9.5

0.6.2

Replace lodash.isequal with is-equal-shallow

0.6.1

Pass down scheduleUpdate to Popper child function to allow programatic updates

Upgrade to Popper.js 1.9.4

Fix modifier.function is deprecated, use modifier.fn #22

0.6.0

Make sure to pass props from above down to child function, fixes #13

Recalculate size of Popper when children change, fixes #15

0.5.0

Use prop-types package instead of React PropTypes #9

Make updateState modifier return data object #11

Removed findDOMNode 🎉

Moved back to tag instead of component. Use a child function now for custom components and pass down the provided ref to the proper component.

Removed default classNames for popper and popper__arrow so we can be unopinionated about styling.

0.4.3

Allow passing children through to components

0.4.2

Move back to translate3d and round values since half pixel placement was the culprit for causing blurry text

0.4.1

Don't use translate3d since it causes blurry text on lower res displays

0.4.0

Remove getRef function since it seems to be causing problems.

Move functional components to classes so we can get nodes more reliably.

Spread modifier styles inside _getPopperStyle #6

0.3.0

Renamed PopperManager -> Manager

Added getRef prop to Target, Popper, and Arrow components

0.2.2

Bundle popper.js with dist build

0.2.1

Remove React ARIA from demos for now

0.2.0

New API see README for full docs

0.1.0

Initial release