Convert Figma logo to code with AI

floating-ui logoreact-popper

🍿⚛Official React library to use Popper, the positioning library

2,502
226
2,502
39

Top Related Projects

A JavaScript library to position floating elements and create interactions for them.

A JavaScript library to position floating elements and create interactions for them.

11,899

Tooltip, popover, dropdown, and menu library

A JavaScript library to position floating elements and create interactions for them.

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.

Quick Overview

React-Popper is a React wrapper around the Popper.js positioning engine. It provides an easy-to-use API for creating tooltips, popovers, and other positioned elements in React applications. This library is part of the Floating UI ecosystem, which aims to provide a comprehensive solution for positioning floating elements in web applications.

Pros

  • Easy integration with React applications
  • Highly customizable positioning options
  • Automatic updates on scroll, resize, and content changes
  • Supports both controlled and uncontrolled usage

Cons

  • Learning curve for advanced usage and customization
  • Potential performance impact for complex layouts with many popper instances
  • Dependency on Popper.js core library

Code Examples

  1. Basic usage with a tooltip:
import { usePopper } from 'react-popper';

function Tooltip() {
  const [referenceElement, setReferenceElement] = useState(null);
  const [popperElement, setPopperElement] = useState(null);
  const { styles, attributes } = usePopper(referenceElement, popperElement);

  return (
    <>
      <button ref={setReferenceElement}>Hover me</button>
      <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
        This is a tooltip
      </div>
    </>
  );
}
  1. Using modifiers for custom positioning:
import { usePopper } from 'react-popper';

function CustomPositionedPopper() {
  const [referenceElement, setReferenceElement] = useState(null);
  const [popperElement, setPopperElement] = useState(null);
  const { styles, attributes } = usePopper(referenceElement, popperElement, {
    modifiers: [
      {
        name: 'offset',
        options: {
          offset: [0, 8],
        },
      },
    ],
  });

  return (
    <>
      <button ref={setReferenceElement}>Click me</button>
      <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
        Custom positioned popper
      </div>
    </>
  );
}
  1. Using arrow modifier:
import { usePopper } from 'react-popper';

function PopperWithArrow() {
  const [referenceElement, setReferenceElement] = useState(null);
  const [popperElement, setPopperElement] = useState(null);
  const [arrowElement, setArrowElement] = useState(null);
  const { styles, attributes } = usePopper(referenceElement, popperElement, {
    modifiers: [
      { name: 'arrow', options: { element: arrowElement } },
    ],
  });

  return (
    <>
      <button ref={setReferenceElement}>Hover me</button>
      <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
        Popper with arrow
        <div ref={setArrowElement} style={styles.arrow} />
      </div>
    </>
  );
}

Getting Started

  1. Install the package:
npm install @popperjs/core react-popper
  1. Import and use the usePopper hook in your React component:
import React, { useState } from 'react';
import { usePopper } from 'react-popper';

function MyComponent() {
  const [referenceElement, setReferenceElement] = useState(null);
  const [popperElement, setPopperElement] = useState(null);
  const { styles, attributes } = usePopper(referenceElement, popperElement);

  return (
    <>
      <button ref={setReferenceElement}>Reference element</button>
      <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
        Popper element
      </div>
    </>
  );
}

Competitor Comparisons

A JavaScript library to position floating elements and create interactions for them.

Pros of floating-ui

  • More flexible and framework-agnostic, allowing use with any JavaScript framework
  • Improved performance with optimized positioning algorithms
  • Broader feature set, including virtual elements and middleware

Cons of floating-ui

  • Steeper learning curve due to increased complexity
  • Requires more manual setup compared to react-popper's higher-level abstraction

Code Comparison

floating-ui:

import {computePosition, flip, shift, offset} from '@floating-ui/dom';

computePosition(referenceEl, floatingEl, {
  placement: 'top',
  middleware: [offset(6), flip(), shift({padding: 5})],
}).then(({x, y}) => {
  Object.assign(floatingEl.style, {
    left: `${x}px`,
    top: `${y}px`,
  });
});

react-popper:

import {usePopper} from 'react-popper';

const {styles, attributes} = usePopper(referenceElement, popperElement, {
  placement: 'top',
  modifiers: [
    {name: 'offset', options: {offset: [0, 6]}},
    {name: 'flip'},
    {name: 'preventOverflow', options: {padding: 5}},
  ],
});

Both libraries provide powerful positioning capabilities, but floating-ui offers more granular control and flexibility at the cost of increased complexity. react-popper provides a more React-specific, higher-level abstraction that may be easier to integrate for React developers.

A JavaScript library to position floating elements and create interactions for them.

Pros of floating-ui

  • More flexible and framework-agnostic, allowing use with any JavaScript framework
  • Improved performance with optimized positioning algorithms
  • Broader feature set, including virtual elements and middleware

Cons of floating-ui

  • Steeper learning curve due to increased complexity
  • Requires more manual setup compared to react-popper's higher-level abstraction

Code Comparison

floating-ui:

import {computePosition, flip, shift, offset} from '@floating-ui/dom';

computePosition(referenceEl, floatingEl, {
  placement: 'top',
  middleware: [offset(6), flip(), shift({padding: 5})],
}).then(({x, y}) => {
  Object.assign(floatingEl.style, {
    left: `${x}px`,
    top: `${y}px`,
  });
});

react-popper:

import {usePopper} from 'react-popper';

const {styles, attributes} = usePopper(referenceElement, popperElement, {
  placement: 'top',
  modifiers: [
    {name: 'offset', options: {offset: [0, 6]}},
    {name: 'flip'},
    {name: 'preventOverflow', options: {padding: 5}},
  ],
});

Both libraries provide powerful positioning capabilities, but floating-ui offers more granular control and flexibility at the cost of increased complexity. react-popper provides a more React-specific, higher-level abstraction that may be easier to integrate for React developers.

11,899

Tooltip, popover, dropdown, and menu library

Pros of Tippy.js

  • More feature-rich out of the box, including animations, themes, and touch support
  • Easier to set up and use for simple tooltip scenarios
  • Smaller bundle size for basic use cases

Cons of Tippy.js

  • Less flexible for complex positioning requirements
  • Not as well-suited for highly customized or dynamic content
  • May require additional setup for React integration

Code Comparison

Tippy.js:

tippy('#myButton', {
  content: 'Hello, Tippy!',
  placement: 'top',
  animation: 'scale'
});

React Popper:

<Popper placement="top" referenceElement={referenceElement}>
  {({ ref, style, placement }) => (
    <div ref={ref} style={style} data-placement={placement}>
      Hello, Popper!
    </div>
  )}
</Popper>

Summary

Tippy.js is a more user-friendly option for simple tooltip scenarios, offering a rich set of features out of the box. It's easier to set up and has a smaller bundle size for basic use cases. However, React Popper provides more flexibility for complex positioning requirements and is better suited for highly customized or dynamic content. React Popper also integrates more seamlessly with React applications, while Tippy.js may require additional setup for React integration.

A JavaScript library to position floating elements and create interactions for them.

Pros of floating-ui

  • More flexible and framework-agnostic, allowing use with any JavaScript framework
  • Improved performance with optimized positioning algorithms
  • Broader feature set, including virtual elements and middleware

Cons of floating-ui

  • Steeper learning curve due to increased complexity
  • Requires more manual setup compared to react-popper's higher-level abstraction

Code Comparison

floating-ui:

import {computePosition, flip, shift, offset} from '@floating-ui/dom';

computePosition(referenceEl, floatingEl, {
  placement: 'top',
  middleware: [offset(6), flip(), shift({padding: 5})],
}).then(({x, y}) => {
  Object.assign(floatingEl.style, {
    left: `${x}px`,
    top: `${y}px`,
  });
});

react-popper:

import {usePopper} from 'react-popper';

const {styles, attributes} = usePopper(referenceElement, popperElement, {
  placement: 'top',
  modifiers: [
    {name: 'offset', options: {offset: [0, 6]}},
    {name: 'flip'},
    {name: 'preventOverflow', options: {padding: 5}},
  ],
});

Both libraries provide powerful positioning capabilities, but floating-ui offers more granular control and flexibility at the cost of increased complexity. react-popper provides a more React-specific, higher-level abstraction that may be easier to integrate for React developers.

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.

Pros of Material-UI

  • Comprehensive UI component library with a wide range of pre-built components
  • Implements Google's Material Design, providing a consistent and modern look
  • Extensive documentation and community support

Cons of Material-UI

  • Larger bundle size due to the extensive component library
  • More opinionated design system, which may require additional customization for unique designs

Code Comparison

Material-UI:

import { Button, Typography } from '@mui/material';

function MyComponent() {
  return (
    <Button variant="contained" color="primary">
      <Typography variant="button">Click me</Typography>
    </Button>
  );
}

React Popper:

import { usePopper } from 'react-popper';

function MyComponent() {
  const [referenceElement, setReferenceElement] = useState(null);
  const [popperElement, setPopperElement] = useState(null);
  const { styles, attributes } = usePopper(referenceElement, popperElement);

  return (
    <>
      <button ref={setReferenceElement}>Reference</button>
      <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
        Popper content
      </div>
    </>
  );
}

Summary

Material-UI offers a comprehensive UI library with Material Design implementation, while React Popper focuses specifically on positioning elements. Material-UI provides a wider range of components but may have a larger bundle size, whereas React Popper is more lightweight and flexible for custom positioning needs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React Popper

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

React wrapper around Popper.


⚠️ This library is in maintenance mode!

This library wraps @popperjs/core, not @floating-ui/dom.

To use the new Floating UI package with React, instead visit https://floating-ui.com/docs/react-dom.


Install

Via package managers:

# With npm
npm i react-popper @popperjs/core

# With Yarn
yarn add react-popper @popperjs/core

Note: @popperjs/core must be installed in your project in order for react-popper to work.

Via script tag (UMD library exposed as ReactPopper):

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

Documentation

The full documentation can be found on the official Popper website:

http://popper.js.org/react-popper

Running Locally

clone repo

git clone git@github.com:popperjs/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/