Skip to content

Commit

Permalink
component.Base: waitForDomRect() #6296
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiu committed Jan 25, 2025
1 parent 5dcba23 commit 9b783da
Showing 2 changed files with 38 additions and 3 deletions.
38 changes: 37 additions & 1 deletion src/component/Base.mjs
Original file line number Diff line number Diff line change
@@ -1684,7 +1684,7 @@ class Component extends Base {
* Convenience shortcut
* @param {String[]|String} id=this.id
* @param {String} appName=this.appName
* @returns {Promise<Neo.util.Rectangle>}
* @returns {Promise<Neo.util.Rectangle||Neo.util.Rectangle[]>}
*/
async getDomRect(id=this.id, appName=this.appName) {
let result = await Neo.main.DomAccess.getBoundingClientRect({appName, id, windowId: this.windowId});
@@ -1696,6 +1696,42 @@ class Component extends Base {
return Rectangle.clone(result)
}

/**
* In case you are sure a DOMRect exists, use getDomRect()
* Otherwise you can wait for it using this method.
* @example:
* await this.render(true);
* await this.waitForDomRect();
* @param {Object} opts
* @param {String} opts.appName=this.appName
* @param {Number} opts.attempts=10 Reruns in case the rect height or width equals 0
* @param {Number} opts.delay=50 Time in ms before checking again
* @param {String[]|String} opts.id=this.id
* @returns {Promise<Neo.util.Rectangle||Neo.util.Rectangle[]>}
*/
async waitForDomRect({appName=this.appName, attempts=10, delay=50, id=this.id}) {
let me = this,
result = await me.getDomRect(id, appName),
reRun = false;

if (Array.isArray(result)) {
result.forEach(rect => {
if (rect.height < 1 || rect.width < 1) {
reRun = true
}
})
} else if (result.height < 1 || result.width < 1) {
reRun = true
}

if (reRun && attempts > 0) {
await me.timeout(delay);
return await me.waitForDomRect({appName, attempts: attempts-1, delay, id})
}

return result
}

/**
* Honors different item roots for mount / render OPs
* @returns {String}
3 changes: 1 addition & 2 deletions src/dialog/Base.mjs
Original file line number Diff line number Diff line change
@@ -426,9 +426,8 @@ class Dialog extends Panel {

// rendered outside the visible area
await me.render(true);
await me.timeout(150);

let [dialogRect, bodyRect] = await me.getDomRect([me.id, 'document.body']);
let [dialogRect, bodyRect] = await me.waitForDomRect({id: [me.id, 'document.body']});

// Move to cover the animation target
await Neo.applyDeltas(appName, {

0 comments on commit 9b783da

Please sign in to comment.