-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[Portal] SSR support, one fewer element in Overlay #2205
Changes from 5 commits
d721833
3fcf864
2a1057a
01b2e1b
857fc02
d47970e
0585e4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import * as classNames from "classnames"; | |
import * as React from "react"; | ||
import { CSSTransition, TransitionGroup } from "react-transition-group"; | ||
|
||
import { findDOMNode } from "react-dom"; | ||
import * as Classes from "../../common/classes"; | ||
import * as Keys from "../../common/keys"; | ||
import { IProps } from "../../common/props"; | ||
|
@@ -144,7 +145,7 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
// an HTMLElement that contains the backdrop and any children, to query for focus target | ||
public containerElement: HTMLElement; | ||
private refHandlers = { | ||
container: (ref: HTMLDivElement) => (this.containerElement = ref), | ||
container: (ref: React.ReactInstance) => (this.containerElement = findDOMNode(ref) as HTMLElement), | ||
}; | ||
|
||
public constructor(props?: IOverlayProps, context?: any) { | ||
|
@@ -160,32 +161,36 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
|
||
const { children, className, usePortal, isOpen, transitionDuration, transitionName } = this.props; | ||
|
||
const childrenWithTransitions = React.Children.map(children, (child?: React.ReactChild) => { | ||
if (child == null || typeof child !== "object") { | ||
return child; | ||
} | ||
// add a special class to each child element that will automatically set the appropriate | ||
// CSS position mode under the hood. also, make the container focusable so we can | ||
// trap focus inside it (via `enforceFocus`). | ||
const decoratedChild = React.cloneElement(child, { | ||
className: classNames(child.props.className, Classes.OVERLAY_CONTENT), | ||
tabIndex: 0, | ||
}); | ||
return ( | ||
<CSSTransition classNames={transitionName} timeout={transitionDuration}> | ||
{decoratedChild} | ||
</CSSTransition> | ||
); | ||
}); | ||
|
||
const transitionGroup = ( | ||
<TransitionGroup appear={true}> | ||
{this.maybeRenderBackdrop()} | ||
{isOpen ? childrenWithTransitions : null} | ||
</TransitionGroup> | ||
); | ||
|
||
const mergedClassName = classNames( | ||
// TransitionGroup types require single array of children; does not support nested arrays. | ||
// So we must collapse backdrop and children into one array, and every item must be wrapped in a | ||
// Transition element (no ReactText allowed). | ||
const childrenWithTransitions = isOpen | ||
? React.Children.map(children, (child?: React.ReactChild) => { | ||
if (child == null) { | ||
return null; | ||
} | ||
// add a special class to each child element that will automatically set the appropriate | ||
// CSS position mode under the hood. also, make the container focusable so we can | ||
// trap focus inside it (via `enforceFocus`). | ||
const decoratedChild = | ||
typeof child !== "object" ? ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: prefer reversing these conditions. read as |
||
<span className={Classes.OVERLAY_CONTENT}>{child}</span> | ||
) : ( | ||
React.cloneElement(child, { | ||
className: classNames(child.props.className, Classes.OVERLAY_CONTENT), | ||
tabIndex: 0, | ||
}) | ||
); | ||
return ( | ||
<CSSTransition classNames={transitionName} timeout={transitionDuration}> | ||
{decoratedChild} | ||
</CSSTransition> | ||
); | ||
}) | ||
: []; | ||
childrenWithTransitions.unshift(this.maybeRenderBackdrop()); | ||
|
||
const containerClasses = classNames( | ||
Classes.OVERLAY, | ||
{ | ||
[Classes.OVERLAY_OPEN]: isOpen, | ||
|
@@ -194,27 +199,21 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
className, | ||
); | ||
|
||
const elementProps = { | ||
className: mergedClassName, | ||
onKeyDown: this.handleKeyDown, | ||
}; | ||
|
||
const transitionGroup = ( | ||
<TransitionGroup | ||
appear={true} | ||
className={containerClasses} | ||
component="div" | ||
onKeyDown={this.handleKeyDown} | ||
ref={this.refHandlers.container} | ||
> | ||
{childrenWithTransitions} | ||
</TransitionGroup> | ||
); | ||
if (usePortal) { | ||
return ( | ||
<Portal | ||
{...elementProps} | ||
containerRef={this.refHandlers.container} | ||
onChildrenMount={this.handleContentMount} | ||
> | ||
{transitionGroup} | ||
</Portal> | ||
); | ||
return <Portal onChildrenMount={this.handleContentMount}>{transitionGroup}</Portal>; | ||
} else { | ||
return ( | ||
<span {...elementProps} ref={this.refHandlers.container}> | ||
{transitionGroup} | ||
</span> | ||
); | ||
return transitionGroup; | ||
} | ||
} | ||
|
||
|
@@ -289,7 +288,7 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
</CSSTransition> | ||
); | ||
} else { | ||
return undefined; | ||
return null; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,10 @@ import * as ReactDOM from "react-dom"; | |
|
||
import * as Classes from "../../common/classes"; | ||
import * as Errors from "../../common/errors"; | ||
import { IProps, removeNonHTMLProps } from "../../common/props"; | ||
import { IProps } from "../../common/props"; | ||
import { safeInvoke } from "../../common/utils"; | ||
|
||
export interface IPortalProps extends IProps, React.HTMLProps<HTMLDivElement> { | ||
/** | ||
* A React `ref` handler callback for the detached container root element. | ||
* As this component renders its contents into a separate container, the result of the `ref` | ||
* prop is not backed by a DOM node. Hence this callback is necessary to get the real DOM node. | ||
*/ | ||
containerRef?: (ref: HTMLDivElement) => void; | ||
|
||
/** | ||
* Callback invoked when the children of this `Portal` have been added to the DOM. | ||
*/ | ||
|
@@ -31,7 +24,7 @@ export interface IPortalState { | |
} | ||
|
||
export interface IPortalContext { | ||
/** Additional class to add to portal element */ | ||
/** Additional CSS classes to add to all `Portal elements in this React context. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing trailing backtick in docs |
||
blueprintPortalClassName?: string; | ||
} | ||
|
||
|
@@ -56,36 +49,51 @@ export class Portal extends React.Component<IPortalProps, IPortalState> { | |
public context: IPortalContext; | ||
public state: IPortalState = { hasMounted: false }; | ||
|
||
private targetElement: HTMLElement; | ||
|
||
constructor(props: IPortalProps, context: IPortalContext) { | ||
super(props, context); | ||
this.targetElement = document.createElement("div"); | ||
this.targetElement.classList.add(Classes.PORTAL); | ||
if (context.blueprintPortalClassName != null) { | ||
this.targetElement.classList.add(context.blueprintPortalClassName); | ||
} | ||
} | ||
private portalElement: HTMLElement; | ||
|
||
public render() { | ||
// Only render `children` once this component has mounted, so they are immediately attached to the DOM tree and | ||
// can do DOM things like measuring or `autoFocus`. See long comment on componentDidMount in | ||
// https://reactjs.org/docs/portals.html#event-bubbling-through-portals | ||
return ReactDOM.createPortal( | ||
<div {...removeNonHTMLProps(this.props)} ref={this.props.containerRef}> | ||
{this.state.hasMounted ? this.props.children : null} | ||
</div>, | ||
this.targetElement, | ||
); | ||
// Only render `children` once this component has mounted in a browser environment, so they are | ||
// immediately attached to the DOM tree and can do DOM things like measuring or `autoFocus`. | ||
// See long comment on componentDidMount in https://reactjs.org/docs/portals.html#event-bubbling-through-portals | ||
return typeof document !== "undefined" && this.state.hasMounted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a little too clever to read as a ternary, please make it more verbose w/ if (typeof document === "undefined") {
return null;
} else if (!this.state.hasMounted) {
return null;
} else {
...
} |
||
? ReactDOM.createPortal(this.props.children, this.portalElement) | ||
: null; | ||
} | ||
|
||
public componentDidMount() { | ||
document.body.appendChild(this.targetElement); | ||
this.portalElement = this.createElement(); | ||
document.body.appendChild(this.portalElement); | ||
safeInvoke(this.props.onChildrenMount); | ||
this.setState({ hasMounted: true }); | ||
} | ||
|
||
public componentDidUpdate(prevProps: IPortalProps) { | ||
// update className prop on portal DOM element | ||
if (this.portalElement != null && prevProps.className !== this.props.className) { | ||
this.portalElement.classList.remove(prevProps.className); | ||
maybeAddClass(this.portalElement.classList, this.props.className); | ||
} | ||
} | ||
|
||
public componentWillUnmount() { | ||
this.targetElement.remove(); | ||
if (this.portalElement != null) { | ||
this.portalElement.remove(); | ||
} | ||
} | ||
|
||
private createElement() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
const container = document.createElement("div"); | ||
container.classList.add(Classes.PORTAL); | ||
maybeAddClass(container.classList, this.props.className); | ||
if (this.context != null) { | ||
maybeAddClass(container.classList, this.context.blueprintPortalClassName); | ||
} | ||
return container; | ||
} | ||
} | ||
|
||
function maybeAddClass(classList: DOMTokenList, className?: string) { | ||
if (className != null && className !== "") { | ||
classList.add(className); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,6 @@ const customChildren = { | |
Toaster: React.createElement(Core.Toast, { message: "Toast" }), | ||
}; | ||
|
||
const skipList = [ | ||
"Portal", // doesn't render any DOM inline | ||
] | ||
|
||
describe("Core isomorphic rendering", () => { | ||
generateIsomorphicTests(Core, customProps, customChildren, skipList); | ||
generateIsomorphicTests(Core, customProps, customChildren); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice 💯 |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this now receives the
Portal
instance, so we unwrap it into underlying DOM node.