@elliemae/react-popper

中文文档
1.0.2发布于 2022/01/19MITTypeScriptESM + CJS近 30 天下载 5085

React wrapper around Popper.js

原始使用文档
文档目录8 节
  1. React Popper
  2. Install
  3. Usage
  4. API documentation
  5. Usage with ReactDOM.createPortal
  6. Usage without a reference HTMLElement
  7. Flow and TypeScript types
  8. Running Locally

React Popper

Build Status

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 [email protected]? 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 following properties:

  • ref: used to retrieve the React refs of the popper element.
  • style: contains the necessary CSS styles (React CSS properties) which are computed by Popper.js to correctly position the popper element.
  • placement: 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.
  • outOfBoundaries: a boolean signifying if the popper element is overflowing its boundaries.
  • scheduleUpdate: a function you can call to schedule a Popper.js position update. It will directly call the Popper#scheduleUpdate method.
  • arrowProps: an object, containing style and ref properties that are identical to the ones provided as the first and second arguments of children, but relative to the arrow element. The style property contains left and top offset values, which are used to center the arrow within the popper. These values can be merged with further custom styling and positioning. See the demo for an example.
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

install dependencies

yarn

run dev mode

yarn demo:dev

open your browser and visit:

http://localhost:1234/

版本与发布记录

@elliemae/react-popper 共发布 2 个版本,首次发布于 2022/01/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2022/01/194 年前
1.0.12022/01/194 年前

关于 @elliemae/react-popper 的常见问题

@elliemae/react-popper 是什么?

React wrapper around Popper.js

如何安装 @elliemae/react-popper?

在项目目录运行 npm install @elliemae/react-popper,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@elliemae/react-popper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elliemae/react-popper 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@elliemae/react-popper 可以商用吗?

@elliemae/react-popper 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elliemae/react-popper 的下载量有多少?

根据 npm 官方下载统计,@elliemae/react-popper 在 2026/10/14 – 2026/11/12 这 30 天内被下载 5,085 次,日均约 170 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elliemae/react-popper 还在维护吗?

最新版本 1.0.2 发布于 2022/01/19(4 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/01/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。