Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Convert OnClickOutsideWrapper to TypeScript (DEV-1496) (#69418)

Fraser336c2a8b1af9
diff --git a/frontend/src/metabase/common/components/OnClickOutsideWrapper/OnClickOutsideWrapper.jsx b/frontend/src/metabase/common/components/OnClickOutsideWrapper/OnClickOutsideWrapper.tsx
similarity index 55%
rename from frontend/src/metabase/common/components/OnClickOutsideWrapper/OnClickOutsideWrapper.jsx
rename to frontend/src/metabase/common/components/OnClickOutsideWrapper/OnClickOutsideWrapper.tsx
index fae6c649d26..8a3bb4cb7d9 100644
--- a/frontend/src/metabase/common/components/OnClickOutsideWrapper/OnClickOutsideWrapper.jsx
+++ b/frontend/src/metabase/common/components/OnClickOutsideWrapper/OnClickOutsideWrapper.tsx
@@ -1,4 +1,4 @@
-import PropTypes from "prop-types";
+import type { ReactNode } from "react";
import { Children, Component, createRef } from "react";
import {
@@ -8,20 +8,32 @@ import {
} from "metabase/common/hooks/use-sequenced-content-close-handler";
import { Box } from "metabase/ui";
-export class OnClickOutsideWrapper extends Component {
- static propTypes = {
- children: PropTypes.node,
- backdropElement: PropTypes.object,
- handleDismissal: PropTypes.func.isRequired,
- ignoreElement: PropTypes.object,
- };
+interface OnClickOutsideWrapperProps {
+ children: ReactNode;
+ backdropElement?: Element;
+ handleDismissal: () => void;
+ ignoreElement?: Element;
+}
- contentRef = createRef();
+interface PopoverData {
+ contentEl: Element;
+ backdropEl?: Element;
+ close: (e: MouseEvent | KeyboardEvent) => void;
+ ignoreEl?: Element;
+}
+
+export class OnClickOutsideWrapper extends Component<OnClickOutsideWrapperProps> {
+ contentRef = createRef<HTMLDivElement>();
+ _timeout: ReturnType<typeof setTimeout> | null = null;
+ popoverData: PopoverData | null = null;
componentDidMount() {
// necessary to ignore click events that fire immediately, causing modals/popovers to close prematurely
this._timeout = setTimeout(() => {
const contentEl = this.contentRef.current;
+ if (!contentEl) {
+ return;
+ }
this.popoverData = {
contentEl,
@@ -32,9 +44,11 @@ export class OnClickOutsideWrapper extends Component {
RENDERED_POPOVERS.push(this.popoverData);
- // HACK: set the z-index of the parent element to ensure it"s always on top
- // NOTE: this actually doesn"t seem to be working correctly for popovers since PopoverBody creates a stacking context
- contentEl.parentNode.style.zIndex = RENDERED_POPOVERS.length + 2; // HACK: add 2 to ensure it"s in front of main and nav elements
+ // HACK: set the z-index of the parent element to ensure it's always on top
+ // NOTE: this actually doesn't seem to be working correctly for popovers since PopoverBody creates a stacking context
+ (contentEl.parentNode as HTMLElement).style.zIndex = String(
+ RENDERED_POPOVERS.length + 2,
+ ); // HACK: add 2 to ensure it's in front of main and nav elements
document.addEventListener("keydown", this._handleEvent, false);
window.addEventListener("mousedown", this._handleEvent, true);
@@ -44,18 +58,23 @@ export class OnClickOutsideWrapper extends Component {
componentWillUnmount() {
document.removeEventListener("keydown", this._handleEvent, false);
window.removeEventListener("mousedown", this._handleEvent, true);
- clearTimeout(this._timeout);
+ if (this._timeout) {
+ clearTimeout(this._timeout);
+ }
// remove from the stack after a delay, if it is removed through some other
// means this will happen too early causing parent modal to close
+ const popoverData = this.popoverData;
setTimeout(() => {
- removePopoverData(this.popoverData);
+ if (popoverData) {
+ removePopoverData(popoverData);
+ }
}, 0);
}
- _handleEvent = (e) => {
- if (shouldClosePopover(e, this.popoverData)) {
- this.popoverData.close();
+ _handleEvent = (e: KeyboardEvent | MouseEvent) => {
+ if (this.popoverData && shouldClosePopover(e, this.popoverData)) {
+ this.popoverData.close(e);
}
};