@@ -33,329 +33,12 @@ Via `script` tag (UMD library exposed as `ReactPopper`):
33
33
<script src =" https://unpkg.com/react-popper/dist/index.umd.js" ></script >
34
34
```
35
35
36
- ## Usage
36
+ ## Documentation
37
37
38
- > Using ` react-popper@0.x ` ? You can find its documentation
39
- > [ clicking here] ( https://github.com/souporserious/react-popper/tree/v0.x )
38
+ The full documentation can be found on the official Popper website:
40
39
41
- ` react-popper ` provides two different APIs to help consume it:
40
+ http://popper.js.org/ react-popper
42
41
43
- ### 1. React Hooks (recommended)
44
-
45
- The ` usePopper ` hook can be used to quickly initialize a Popper, it requires a
46
- basic understanding of how the
47
- [ React Hooks] ( https://reactjs.org/docs/hooks-overview.html ) work.
48
-
49
- ``` jsx
50
- import { usePopper } from ' react-popper' ;
51
-
52
- const Example = () => {
53
- const [referenceElement , setReferenceElement ] = React .useState (null );
54
- const [popperElement , setPopperElement ] = React .useState (null );
55
- const [arrowElement , setArrowElement ] = React .useState (null );
56
- const { styles , attributes } = usePopper (referenceElement, popperElement, {
57
- modifiers: [{ name: ' arrow' , options: { element: arrowElement } }],
58
- });
59
-
60
- return (
61
- <>
62
- < button type= " button" ref= {setReferenceElement}>
63
- Reference element
64
- < / button>
65
-
66
- < div ref= {setPopperElement} style= {styles .popper } {... attributes .popper }>
67
- Popper element
68
- < div ref= {setArrowElement} style= {styles .arrow } / >
69
- < / div>
70
- < / >
71
- );
72
- };
73
- ```
74
-
75
- ### 2. Render Props (legacy)
76
-
77
- This is a legacy API for compatibility with v1.x users moving to Popper v2. We
78
- recommend using the ` usePopper ` Hook in new code.
79
-
80
- <details >
81
- <summary >View details</summary >
82
-
83
- The ` Manager ` , ` Reference ` and ` Popper ` render-props components offer an
84
- alternative API to initialize a Popper instance, they require a basic
85
- understanding of how the
86
- [ React Render Props] ( https://reactjs.org/docs/render-props.html ) work.
87
-
88
- ``` jsx
89
- import { Manager , Reference , Popper } from ' react-popper' ;
90
-
91
- const Example = () => (
92
- < Manager>
93
- < Reference>
94
- {({ ref }) => (
95
- < button type= " button" ref= {ref}>
96
- Reference element
97
- < / button>
98
- )}
99
- < / Reference>
100
- < Popper placement= " right" >
101
- {({ ref, style, placement, arrowProps }) => (
102
- < div ref= {ref} style= {style} data- placement= {placement}>
103
- Popper element
104
- < div ref= {arrowProps .ref } style= {arrowProps .style } / >
105
- < / div>
106
- )}
107
- < / Popper>
108
- < / Manager>
109
- );
110
- ```
111
-
112
- </details >
113
-
114
- ## API documentation
115
-
116
- ### ` usePopper `
117
-
118
- The ` usePopper ` hook provides an API almost identical to the ones of
119
- [ ` createPopper ` ] ( https://popper.js.org/docs/v2/constructors/#createpopper )
120
- constructor.
121
-
122
- Rather than returning a Popper instance, it will return an object containing the
123
- following properties:
124
-
125
- ##### ` styles `
126
-
127
- The ` styles ` property is an object, its properties are ` popper ` , and ` arrow ` .
128
- The two properties are a
129
- [ ` CSSStyleDeclaration ` interface] ( https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration )
130
- describing the necessary CSS properties needed to properly position the two
131
- elements.
132
-
133
- You can directly assign the value of the two properties to the React ` style `
134
- property of your components.
135
-
136
- ``` js
137
- < div style= {styles .popper } / >
138
- ```
139
-
140
- ##### ` attributes `
141
-
142
- Similar to ` styles ` , the ` attributes ` object lists the ` popper ` and ` arrow ` HTML
143
- attributes, by default, only ` popper ` will hold some attributes (e.g.
144
- ` data-popper-placement ` ), but more generically, any HTML attribute described by
145
- the Popper documentation will be available inside these properties.
146
-
147
- The easiest way to consume their values is by destructuring them directly onto
148
- your React component.
149
-
150
- ``` js
151
- < div {... attributes .popper } / >
152
- ```
153
-
154
- ##### ` update ` , ` forceUpdate ` , and ` state `
155
-
156
- These properties match the ones described in the
157
- [ Popper docs] ( https://popper.js.org/docs/v2/constructors/#types ) , the only
158
- difference is that they can be ` null ` if Popper isn't yet been initialized or
159
- has been destroyed.
160
-
161
- ### Render Props
162
-
163
- This is a legacy API for compatibility with v1.x users moving to Popper v2. We
164
- recommend using the ` usePopper ` Hook in new code.
165
-
166
- <details >
167
- <summary >View details</summary >
168
-
169
- The ` Manager ` component is a simple wrapper that needs to surround all the other
170
- ` react-popper ` components in order to make them communicate with each others.
171
-
172
- The ` Popper ` component accepts the properties ` children ` , ` placement ` ,
173
- ` modifiers ` and ` strategy ` .
174
-
175
- ``` jsx
176
- < Popper
177
- innerRef= {(node ) => this .popperNode = node}
178
- placement= " right"
179
- modifiers= {[{ name: ' preventOverflow' , enabled: false }]}
180
- strategy= " fixed"
181
- >
182
- { props => [... ] }
183
- < / Popper>
184
- ```
185
-
186
- ##### ` children `
187
-
188
- ``` js
189
- children: ({|
190
- ref : (? HTMLElement ) => void ,
191
- style: { [string]: string | number },
192
- placement : ? Placement,
193
- isReferenceHidden: ? boolean,
194
- hasPopperEscaped: ? boolean,
195
- update: () => void ,
196
- forceUpdate : () => void ,
197
- arrowProps : {
198
- ref : (? HTMLElement ) => void ,
199
- style: { [string]: string | number },
200
- },
201
- | }) => Node
202
- ` ` `
203
-
204
- A function (render prop) that takes as argument an object containing the
205
- following properties:
206
-
207
- - **` ref` **: used to retrieve the
208
- [React refs](https://reactjs.org/docs/refs-and-the-dom.html) of the **popper**
209
- element.
210
- - **` style` **: contains the necessary CSS styles (React CSS properties) which
211
- are computed by Popper.js to correctly position the **popper** element.
212
- - **` placement` **: describes the placement of your popper after Popper.js has
213
- applied all the modifiers that may have flipped or altered the originally
214
- provided ` placement` property. You can use this to alter the style of the
215
- popper and or of the arrow according to the definitive placement. For
216
- instance, you can use this property to orient the arrow to the right
217
- direction.
218
- - **` isReferenceHidden` **: a boolean signifying the reference element is fully
219
- clipped and hidden from view.
220
- - **` hasPopperEscaped` **: a boolean signifying the popper escapes the reference
221
- element's boundary (and so it appears detached).
222
- - **` update` **: a function you can ask Popper to recompute your tooltip's
223
- position . It will directly call the
224
- [Popper#update](https://popper.js.org/docs/v2/lifecycle/#manual-update)
225
- method.
226
- - **` arrowProps` **: an object, containing ` style` and ` ref` properties that are
227
- identical to the ones provided as the first and second arguments of
228
- ` children` , but relative to the **arrow** element. The ` style` property
229
- contains ` left` and ` top` offset values, which are used to center the arrow
230
- within the popper. These values can be merged with further custom styling and
231
- positioning. See
232
- [the demo](https://github.com/FezVrasta/react-popper/blob/8994933c430e48ab62e71495be71e4f440b48a5a/demo/styles.js#L100)
233
- for an example.
234
-
235
- ##### ` innerRef`
236
-
237
- ` ` ` js
238
- innerRef?: (? HTMLElement ) => void
239
- ` ` `
240
-
241
- Function that can be used to obtain popper reference
242
-
243
- ##### ` placement`
244
-
245
- ` ` ` js
246
- placement?: PopperJS$Placement;
247
- ` ` `
248
-
249
- One of the accepted placement values listed in the
250
- [Popper.js documentation](https://popper.js.org/popper-documentation.html#Popper.placements).
251
- Your popper is going to be placed according to the value of this property.
252
- Defaults to ` bottom` .
253
-
254
- ##### ` strategy`
255
-
256
- Describes the positioning strategy to use. By default, it is ` absolute` , which
257
- in the simplest cases does not require repositioning of the popper. If your
258
- reference element is in a ` fixed` container, use the ` fixed` strategy.
259
- [Read More](https://popper.js.org/docs/v2/constructors/#strategy)
260
-
261
- ##### ` modifiers`
262
-
263
- ` ` ` js
264
- modifiers?: PopperJS$Modifiers;
265
- ` ` `
266
-
267
- An object containing custom settings for the
268
- [Popper.js modifiers](https://popper.js.org/docs/v2/modifiers/).
269
- You can use this property to override their settings or to inject your custom
270
- ones.
271
-
272
- </details>
273
-
274
- ## Usage with ` ReactDOM .createPortal `
275
-
276
- Popper.js is smart enough to work even if the **popper** and **reference**
277
- elements aren't in the same DOM context.
278
- This means that you can use
279
- [` ReactDOM .createPortal ` ](https://reactjs.org/docs/portals.html) (or any pre
280
- React 16 alternative) to move the popper component somewhere else in the DOM.
281
-
282
- This can be useful if you want to position a tooltip inside an
283
- ` overflow: hidden ` container that you want to make overflow.
284
-
285
- **Please note that you can also try ` strategy : ' fixed' ` to obtain a similar
286
- effect with less hassle.**
287
-
288
- ` ` ` jsx
289
- import { usePopper } from ' react-popper' ;
290
-
291
- const Example = () => {
292
- const [referenceElement , setReferenceElement ] = React .useState (null );
293
- const [popperElement , setPopperElement ] = React .useState (null );
294
- const { styles , attributes } = usePopper (referenceElement, popperElement);
295
-
296
- return (
297
- <>
298
- < button type= " button" ref= {setReferenceElement}>
299
- Reference
300
- < / button>
301
- {ReactDOM .createPortal (
302
- < div
303
- ref= {setPopperElement}
304
- style= {styles .popper }
305
- {... attributes .popper }
306
- >
307
- Popper
308
- < / div> ,
309
- document .querySelector (' #destination' )
310
- )}
311
- < / >
312
- );
313
- };
314
- ` ` `
315
-
316
- ## Usage without a reference ` HTMLElement `
317
-
318
- Whenever you need to position a popper based on some arbitrary coordinates, you
319
- can provide Popper with a
320
- [virtual element](https://popper.js.org/docs/v2/virtual-elements/).
321
-
322
- ` ` ` jsx
323
- import { usePopper } from ' react-popper' ;
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 virtualReference = {
329
- getBoundingClientRect () {
330
- return {
331
- top: 10 ,
332
- left: 10 ,
333
- bottom: 20 ,
334
- right: 100 ,
335
- width: 90 ,
336
- height: 10 ,
337
- };
338
- },
339
- };
340
-
341
- const Example = () => {
342
- const [popperElement , setPopperElement ] = React .useState (null );
343
- const { styles , attributes } = usePopper (virtualReference, popperElement);
344
-
345
- return (
346
- < div ref= {setPopperElement} style= {styles .popper } {... attributes .popper }>
347
- Popper element
348
- < / div>
349
- );
350
- };
351
- ` ` `
352
-
353
- ## Flow and TypeScript types
354
-
355
- This library is built with Flow but it supports TypeScript as well.
356
-
357
- You can find the exported Flow types in ` src / index .js ` , and the TypeScript
358
- definitions in ` typings / react - popper .d .ts ` .
359
42
360
43
## Running Locally
361
44
0 commit comments