You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.9 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ## React Popper
  2. [![Build Status](https://travis-ci.org/FezVrasta/react-popper.svg?branch=master)](https://travis-ci.org/FezVrasta/react-popper)
  3. [![npm version](https://img.shields.io/npm/v/react-popper.svg)](https://www.npmjs.com/package/react-popper)
  4. [![npm downloads](https://img.shields.io/npm/dm/react-popper.svg)](https://www.npmjs.com/package/react-popper)
  5. [![Dependency Status](https://david-dm.org/souporserious/react-popper.svg)](https://david-dm.org/souporserious/react-popper)
  6. [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  7. [![Get support or discuss](https://img.shields.io/badge/chat-on_spectrum-6833F9.svg?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyBpZD0iTGl2ZWxsb18xIiBkYXRhLW5hbWU9IkxpdmVsbG8gMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB2aWV3Qm94PSIwIDAgMTAgOCI%2BPGRlZnM%2BPHN0eWxlPi5jbHMtMXtmaWxsOiNmZmY7fTwvc3R5bGU%2BPC9kZWZzPjx0aXRsZT5zcGVjdHJ1bTwvdGl0bGU%2BPHBhdGggY2xhc3M9ImNscy0xIiBkPSJNNSwwQy40MiwwLDAsLjYzLDAsMy4zNGMwLDEuODQuMTksMi43MiwxLjc0LDMuMWgwVjcuNThhLjQ0LjQ0LDAsMCwwLC42OC4zNUw0LjM1LDYuNjlINWM0LjU4LDAsNS0uNjMsNS0zLjM1UzkuNTgsMCw1LDBaTTIuODMsNC4xOGEuNjMuNjMsMCwxLDEsLjY1LS42M0EuNjQuNjQsMCwwLDEsMi44Myw0LjE4Wk01LDQuMThhLjYzLjYzLDAsMSwxLC42NS0uNjNBLjY0LjY0LDAsMCwxLDUsNC4xOFptMi4xNywwYS42My42MywwLDEsMSwuNjUtLjYzQS42NC42NCwwLDAsMSw3LjE3LDQuMThaIi8%2BPC9zdmc%2B)](https://spectrum.chat/popper-js/react-popper)
  8. React wrapper around [Popper.js](https://popper.js.org).
  9. **important note:** popper.js is **not** a tooltip library, it's a _positioning engine_ to be used to build features such as (but not restricted to) tooltips.
  10. ## Install
  11. Via package managers:
  12. ```bash
  13. npm install react-popper --save
  14. # or
  15. yarn add react-popper
  16. ```
  17. Via `script` tag (UMD library exposed as `ReactPopper`):
  18. ```html
  19. <script src="https://unpkg.com/react-popper/dist/index.umd.js"></script>
  20. ```
  21. ## Usage
  22. > Using `react-popper@0.x`? You can find its documentation [clicking here](https://github.com/souporserious/react-popper/tree/v0.x)
  23. Example:
  24. ```jsx
  25. import { Manager, Reference, Popper } from 'react-popper';
  26. const Example = () => (
  27. <Manager>
  28. <Reference>
  29. {({ ref }) => (
  30. <button type="button" ref={ref}>
  31. Reference element
  32. </button>
  33. )}
  34. </Reference>
  35. <Popper placement="right">
  36. {({ ref, style, placement, arrowProps }) => (
  37. <div ref={ref} style={style} data-placement={placement}>
  38. Popper element
  39. <div ref={arrowProps.ref} style={arrowProps.style} />
  40. </div>
  41. )}
  42. </Popper>
  43. </Manager>
  44. );
  45. ```
  46. `react-popper` makes use of a React pattern called **"render prop"**, if you are not
  47. familiar with it, please read more [on the official React documentation](https://reactjs.org/docs/render-props.html).
  48. > Using React <=15 or Preact? The components created with them don't support to return
  49. > [fragments](https://reactjs.org/docs/fragments.html), this means that you will need to
  50. > wrap `<Reference />` and `<Popper />` into a single, common, `<div />` to make `react-popper` work.
  51. ### API documentation
  52. The `Manager` component is a simple wrapper that needs to surround all the other `react-popper` components in order
  53. to make them communicate with each others.
  54. The `Popper` component accepts the properties `children`, `placement`, `modifiers`, `eventsEnabled` and `positionFixed`.
  55. ```jsx
  56. <Popper
  57. innerRef={(node) => this.popperNode = node}
  58. placement="right"
  59. modifiers={{ preventOverflow: { enabled: false } }}
  60. eventsEnabled={true}
  61. positionFixed={false}
  62. >
  63. { props => [...] }
  64. </Popper>
  65. ```
  66. ##### `children`
  67. ```js
  68. children: ({|
  69. ref: (?HTMLElement) => void,
  70. style: { [string]: string | number },
  71. placement: ?Placement,
  72. outOfBoundaries: ?boolean,
  73. scheduleUpdate: () => void,
  74. arrowProps: {
  75. ref: (?HTMLElement) => void,
  76. style: { [string]: string | number },
  77. },
  78. |}) => Node
  79. ```
  80. A function (render prop) that takes as argument an object containing the following properties:
  81. - **`ref`**: used to retrieve the [React refs](https://reactjs.org/docs/refs-and-the-dom.html) of the **popper** element.
  82. - **`style`**: contains the necessary CSS styles (React CSS properties) which are computed by Popper.js to correctly position the **popper** element.
  83. - **`placement`**: describes the placement of your popper after Popper.js has applied all the modifiers
  84. that may have flipped or altered the originally provided `placement` property. You can use this to alter the
  85. style of the popper and or of the arrow according to the definitive placement. For instance, you can use this
  86. property to orient the arrow to the right direction.
  87. - **`outOfBoundaries`**: a boolean signifying if the popper element is overflowing its boundaries.
  88. - **`scheduleUpdate`**: a function you can call to schedule a Popper.js position update. It will directly call the [Popper#scheduleUpdate](https://popper.js.org/popper-documentation.html#Popper.scheduleUpdate) method.
  89. - **`arrowProps`**: an object, containing `style` and `ref` properties that are identical to the
  90. ones provided as the first and second arguments of `children`, but relative to the **arrow** element. The `style` property contains `left` and `top` offset values, which are used to center the arrow within the popper. These values can be merged with further custom styling and positioning. See [the demo](https://github.com/FezVrasta/react-popper/blob/8994933c430e48ab62e71495be71e4f440b48a5a/demo/styles.js#L100) for an example.
  91. ##### `innerRef`
  92. ```js
  93. innerRef?: (?HTMLElement) => void
  94. ```
  95. Function that can be used to obtain popper reference
  96. ##### `placement`
  97. ```js
  98. placement?: PopperJS$Placement;
  99. ```
  100. One of the accepted placement values listed in the [Popper.js documentation](https://popper.js.org/popper-documentation.html#Popper.placements).
  101. Your popper is going to be placed according to the value of this property.
  102. Defaults to `bottom`.
  103. ```js
  104. outOfBoundaries: ?boolean;
  105. ```
  106. A boolean that can be used to hide the popper element in case it's overflowing
  107. from its boundaries. [Read more](https://popper.js.org/popper-documentation.html#modifiers..hide).
  108. ##### `eventsEnabled`
  109. ```js
  110. eventsEnabled?: boolean;
  111. ```
  112. Tells `react-popper` to enable or disable the [Popper.js event listeners](https://popper.js.org/popper-documentation.html#Popper.Defaults.eventsEnabled). `true` by default.
  113. ##### `positionFixed`
  114. Set this property to `true` to tell Popper.js to use the `position: fixed` strategy
  115. to position the popper element. By default it's false, meaning that it will use the
  116. `position: absolute` strategy.
  117. ##### `modifiers`
  118. ```js
  119. modifiers?: PopperJS$Modifiers;
  120. ```
  121. An object containing custom settings for the [Popper.js modifiers](https://popper.js.org/popper-documentation.html#modifiers).
  122. You can use this property to override their settings or to inject your custom ones.
  123. ## Usage with `ReactDOM.createPortal`
  124. Popper.js is smart enough to work even if the **popper** and **reference** elements aren't
  125. in the same DOM context.
  126. This means that you can use [`ReactDOM.createPortal`](https://reactjs.org/docs/portals.html)
  127. (or any pre React 16 alternative) to move the popper component somewhere else in the DOM.
  128. This can be useful if you want to position a tooltip inside an `overflow: hidden` container
  129. that you want to make overflow. Please note that you can also try the `positionFixed` strategy
  130. to obtain a similar effect with less hassle.
  131. ```jsx
  132. import { Manager, Reference, Popper } from 'react-popper';
  133. const Example = () => (
  134. <Manager>
  135. <Reference>
  136. {({ ref }) => (
  137. <button type="button" ref={ref}>
  138. Reference
  139. </button>
  140. )}
  141. </Reference>
  142. {ReactDOM.createPortal(
  143. <Popper>
  144. {({ placement, ref, style }) => (
  145. <div ref={ref} style={style} data-placement={placement}>
  146. Popper
  147. </div>
  148. )}
  149. </Popper>,
  150. document.querySelector('#destination')
  151. )}
  152. </Manager>
  153. );
  154. ```
  155. ## Usage without a reference `HTMLElement`
  156. Whenever you need to position a popper based on some arbitrary coordinates, you can provide `Popper` with a `referenceElement` property that is going to be used in place of the `referenceProps.getRef` React ref.
  157. The `referenceElement` property must be an object with an interface compatible with an `HTMLElement` as described in the [Popper.js referenceObject documentation](https://popper.js.org/popper-documentation.html#referenceObject), this implies that you may also provide a real HTMLElement if needed.
  158. If `referenceElement` is defined, it will take precedence over any `referenceProps.ref` provided refs.
  159. ```jsx
  160. import { Popper } from 'react-popper';
  161. class VirtualReference {
  162. getBoundingClientRect() {
  163. return {
  164. top: 10,
  165. left: 10,
  166. bottom: 20,
  167. right: 100,
  168. width: 90,
  169. height: 10,
  170. };
  171. }
  172. get clientWidth() {
  173. return this.getBoundingClientRect().width;
  174. }
  175. get clientHeight() {
  176. return this.getBoundingClientRect().height;
  177. }
  178. }
  179. // This is going to create a virtual reference element
  180. // positioned 10px from top and left of the document
  181. // 90px wide and 10px high
  182. const virtualReferenceElement = new VirtualReference();
  183. // This popper will be positioned relatively to the
  184. // virtual reference element defined above
  185. const Example = () => (
  186. <Popper referenceElement={virtualReferenceElement}>
  187. {({ ref, style, placement, arrowProps }) => (
  188. <div ref={ref} style={style} data-placement={placement}>
  189. Popper element
  190. <div ref={arrowProps.ref} style={arrowProps.style} />
  191. </div>
  192. )}
  193. </Popper>
  194. );
  195. ```
  196. ## Flow and TypeScript types
  197. This library is built with Flow but it supports TypeScript as well.
  198. You can find the exported Flow types in `src/index.js`, and the
  199. TypeScript definitions in `typings/react-popper.d.ts`.
  200. ## Running Locally
  201. #### clone repo
  202. `git clone git@github.com:FezVrasta/react-popper.git`
  203. #### move into folder
  204. `cd ~/react-popper`
  205. #### install dependencies
  206. `npm install` or `yarn`
  207. #### run dev mode
  208. `npm run demo:dev` or `yarn demo:dev`
  209. #### open your browser and visit:
  210. `http://localhost:1234/`