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

[core] feat(Overlay, Portal): allow portal to stop event propagation #6093

Merged
merged 2 commits into from
May 4, 2023
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
15 changes: 14 additions & 1 deletion packages/core/src/components/overlay/overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ export interface IOverlayableProps extends IOverlayLifecycleProps {
*/
portalContainer?: HTMLElement;

/**
* A list of DOM events which should be stopped from propagating through the Portal.
* This prop is ignored if `usePortal` is `false`.
*
* @see https://legacy.reactjs.org/docs/portals.html#event-bubbling-through-portals
* @see https://github.com/palantir/blueprint/issues/6124
*/
portalStopPropagationEvents?: Array<keyof HTMLElementEventMap>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted the name of this prop based on the other "portal" props on this interface. I admit it's getting a bit unwieldy and a portalProps prop might be better, but this is OK for now...


/**
* 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).
Expand Down Expand Up @@ -318,7 +327,11 @@ export class Overlay extends AbstractPureComponent2<OverlayProps, IOverlayState>
);
if (usePortal) {
return (
<Portal className={this.props.portalClassName} container={this.props.portalContainer}>
<Portal
className={this.props.portalClassName}
container={this.props.portalContainer}
stopPropagationEvents={this.props.portalStopPropagationEvents}
>
{transitionGroup}
</Portal>
);
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/components/portal/portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export interface IPortalProps extends Props {
* @default document.body
*/
container?: HTMLElement;

/**
* A list of DOM events which should be stopped from propagating through this portal element.
*
* @see https://legacy.reactjs.org/docs/portals.html#event-bubbling-through-portals
* @see https://github.com/palantir/blueprint/issues/6124
*/
stopPropagationEvents?: Array<keyof HTMLElementEventMap>;
}

export interface IPortalState {
Expand Down Expand Up @@ -103,6 +111,7 @@ export class Portal extends React.Component<PortalProps, IPortalState> {
}
this.portalElement = this.createContainerElement();
this.props.container.appendChild(this.portalElement);
this.addStopPropagationListeners(this.props.stopPropagationEvents);
/* eslint-disable-next-line react/no-did-mount-set-state */
this.setState({ hasMounted: true }, this.props.onChildrenMount);
}
Expand All @@ -113,9 +122,15 @@ export class Portal extends React.Component<PortalProps, IPortalState> {
maybeRemoveClass(this.portalElement.classList, prevProps.className);
maybeAddClass(this.portalElement.classList, this.props.className);
}

if (this.portalElement != null && prevProps.stopPropagationEvents !== this.props.stopPropagationEvents) {
this.removeStopPropagationListeners(prevProps.stopPropagationEvents);
this.addStopPropagationListeners(this.props.stopPropagationEvents);
}
}

public componentWillUnmount() {
this.removeStopPropagationListeners(this.props.stopPropagationEvents);
this.portalElement?.remove();
}

Expand All @@ -128,6 +143,14 @@ export class Portal extends React.Component<PortalProps, IPortalState> {
}
return container;
}

private addStopPropagationListeners(eventNames?: Array<keyof HTMLElementEventMap>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed these functions to be more specific

eventNames?.forEach(event => this.portalElement?.addEventListener(event, handleStopProgation));
}

private removeStopPropagationListeners(events?: Array<keyof HTMLElementEventMap>) {
events?.forEach(event => this.portalElement?.removeEventListener(event, handleStopProgation));
}
}

function maybeRemoveClass(classList: DOMTokenList, className?: string) {
Expand All @@ -141,3 +164,7 @@ function maybeAddClass(classList: DOMTokenList, className?: string) {
classList.add(...className.split(" "));
}
}

function handleStopProgation(e: Event) {
e.stopPropagation();
}