@@ -17,8 +17,10 @@ engine_ to be used to build features such as (but not restricted to) tooltips.
17
17
Via package managers:
18
18
19
19
``` bash
20
- npm install react-popper @popperjs/core --save
21
- # or
20
+ # With npm
21
+ npm i react-popper @popperjs/core
22
+
23
+ # With Yarn
22
24
yarn add react-popper @popperjs/core
23
25
```
24
26
@@ -262,34 +264,37 @@ This means that you can use
262
264
React 16 alternative) to move the popper component somewhere else in the DOM.
263
265
264
266
This can be useful if you want to position a tooltip inside an
265
- ` overflow: hidden ` container that you want to make overflow. Please note that
266
- you can also try the ` positionFixed ` strategy to obtain a similar effect with
267
- less hassle.
267
+ ` overflow: hidden ` container that you want to make overflow.
268
+
269
+ **Please note that you can also try ` strategy = " fixed" ` to obtain a similar
270
+ effect with less hassle.**
268
271
269
272
` ` ` jsx
270
- import { Manager , Reference , Popper } from ' react-popper' ;
273
+ import { usePopper } from ' react-popper' ;
271
274
272
- const Example = () => (
273
- < Manager>
274
- < Reference>
275
- {({ ref }) => (
276
- < button type= " button" ref= {ref}>
277
- Reference
278
- < / button>
275
+ const Example = () => {
276
+ const [referenceElement , setReferenceElement ] = React .useState (null );
277
+ const [popperElement , setPopperElement ] = React .useState (null );
278
+ const { styles , attributes } = usePopper (referenceElement, popperElement);
279
+
280
+ return (
281
+ <>
282
+ < button type= " button" ref= {setReferenceElement}>
283
+ Reference
284
+ < / button>
285
+ {ReactDOM .createPortal (
286
+ < div
287
+ ref= {setPopperElement}
288
+ style= {styles .popper }
289
+ {... attributes .popper }
290
+ >
291
+ Popper
292
+ < / div> ,
293
+ document .querySelector (' #destination' )
279
294
)}
280
- < / Reference>
281
- {ReactDOM .createPortal (
282
- < Popper>
283
- {({ placement, ref, style }) => (
284
- < div ref= {ref} style= {style} data- placement= {placement}>
285
- Popper
286
- < / div>
287
- )}
288
- < / Popper> ,
289
- document .querySelector (' #destination' )
290
- )}
291
- < / Manager>
292
- );
295
+ < / >
296
+ );
297
+ };
293
298
` ` `
294
299
295
300
## Usage without a reference ` HTMLElement `
@@ -307,9 +312,12 @@ If `referenceElement` is defined, it will take precedence over any
307
312
` referenceProps .ref ` provided refs.
308
313
309
314
` ` ` jsx
310
- import { Popper } from ' react-popper' ;
315
+ import { usePopper } from ' react-popper' ;
311
316
312
- class VirtualReference {
317
+ // This is going to create a virtual reference element
318
+ // positioned 10px from top and left of the document
319
+ // 90px wide and 10px high
320
+ const virtualReference = {
313
321
getBoundingClientRect () {
314
322
return {
315
323
top: 10 ,
@@ -319,26 +327,21 @@ class VirtualReference {
319
327
width: 90 ,
320
328
height: 10 ,
321
329
};
322
- }
323
- }
324
-
325
- // This is going to create a virtual reference element
326
- // positioned 10px from top and left of the document
327
- // 90px wide and 10px high
328
- const virtualReferenceElement = new VirtualReference ();
330
+ },
331
+ };
329
332
330
333
// This popper will be positioned relatively to the
331
334
// virtual reference element defined above
332
- const Example = () => (
333
- < Popper referenceElement = {virtualReferenceElement} >
334
- {({ ref, style, placement, arrowProps }) => (
335
- < div ref = {ref} style = {style} data - placement = {placement} >
336
- Popper element
337
- < div ref= {arrowProps . ref } style= {arrowProps . style } / >
338
- < / div >
339
- )}
340
- < / Popper >
341
- ) ;
335
+ const Example = () => {
336
+ const [ popperElement , setPopperElement ] = React . useState ( null );
337
+ const { styles , attributes } = usePopper (virtualReference, popperElement);
338
+
339
+ return (
340
+ < div ref= {setPopperElement } style= {styles . popper } { ... attributes . popper } >
341
+ Popper element
342
+ < / div >
343
+ );
344
+ } ;
342
345
` ` `
343
346
344
347
## Flow and TypeScript types
0 commit comments