Skip to content

Commit

Permalink
feat: Add waitForElement helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
corovcam committed May 22, 2024
1 parent f526300 commit f4d3e15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
21 changes: 1 addition & 20 deletions src/components/file-management-dialog/UppyDashboardDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useEffect, useRef, useCallback } from "preact/hooks";
import { useUppyContext, useAppContext, useWorker } from "../../hooks";
import czechLocale from "../../utils/locales/czechLocale";
import englishLocale from "../../utils/locales/englishLocale";
import { waitForElement } from "../../utils/helpers";

import { debugLogger } from "@uppy/core";
import { DashboardModal } from "@uppy/react";
Expand All @@ -14,26 +15,6 @@ import cs_CZ from "@uppy/locales/lib/cs_CZ";

import PropTypes from "prop-types";

function waitForElement(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}

const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve(document.querySelector(selector));
}
});

observer.observe(document.body, {
childList: true,
subtree: true
});
});
}

const UppyDashboardDialog = ({
modalOpen,
setModalOpen,
Expand Down
19 changes: 19 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function waitForElement(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}

const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve(document.querySelector(selector));
}
});

observer.observe(document.body, {
childList: true,
subtree: true
});
});
}

0 comments on commit f4d3e15

Please sign in to comment.