|
1 | | -import { isPlatformBrowser } from '@angular/common'; |
2 | 1 | import { |
3 | 2 | EnvironmentProviders, |
4 | 3 | inject, |
5 | 4 | Injector, |
6 | 5 | provideEnvironmentInitializer, |
7 | 6 | } from '@angular/core'; |
8 | 7 | import { createCustomElement } from '@angular/elements'; |
9 | | -import { usePlatformOnly } from '@angularity/core'; |
10 | 8 |
|
11 | 9 | import { ELEMENT_REGISTRY } from './element-registry'; |
12 | 10 | import { Elements } from './elements'; |
13 | 11 |
|
14 | 12 | /** |
15 | | - * Offers a declarative approach to register Angular Elements in the browser platform. |
| 13 | + * Offers a declarative approach to register Angular Elements. |
16 | 14 | * |
17 | | - * @remarks The returned providers are for `EnvironmentInjector` only, e.g. `app.config.ts`, |
18 | | - * route declarations, and NgModules. The registered Angular Elements will not be unregistered |
19 | | - * when the `EnvironmentInjector` is destroyed, so make sure the `EnvironmentInjector` will not |
20 | | - * be destroyed anytime in the application's lifecycle. |
| 15 | + * The elements can be either provided synchronously or asynchronously |
| 16 | + * via a promise. |
21 | 17 | * |
22 | | - * @remarks If the current platform is not browser, this is a noop. |
| 18 | + * @remarks Noop if the current platform is not browser. |
23 | 19 | * |
24 | 20 | * @example |
25 | 21 | * ```ts |
26 | | - * export const APP_ELEMENTS: Elements = { |
| 22 | + * export const appElements: Elements = { |
27 | 23 | * 'my-button': ButtonComponent, |
28 | 24 | * 'my-icon': IconComponent, |
29 | 25 | * 'my-icon-button': IconButtonComponent, |
30 | 26 | * }; |
31 | 27 | * ``` |
32 | 28 | * ```ts |
33 | 29 | * providers: [ |
34 | | - * provideElements({ elements: APP_ELEMENTS }), |
| 30 | + * provideElements(appElements), |
| 31 | + * ] |
| 32 | + * ``` |
| 33 | + * ```ts |
| 34 | + * providers: [ |
| 35 | + * provideElements(import('./app-elements').then(m => m.appElements)), |
35 | 36 | * ] |
36 | 37 | * ``` |
37 | 38 | */ |
38 | 39 | export function provideElements( |
39 | | - config: ProvideElementsConfig, |
| 40 | + elements: Elements | Promise<Elements>, |
40 | 41 | ): EnvironmentProviders { |
41 | | - return provideEnvironmentInitializer(() => { |
42 | | - usePlatformOnly(isPlatformBrowser, () => { |
43 | | - const registry = inject(ELEMENT_REGISTRY); |
44 | | - const injector = inject(Injector); |
45 | | - for (const [name, type] of Object.entries(config.elements)) { |
46 | | - const element = createCustomElement(type, { injector }); |
47 | | - registry.define(name, element); |
48 | | - } |
49 | | - }); |
| 42 | + return provideEnvironmentInitializer(async () => { |
| 43 | + const registry = inject(ELEMENT_REGISTRY); |
| 44 | + if (!registry) return; |
| 45 | + const injector = inject(Injector); |
| 46 | + for (const [name, type] of Object.entries(await elements)) { |
| 47 | + const element = createCustomElement(type, { injector }); |
| 48 | + registry.define(name, element); |
| 49 | + } |
50 | 50 | }); |
51 | 51 | } |
52 | | - |
53 | | -/** |
54 | | - * @see `provideElements` |
55 | | - */ |
56 | | -export interface ProvideElementsConfig { |
57 | | - elements: Elements; |
58 | | -} |
0 commit comments