Skip to content

Commit

Permalink
Add mountNode prop to Popup module
Browse files Browse the repository at this point in the history
If a React app with SUIR is running in a hidden IFRAME and rendering
some or all of its virtual DOM to the parent frame or another frame,
then the Popup component should not insert popup elements into the
document.body of the React IFRAME, but instead should inject into the
frame containing the Popup trigger, or even into some other container
as may be more appropriate (due to size or layering concerns between
multiple frames).

Adding a `mountNode` prop to Popup allows developers to specify the
appropriate container for a displayed popup element in cases such as
the above example.
  • Loading branch information
svicalifornia committed Apr 29, 2022
1 parent 49f0056 commit 7832ab6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/modules/Popup/Popup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export interface StrictPopupProps extends StrictPortalProps {
/** Invert the colors of the popup */
inverted?: boolean

/** The node where the popup should mount. Defaults to document.body. */
mountNode?: any

/**
* Offset values in px unit to apply to rendered popup. The basic offset accepts an
* array with two numbers in the form [skidding, distance]:
Expand Down
9 changes: 9 additions & 0 deletions src/modules/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
customPropTypes,
getElementType,
getUnhandledProps,
isBrowser,
makeDebugger,
SUI,
useKeyOnly,
Expand Down Expand Up @@ -68,6 +69,9 @@ export default class Popup extends Component {
clearTimeout(this.timeoutId)
}

// Do not access document when server side rendering
getMountNode = () => (isBrowser() ? this.props.mountNode || document.body : null)

getPortalProps = () => {
debug('getPortalProps()')
const portalProps = {}
Expand Down Expand Up @@ -231,6 +235,7 @@ export default class Popup extends Component {
trigger,
} = this.props
const { closed, portalRestProps } = this.state
const mountNode = this.getMountNode()

if (closed || disabled) {
return trigger
Expand Down Expand Up @@ -285,6 +290,7 @@ export default class Popup extends Component {
return (
<Portal
{...mergedPortalProps}
mountNode={mountNode}
onClose={this.handleClose}
onMount={this.handlePortalMount}
onOpen={this.handleOpen}
Expand Down Expand Up @@ -349,6 +355,9 @@ Popup.propTypes = {
/** Invert the colors of the Popup. */
inverted: PropTypes.bool,

/** The node where the popup should mount. Defaults to document.body. */
mountNode: PropTypes.any,

/**
* Offset values in px unit to apply to rendered popup. The basic offset accepts an
* array with two numbers in the form [skidding, distance]:
Expand Down

0 comments on commit 7832ab6

Please sign in to comment.