Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 80a3eb7

Browse files
authored
Merge pull request #346 from atomiks/patch-1
docs: optimize for hooks usage, fix v1 leftovers
2 parents 5b0a907 + 9024a6e commit 80a3eb7

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

README.md

+48-45
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ engine_ to be used to build features such as (but not restricted to) tooltips.
1717
Via package managers:
1818

1919
```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
2224
yarn add react-popper @popperjs/core
2325
```
2426

@@ -262,34 +264,37 @@ This means that you can use
262264
React 16 alternative) to move the popper component somewhere else in the DOM.
263265
264266
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.**
268271
269272
```jsx
270-
import { Manager, Reference, Popper } from 'react-popper';
273+
import { usePopper } from 'react-popper';
271274

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')
279294
)}
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+
};
293298
```
294299
295300
## Usage without a reference `HTMLElement`
@@ -307,9 +312,12 @@ If `referenceElement` is defined, it will take precedence over any
307312
`referenceProps.ref` provided refs.
308313
309314
```jsx
310-
import { Popper } from 'react-popper';
315+
import { usePopper } from 'react-popper';
311316

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 = {
313321
getBoundingClientRect() {
314322
return {
315323
top: 10,
@@ -319,26 +327,21 @@ class VirtualReference {
319327
width: 90,
320328
height: 10,
321329
};
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+
};
329332

330333
// This popper will be positioned relatively to the
331334
// 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+
};
342345
```
343346
344347
## Flow and TypeScript types

0 commit comments

Comments
 (0)