Skip to content

Commit 5f6454e

Browse files
committed
wip 2
1 parent cd7101b commit 5f6454e

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

packages/react/src/utils/useWaitForComponentMount.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const createAwaitableMutationObserver = (
99

1010
return (options: { selector: string; root?: HTMLElement | null; timeout?: number }) =>
1111
new Promise<void>((resolve, reject) => {
12-
console.log('Selector', options.selector);
1312
const { root = document?.body, selector, timeout = 0 } = options;
1413

1514
if (!root) {
@@ -31,19 +30,15 @@ const createAwaitableMutationObserver = (
3130
// Set up a MutationObserver to detect when the element has children
3231
const observer = new MutationObserver(mutationsList => {
3332
for (const mutation of mutationsList) {
34-
console.log('Mutation', mutation);
3533
if (!elementToWatch && selector) {
3634
elementToWatch = root?.querySelector(selector);
3735
}
38-
console.log('elementToWatch', elementToWatch);
3936

4037
if (
4138
(globalOptions.childList && mutation.type === 'childList') ||
4239
(globalOptions.attributes && mutation.type === 'attributes')
4340
) {
44-
console.log('isReady', isReady(elementToWatch, selector));
4541
if (isReady(elementToWatch, selector)) {
46-
console.log('disconnecting');
4742
observer.disconnect();
4843
resolve();
4944
return;
@@ -67,17 +62,17 @@ const createAwaitableMutationObserver = (
6762
const waitForElementChildren = createAwaitableMutationObserver({
6863
childList: true,
6964
subtree: true,
70-
isReady: (el: HTMLElement | null) => !!el?.childElementCount && el.childElementCount > 0,
65+
isReady: (el, selector) => !!el?.childElementCount && el?.matches?.(selector) && el.childElementCount > 0,
7166
});
7267

73-
const waitForElementAttribute = createAwaitableMutationObserver({
74-
attributes: true,
75-
// childList: true,
76-
// subtree: true,
77-
isReady: (el: HTMLElement | null, selector: string) => {
78-
return el?.matches?.(selector) ?? false;
79-
},
80-
});
68+
// const waitForElementAttribute = createAwaitableMutationObserver({
69+
// attributes: true,
70+
// // childList: true,
71+
// // subtree: true,
72+
// isReady: (el: HTMLElement | null, selector: string) => {
73+
// return el?.matches?.(selector) ?? false;
74+
// },
75+
// });
8176

8277
/**
8378
* Detect when a Clerk component has mounted by watching DOM updates to an element with a `data-clerk-component="${component}"` property.
@@ -98,11 +93,12 @@ export function useWaitForComponentMount(
9893
const selector = `[data-clerk-component="${component}"]`;
9994
const attributeSelector = options?.selector;
10095
console.log('attributeSelector', attributeSelector, attributeSelector + selector);
101-
watcherRef.current = (
102-
attributeSelector
103-
? waitForElementAttribute({ selector: attributeSelector + selector })
104-
: waitForElementChildren({ selector })
105-
)
96+
watcherRef.current = waitForElementChildren({
97+
selector: attributeSelector ? attributeSelector + selector : selector,
98+
})
99+
// .then(res => {
100+
// return attributeSelector ? waitForElementAttribute({ selector: attributeSelector + selector }) : res;
101+
// })
106102
.then(() => {
107103
console.log('rendered', component);
108104
setStatus('rendered');

0 commit comments

Comments
 (0)