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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Overlay] better lifecycle props for all overlay components #2581
Changes from 7 commits
bff8489
7966baf
920ec84
2082975
e7abc1d
67bca7b
be13a7e
09a6c59
26b2ecc
9b9a0ec
4605ede
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ import { IProps } from "../../common/props"; | |
import { safeInvoke } from "../../common/utils"; | ||
import { Portal } from "../portal/portal"; | ||
|
||
export interface IOverlayableProps { | ||
export interface IOverlayableProps extends IOverlayLifecycleProps { | ||
/** | ||
* Whether the overlay should acquire application focus when it first opens. | ||
* @default true | ||
|
@@ -72,11 +72,26 @@ export interface IOverlayableProps { | |
/** | ||
* A callback that is invoked when user interaction causes the overlay to close, such as | ||
* clicking on the overlay or pressing the `esc` key (if enabled). | ||
* | ||
* Receives the event from the user's interaction, if there was an event (generally either a | ||
* mouse or key event). Note that, since this component is controlled by the `isOpen` prop, it | ||
* will not actually close itself until that prop becomes `false`. | ||
*/ | ||
onClose?(event?: React.SyntheticEvent<HTMLElement>): void; | ||
onClose?: (event?: React.SyntheticEvent<HTMLElement>) => void; | ||
} | ||
|
||
export interface IOverlayLifecycleProps { | ||
/** Lifecycle method invoked when an Overlay begins to close. (Specifically, when the close transition begins.) */ | ||
onClosing?: () => void; | ||
|
||
/** Lifecycle method invoked when an Overlay has finished transitioning to the closed state. */ | ||
onClosed?: () => void; | ||
|
||
/** Lifecycle method invoked when an Overlay begins to open. */ | ||
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. i think it might be clearer to say: Because we're dealing with transitions, "when" is ambiguous so we should say "before" or "after" the transition. Same with |
||
onOpening?: () => void; | ||
|
||
/** Lifecycle method invoked when an Overlay has finished transitioning to the open state. */ | ||
onOpened?: () => void; | ||
} | ||
|
||
export interface IBackdropProps { | ||
|
@@ -101,16 +116,6 @@ export interface IBackdropProps { | |
} | ||
|
||
export interface IOverlayProps extends IOverlayableProps, IBackdropProps, IProps { | ||
/** Lifecycle callback invoked after the overlay opens and is mounted in the DOM. */ | ||
didOpen?: () => any; | ||
|
||
/** | ||
* Lifecycle callback invoked after a child element finishes exiting the DOM. | ||
* This will be invoked for each child of the `Overlay` except for the backdrop element. | ||
* The argument is the underlying HTML element that left the DOM. | ||
*/ | ||
didClose?: (node: HTMLElement) => any; | ||
|
||
/** | ||
* Toggles the visibility of the overlay and its children. | ||
* This prop is required because the component is controlled. | ||
|
@@ -195,7 +200,7 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
</TransitionGroup> | ||
); | ||
if (usePortal) { | ||
return <Portal onChildrenMount={this.handleContentMount}>{transitionGroup}</Portal>; | ||
return <Portal>{transitionGroup}</Portal>; | ||
} else { | ||
return transitionGroup; | ||
} | ||
|
@@ -266,9 +271,16 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
) : ( | ||
<span className={Classes.OVERLAY_CONTENT}>{child}</span> | ||
); | ||
const { transitionDuration, transitionName } = this.props; | ||
const { onOpening, onOpened, onClosing, onClosed, transitionDuration, transitionName } = this.props; | ||
return ( | ||
<CSSTransition classNames={transitionName} onExited={this.props.didClose} timeout={transitionDuration}> | ||
<CSSTransition | ||
classNames={transitionName} | ||
onEntering={onOpening} | ||
onEntered={onOpened} | ||
onExiting={onClosing} | ||
onExited={onClosed} | ||
timeout={transitionDuration} | ||
> | ||
{decoratedChild} | ||
</CSSTransition> | ||
); | ||
|
@@ -339,9 +351,7 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
document.addEventListener("mousedown", this.handleDocumentClick); | ||
} | ||
|
||
if (!this.props.usePortal) { | ||
safeInvoke(this.props.didOpen); | ||
} else if (this.props.hasBackdrop) { | ||
if (this.props.hasBackdrop && this.props.usePortal) { | ||
// add a class to the body to prevent scrolling of content below the overlay | ||
document.body.classList.add(Classes.OVERLAY_OPEN); | ||
} | ||
|
@@ -378,12 +388,6 @@ export class Overlay extends React.PureComponent<IOverlayProps, IOverlayState> { | |
} | ||
}; | ||
|
||
private handleContentMount = () => { | ||
if (this.props.isOpen) { | ||
safeInvoke(this.props.didOpen); | ||
} | ||
}; | ||
|
||
private handleDocumentFocus = (e: FocusEvent) => { | ||
if ( | ||
this.props.enforceFocus && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,31 +134,11 @@ export interface IPopoverProps extends IOverlayableProps, IProps { | |
*/ | ||
popoverClassName?: string; | ||
|
||
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 major API deprecation. What is the upgrade path for existing code? 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. Note: this interface inherits the new lifecycle interface, so this will just be a rename. 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. |
||
/** | ||
* Callback invoked after the popover closes and has been removed from the DOM. | ||
*/ | ||
popoverDidClose?: () => void; | ||
|
||
/** | ||
* Callback invoked when the popover opens after it is added to the DOM. | ||
*/ | ||
popoverDidOpen?: () => void; | ||
|
||
/** | ||
* Ref supplied to the `Classes.POPOVER` element. | ||
*/ | ||
popoverRef?: (ref: HTMLDivElement | null) => void; | ||
|
||
/** | ||
* Callback invoked when a popover begins to close. | ||
*/ | ||
popoverWillClose?: () => void; | ||
|
||
/** | ||
* Callback invoked before the popover opens. | ||
*/ | ||
popoverWillOpen?: () => void; | ||
|
||
/** | ||
* Space-delimited string of class names applied to the | ||
* portal that holds the popover if `usePortal={true}`. | ||
|
@@ -248,8 +228,6 @@ export class Popover extends AbstractPureComponent<IPopoverProps, IPopoverState> | |
/** DOM element that contains the target. */ | ||
public targetElement: HTMLElement; | ||
|
||
// a flag that is set to true while we are waiting for the underlying Portal to complete rendering | ||
private isContentMounting = false; | ||
private cancelOpenTimeout: () => void; | ||
|
||
// a flag that lets us detect mouse movement between the target and popover, | ||
|
@@ -277,7 +255,7 @@ export class Popover extends AbstractPureComponent<IPopoverProps, IPopoverState> | |
} | ||
|
||
public render() { | ||
const { className, disabled, hasBackdrop, targetClassName, targetElementTag } = this.props; | ||
const { className, disabled, targetClassName, targetElementTag } = this.props; | ||
const { isOpen } = this.state; | ||
const isHoverInteractionKind = this.isHoverInteractionKind(); | ||
|
||
|
@@ -333,15 +311,17 @@ export class Popover extends AbstractPureComponent<IPopoverProps, IPopoverState> | |
canEscapeKeyClose={this.props.canEscapeKeyClose} | ||
canOutsideClickClose={this.props.interactionKind === PopoverInteractionKind.CLICK} | ||
className={this.props.portalClassName} | ||
didClose={this.props.popoverDidClose} | ||
didOpen={this.handleContentMount} | ||
enforceFocus={this.props.enforceFocus} | ||
hasBackdrop={hasBackdrop} | ||
usePortal={this.props.usePortal} | ||
hasBackdrop={this.props.hasBackdrop} | ||
isOpen={isOpen && !isContentEmpty} | ||
onClose={this.handleOverlayClose} | ||
onClosed={this.props.onClosed} | ||
onClosing={this.props.onClosing} | ||
onOpened={this.props.onOpened} | ||
onOpening={this.props.onOpening} | ||
transitionDuration={this.props.transitionDuration} | ||
transitionName={Classes.POPOVER} | ||
usePortal={this.props.usePortal} | ||
> | ||
{this.renderPopper(children.content)} | ||
</Overlay> | ||
|
@@ -369,15 +349,6 @@ export class Popover extends AbstractPureComponent<IPopoverProps, IPopoverState> | |
} | ||
} | ||
|
||
public componentWillUpdate(_: IPopoverProps, nextState: IPopoverState) { | ||
if (!this.state.isOpen && nextState.isOpen) { | ||
this.isContentMounting = true; | ||
Utils.safeInvoke(this.props.popoverWillOpen); | ||
} else if (this.state.isOpen && !nextState.isOpen) { | ||
Utils.safeInvoke(this.props.popoverWillClose); | ||
} | ||
} | ||
|
||
public componentDidUpdate() { | ||
this.updateDarkParent(); | ||
} | ||
|
@@ -504,13 +475,6 @@ export class Popover extends AbstractPureComponent<IPopoverProps, IPopoverState> | |
} | ||
} | ||
|
||
private handleContentMount = () => { | ||
if (this.isContentMounting) { | ||
Utils.safeInvoke(this.props.popoverDidOpen); | ||
this.isContentMounting = false; | ||
} | ||
}; | ||
|
||
private handleTargetFocus = (e: React.FocusEvent<HTMLElement>) => { | ||
if (this.props.openOnTargetFocus && this.isHoverInteractionKind()) { | ||
if (e.relatedTarget == null && !this.lostFocusOnSamePage) { | ||
|
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 seems like a pretty major change where users could possible pass props to the popover this way, now they can't.
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 component is not even exported, so it think we're safe here. users use either a decorator
@ContextMenuTarget
or a static methodContextMenu.show()
(which looks like this component but isn't).