Skip to content

Commit 6ec387a

Browse files
authored
fix(web-components): use defined in querySelector for requestIdleCallback (#35142)
1 parent e5acc16 commit 6ec387a

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "fix: use defined selector in waitForConnectedDescendants",
4+
"packageName": "@fluentui/web-components",
5+
"email": "863023+radium-v@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

packages/web-components/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
],
2828
"exports": {
2929
".": "./dist/esm/index.js",
30+
"./utils/behaviors/*.js": "./dist/esm/utils/behaviors/*.js",
31+
"./utils/*.js": "./dist/esm/utils/*.js",
3032
"./utilities.js": "./dist/esm/utils/index.js",
3133
"./theme/*.js": "./dist/esm/theme/*.js",
3234
"./*/base.js": "./dist/esm/*/*.base.js",

packages/web-components/src/listbox/listbox.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ export class Listbox extends FASTElement {
216216
* @public
217217
*/
218218
public slotchangeHandler(e: Event): void {
219+
const target = e.target as HTMLSlotElement;
219220
waitForConnectedDescendants(this, () => {
220-
const options = (e.target as HTMLSlotElement)
221+
const options = target
221222
.assignedElements()
222223
.filter<DropdownOption>((option): option is DropdownOption => isDropdownOption(option));
223224

packages/web-components/src/utils/request-idle-callback.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @public
99
*/
1010
export function requestIdleCallback(
11-
callback: (args: { didTimeout: boolean }) => void,
11+
callback: (deadline: IdleDeadline) => void,
1212
options?: { timeout: number },
1313
): ReturnType<typeof globalThis.requestIdleCallback | typeof setTimeout> {
1414
if ('requestIdleCallback' in globalThis) {
@@ -19,6 +19,7 @@ export function requestIdleCallback(
1919
return setTimeout(() => {
2020
callback({
2121
didTimeout: options?.timeout ? Date.now() - start >= options.timeout : false,
22+
timeRemaining: () => 0,
2223
});
2324
}, 1);
2425
}
@@ -58,30 +59,25 @@ export function cancelIdleCallback(id: ReturnType<typeof globalThis.requestIdleC
5859
export function waitForConnectedDescendants(
5960
target: HTMLElement,
6061
callback: () => void,
61-
options = { shallow: false, timeout: 50 },
62+
options?: { shallow?: boolean; timeout?: number },
6263
): void {
63-
let idleCallbackId: ReturnType<typeof requestIdleCallback> | void;
64-
const query = options.shallow ? ':scope > *' : '*';
64+
let idleCallbackId: ReturnType<typeof requestIdleCallback> | undefined;
65+
const shallow = options?.shallow ?? false;
66+
const query = `${shallow ? ':scope > ' : ''}:not(:defined)`;
67+
const timeout = options?.timeout ?? 50;
6568

66-
if (!target.isConnected) {
67-
return;
68-
}
69-
70-
const scheduleCheck = () => {
69+
const scheduleCheck = (deadline?: IdleDeadline) => {
7170
if (idleCallbackId) {
72-
idleCallbackId = cancelIdleCallback(idleCallbackId);
71+
cancelIdleCallback(idleCallbackId);
72+
idleCallbackId = undefined;
7373
}
7474

75-
if (
76-
Array.from(target.querySelectorAll(query))
77-
.filter(el => el.tagName.includes('-'))
78-
.every(el => el.isConnected)
79-
) {
75+
if (!target.querySelector(query) || (deadline && deadline.timeRemaining() <= 0)) {
8076
callback();
8177
return;
8278
}
8379

84-
idleCallbackId = requestIdleCallback(scheduleCheck, { timeout: options.timeout });
80+
idleCallbackId = requestIdleCallback(scheduleCheck, { timeout });
8581
};
8682

8783
scheduleCheck();

0 commit comments

Comments
 (0)