What is popper.js?
The popper.js npm package is a library used for managing poppers in web applications. Poppers are elements on the screen that 'pop out' from the flow of the document and float near a target element. The library is commonly used for tooltips, popovers, dropdowns, and similar components that require positioning near a reference element.
What are popper.js's main functionalities?
Positioning tooltips
This code sample demonstrates how to create a popper instance to position a tooltip above a button. The 'createPopper' function from popper.js is used to bind the tooltip to the button with a placement option set to 'top'.
import { createPopper } from '@popperjs/core';
const tooltip = document.querySelector('#tooltip');
const button = document.querySelector('#button');
const popperInstance = createPopper(button, tooltip, {
placement: 'top',
});
Creating dropdowns
This code sample shows how to use popper.js to create a dropdown menu that appears below the start of a reference element. The 'createPopper' function is used to attach the dropdown to the reference element.
import { createPopper } from '@popperjs/core';
const dropdown = document.querySelector('#dropdown');
const referenceElement = document.querySelector('#referenceElement');
const popperInstance = createPopper(referenceElement, dropdown, {
placement: 'bottom-start',
});
Modifying popper behavior with modifiers
This example illustrates how to use modifiers to alter the behavior of a popper. Here, an 'offset' modifier is added to provide a gap between the popper and the reference element.
import { createPopper } from '@popperjs/core';
const popper = document.querySelector('#popper');
const referenceElement = document.querySelector('#referenceElement');
const popperInstance = createPopper(referenceElement, popper, {
modifiers: [
{
name: 'offset',
options: {
offset: [0, 8],
},
},
],
});
Other packages similar to popper.js
tippy.js
Tippy.js is a highly customizable tooltip and popover library powered by popper.js. It provides a simpler API for creating tooltips and popovers and includes additional features like animations and themes. It is built on top of popper.js and abstracts some of the complexity involved in creating poppers.
tooltip.js
Tooltip.js is another library for creating and managing tooltips in web applications. It is also built on top of popper.js and provides a simpler interface for creating tooltips. However, it is less feature-rich compared to tippy.js and focuses mainly on tooltip functionality.
floating-ui
Floating UI is a low-level library for positioning floating elements. It is the successor to popper.js and provides more advanced features and better performance. It offers a more modular approach, allowing developers to import only the parts they need.
Popper.js
A library used to position poppers in web applications.
Wut? Poppers?
A popper is an element on the screen which "pops out" from the natural flow of your application.
Common examples of poppers are tooltips, popovers and drop-downs.
So, yet another tooltip library?
Well, basically, no.
Popper.js is a positioning engine, its purpose is to calculate the position of an element
to make it possible to position it near a given reference element.
The engine is completely modular and most of its features are implemented as modifiers
(similar to middlewares or plugins).
The whole code base is written in ES2015 and its features are automatically tested on real browsers thank to SauceLabs and TravisCI.
Popper.js has zero dependencies. No jQuery, no LoDash, nothing.
It's used by big companies like Microsoft in WebClipper and Atlassian in AtlasKit.
Popper.js
This is the engine, the library that computes and, optionally, applies the styles to
the poppers.
Some of the key points are:
- Position elements keeping them in their original DOM context (doesn't mess with your DOM!);
- Allows to export the computed informations to integrate with React and other view libraries;
- Supports Shadow DOM elements;
- Completely customizable thanks to the modifiers based structure;
Visit our project page to see a lot of examples of what you can do with Popper.js!
Find the documentation here.
Tooltip.js
Since lot of users just need a simple way to integrate powerful tooltips in their projects,
we created Tooltip.js.
It's a small library that makes it easy to automatically create tooltips using as engine Popper.js.
It's API is almost identical to the famous tooltip system of Bootstrap, in this way it will be
easy to integrate it in your projects.
The tooltips generated by Tooltip.js are accessible thanks to the aria
tags.
Find the documentation here.
Installation
Popper.js is available on the following package managers and CDNs:
Tooltip.js as well:
Usage
Given an existing popper, ask Popper.js to position it near its button
var reference = document.querySelector('.my-button');
var popper = document.querySelector('.my-popper');
var anotherPopper = new Popper(
reference,
popper,
{
}
);
Callbacks
Popper.js supports two kind of callbacks, the onCreate
callback is called after
the popper has been initalized. The onUpdate
one is called on any subsequent update.
const reference = document.querySelector('.my-button');
const popper = document.querySelector('.my-popper');
new Popper(reference, popper, {
onCreate: (data) => {
},
onUpdate: (data) => {
}
});
Writing your own modifiers
Popper.js is based on a "plugin-like" architecture, most of the features of it are fully encapsulated "modifiers".
A modifier is a function that is called each time Popper.js needs to compute the position of the popper. For this reason, modifiers should be very performant to avoid bottlenecks.
To learn how to create a modifier, read the modifiers documentaton
React, Vue.js, AngularJS, Ember.js (etc...) integration
Integrate 3rd party libraries in React or other libraries can be a pain because
they usually alter the DOM and drives the libraries crazy.
Popper.js limits all its DOM modifications inside the applyStyle
modifier,
you can simply disable it and manually apply the popper coordinates using
your library of choice.
This made possible for some great developers to create libraries based on Popper.js
that integrate in popular frameworks:
Alternatively, you may even define your own applyStyles
with your custom one and
integrate Popper.js by yourself!
function applyReactStyle(data) {
};
const reference = document.querySelector('.my-button');
const popper = document.querySelector('.my-popper');
new Popper(reference, popper, {
modifiers: {
applyStyle: { enabled: false },
applyReactStyle: {
enabled: true,
function: applyReactStyle,
order: 800,
},
},
});
Migration from Popper.js v0
Since the API changed, we prepared some migration instructions to make it easy to upgrade to
Popper.js v1.
https://github.com/FezVrasta/popper.js/issues/62
Feel free to comment inside the issue if you have any questions.
Performances
Popper.js is very performant. It usually takes 0.5ms to compute a popper's position (on an iMac with 3.5G GHz Intel Core i5).
This means that it will not cause any jank, leading to a smooth user experience.
Notes
Libraries using Popper.js
The aim of Popper.js is to provide a stable and powerful positioning engine ready to
be used in 3rd party libraries.
Lot of projects are already using Popper.js, you can see the complete list visiting
the related npm page.
Another useful list is provided by NPM-Graph.
Credits
I want to thank some friends and projects for the work they did:
- @AndreaScn for its work on the GitHub Page and the manual testing he did during the development;
- @vampolo for the original idea and for the name of the library;
- Sysdig for all the awesome things I learned during these years that made possible for me to write this library;
- Tether.js for having inspired me in writing a positioning library ready for the real world;
- The Contributors for their much appreciated Pull Requests and bug reports;
- you for the star you'll give to this project and for being so awesome to give this project a try :)
Copyright and license
Code and documentation copyright 2016 Federico Zivolo. Code released under the MIT license. Docs released under Creative Commons.