@microsoft/fast-html 1.0.0-alpha.3 → 1.0.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +255 -18
  2. package/dist/dts/components/element.d.ts +10 -0
  3. package/dist/dts/components/index.d.ts +3 -1
  4. package/dist/dts/components/observer-map.d.ts +26 -0
  5. package/dist/dts/components/request-idle-callback.d.ts +41 -0
  6. package/dist/dts/components/schema.d.ts +144 -0
  7. package/dist/dts/components/template.d.ts +83 -7
  8. package/dist/dts/components/utilities.d.ts +109 -18
  9. package/dist/dts/fixtures/lifecycle-callbacks/lifecycle-callbacks.spec.d.ts +1 -0
  10. package/dist/dts/fixtures/lifecycle-callbacks/main.d.ts +5 -0
  11. package/dist/dts/fixtures/observer-map/main.d.ts +1 -0
  12. package/dist/dts/fixtures/observer-map/observer-map.spec.d.ts +1 -0
  13. package/dist/dts/index.d.ts +1 -1
  14. package/dist/esm/components/element.js +23 -0
  15. package/dist/esm/components/index.js +3 -1
  16. package/dist/esm/components/observer-map.js +53 -0
  17. package/dist/esm/components/observer-map.spec.js +19 -0
  18. package/dist/esm/components/request-idle-callback.js +72 -0
  19. package/dist/esm/components/schema.js +250 -0
  20. package/dist/esm/components/schema.spec.js +485 -0
  21. package/dist/esm/components/template.js +199 -111
  22. package/dist/esm/components/utilities.js +741 -43
  23. package/dist/esm/components/utilities.spec.js +317 -44
  24. package/dist/esm/fixtures/attribute/main.js +3 -2
  25. package/dist/esm/fixtures/binding/binding.spec.js +6 -0
  26. package/dist/esm/fixtures/binding/main.js +13 -2
  27. package/dist/esm/fixtures/children/children.spec.js +4 -0
  28. package/dist/esm/fixtures/children/main.js +3 -2
  29. package/dist/esm/fixtures/dot-syntax/dot-syntax.spec.js +109 -2
  30. package/dist/esm/fixtures/dot-syntax/main.js +30 -4
  31. package/dist/esm/fixtures/event/event.spec.js +28 -5
  32. package/dist/esm/fixtures/event/main.js +21 -5
  33. package/dist/esm/fixtures/lifecycle-callbacks/lifecycle-callbacks.spec.js +166 -0
  34. package/dist/esm/fixtures/lifecycle-callbacks/main.js +126 -0
  35. package/dist/esm/fixtures/observer-map/main.js +375 -0
  36. package/dist/esm/fixtures/observer-map/observer-map.spec.js +251 -0
  37. package/dist/esm/fixtures/ref/main.js +3 -2
  38. package/dist/esm/fixtures/ref/ref.spec.js +2 -6
  39. package/dist/esm/fixtures/repeat/main.js +27 -2
  40. package/dist/esm/fixtures/repeat/repeat.spec.js +16 -6
  41. package/dist/esm/fixtures/slotted/main.js +15 -4
  42. package/dist/esm/fixtures/slotted/slotted.spec.js +18 -19
  43. package/dist/esm/fixtures/when/main.js +139 -2
  44. package/dist/esm/fixtures/when/when.spec.js +64 -1
  45. package/dist/esm/index.js +1 -1
  46. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  47. package/dist/fast-html.api.json +333 -0
  48. package/dist/fast-html.d.ts +282 -6
  49. package/dist/fast-html.untrimmed.d.ts +282 -6
  50. package/package.json +12 -7
  51. package/rules/attribute-directives.yml +38 -0
  52. package/rules/call-expression-with-event-argument.yml +41 -0
  53. package/rules/member-expression.yml +33 -0
  54. package/rules/tag-function-to-template-literal.yml +16 -0
  55. package/dist/esm/fixtures/partial/main.js +0 -31
  56. package/dist/esm/fixtures/partial/partial.spec.js +0 -14
  57. /package/dist/dts/{fixtures/partial/main.d.ts → components/observer-map.spec.d.ts} +0 -0
  58. /package/dist/dts/{fixtures/partial/partial.spec.d.ts → components/schema.spec.d.ts} +0 -0
@@ -1,4 +1,41 @@
1
- import { FASTElement } from "@microsoft/fast-element";
1
+ import { FASTElement, HydrationControllerCallbacks, TemplateLifecycleCallbacks } from "@microsoft/fast-element";
2
+ import "@microsoft/fast-element/install-hydratable-view-templates.js";
3
+ /**
4
+ * Values for the observerMap element option.
5
+ */
6
+ export declare const ObserverMapOption: {
7
+ readonly all: "all";
8
+ };
9
+ /**
10
+ * Type for the observerMap element option.
11
+ */
12
+ export type ObserverMapOption = (typeof ObserverMapOption)[keyof typeof ObserverMapOption];
13
+ /**
14
+ * Element options the TemplateElement will use to update the registered element
15
+ */
16
+ export interface ElementOptions {
17
+ observerMap?: ObserverMapOption;
18
+ }
19
+ /**
20
+ * A dictionary of element options the TemplateElement will use to update the registered element
21
+ */
22
+ export interface ElementOptionsDictionary<ElementOptionsType = ElementOptions> {
23
+ [key: string]: ElementOptionsType;
24
+ }
25
+ /**
26
+ * Lifecycle callbacks for template and hydration events.
27
+ * Combines template lifecycle callbacks with hydration callbacks and adds template-processing events.
28
+ */
29
+ export interface HydrationLifecycleCallbacks extends HydrationControllerCallbacks, TemplateLifecycleCallbacks {
30
+ /**
31
+ * Called after the JS class definition has been registered
32
+ */
33
+ elementDidRegister?(name: string): void;
34
+ /**
35
+ * Called before the template has been evaluated and assigned
36
+ */
37
+ templateWillUpdate?(name: string): void;
38
+ }
2
39
  /**
3
40
  * The <f-template> custom element that will provide view logic to the element
4
41
  */
@@ -7,12 +44,52 @@ declare class TemplateElement extends FASTElement {
7
44
  * The name of the custom element this template will be applied to
8
45
  */
9
46
  name?: string;
10
- private partials;
47
+ /**
48
+ * A dictionary of custom element options
49
+ */
50
+ static elementOptions: ElementOptionsDictionary;
51
+ /**
52
+ * ObserverMap instance for caching binding paths
53
+ */
54
+ private observerMap?;
55
+ /**
56
+ * Default element options
57
+ */
58
+ private static defaultElementOptions;
59
+ /**
60
+ * Metadata containing JSON schema for properties on a custom eleemnt
61
+ */
62
+ private schema?;
63
+ /**
64
+ * Lifecycle callbacks for hydration events
65
+ */
66
+ private static lifecycleCallbacks;
67
+ /**
68
+ * Configure lifecycle callbacks for hydration events.
69
+ *
70
+ * @param callbacks - Lifecycle callbacks to configure.
71
+ * @returns The {@link TemplateElement} class.
72
+ */
73
+ static config(callbacks: HydrationLifecycleCallbacks): typeof TemplateElement;
74
+ /**
75
+ * Set options for custom elements.
76
+ *
77
+ * @param elementOptions - A dictionary of custom element options
78
+ * @returns The TemplateElement class.
79
+ */
80
+ static options(elementOptions?: ElementOptionsDictionary): typeof TemplateElement;
81
+ constructor();
82
+ /**
83
+ * Set options for a custom element
84
+ * @param name - The name of the custom element to set options for.
85
+ */
86
+ private static setOptions;
11
87
  connectedCallback(): void;
12
88
  /**
13
89
  * Resolve strings and values from an innerHTML string
14
90
  * @param innerHTML - The innerHTML.
15
91
  * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
92
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
16
93
  */
17
94
  private resolveStringsAndValues;
18
95
  /**
@@ -26,6 +103,8 @@ declare class TemplateElement extends FASTElement {
26
103
  * @param behaviorConfig - The directive behavior configuration object.
27
104
  * @param externalValues - The interpreted values from the parent.
28
105
  * @param innerHTML - The innerHTML.
106
+ * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
107
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
29
108
  */
30
109
  private resolveTemplateDirective;
31
110
  /**
@@ -42,6 +121,7 @@ declare class TemplateElement extends FASTElement {
42
121
  * @param values - The interpreted values.
43
122
  * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
44
123
  * @param behaviorConfig - The binding behavior configuration object.
124
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
45
125
  */
46
126
  private resolveDataBinding;
47
127
  /**
@@ -50,12 +130,8 @@ declare class TemplateElement extends FASTElement {
50
130
  * @param strings - The strings array.
51
131
  * @param values - The interpreted values.
52
132
  * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
133
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
53
134
  */
54
135
  private resolveInnerHTML;
55
- /**
56
- * Resolve all partial templates
57
- * @param unresolvedInnerHTML - The innerHTML.
58
- */
59
- private resolveAllPartials;
60
136
  }
61
137
  export { TemplateElement };
@@ -1,9 +1,12 @@
1
+ import { JSONSchema, JSONSchemaDefinition, Schema } from "./schema.js";
1
2
  type BehaviorType = "dataBinding" | "templateDirective";
2
- type TemplateDirective = "when" | "repeat" | "apply";
3
+ type TemplateDirective = "when" | "repeat";
3
4
  export type AttributeDirective = "children" | "slotted" | "ref";
5
+ type DataBindingBindingType = "client" | "default" | "unescaped";
4
6
  interface BehaviorConfig {
5
7
  type: BehaviorType;
6
8
  }
9
+ export type PathType = "access" | "default" | "event" | "repeat";
7
10
  export interface ContentDataBindingBehaviorConfig extends BaseDataBindingBehaviorConfig {
8
11
  subtype: "content";
9
12
  }
@@ -18,6 +21,7 @@ export interface AttributeDirectiveBindingBehaviorConfig extends BaseDataBinding
18
21
  export type DataBindingBehaviorConfig = ContentDataBindingBehaviorConfig | AttributeDataBindingBehaviorConfig | AttributeDirectiveBindingBehaviorConfig;
19
22
  export interface BaseDataBindingBehaviorConfig extends BehaviorConfig {
20
23
  type: "dataBinding";
24
+ bindingType: DataBindingBindingType;
21
25
  openingStartIndex: number;
22
26
  openingEndIndex: number;
23
27
  closingStartIndex: number;
@@ -32,10 +36,9 @@ export interface TemplateDirectiveBehaviorConfig extends BehaviorConfig {
32
36
  closingTagStartIndex: number;
33
37
  closingTagEndIndex: number;
34
38
  }
35
- interface PartialTemplateConfig {
36
- innerHTML: string;
37
- startIndex: number;
38
- endIndex: number;
39
+ export interface ChildrenMap {
40
+ customElementName: string;
41
+ attributeName: string;
39
42
  }
40
43
  /**
41
44
  * Get the index of the next matching tag
@@ -52,18 +55,6 @@ export declare function getIndexOfNextMatchingTag(openingTagStartSlice: string,
52
55
  * @returns DataBindingBehaviorConfig | DirectiveBehaviorConfig | null - A configuration object or null
53
56
  */
54
57
  export declare function getNextBehavior(innerHTML: string): DataBindingBehaviorConfig | TemplateDirectiveBehaviorConfig | null;
55
- /**
56
- * Gets all the partials with their IDs
57
- * @param innerHTML - The innerHTML string to evaluate
58
- * @param offset - The index offset from the innerHTML
59
- * @param partials - The partials found
60
- * @returns {[key: string]: PartialTemplateConfig}
61
- */
62
- export declare function getAllPartials(innerHTML: string, offset?: number, partials?: {
63
- [key: string]: PartialTemplateConfig;
64
- }): {
65
- [key: string]: PartialTemplateConfig;
66
- };
67
58
  /**
68
59
  * Create a function to resolve a value from an object using a path with dot syntax.
69
60
  * e.g. "foo.bar"
@@ -71,5 +62,105 @@ export declare function getAllPartials(innerHTML: string, offset?: number, parti
71
62
  * @param self - Where the first item in the path path refers to the item itself (used by repeat).
72
63
  * @returns A function to access the value from a given path.
73
64
  */
74
- export declare function pathResolver(path: string, self?: boolean): (accessibleObject: any) => any;
65
+ export declare function pathResolver(path: string, contextPath: string | null, level: number, rootSchema: JSONSchema): (accessibleObject: any, context: any) => any;
66
+ export declare function bindingResolver(previousString: string | null, rootPropertyName: string | null, path: string, parentContext: string | null, type: PathType, schema: Schema, currentContext: string | null, level: number): (accessibleObject: any, context: any) => any;
67
+ export declare function expressionResolver(rootPropertyName: string | null, expression: ChainedExpression, parentContext: string | null, level: number, schema: Schema): (accessibleObject: any, context: any) => any;
68
+ /**
69
+ * Extracts all paths from a ChainedExpression, including nested expressions
70
+ * @param chainedExpression - The chained expression to extract paths from
71
+ * @returns A Set containing all unique paths found in the expression chain
72
+ */
73
+ export declare function extractPathsFromChainedExpression(chainedExpression: ChainedExpression): Set<string>;
74
+ /**
75
+ * Available operators include:
76
+ *
77
+ * - access (no operator)
78
+ * - not (!)
79
+ * - equals (==)
80
+ * - not equal (!=)
81
+ * - greater than or equal (>=)
82
+ * - greater than (>)
83
+ * - less than or equal (<=)
84
+ * - less than (<)
85
+ * - or (||)
86
+ * - and (&&) and the HTML character entity (&amp;&amp;)
87
+ */
88
+ type Operator = "access" | "!" | "==" | "!=" | ">=" | ">" | "<=" | "<";
89
+ type ChainingOperator = "||" | "&&" | "&amp;&amp;";
90
+ interface Expression {
91
+ operator: Operator;
92
+ left: string;
93
+ leftIsValue: boolean | null;
94
+ right: string | boolean | number | null;
95
+ rightIsValue: boolean | null;
96
+ }
97
+ export interface ChainedExpression {
98
+ operator?: ChainingOperator;
99
+ expression: Expression;
100
+ next?: ChainedExpression;
101
+ }
102
+ /**
103
+ * Gets the expression chain as a configuration object
104
+ * @param value - The binding string value
105
+ * @returns - A configuration object containing information about the expression
106
+ */
107
+ export declare function getExpressionChain(value: string): ChainedExpression | void;
108
+ /**
109
+ * This is the transform utility for rationalizing declarative HTML syntax
110
+ * with bindings in the ViewTemplate
111
+ * @param innerHTML The innerHTML to transform
112
+ * @param index The index to start the current slice of HTML to evaluate
113
+ */
114
+ export declare function transformInnerHTML(innerHTML: string, index?: number): string;
115
+ /**
116
+ * Resolves f-when
117
+ * @param self - Where the first item in the path path refers to the item itself (used by repeat).
118
+ * @param chainedExpression - The chained expression which includes the expression and the next expression
119
+ * if there is another in the chain
120
+ * @returns - A binding that resolves the chained expression logic
121
+ */
122
+ export declare function resolveWhen(rootPropertyName: string | null, expression: ChainedExpression, parentContext: string | null, level: number, schema: Schema): (x: boolean, c: any) => any;
123
+ /**
124
+ * Find a definition
125
+ * This may exist as a $ref at the root or as a $ref in any anyOf or not at all
126
+ * if the Observer Map has not been enabled on a child component
127
+ * @param schema - The JSON schema to find the ref in
128
+ * @returns The definition or null
129
+ */
130
+ export declare function findDef(schema: JSONSchema | JSONSchemaDefinition): string | null;
131
+ /**
132
+ * Assign observables to data
133
+ * @param schema - The schema
134
+ * @param rootSchema - The root schema mapping to the root property
135
+ * @param data - The data
136
+ * @param target - The target custom element
137
+ * @param rootProperty - The root property
138
+ * @returns
139
+ */
140
+ export declare function assignObservables(schema: JSONSchema | JSONSchemaDefinition, rootSchema: JSONSchema, data: any, target: any, rootProperty: string): typeof Proxy;
141
+ /**
142
+ * Assign a proxy to an object
143
+ * @param schema - The current schema
144
+ * @param rootSchema - The root schema for the root property
145
+ * @param target - The target custom element
146
+ * @param rootProperty - The root property
147
+ * @param object - The object to assign the proxy to
148
+ * @returns Proxy object
149
+ */
150
+ export declare function assignProxy(schema: JSONSchema | JSONSchemaDefinition, rootSchema: JSONSchema, target: any, rootProperty: string, object: any): typeof Proxy;
151
+ /**
152
+ * Get the root property name
153
+ * @param rootPropertyName - The root property
154
+ * @param path - The dot syntax path
155
+ * @param context - The context created by a repeat
156
+ * @param type - The type of path binding
157
+ * @returns
158
+ */
159
+ export declare function getRootPropertyName(rootPropertyName: string | null, path: string, context: null | string, type: PathType): string | null;
160
+ /**
161
+ * Get details of bindings to the attributes of child custom elements
162
+ * @param previousString - The previous string before the binding
163
+ * @returns null, or a custom element name and attribute name
164
+ */
165
+ export declare function getChildrenMap(previousString: string | null): ChildrenMap | null;
75
166
  export {};
@@ -0,0 +1,5 @@
1
+ export declare const lifecycleEvents: Array<{
2
+ callback: string;
3
+ name?: string;
4
+ }>;
5
+ export declare let hydrationCompleteEmitted: boolean;
@@ -0,0 +1 @@
1
+ export {};
@@ -1 +1 @@
1
- export { TemplateElement } from "./components/index.js";
1
+ export { RenderableFASTElement, TemplateElement, ObserverMap, } from "./components/index.js";
@@ -0,0 +1,23 @@
1
+ import { attr } from "@microsoft/fast-element";
2
+ /**
3
+ * A mixin function that extends a base class with additional functionality for
4
+ * rendering and hydration.
5
+ *
6
+ * @param BaseCtor - The base class to extend.
7
+ * @returns A new class that extends the provided base class with additional functionality for rendering and hydration.
8
+ * @public
9
+ */
10
+ export function RenderableFASTElement(BaseCtor) {
11
+ const C = class extends BaseCtor {
12
+ constructor(...args) {
13
+ var _a, _b;
14
+ super(...args);
15
+ this.deferHydration = true;
16
+ ((_b = (_a = this.prepare) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : Promise.resolve()).then(() => {
17
+ this.deferHydration = false;
18
+ });
19
+ }
20
+ };
21
+ attr({ mode: "boolean", attribute: "defer-hydration" })(C.prototype, "deferHydration");
22
+ return C;
23
+ }
@@ -1 +1,3 @@
1
- export { TemplateElement } from "./template.js";
1
+ export { RenderableFASTElement } from "./element.js";
2
+ export { ObserverMap } from "./observer-map.js";
3
+ export { ObserverMapOption, TemplateElement, } from "./template.js";
@@ -0,0 +1,53 @@
1
+ import { Observable } from "@microsoft/fast-element/observable.js";
2
+ import { assignObservables } from "./utilities.js";
3
+ /**
4
+ * ObserverMap provides functionality for caching binding paths, extracting root properties,
5
+ * and defining observable properties on class prototypes
6
+ */
7
+ export class ObserverMap {
8
+ constructor(classPrototype, schema) {
9
+ /**
10
+ * Creates a property change handler function for observable properties
11
+ * This handler is called when an observable property transitions from undefined to a defined value
12
+ * @param propertyName - The name of the property for which to create the change handler
13
+ * @returns A function that handles property changes and sets up proxies for object values
14
+ */
15
+ this.defineChanged = (propertyName) => {
16
+ const getAndAssignObservablesAlias = this.getAndAssignObservables;
17
+ const schema = this.schema;
18
+ function instanceResolverChanged(prev, next) {
19
+ if (prev === undefined ||
20
+ ((prev === null || prev === void 0 ? void 0 : prev.$isProxy) && !(next === null || next === void 0 ? void 0 : next.$isProxy)) ||
21
+ (Array.isArray(prev) &&
22
+ Array.isArray(next) &&
23
+ !(next === null || next === void 0 ? void 0 : next.$fastController))) {
24
+ const proxy = getAndAssignObservablesAlias(this, propertyName, next, schema);
25
+ this[propertyName] = proxy;
26
+ }
27
+ }
28
+ return instanceResolverChanged;
29
+ };
30
+ this.classPrototype = classPrototype;
31
+ this.schema = schema;
32
+ }
33
+ defineProperties() {
34
+ const propertyNames = this.schema.getRootProperties();
35
+ for (const propertyName of propertyNames) {
36
+ Observable.defineProperty(this.classPrototype, propertyName);
37
+ this.classPrototype[`${propertyName}Changed`] =
38
+ this.defineChanged(propertyName);
39
+ }
40
+ }
41
+ /**
42
+ * Creates a proxy for an object that intercepts property mutations and triggers Observable notifications
43
+ * @param target - The target instance that owns the root property
44
+ * @param rootProperty - The name of the root property for notification purposes
45
+ * @param object - The object to wrap with a proxy
46
+ * @returns A proxy that triggers notifications on property mutations
47
+ */
48
+ getAndAssignObservables(target, rootProperty, object, schema) {
49
+ let proxiedObject = object;
50
+ proxiedObject = assignObservables(schema.getSchema(rootProperty), schema.getSchema(rootProperty), proxiedObject, target, rootProperty);
51
+ return proxiedObject;
52
+ }
53
+ }
@@ -0,0 +1,19 @@
1
+ import { __awaiter } from "tslib";
2
+ import { expect, test } from "@playwright/test";
3
+ import { ObserverMap } from "./observer-map.js";
4
+ import { Schema } from "./schema.js";
5
+ test.describe("ObserverMap", () => __awaiter(void 0, void 0, void 0, function* () {
6
+ let observerMap;
7
+ let schema = new Schema("test-class");
8
+ class TestClass {
9
+ }
10
+ test.beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
11
+ // Use TestClass.prototype so instances will have the observable properties
12
+ observerMap = new ObserverMap(TestClass.prototype, schema);
13
+ }));
14
+ test("should create new instances", () => __awaiter(void 0, void 0, void 0, function* () {
15
+ const instance1 = new ObserverMap(TestClass.prototype, schema);
16
+ const instance2 = new ObserverMap(TestClass.prototype, schema);
17
+ expect(instance1).not.toBe(instance2);
18
+ }));
19
+ }));
@@ -0,0 +1,72 @@
1
+ /**
2
+ * A ponyfill for requestIdleCallback that falls back to setTimeout.
3
+ * Uses the native requestIdleCallback when available.
4
+ *
5
+ * @param callback - The function to call when the browser is idle.
6
+ * @param options - Options object that may contain a timeout property.
7
+ * @returns An ID that can be used to cancel the callback.
8
+ * @public
9
+ */
10
+ export function requestIdleCallback(callback, options) {
11
+ if ("requestIdleCallback" in globalThis) {
12
+ return globalThis.requestIdleCallback(callback, options);
13
+ }
14
+ const start = Date.now();
15
+ return setTimeout(() => {
16
+ callback({
17
+ didTimeout: (options === null || options === void 0 ? void 0 : options.timeout) ? Date.now() - start >= options.timeout : false,
18
+ timeRemaining: () => 0,
19
+ });
20
+ }, 1);
21
+ }
22
+ /**
23
+ * A ponyfill for cancelIdleCallback that falls back to clearTimeout.
24
+ * Uses the native cancelIdleCallback when available.
25
+ *
26
+ * @param id - The ID of the callback to cancel.
27
+ * @public
28
+ */
29
+ export function cancelIdleCallback(id) {
30
+ if ("cancelIdleCallback" in globalThis) {
31
+ globalThis.cancelIdleCallback(id);
32
+ }
33
+ else {
34
+ clearTimeout(id);
35
+ }
36
+ }
37
+ /**
38
+ * Waits for all custom element descendants of a target element to be connected.
39
+ *
40
+ * @param target - The target element to observe.
41
+ * @param callback - The function to call when all custom element descendants are connected.
42
+ * @param options - Options object that may contain a timeout property for the idle callback.
43
+ *
44
+ * @remarks
45
+ * This function uses requestIdleCallback to periodically check if all custom element
46
+ * descendants (elements with a hyphen in their tag name) are connected to the DOM.
47
+ * Once all such elements are connected, the provided callback is invoked.
48
+ *
49
+ * The `timeout` option specifies the maximum time to wait for each idle callback before
50
+ * rechecking, defaulting to 50 milliseconds.
51
+ *
52
+ * @public
53
+ */
54
+ export function waitForConnectedDescendants(target, callback, options) {
55
+ var _a, _b;
56
+ let idleCallbackId;
57
+ const shallow = (_a = options === null || options === void 0 ? void 0 : options.shallow) !== null && _a !== void 0 ? _a : false;
58
+ const query = `${shallow ? ":scope > " : ""}:not(:defined)`;
59
+ const timeout = (_b = options === null || options === void 0 ? void 0 : options.timeout) !== null && _b !== void 0 ? _b : 50;
60
+ const scheduleCheck = (deadline) => {
61
+ if (idleCallbackId) {
62
+ cancelIdleCallback(idleCallbackId);
63
+ idleCallbackId = undefined;
64
+ }
65
+ if (!target.querySelector(query) || (deadline && deadline.timeRemaining() <= 0)) {
66
+ callback();
67
+ return;
68
+ }
69
+ idleCallbackId = requestIdleCallback(scheduleCheck, { timeout });
70
+ };
71
+ scheduleCheck();
72
+ }