Skip to content

Commit

Permalink
[Popper] Add a modifiers property (mui#12108)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored and Joe Shelby committed Jul 14, 2018
1 parent 5f6597e commit 61eca44
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
49 changes: 23 additions & 26 deletions docs/src/pages/utils/popper/ScrollPlayground.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,17 @@ class AnchorPlayground extends React.Component {
<Popper
placement="${placement}"
disablePortal={${disablePortal}}
popperOptions={{
modifiers: {
flip: {
enabled: ${flip},
},
preventOverflow: {
enabled: ${preventOverflow !== 'disabled'},
boundariesElement: '${preventOverflow === 'disabled' ? 'scrollParent' : preventOverflow}',
},
arrow: {
enabled: ${arrow},
},
modifiers={{
flip: {
enabled: ${flip},
},
preventOverflow: {
enabled: ${preventOverflow !== 'disabled'},
boundariesElement: '${preventOverflow === 'disabled' ? 'scrollParent' : preventOverflow}',
},
arrow: {
enabled: ${arrow},
element: arrowRef,
},
}}
>
Expand Down Expand Up @@ -202,20 +201,18 @@ class AnchorPlayground extends React.Component {
placement={placement}
disablePortal={disablePortal}
className={classes.popper}
popperOptions={{
modifiers: {
flip: {
enabled: flip,
},
arrow: {
enabled: arrow,
element: arrowRef,
},
preventOverflow: {
enabled: preventOverflow !== 'disabled',
boundariesElement:
preventOverflow === 'disabled' ? 'scrollParent' : preventOverflow,
},
modifiers={{
flip: {
enabled: flip,
},
arrow: {
enabled: arrow,
element: arrowRef,
},
preventOverflow: {
enabled: preventOverflow !== 'disabled',
boundariesElement:
preventOverflow === 'disabled' ? 'scrollParent' : preventOverflow,
},
}}
>
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/Popper/Popper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PopperProps extends React.HTMLAttributes<HTMLDivElement> {
container?: PortalProps['container'];
disablePortal?: PortalProps['disablePortal'];
keepMounted?: boolean;
modifiers?: Object;
open: boolean;
placement?:
| 'bottom-end'
Expand Down
24 changes: 22 additions & 2 deletions packages/material-ui/src/Popper/Popper.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Popper extends React.Component {
if (
prevProps.anchorEl !== this.props.anchorEl ||
prevProps.popperOptions !== this.props.popperOptions ||
prevProps.modifiers !== this.props.modifiers ||
prevProps.disablePortal !== this.props.disablePortal ||
prevProps.placement !== this.props.placement
) {
Expand Down Expand Up @@ -81,7 +82,15 @@ class Popper extends React.Component {
}

handleRendered = () => {
const { anchorEl, open, placement, popperOptions = {}, theme, disablePortal } = this.props;
const {
anchorEl,
modifiers,
open,
placement,
popperOptions = {},
theme,
disablePortal,
} = this.props;
const popperNode = ReactDOM.findDOMNode(this);

if (this.popper) {
Expand All @@ -105,6 +114,7 @@ class Popper extends React.Component {
boundariesElement: 'viewport',
},
}),
...modifiers,
...popperOptions.modifiers,
},
// We could have been using a custom modifier like react-popper is doing.
Expand Down Expand Up @@ -193,7 +203,7 @@ Popper.propTypes = {
/**
* Popper render function or node.
*/
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
/**
* A node, component instance, or function that returns either.
* The `container` will passed to the Modal component.
Expand All @@ -212,6 +222,16 @@ Popper.propTypes = {
* when you want to maximize the responsiveness of the Popper.
*/
keepMounted: PropTypes.bool,
/**
* Popper.js is based on a "plugin-like" architecture,
* most of its features are fully encapsulated "modifiers".
*
* A modifier is a function that is called each time Popper.js needs to
* compute the position of the popper.
* For this reason, modifiers should be very performant to avoid bottlenecks.
* To learn how to create a modifier, [read the modifiers documentation](https://github.com/FezVrasta/popper.js/blob/master/docs/_includes/popper-documentation.md#modifiers--object).
*/
modifiers: PropTypes.object,
/**
* If `true`, the popper is visible.
*/
Expand Down
3 changes: 2 additions & 1 deletion pages/api/popper.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ Poppers rely on the 3rd party library [Popper.js](https://github.com/FezVrasta/p
| Name | Type | Default | Description |
|:-----|:-----|:--------|:------------|
| <span class="prop-name">anchorEl</span> | <span class="prop-type">union:&nbsp;object&nbsp;&#124;<br>&nbsp;func<br> |   | This is the DOM element, or a function that returns the DOM element, that may be used to set the position of the popover. |
| <span class="prop-name">children</span> | <span class="prop-type">union:&nbsp;node&nbsp;&#124;<br>&nbsp;func<br> |   | Popper render function or node. |
| <span class="prop-name required">children *</span> | <span class="prop-type">union:&nbsp;node&nbsp;&#124;<br>&nbsp;func<br> |   | Popper render function or node. |
| <span class="prop-name">container</span> | <span class="prop-type">union:&nbsp;object&nbsp;&#124;<br>&nbsp;func<br> |   | A node, component instance, or function that returns either. The `container` will passed to the Modal component. By default, it uses the body of the anchorEl's top-level document object, so it's simply `document.body` most of the time. |
| <span class="prop-name">disablePortal</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | Disable the portal behavior. The children stay within it's parent DOM hierarchy. |
| <span class="prop-name">keepMounted</span> | <span class="prop-type">bool |   | Always keep the children in the DOM. This property can be useful in SEO situation or when you want to maximize the responsiveness of the Popper. |
| <span class="prop-name">modifiers</span> | <span class="prop-type">object |   | Popper.js is based on a "plugin-like" architecture, most of its features are fully encapsulated "modifiers".<br>A modifier is a function that is called each time Popper.js needs to compute the position of the popper. For this reason, modifiers should be very performant to avoid bottlenecks. To learn how to create a modifier, [read the modifiers documentation](https://github.com/FezVrasta/popper.js/blob/master/docs/_includes/popper-documentation.md#modifiers--object). |
| <span class="prop-name required">open *</span> | <span class="prop-type">bool |   | If `true`, the popper is visible. |
| <span class="prop-name">placement</span> | <span class="prop-type">enum:&nbsp;'bottom-end', 'bottom-start', 'bottom', 'left-end', 'left-start', 'left', 'right-end', 'right-start', 'right', 'top-end', 'top-start', 'top'<br> | <span class="prop-default">'bottom'</span> | Popper placement. |
| <span class="prop-name">popperOptions</span> | <span class="prop-type">object |   | Options provided to the [`popper.js`](https://github.com/FezVrasta/popper.js) instance. |
Expand Down

0 comments on commit 61eca44

Please sign in to comment.