@microsoft/fast-element 2.0.0-beta.2 → 2.0.0-beta.5

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 (54) hide show
  1. package/CHANGELOG.json +147 -0
  2. package/CHANGELOG.md +42 -1
  3. package/dist/dts/components/fast-definitions.d.ts +9 -8
  4. package/dist/dts/components/fast-element.d.ts +12 -24
  5. package/dist/dts/context.d.ts +1 -1
  6. package/dist/dts/di/di.d.ts +858 -0
  7. package/dist/dts/hooks.d.ts +2 -2
  8. package/dist/dts/interfaces.d.ts +40 -7
  9. package/dist/dts/observation/observable.d.ts +19 -13
  10. package/dist/dts/styles/element-styles.d.ts +6 -0
  11. package/dist/dts/templating/binding-signal.d.ts +10 -27
  12. package/dist/dts/templating/binding-two-way.d.ts +16 -41
  13. package/dist/dts/templating/binding.d.ts +79 -118
  14. package/dist/dts/templating/html-directive.d.ts +31 -3
  15. package/dist/dts/templating/render.d.ts +277 -0
  16. package/dist/dts/templating/repeat.d.ts +12 -16
  17. package/dist/dts/templating/template.d.ts +3 -3
  18. package/dist/dts/templating/when.d.ts +3 -3
  19. package/dist/dts/testing/exports.d.ts +2 -0
  20. package/dist/dts/testing/fixture.d.ts +90 -0
  21. package/dist/dts/testing/timeout.d.ts +7 -0
  22. package/dist/esm/components/fast-definitions.js +25 -27
  23. package/dist/esm/components/fast-element.js +20 -11
  24. package/dist/esm/context.js +5 -1
  25. package/dist/esm/debug.js +36 -4
  26. package/dist/esm/di/di.js +1351 -0
  27. package/dist/esm/observation/arrays.js +303 -2
  28. package/dist/esm/observation/observable.js +11 -6
  29. package/dist/esm/platform.js +1 -1
  30. package/dist/esm/styles/element-styles.js +14 -0
  31. package/dist/esm/templating/binding-signal.js +56 -61
  32. package/dist/esm/templating/binding-two-way.js +56 -34
  33. package/dist/esm/templating/binding.js +137 -156
  34. package/dist/esm/templating/compiler.js +30 -7
  35. package/dist/esm/templating/html-directive.js +16 -2
  36. package/dist/esm/templating/render.js +392 -0
  37. package/dist/esm/templating/repeat.js +57 -40
  38. package/dist/esm/templating/template.js +8 -5
  39. package/dist/esm/templating/view.js +3 -1
  40. package/dist/esm/templating/when.js +5 -4
  41. package/dist/esm/testing/exports.js +2 -0
  42. package/dist/esm/testing/fixture.js +88 -0
  43. package/dist/esm/testing/timeout.js +24 -0
  44. package/dist/fast-element.api.json +2828 -2758
  45. package/dist/fast-element.d.ts +218 -230
  46. package/dist/fast-element.debug.js +656 -257
  47. package/dist/fast-element.debug.min.js +1 -1
  48. package/dist/fast-element.js +620 -253
  49. package/dist/fast-element.min.js +1 -1
  50. package/dist/fast-element.untrimmed.d.ts +226 -235
  51. package/docs/api-report.md +88 -91
  52. package/package.json +15 -6
  53. package/dist/dts/observation/splice-strategies.d.ts +0 -13
  54. package/dist/esm/observation/splice-strategies.js +0 -400
@@ -0,0 +1,277 @@
1
+ import type { FASTElement } from "../components/fast-element.js";
2
+ import { Constructable } from "../interfaces.js";
3
+ import type { Behavior } from "../observation/behavior.js";
4
+ import type { Subscriber } from "../observation/notifier.js";
5
+ import type { ExecutionContext, Expression, ExpressionObserver } from "../observation/observable.js";
6
+ import { ContentTemplate, ContentView } from "./binding.js";
7
+ import { AddViewBehaviorFactory, Binding, HTMLDirective, ViewBehaviorFactory, ViewBehaviorTargets } from "./html-directive.js";
8
+ import { CaptureType, SyntheticViewTemplate, TemplateValue, ViewTemplate } from "./template.js";
9
+ /**
10
+ * A Behavior that enables advanced rendering.
11
+ * @public
12
+ */
13
+ export declare class RenderBehavior<TSource = any, TParent = any> implements Behavior, Subscriber {
14
+ private directive;
15
+ private location;
16
+ private source;
17
+ private view;
18
+ private template;
19
+ private templateBindingObserver;
20
+ private data;
21
+ private dataBindingObserver;
22
+ private originalContext;
23
+ private childContext;
24
+ /**
25
+ * Creates an instance of RenderBehavior.
26
+ * @param location - A Node representing the location where this behavior will render.
27
+ * @param dataBinding - A binding expression that returns the data to render.
28
+ * @param templateBinding - A binding expression that returns the template to use with the data.
29
+ */
30
+ constructor(directive: RenderDirective, location: Node);
31
+ /**
32
+ * Bind this behavior to the source.
33
+ * @param source - The source to bind to.
34
+ * @param context - The execution context that the binding is operating within.
35
+ */
36
+ bind(source: TSource, context: ExecutionContext): void;
37
+ /**
38
+ * Unbinds this behavior from the source.
39
+ * @param source - The source to unbind from.
40
+ */
41
+ unbind(source: TSource, context: ExecutionContext<TParent>): void;
42
+ /** @internal */
43
+ handleChange(source: any, observer: ExpressionObserver): void;
44
+ private refreshView;
45
+ }
46
+ /**
47
+ * A Directive that enables use of the RenderBehavior.
48
+ * @public
49
+ */
50
+ export declare class RenderDirective<TSource = any> implements HTMLDirective, ViewBehaviorFactory {
51
+ readonly dataBinding: Binding<TSource>;
52
+ readonly templateBinding: Binding<TSource, ContentTemplate>;
53
+ readonly templateBindingDependsOnData: boolean;
54
+ /**
55
+ * The unique id of the factory.
56
+ */
57
+ id: string;
58
+ /**
59
+ * The structural id of the DOM node to which the created behavior will apply.
60
+ */
61
+ nodeId: string;
62
+ /**
63
+ * Creates an instance of RenderDirective.
64
+ * @param dataBinding - A binding expression that returns the data to render.
65
+ * @param templateBinding - A binding expression that returns the template to use to render the data.
66
+ */
67
+ constructor(dataBinding: Binding<TSource>, templateBinding: Binding<TSource, ContentTemplate>, templateBindingDependsOnData: boolean);
68
+ /**
69
+ * Creates HTML to be used within a template.
70
+ * @param add - Can be used to add behavior factories to a template.
71
+ */
72
+ createHTML(add: AddViewBehaviorFactory): string;
73
+ /**
74
+ * Creates a behavior.
75
+ * @param targets - The targets available for behaviors to be attached to.
76
+ */
77
+ createBehavior(targets: ViewBehaviorTargets): RenderBehavior<TSource>;
78
+ }
79
+ /**
80
+ * Provides instructions for how to render a type.
81
+ * @public
82
+ */
83
+ export interface RenderInstruction {
84
+ /**
85
+ * Identifies this as a RenderInstruction.
86
+ */
87
+ brand: symbol;
88
+ /**
89
+ * The type this instruction is associated with.
90
+ */
91
+ type: Constructable;
92
+ /**
93
+ * The template to use when rendering.
94
+ */
95
+ template: ContentTemplate;
96
+ /**
97
+ * A name that can be used to identify the instruction.
98
+ */
99
+ name: string;
100
+ }
101
+ /**
102
+ * Render options that are common to all configurations.
103
+ * @public
104
+ */
105
+ export declare type CommonRenderOptions = {
106
+ /**
107
+ * The type this instruction is associated with.
108
+ */
109
+ type: Constructable;
110
+ /**
111
+ * A name that can be used to identify the instruction.
112
+ */
113
+ name?: string;
114
+ };
115
+ /**
116
+ * Render options used to specify a template.
117
+ * @public
118
+ */
119
+ export declare type TemplateRenderOptions = CommonRenderOptions & {
120
+ /**
121
+ * The template to use when rendering.
122
+ */
123
+ template: ContentTemplate;
124
+ };
125
+ /**
126
+ * Render options that are common to all element render instructions.
127
+ * @public
128
+ */
129
+ export declare type BaseElementRenderOptions<TSource = any, TParent = any> = CommonRenderOptions & {
130
+ /**
131
+ * Attributes to use when creating the element template.
132
+ */
133
+ attributes?: Record<string, string | TemplateValue<TSource, TParent>>;
134
+ /**
135
+ * Content to use when creating the element template.
136
+ */
137
+ content?: string | SyntheticViewTemplate;
138
+ };
139
+ /**
140
+ * Render options used to specify an element.
141
+ * @public
142
+ */
143
+ export declare type ElementConstructorRenderOptions<TSource = any, TParent = any> = BaseElementRenderOptions<TSource, TParent> & {
144
+ /**
145
+ * The element to use when rendering.
146
+ */
147
+ element: Constructable<FASTElement>;
148
+ };
149
+ /**
150
+ * Render options use to specify an element by tag name.
151
+ * @public
152
+ */
153
+ export declare type TagNameRenderOptions<TSource = any, TParent = any> = BaseElementRenderOptions<TSource, TParent> & {
154
+ /**
155
+ * The tag name to use when rendering.
156
+ */
157
+ tagName: string;
158
+ };
159
+ declare function createElementTemplate<TSource = any, TParent = any>(tagName: string, attributes?: Record<string, string | TemplateValue<TSource, TParent>>, content?: string | ContentTemplate): ViewTemplate<TSource, TParent>;
160
+ declare function create(options: TagNameRenderOptions): RenderInstruction;
161
+ declare function create(options: ElementConstructorRenderOptions): RenderInstruction;
162
+ declare function create(options: TemplateRenderOptions): RenderInstruction;
163
+ declare function instanceOf(object: any): object is RenderInstruction;
164
+ declare function register(options: TagNameRenderOptions): RenderInstruction;
165
+ declare function register(options: ElementConstructorRenderOptions): RenderInstruction;
166
+ declare function register(options: TemplateRenderOptions): RenderInstruction;
167
+ declare function register(instruction: RenderInstruction): RenderInstruction;
168
+ declare function getByType(type: Constructable, name?: string): RenderInstruction | undefined;
169
+ declare function getForInstance(object: any, name?: string): RenderInstruction | undefined;
170
+ /**
171
+ * Provides APIs for creating and interacting with render instructions.
172
+ * @public
173
+ */
174
+ export declare const RenderInstruction: Readonly<{
175
+ /**
176
+ * Checks whether the provided object is a RenderInstruction.
177
+ * @param object - The object to check.
178
+ * @returns true if the object is a RenderInstruction; false otherwise
179
+ */
180
+ instanceOf: typeof instanceOf;
181
+ /**
182
+ * Creates a RenderInstruction for a set of options.
183
+ * @param options - The options to use when creating the RenderInstruction.
184
+ */
185
+ create: typeof create;
186
+ /**
187
+ * Creates a template based on a tag name.
188
+ * @param tagName - The tag name to use when creating the template.
189
+ * @param attributes - The attributes to apply to the element.
190
+ * @param content - The content to insert into the element.
191
+ * @returns A template based on the provided specifications.
192
+ */
193
+ createElementTemplate: typeof createElementTemplate;
194
+ /**
195
+ * Creates and registers an instruction.
196
+ * @param options The options to use when creating the RenderInstruction.
197
+ * @remarks
198
+ * A previously created RenderInstruction can also be registered.
199
+ */
200
+ register: typeof register;
201
+ /**
202
+ * Finds a previously registered RenderInstruction by type and optionally by name.
203
+ * @param type - The type to retrieve the RenderInstruction for.
204
+ * @param name - An optional name used in differentiating between multiple registered instructions.
205
+ * @returns The located RenderInstruction that matches the criteria or undefined if none is found.
206
+ */
207
+ getByType: typeof getByType;
208
+ /**
209
+ * Finds a previously registered RenderInstruction for the instance's type and optionally by name.
210
+ * @param object - The instance to retrieve the RenderInstruction for.
211
+ * @param name - An optional name used in differentiating between multiple registered instructions.
212
+ * @returns The located RenderInstruction that matches the criteria or undefined if none is found.
213
+ */
214
+ getForInstance: typeof getForInstance;
215
+ }>;
216
+ /**
217
+ * Decorates a type with render instruction metadata.
218
+ * @param options - The options used in creating the RenderInstruction.
219
+ * @public
220
+ */
221
+ export declare function renderWith(options: Omit<TagNameRenderOptions, "type">): ClassDecorator;
222
+ /**
223
+ * Decorates a type with render instruction metadata.
224
+ * @param options - The options used in creating the RenderInstruction.
225
+ * @public
226
+ */
227
+ export declare function renderWith(options: Omit<ElementConstructorRenderOptions, "type">): ClassDecorator;
228
+ /**
229
+ * Decorates a type with render instruction metadata.
230
+ * @param options - The options used in creating the RenderInstruction.
231
+ * @public
232
+ */
233
+ export declare function renderWith(options: Omit<TemplateRenderOptions, "type">): ClassDecorator;
234
+ /**
235
+ * Decorates a type with render instruction metadata.
236
+ * @param element - The element to use to render the decorated class.
237
+ * @param name - An optional name to differentiate the render instruction.
238
+ * @public
239
+ */
240
+ export declare function renderWith(element: Constructable<FASTElement>, name?: string): ClassDecorator;
241
+ /**
242
+ * Decorates a type with render instruction metadata.
243
+ * @param template - The template to use to render the decorated class.
244
+ * @param name - An optional name to differentiate the render instruction.
245
+ * @public
246
+ */
247
+ export declare function renderWith(template: ContentTemplate, name?: string): ClassDecorator;
248
+ /**
249
+ * @internal
250
+ */
251
+ export declare class NodeTemplate implements ContentTemplate, ContentView {
252
+ readonly node: Node;
253
+ constructor(node: Node);
254
+ bind(source: any, context: ExecutionContext<any>): void;
255
+ unbind(): void;
256
+ insertBefore(refNode: Node): void;
257
+ remove(): void;
258
+ create(): ContentView;
259
+ }
260
+ /**
261
+ * Creates a RenderDirective for use in advanced rendering scenarios.
262
+ * @param value - The binding expression that returns the data to be rendered. The expression
263
+ * can also return a Node to render directly.
264
+ * @param template - A template to render the data with
265
+ * or a string to indicate which RenderInstruction to use when looking up a RenderInstruction.
266
+ * Expressions can also be provided to dynamically determine either the template or the name.
267
+ * @returns A RenderDirective suitable for use in a template.
268
+ * @remarks
269
+ * If no binding is provided, then a default binding that returns the source is created.
270
+ * If no template is provided, then a binding is created that will use registered
271
+ * RenderInstructions to determine the view.
272
+ * If the template binding returns a string, then it will be used to look up a
273
+ * RenderInstruction to determine the view.
274
+ * @public
275
+ */
276
+ export declare function render<TSource = any, TItem = any>(value?: Expression<TSource, TItem> | Binding<TSource, TItem> | {}, template?: ContentTemplate | string | Expression<TSource, ContentTemplate | string | Node> | Binding<TSource, ContentTemplate | string | Node>): CaptureType<TSource>;
277
+ export {};
@@ -1,8 +1,8 @@
1
1
  import type { Behavior } from "../observation/behavior.js";
2
2
  import type { Subscriber } from "../observation/notifier.js";
3
- import { Binding, ExecutionContext } from "../observation/observable.js";
3
+ import { ExecutionContext, Expression, ExpressionObserver } from "../observation/observable.js";
4
4
  import { Splice } from "../observation/arrays.js";
5
- import { AddViewBehaviorFactory, HTMLDirective, ViewBehaviorFactory, ViewBehaviorTargets } from "./html-directive.js";
5
+ import { AddViewBehaviorFactory, Binding, HTMLDirective, ViewBehaviorFactory, ViewBehaviorTargets } from "./html-directive.js";
6
6
  import type { CaptureType, SyntheticViewTemplate, ViewTemplate } from "./template.js";
7
7
  /**
8
8
  * Options for configuring repeat behavior.
@@ -23,10 +23,8 @@ export interface RepeatOptions {
23
23
  * @public
24
24
  */
25
25
  export declare class RepeatBehavior<TSource = any> implements Behavior, Subscriber {
26
+ private directive;
26
27
  private location;
27
- private itemsBinding;
28
- private templateBinding;
29
- private options;
30
28
  private source;
31
29
  private views;
32
30
  private template;
@@ -40,13 +38,13 @@ export declare class RepeatBehavior<TSource = any> implements Behavior, Subscrib
40
38
  /**
41
39
  * Creates an instance of RepeatBehavior.
42
40
  * @param location - The location in the DOM to render the repeat.
43
- * @param itemsBinding - The array to render.
41
+ * @param dataBinding - The array to render.
44
42
  * @param isItemsBindingVolatile - Indicates whether the items binding has volatile dependencies.
45
43
  * @param templateBinding - The template to render for each item.
46
44
  * @param isTemplateBindingVolatile - Indicates whether the template binding has volatile dependencies.
47
45
  * @param options - Options used to turn on special repeat features.
48
46
  */
49
- constructor(location: Node, itemsBinding: Binding<TSource, any[]>, isItemsBindingVolatile: boolean, templateBinding: Binding<TSource, SyntheticViewTemplate>, isTemplateBindingVolatile: boolean, options: RepeatOptions);
47
+ constructor(directive: RepeatDirective, location: Node);
50
48
  /**
51
49
  * Bind this behavior to the source.
52
50
  * @param source - The source to bind to.
@@ -63,7 +61,7 @@ export declare class RepeatBehavior<TSource = any> implements Behavior, Subscrib
63
61
  * @param source - The source of the change.
64
62
  * @param args - The details about what was changed.
65
63
  */
66
- handleChange(source: any, args: Splice[]): void;
64
+ handleChange(source: any, args: Splice[] | ExpressionObserver): void;
67
65
  private observeItems;
68
66
  private updateViews;
69
67
  private refreshAllViews;
@@ -74,11 +72,9 @@ export declare class RepeatBehavior<TSource = any> implements Behavior, Subscrib
74
72
  * @public
75
73
  */
76
74
  export declare class RepeatDirective<TSource = any> implements HTMLDirective, ViewBehaviorFactory {
77
- readonly itemsBinding: Binding;
75
+ readonly dataBinding: Binding<TSource>;
78
76
  readonly templateBinding: Binding<TSource, SyntheticViewTemplate>;
79
77
  readonly options: RepeatOptions;
80
- private isItemsBindingVolatile;
81
- private isTemplateBindingVolatile;
82
78
  /**
83
79
  * The unique id of the factory.
84
80
  */
@@ -94,11 +90,11 @@ export declare class RepeatDirective<TSource = any> implements HTMLDirective, Vi
94
90
  createHTML(add: AddViewBehaviorFactory): string;
95
91
  /**
96
92
  * Creates an instance of RepeatDirective.
97
- * @param itemsBinding - The binding that provides the array to render.
93
+ * @param dataBinding - The binding that provides the array to render.
98
94
  * @param templateBinding - The template binding used to obtain a template to render for each item in the array.
99
95
  * @param options - Options used to turn on special repeat features.
100
96
  */
101
- constructor(itemsBinding: Binding, templateBinding: Binding<TSource, SyntheticViewTemplate>, options: RepeatOptions);
97
+ constructor(dataBinding: Binding<TSource>, templateBinding: Binding<TSource, SyntheticViewTemplate>, options: RepeatOptions);
102
98
  /**
103
99
  * Creates a behavior for the provided target node.
104
100
  * @param target - The node instance to create the behavior for.
@@ -107,10 +103,10 @@ export declare class RepeatDirective<TSource = any> implements HTMLDirective, Vi
107
103
  }
108
104
  /**
109
105
  * A directive that enables list rendering.
110
- * @param itemsBinding - The array to render.
111
- * @param templateOrTemplateBinding - The template or a template binding used obtain a template
106
+ * @param items - The array to render.
107
+ * @param template - The template or a template binding used obtain a template
112
108
  * to render for each item in the array.
113
109
  * @param options - Options used to turn on special repeat features.
114
110
  * @public
115
111
  */
116
- export declare function repeat<TSource = any, TArray extends ReadonlyArray<any> = ReadonlyArray<any>>(itemsBinding: Binding<TSource, TArray, ExecutionContext<TSource>>, templateOrTemplateBinding: ViewTemplate | Binding<TSource, ViewTemplate>, options?: RepeatOptions): CaptureType<TSource>;
112
+ export declare function repeat<TSource = any, TArray extends ReadonlyArray<any> = ReadonlyArray<any>>(items: Expression<TSource, TArray, ExecutionContext<TSource>> | Binding<TSource, TArray> | ReadonlyArray<any>, template: Expression<TSource, ViewTemplate> | Binding<TSource, ViewTemplate> | ViewTemplate, options?: RepeatOptions): CaptureType<TSource>;
@@ -1,5 +1,5 @@
1
- import { Binding, ExecutionContext } from "../observation/observable.js";
2
- import { HTMLDirective, ViewBehaviorFactory } from "./html-directive.js";
1
+ import { ExecutionContext, Expression } from "../observation/observable.js";
2
+ import { Binding, HTMLDirective, ViewBehaviorFactory } from "./html-directive.js";
3
3
  import type { ElementView, HTMLView, SyntheticView } from "./view.js";
4
4
  /**
5
5
  * A template capable of creating views specifically for rendering custom elements.
@@ -87,7 +87,7 @@ export interface CaptureType<TSource> {
87
87
  * Represents the types of values that can be interpolated into a template.
88
88
  * @public
89
89
  */
90
- export declare type TemplateValue<TSource, TParent = any> = Binding<TSource, any, TParent> | HTMLDirective | CaptureType<TSource>;
90
+ export declare type TemplateValue<TSource, TParent = any> = Expression<TSource, any, TParent> | Binding<TSource, any, TParent> | HTMLDirective | CaptureType<TSource>;
91
91
  /**
92
92
  * Transforms a template literal string into a ViewTemplate.
93
93
  * @param strings - The string fragments that are interpolated with the values.
@@ -1,10 +1,10 @@
1
- import type { Binding } from "../observation/observable.js";
1
+ import type { Expression } from "../observation/observable.js";
2
2
  import type { CaptureType, SyntheticViewTemplate } from "./template.js";
3
3
  /**
4
4
  * A directive that enables basic conditional rendering in a template.
5
- * @param binding - The condition to test for rendering.
5
+ * @param condition - The condition to test for rendering.
6
6
  * @param templateOrTemplateBinding - The template or a binding that gets
7
7
  * the template to render when the condition is true.
8
8
  * @public
9
9
  */
10
- export declare function when<TSource = any, TReturn = any>(binding: Binding<TSource, TReturn>, templateOrTemplateBinding: SyntheticViewTemplate | Binding<TSource, SyntheticViewTemplate>): CaptureType<TSource>;
10
+ export declare function when<TSource = any, TReturn = any>(condition: Expression<TSource, TReturn> | boolean, templateOrTemplateBinding: SyntheticViewTemplate | Expression<TSource, SyntheticViewTemplate>): CaptureType<TSource>;
@@ -0,0 +1,2 @@
1
+ export { timeout } from "./timeout.js";
2
+ export * from "./fixture.js";
@@ -0,0 +1,90 @@
1
+ import type { Constructable } from "../interfaces.js";
2
+ import { ExecutionContext } from "../observation/observable.js";
3
+ import { ViewTemplate } from "../templating/template.js";
4
+ import type { HTMLView } from "../templating/view.js";
5
+ /**
6
+ * Options used to customize the creation of the test fixture.
7
+ * @public
8
+ */
9
+ export interface FixtureOptions {
10
+ /**
11
+ * The document to run the fixture in.
12
+ * @defaultValue `globalThis.document`
13
+ */
14
+ document?: Document;
15
+ /**
16
+ * The parent element to append the fixture to.
17
+ * @defaultValue An instance of `HTMLDivElement`.
18
+ */
19
+ parent?: HTMLElement;
20
+ /**
21
+ * The data source to bind the HTML to.
22
+ * @defaultValue An empty object.
23
+ */
24
+ source?: any;
25
+ /**
26
+ * The execution context to use during binding.
27
+ * @defaultValue {@link @microsoft/fast-element#ExecutionContext}
28
+ */
29
+ context?: ExecutionContext;
30
+ }
31
+ /**
32
+ * A test fixture.
33
+ * @public
34
+ */
35
+ export interface Fixture<TElement = HTMLElement> {
36
+ /**
37
+ * The document the fixture is running in.
38
+ */
39
+ document: Document;
40
+ /**
41
+ * The template the fixture was created from.
42
+ */
43
+ template: ViewTemplate;
44
+ /**
45
+ * The view that was created from the fixture's template.
46
+ */
47
+ view: HTMLView;
48
+ /**
49
+ * The parent element that the view was appended to.
50
+ * @remarks
51
+ * This element will be appended to the DOM only
52
+ * after {@link Fixture.connect} has been called.
53
+ */
54
+ parent: HTMLElement;
55
+ /**
56
+ * The first element in the {@link Fixture.view}.
57
+ */
58
+ element: TElement;
59
+ /**
60
+ * Adds the {@link Fixture.parent} to the DOM, causing the
61
+ * connect lifecycle to begin.
62
+ * @remarks
63
+ * Yields control to the caller one Microtask later, in order to
64
+ * ensure that the DOM has settled.
65
+ */
66
+ connect: () => Promise<void>;
67
+ /**
68
+ * Removes the {@link Fixture.parent} from the DOM, causing the
69
+ * disconnect lifecycle to begin.
70
+ * @remarks
71
+ * Yields control to the caller one Microtask later, in order to
72
+ * ensure that the DOM has settled.
73
+ */
74
+ disconnect: () => Promise<void>;
75
+ }
76
+ /**
77
+ * Creates a random, unique name suitable for use as a Custom Element name.
78
+ * @public
79
+ */
80
+ export declare function uniqueElementName(prefix?: string): string;
81
+ /**
82
+ * Creates a test fixture suitable for testing custom elements, templates, and bindings.
83
+ * @param templateNameOrType An HTML template or single element name to create the fixture for.
84
+ * @param options Enables customizing fixture creation behavior.
85
+ * @remarks
86
+ * Yields control to the caller one Microtask later, in order to
87
+ * ensure that the DOM has settled.
88
+ * @public
89
+ */
90
+ export declare function fixture<TElement = HTMLElement>(templateNameOrType: ViewTemplate | string | Constructable<TElement>, options?: FixtureOptions): Promise<Fixture<TElement>>;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A timeout helper for use in tests.
3
+ * @param timeout The length of the timeout.
4
+ * @returns A promise that resolves once the configured time has elapsed.
5
+ * @public
6
+ */
7
+ export declare function timeout(timeout?: number): Promise<void>;
@@ -11,19 +11,15 @@ const fastElementRegistry = FAST.getById(4 /* KernelServiceId.elementRegistry */
11
11
  * @public
12
12
  */
13
13
  export class FASTElementDefinition {
14
- /**
15
- * Creates an instance of FASTElementDefinition.
16
- * @param type - The type this definition is being created for.
17
- * @param nameOrConfig - The name of the element to define or a config object
18
- * that describes the element to define.
19
- */
20
14
  constructor(type, nameOrConfig = type.definition) {
15
+ this.platformDefined = false;
21
16
  if (isString(nameOrConfig)) {
22
17
  nameOrConfig = { name: nameOrConfig };
23
18
  }
24
19
  this.type = type;
25
20
  this.name = nameOrConfig.name;
26
21
  this.template = nameOrConfig.template;
22
+ const proto = type.prototype;
27
23
  const attributes = AttributeDefinition.collect(type, nameOrConfig.attributes);
28
24
  const observedAttributes = new Array(attributes.length);
29
25
  const propertyLookup = {};
@@ -33,9 +29,13 @@ export class FASTElementDefinition {
33
29
  observedAttributes[i] = current.attribute;
34
30
  propertyLookup[current.name] = current;
35
31
  attributeLookup[current.attribute] = current;
32
+ Observable.defineProperty(proto, current);
36
33
  }
34
+ Reflect.defineProperty(type, "observedAttributes", {
35
+ value: observedAttributes,
36
+ enumerable: true,
37
+ });
37
38
  this.attributes = attributes;
38
- this.observedAttributes = observedAttributes;
39
39
  this.propertyLookup = propertyLookup;
40
40
  this.attributeLookup = attributeLookup;
41
41
  this.shadowOptions =
@@ -48,20 +48,14 @@ export class FASTElementDefinition {
48
48
  nameOrConfig.elementOptions === void 0
49
49
  ? defaultElementOptions
50
50
  : Object.assign(Object.assign({}, defaultElementOptions), nameOrConfig.elementOptions);
51
- this.styles =
52
- nameOrConfig.styles === void 0
53
- ? void 0
54
- : Array.isArray(nameOrConfig.styles)
55
- ? new ElementStyles(nameOrConfig.styles)
56
- : nameOrConfig.styles instanceof ElementStyles
57
- ? nameOrConfig.styles
58
- : new ElementStyles([nameOrConfig.styles]);
51
+ this.styles = ElementStyles.normalize(nameOrConfig.styles);
52
+ fastElementRegistry.register(this);
59
53
  }
60
54
  /**
61
55
  * Indicates if this element has been defined in at least one registry.
62
56
  */
63
57
  get isDefined() {
64
- return !!fastElementRegistry.getByType(this.type);
58
+ return this.platformDefined;
65
59
  }
66
60
  /**
67
61
  * Defines a custom element based on this definition.
@@ -71,22 +65,26 @@ export class FASTElementDefinition {
71
65
  */
72
66
  define(registry = customElements) {
73
67
  const type = this.type;
74
- if (fastElementRegistry.register(this)) {
75
- const attributes = this.attributes;
76
- const proto = type.prototype;
77
- for (let i = 0, ii = attributes.length; i < ii; ++i) {
78
- Observable.defineProperty(proto, attributes[i]);
79
- }
80
- Reflect.defineProperty(type, "observedAttributes", {
81
- value: this.observedAttributes,
82
- enumerable: true,
83
- });
84
- }
85
68
  if (!registry.get(this.name)) {
69
+ this.platformDefined = true;
86
70
  registry.define(this.name, type, this.elementOptions);
87
71
  }
88
72
  return this;
89
73
  }
74
+ /**
75
+ * Creates an instance of FASTElementDefinition.
76
+ * @param type - The type this definition is being created for.
77
+ * @param nameOrDef - The name of the element to define or a config object
78
+ * that describes the element to define.
79
+ */
80
+ static compose(type, nameOrDef) {
81
+ const found = fastElementRegistry.getByType(type);
82
+ if (found) {
83
+ return new FASTElementDefinition(class extends type {
84
+ }, nameOrDef);
85
+ }
86
+ return new FASTElementDefinition(type, nameOrDef);
87
+ }
90
88
  }
91
89
  /**
92
90
  * Gets the element definition associated with the specified type.
@@ -1,3 +1,4 @@
1
+ import { isFunction } from "../interfaces.js";
1
2
  import { Controller } from "./controller.js";
2
3
  import { FASTElementDefinition, } from "./fast-definitions.js";
3
4
  /* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */
@@ -22,6 +23,21 @@ function createFASTElement(BaseType) {
22
23
  }
23
24
  };
24
25
  }
26
+ function compose(type, nameOrDef) {
27
+ if (isFunction(type)) {
28
+ return FASTElementDefinition.compose(type, nameOrDef);
29
+ }
30
+ return FASTElementDefinition.compose(this, type);
31
+ }
32
+ function define(type, nameOrDef) {
33
+ if (isFunction(type)) {
34
+ return FASTElementDefinition.compose(type, nameOrDef).define().type;
35
+ }
36
+ return FASTElementDefinition.compose(this, type).define().type;
37
+ }
38
+ function from(BaseType) {
39
+ return createFASTElement(BaseType);
40
+ }
25
41
  /**
26
42
  * A minimal base class for FASTElements that also provides
27
43
  * static helpers for working with FASTElements.
@@ -33,26 +49,19 @@ export const FASTElement = Object.assign(createFASTElement(HTMLElement), {
33
49
  * provided base type.
34
50
  * @param BaseType - The base element type to inherit from.
35
51
  */
36
- from(BaseType) {
37
- return createFASTElement(BaseType);
38
- },
52
+ from,
39
53
  /**
40
54
  * Defines a platform custom element based on the provided type and definition.
41
55
  * @param type - The custom element type to define.
42
56
  * @param nameOrDef - The name of the element to define or a definition object
43
57
  * that describes the element to define.
44
58
  */
45
- define(type, nameOrDef) {
46
- return this.metadata(type, nameOrDef).define().type;
47
- },
59
+ define,
48
60
  /**
49
61
  * Defines metadata for a FASTElement which can be used to later define the element.
50
- * IMPORTANT: This API will be renamed to "compose" in a future beta.
51
62
  * @public
52
63
  */
53
- metadata(type, nameOrDef) {
54
- return new FASTElementDefinition(type, nameOrDef);
55
- },
64
+ compose,
56
65
  });
57
66
  /**
58
67
  * Decorator: Defines a platform custom element based on `FASTElement`.
@@ -63,6 +72,6 @@ export const FASTElement = Object.assign(createFASTElement(HTMLElement), {
63
72
  export function customElement(nameOrDef) {
64
73
  /* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */
65
74
  return function (type) {
66
- FASTElement.define(type, nameOrDef);
75
+ define(type, nameOrDef);
67
76
  };
68
77
  }