Skip to content

Commit

Permalink
Merge branch 'main' into dom-already
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Aug 20, 2024
2 parents e906148 + 9517124 commit 8b9e3f9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function elementReady(selector, {

// When it's ready, only stop if requested or found
if (isDomReady(target) && (stopOnDomReady || element)) {
return element ?? undefined; // No `null`
return element;
}

let current = element;
Expand Down Expand Up @@ -127,11 +127,14 @@ export function observeReadyElements(selector, {
};
}

function getMatchingElement({target, selector, predicate} = {}) {
if (predicate) {
const elements = target.querySelectorAll(selector);
return [...elements].find(element => predicate(element));
function getMatchingElement({target, selector, predicate}) {
if (!predicate) {
return target.querySelector(selector) ?? undefined; // No `null`
}

return target.querySelector(selector);
for (const element of target.querySelectorAll(selector)) {
if (predicate(element)) {
return element;
}
}
}

0 comments on commit 8b9e3f9

Please sign in to comment.