Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Popper] Add a modifiers property #12108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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