1 | <!-- <HEADER> // IGNORE IT -->
|
---|
2 | <p align="center">
|
---|
3 | <img src="https://rawcdn.githack.com/popperjs/popper-core/8805a5d7599e14619c9e7ac19a3713285d8e5d7f/docs/src/images/popper-logo-outlined.svg" alt="Popper" height="300px"/>
|
---|
4 | </p>
|
---|
5 |
|
---|
6 | <div align="center">
|
---|
7 | <h1>Tooltip & Popover Positioning Engine</h1>
|
---|
8 | </div>
|
---|
9 |
|
---|
10 | <p align="center">
|
---|
11 | <a href="https://www.npmjs.com/package/@popperjs/core">
|
---|
12 | <img src="https://img.shields.io/npm/v/@popperjs/core?style=for-the-badge" alt="npm version" />
|
---|
13 | </a>
|
---|
14 | <a href="https://www.npmjs.com/package/@popperjs/core">
|
---|
15 | <img src="https://img.shields.io/endpoint?style=for-the-badge&url=https://runkit.io/fezvrasta/combined-npm-downloads/1.0.0?packages=popper.js,@popperjs/core" alt="npm downloads per month (popper.js + @popperjs/core)" />
|
---|
16 | </a>
|
---|
17 | <a href="https://rollingversions.com/popperjs/popper-core">
|
---|
18 | <img src="https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen?style=for-the-badge" alt="Rolling Versions" />
|
---|
19 | </a>
|
---|
20 | </p>
|
---|
21 |
|
---|
22 | <br />
|
---|
23 | <!-- </HEADER> // NOW BEGINS THE README -->
|
---|
24 |
|
---|
25 | **Positioning tooltips and popovers is difficult. Popper is here to help!**
|
---|
26 |
|
---|
27 | Given an element, such as a button, and a tooltip element describing it, Popper
|
---|
28 | will automatically put the tooltip in the right place near the button.
|
---|
29 |
|
---|
30 | It will position _any_ UI element that "pops out" from the flow of your document
|
---|
31 | and floats near a target element. The most common example is a tooltip, but it
|
---|
32 | also includes popovers, drop-downs, and more. All of these can be generically
|
---|
33 | described as a "popper" element.
|
---|
34 |
|
---|
35 | ## Demo
|
---|
36 |
|
---|
37 | [![Popper visualized](https://i.imgur.com/F7qWsmV.jpg)](https://popper.js.org)
|
---|
38 |
|
---|
39 | ## Docs
|
---|
40 |
|
---|
41 | - [v2.x (latest)](https://popper.js.org/docs/v2/)
|
---|
42 | - [v1.x](https://popper.js.org/docs/v1/)
|
---|
43 |
|
---|
44 | We've created a
|
---|
45 | [Migration Guide](https://popper.js.org/docs/v2/migration-guide/) to help you
|
---|
46 | migrate from Popper 1 to Popper 2.
|
---|
47 |
|
---|
48 | To contribute to the Popper website and documentation, please visit the [dedicated repository](https://github.com/popperjs/website).
|
---|
49 |
|
---|
50 | ## Why not use pure CSS?
|
---|
51 |
|
---|
52 | CSS tooltips have accessibility and usability problems:
|
---|
53 |
|
---|
54 | - **Clipping and overflow issues**: CSS tooltips will not be prevented from
|
---|
55 | overflowing clipping boundaries, such as the viewport. The tooltip gets
|
---|
56 | partially cut off or overflows if it's near the edge since there is no dynamic
|
---|
57 | positioning logic. When using Popper, your tooltip will always be positioned
|
---|
58 | in the right place.
|
---|
59 | - **No flipping**: CSS tooltips will not flip to a different placement to fit
|
---|
60 | better in view if necessary. Popper automatically flips the tooltip to make it
|
---|
61 | fit in view as best as possible for the user.
|
---|
62 | - **Using HTML**: Popovers containing interactive HTML are difficult or not
|
---|
63 | possible to create without UX issues using pure CSS. Popper positions any HTML
|
---|
64 | element – no pseudo-elements are used.
|
---|
65 | - **No virtual positioning**: CSS tooltips cannot follow the mouse cursor or be
|
---|
66 | used as a context menu. Popper allows you to position your tooltip relative to
|
---|
67 | any coordinates you desire.
|
---|
68 | - **Lack of extensibility**: CSS tooltips cannot be easily extended to fit any
|
---|
69 | arbitrary use case you may need to adjust for. Popper is built with
|
---|
70 | extensibility in mind.
|
---|
71 |
|
---|
72 | ## Why Popper?
|
---|
73 |
|
---|
74 | With the CSS drawbacks out of the way, we now move on to Popper in the
|
---|
75 | JavaScript space itself.
|
---|
76 |
|
---|
77 | Naive JavaScript tooltip implementations usually have the following problems:
|
---|
78 |
|
---|
79 | - **Scrolling containers**: They don't ensure the tooltip stays with the
|
---|
80 | reference element while scrolling when inside any number of scrolling
|
---|
81 | containers.
|
---|
82 | - **DOM context**: They often require the tooltip move outside of its original
|
---|
83 | DOM context because they don't handle `offsetParent` contexts.
|
---|
84 | - **Configurability**: They often lack advanced configurability to suit any
|
---|
85 | possible use case.
|
---|
86 | - **Size**: They are usually relatively large in size, or require an ancient
|
---|
87 | jQuery dependency.
|
---|
88 | - **Performance**: They often have runtime performance issues and update the
|
---|
89 | tooltip position too slowly.
|
---|
90 |
|
---|
91 | **Popper solves all of these key problems in an elegant, performant manner.** It
|
---|
92 | is a lightweight ~3 kB library that aims to provide a reliable and extensible
|
---|
93 | positioning engine you can use to ensure all your popper elements are positioned
|
---|
94 | in the right place.
|
---|
95 |
|
---|
96 | When you start writing your own popper implementation, you'll quickly run into
|
---|
97 | all of the problems mentioned above. These widgets are incredibly common in our
|
---|
98 | UIs; we've done the hard work figuring this out so you don't need to spend hours
|
---|
99 | fixing and handling numerous edge cases that we already ran into while building
|
---|
100 | the library!
|
---|
101 |
|
---|
102 | Popper is used in popular libraries like Bootstrap, Foundation, Material UI, and
|
---|
103 | more. It's likely you've already used popper elements on the web positioned by
|
---|
104 | Popper at some point in the past few years.
|
---|
105 |
|
---|
106 | Since we write UIs using powerful abstraction libraries such as React or Angular
|
---|
107 | nowadays, you'll also be glad to know Popper can fully integrate with them and
|
---|
108 | be a good citizen together with your other components. Check out `react-popper`
|
---|
109 | for the official Popper wrapper for React.
|
---|
110 |
|
---|
111 | ## Installation
|
---|
112 |
|
---|
113 | ### 1. Package Manager
|
---|
114 |
|
---|
115 | ```bash
|
---|
116 | # With npm
|
---|
117 | npm i @popperjs/core
|
---|
118 |
|
---|
119 | # With Yarn
|
---|
120 | yarn add @popperjs/core
|
---|
121 | ```
|
---|
122 |
|
---|
123 | ### 2. CDN
|
---|
124 |
|
---|
125 | ```html
|
---|
126 | <!-- Development version -->
|
---|
127 | <script src="https://unpkg.com/@popperjs/core@2/dist/umd/popper.js"></script>
|
---|
128 |
|
---|
129 | <!-- Production version -->
|
---|
130 | <script src="https://unpkg.com/@popperjs/core@2"></script>
|
---|
131 | ```
|
---|
132 |
|
---|
133 | ### 3. Direct Download?
|
---|
134 |
|
---|
135 | Managing dependencies by "directly downloading" them and placing them into your
|
---|
136 | source code is not recommended for a variety of reasons, including missing out
|
---|
137 | on feat/fix updates easily. Please use a versioning management system like a CDN
|
---|
138 | or npm/Yarn.
|
---|
139 |
|
---|
140 | ## Usage
|
---|
141 |
|
---|
142 | The most straightforward way to get started is to import Popper from the `unpkg`
|
---|
143 | CDN, which includes all of its features. You can call the `Popper.createPopper`
|
---|
144 | constructor to create new popper instances.
|
---|
145 |
|
---|
146 | Here is a complete example:
|
---|
147 |
|
---|
148 | ```html
|
---|
149 | <!DOCTYPE html>
|
---|
150 | <title>Popper example</title>
|
---|
151 |
|
---|
152 | <style>
|
---|
153 | #tooltip {
|
---|
154 | background-color: #333;
|
---|
155 | color: white;
|
---|
156 | padding: 5px 10px;
|
---|
157 | border-radius: 4px;
|
---|
158 | font-size: 13px;
|
---|
159 | }
|
---|
160 | </style>
|
---|
161 |
|
---|
162 | <button id="button" aria-describedby="tooltip">I'm a button</button>
|
---|
163 | <div id="tooltip" role="tooltip">I'm a tooltip</div>
|
---|
164 |
|
---|
165 | <script src="https://unpkg.com/@popperjs/core@^2.0.0"></script>
|
---|
166 | <script>
|
---|
167 | const button = document.querySelector('#button');
|
---|
168 | const tooltip = document.querySelector('#tooltip');
|
---|
169 |
|
---|
170 | // Pass the button, the tooltip, and some options, and Popper will do the
|
---|
171 | // magic positioning for you:
|
---|
172 | Popper.createPopper(button, tooltip, {
|
---|
173 | placement: 'right',
|
---|
174 | });
|
---|
175 | </script>
|
---|
176 | ```
|
---|
177 |
|
---|
178 | Visit the [tutorial](https://popper.js.org/docs/v2/tutorial/) for an example of
|
---|
179 | how to build your own tooltip from scratch using Popper.
|
---|
180 |
|
---|
181 | ### Module bundlers
|
---|
182 |
|
---|
183 | You can import the `createPopper` constructor from the fully-featured file:
|
---|
184 |
|
---|
185 | ```js
|
---|
186 | import { createPopper } from '@popperjs/core';
|
---|
187 |
|
---|
188 | const button = document.querySelector('#button');
|
---|
189 | const tooltip = document.querySelector('#tooltip');
|
---|
190 |
|
---|
191 | // Pass the button, the tooltip, and some options, and Popper will do the
|
---|
192 | // magic positioning for you:
|
---|
193 | createPopper(button, tooltip, {
|
---|
194 | placement: 'right',
|
---|
195 | });
|
---|
196 | ```
|
---|
197 |
|
---|
198 | All the modifiers listed in the docs menu will be enabled and "just work", so
|
---|
199 | you don't need to think about setting Popper up. The size of Popper including
|
---|
200 | all of its features is about 5 kB minzipped, but it may grow a bit in the
|
---|
201 | future.
|
---|
202 |
|
---|
203 | #### Popper Lite (tree-shaking)
|
---|
204 |
|
---|
205 | If bundle size is important, you'll want to take advantage of tree-shaking. The
|
---|
206 | library is built in a modular way to allow to import only the parts you really
|
---|
207 | need.
|
---|
208 |
|
---|
209 | ```js
|
---|
210 | import { createPopperLite as createPopper } from "@popperjs/core";
|
---|
211 | ```
|
---|
212 |
|
---|
213 | The Lite version includes the most necessary modifiers that will compute the
|
---|
214 | offsets of the popper, compute and add the positioning styles, and add event
|
---|
215 | listeners. This is close in bundle size to pure CSS tooltip libraries, and
|
---|
216 | behaves somewhat similarly.
|
---|
217 |
|
---|
218 | However, this does not include the features that makes Popper truly useful.
|
---|
219 |
|
---|
220 | The two most useful modifiers not included in Lite are `preventOverflow` and
|
---|
221 | `flip`:
|
---|
222 |
|
---|
223 | ```js
|
---|
224 | import { createPopperLite as createPopper, preventOverflow, flip } from "@popperjs/core";
|
---|
225 |
|
---|
226 | const button = document.querySelector('#button');
|
---|
227 | const tooltip = document.querySelector('#tooltip');
|
---|
228 |
|
---|
229 | createPopper(button, tooltip, {
|
---|
230 | modifiers: [preventOverflow, flip],
|
---|
231 | });
|
---|
232 | ```
|
---|
233 |
|
---|
234 | As you make more poppers, you may be finding yourself needing other modifiers
|
---|
235 | provided by the library.
|
---|
236 |
|
---|
237 | See [tree-shaking](https://popper.js.org/docs/v2/tree-shaking/) for more
|
---|
238 | information.
|
---|
239 |
|
---|
240 | ## Distribution targets
|
---|
241 |
|
---|
242 | Popper is distributed in 3 different versions, in 3 different file formats.
|
---|
243 |
|
---|
244 | The 3 file formats are:
|
---|
245 |
|
---|
246 | - `esm` (works with `import` syntax — **recommended**)
|
---|
247 | - `umd` (works with `<script>` tags or RequireJS)
|
---|
248 | - `cjs` (works with `require()` syntax)
|
---|
249 |
|
---|
250 | There are two different `esm` builds, one for bundler consumers (e.g. webpack,
|
---|
251 | Rollup, etc..), which is located under `/lib`, and one for browsers with native
|
---|
252 | support for ES Modules, under `/dist/esm`. The only difference within the two,
|
---|
253 | is that the browser-compatible version doesn't make use of
|
---|
254 | `process.env.NODE_ENV` to run development checks.
|
---|
255 |
|
---|
256 | The 3 versions are:
|
---|
257 |
|
---|
258 | - `popper`: includes all the modifiers (features) in one file (**default**);
|
---|
259 | - `popper-lite`: includes only the minimum amount of modifiers to provide the
|
---|
260 | basic functionality;
|
---|
261 | - `popper-base`: doesn't include any modifier, you must import them separately;
|
---|
262 |
|
---|
263 | Below you can find the size of each version, minified and compressed with the
|
---|
264 | [Brotli compression algorithm](https://medium.com/groww-engineering/enable-brotli-compression-in-webpack-with-fallback-to-gzip-397a57cf9fc6):
|
---|
265 |
|
---|
266 | <!-- Don't change the labels to use hyphens, it breaks, even when encoded -->
|
---|
267 |
|
---|
268 | ![](https://badge-size.now.sh/https://unpkg.com/@popperjs/core/dist/umd/popper.min.js?compression=brotli&label=popper)
|
---|
269 | ![](https://badge-size.now.sh/https://unpkg.com/@popperjs/core/dist/umd/popper-lite.min.js?compression=brotli&label=popper%20lite)
|
---|
270 | ![](https://badge-size.now.sh/https://unpkg.com/@popperjs/core/dist/umd/popper-base.min.js?compression=brotli&label=popper%20base)
|
---|
271 |
|
---|
272 | ## Hacking the library
|
---|
273 |
|
---|
274 | If you want to play with the library, implement new features, fix a bug you
|
---|
275 | found, or simply experiment with it, this section is for you!
|
---|
276 |
|
---|
277 | First of all, make sure to have
|
---|
278 | [Yarn installed](https://yarnpkg.com/lang/en/docs/install).
|
---|
279 |
|
---|
280 | Install the development dependencies:
|
---|
281 |
|
---|
282 | ```bash
|
---|
283 | yarn install
|
---|
284 | ```
|
---|
285 |
|
---|
286 | And run the development environment:
|
---|
287 |
|
---|
288 | ```bash
|
---|
289 | yarn dev
|
---|
290 | ```
|
---|
291 |
|
---|
292 | Then, simply open one the development server web page:
|
---|
293 |
|
---|
294 | ```bash
|
---|
295 | # macOS and Linux
|
---|
296 | open localhost:5000
|
---|
297 |
|
---|
298 | # Windows
|
---|
299 | start localhost:5000
|
---|
300 | ```
|
---|
301 |
|
---|
302 | From there, you can open any of the examples (`.html` files) to fiddle with
|
---|
303 | them.
|
---|
304 |
|
---|
305 | Now any change you will made to the source code, will be automatically compiled,
|
---|
306 | you just need to refresh the page.
|
---|
307 |
|
---|
308 | If the page is not working properly, try to go in _"Developer Tools >
|
---|
309 | Application > Clear storage"_ and click on "_Clear site data_".
|
---|
310 | To run the examples you need a browser with
|
---|
311 | [JavaScript modules via script tag support](https://caniuse.com/#feat=es6-module).
|
---|
312 |
|
---|
313 | ## Test Suite
|
---|
314 |
|
---|
315 | Popper is currently tested with unit tests, and functional tests. Both of them
|
---|
316 | are run by Jest.
|
---|
317 |
|
---|
318 | ### Unit Tests
|
---|
319 |
|
---|
320 | The unit tests use JSDOM to provide a primitive document object API, they are
|
---|
321 | used to ensure the utility functions behave as expected in isolation.
|
---|
322 |
|
---|
323 | ### Functional Tests
|
---|
324 |
|
---|
325 | The functional tests run with Puppeteer, to take advantage of a complete browser
|
---|
326 | environment. They are currently running on Chromium, and Firefox.
|
---|
327 |
|
---|
328 | You can run them with `yarn test:functional`. Set the `PUPPETEER_BROWSER`
|
---|
329 | environment variable to `firefox` to run them on the Mozilla browser.
|
---|
330 |
|
---|
331 | The assertions are written in form of image snapshots, so that it's easy to
|
---|
332 | assert for the correct Popper behavior without having to write a lot of offsets
|
---|
333 | comparisons manually.
|
---|
334 |
|
---|
335 | You can mark a `*.test.js` file to run in the Puppeteer environment by
|
---|
336 | prepending a `@jest-environment puppeteer` JSDoc comment to the interested file.
|
---|
337 |
|
---|
338 | Here's an example of a basic functional test:
|
---|
339 |
|
---|
340 | ```js
|
---|
341 | /**
|
---|
342 | * @jest-environment puppeteer
|
---|
343 | * @flow
|
---|
344 | */
|
---|
345 | import { screenshot } from '../utils/puppeteer.js';
|
---|
346 |
|
---|
347 | it('should position the popper on the right', async () => {
|
---|
348 | const page = await browser.newPage();
|
---|
349 | await page.goto(`${TEST_URL}/basic.html`);
|
---|
350 |
|
---|
351 | expect(await screenshot(page)).toMatchImageSnapshot();
|
---|
352 | });
|
---|
353 | ```
|
---|
354 |
|
---|
355 | You can find the complete
|
---|
356 | [`jest-puppeteer` documentation here](https://github.com/smooth-code/jest-puppeteer#api),
|
---|
357 | and the
|
---|
358 | [`jest-image-snapshot` documentation here](https://github.com/americanexpress/jest-image-snapshot#%EF%B8%8F-api).
|
---|
359 |
|
---|
360 | ## License
|
---|
361 |
|
---|
362 | MIT
|
---|