Counter: {{ counter }}
-Counter: {{ counter }}
+
In this example we're pre-configuring
- LazyElementsModule
with the element config. That way we
- don't have to specify url of the element in the
- *axLazyElement
directive. The element will be lazy-loaded
- the same way as if we specified url directly.
+ LazyElementsModule
+ with the element config. That way we don't have to specify url of the
+ element in the
+ *axLazyElement
+ directive. The element will be lazy-loaded the same way as if we
+ specified url directly.
Dynamic configuration resolved at runtime-- ---+Toggle me -Value: {{ flag }}
-+ + ++ Toggle me + +Value: {{ flag }}
+In this example we're pre-configuring -
LazyElementsModule
with the element config that is provided - during runtime. We're usingLAZY_ELEMENT_CONFIGS
token as a - multi provider with a custom factory function. +LazyElementsModule
+ with the element config that is provided during runtime. We're using +LAZY_ELEMENT_CONFIGS
+ token as a multi provider with a custom factory function.Elements configured in a feature module with HTML inline options-- ---+Loading... -Click me -+ ++ +Loading... ++ Click me + +- We don't have to specify
url
when using pre-configured -LazyElementsModule
. We still might need to pass in -null
url into*axLazyElement
directive in case - we want to use other options likemodule
or -loadingTemplate
. + We don't have to specify +url
+ when using pre-configured +LazyElementsModule
+ . We still might need to pass in +null
+ url into +*axLazyElement
+ directive in case we want to use other options like +module
+ or +loadingTemplate
+ .- Elements configured in a feature module with
isModule
option + Elements configured in a feature module with +isModule
+ option-- ---+Loading... -- Click me -+ + +Loading... ++ Click me + +- We can also pre-configure
isModule
flag by passing it to - theLazyElementsModule
config. That way we don't have to - specify it in the template for module style elements! + We can also pre-configure +isModule
+ flag by passing it to the +LazyElementsModule
+ config. That way we don't have to specify it in the template for module + style elements!- Elements configured in
AppModule
or -CoreModule
usingforRoot
+ Elements configured in +AppModule
+ or +CoreModule
+ using +forRoot
-- ---+- + + Some options can be pre-configured for the whole application using -
LazyElementsModule.forRoot()
. This is usually done in -AppModule
or in theCoreModule
. That way we - don't have to specify this options in the feature modules while still - preserving posibility to override them if necessary. The -forRoot()
accepts alsoelementConfigs
so they - can be pre-configured centrally removing the need to configure them in - feature modules or inline in the templates. +LazyElementsModule.forRoot()
+ . This is usually done in +AppModule
+ or in the +CoreModule
+ . That way we don't have to specify this options in the feature modules + while still preserving posibility to override them if necessary. The +forRoot()
+ accepts also +elementConfigs
+ so they can be pre-configured centrally removing the need to configure + them in feature modules or inline in the templates.Preloading+ Preload everything + +- You can inject
LazyElementLoaderService
and call the -preload
method to preload all the configured modules, or - specify the list oftags
you want to preload. You can try - to refresh the page, preload elements and then try previous examples. - Notice how they show elements immediately (depending on the newtrowk - conditions ) because they were pre-loaded! Check out the dev tools - network tab to see it in action... + You can inject +LazyElementLoaderService
+ and call the +preload
+ method to preload all the configured modules, or specify the list of +tags
+ you want to preload. You can try to refresh the page, preload elements + and then try previous examples. Notice how they show elements + immediately (depending on the newtrowk conditions ) because they were + pre-loaded! Check out the dev tools network tab to see it in action...PreloadingHooks
-- ---+- + + @@ -17,99 +17,97 @@- By providing hooks in
ElementConfig
or + By providing hooks in +ElementConfig
+ orLazyElementRootOptions
you can run custom logic after certain points in the element's lifecycle. Clicking the run button below will trigger the download of diff --git a/projects/elements-demo/src/app/features/examples/advanced/advanced.component.scss b/projects/elements-demo/src/app/features/examples/advanced/advanced.component.scss index eef9800e..68916aa1 100644 --- a/projects/elements-demo/src/app/features/examples/advanced/advanced.component.scss +++ b/projects/elements-demo/src/app/features/examples/advanced/advanced.component.scss @@ -38,29 +38,6 @@ flex-direction: column; margin: 0 0 50px 0; - .implementation { - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - flex: 1; - margin: 0 20px 20px 0; - border: 2px solid #eee; - padding: 20px; - - p { - margin: 10px 0 0 0; - } - - .additional-btn { - margin-top: 10px; - } - - wired-button { - padding: 10px 15px; - } - } - .description { flex: 1.5; } diff --git a/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts b/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts index 866bea45..f5550386 100644 --- a/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts +++ b/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts @@ -14,6 +14,8 @@ import { MatIconModule } from '@angular/material/icon'; import { MatButtonModule } from '@angular/material/button'; import { HighlightModule } from 'ngx-highlightjs'; +import { ExampleComponent } from '../../../shared/example/example.component'; + @Component({ selector: 'demo-advanced', templateUrl: './advanced.component.html', @@ -26,18 +28,11 @@ import { HighlightModule } from 'ngx-highlightjs'; MatButtonModule, HighlightModule, LazyElementDirective, + ExampleComponent, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], }) export class AdvancedComponent implements OnInit { - // example toggles - example1 = false; - example2 = false; - example3 = false; - example4 = false; - example6 = false; - example7 = false; - // example code examples codeExample1module = CODE_EXAMPLE_1_MODULE; codeExample1html = CODE_EXAMPLE_1_HTML; diff --git a/projects/elements-demo/src/app/features/examples/basic/basic.component.html b/projects/elements-demo/src/app/features/examples/basic/basic.component.html index 2fac8767..6302fd33 100644 --- a/projects/elements-demo/src/app/features/examples/basic/basic.component.html +++ b/projects/elements-demo/src/app/features/examples/basic/basic.component.html @@ -5,9 +5,9 @@Quick navigation
- Loading template
- Error template
- - Advanced Angular template bindings + + Advanced Angular template bindings +
Basic
Basic usage
-- ---+- favorite - -+ ++ ++ favorite + +The most simple example, all we need is to use element tag, for example -
<mwc-icon>
and add -*axLazyElement
directive with the url pointing to the - element implementation. +<mwc-icon>
+ and add +*axLazyElement
+ directive with the url pointing to the element implementation.- Use loading
<ng-template #loading>
+ Use loading +<ng-template #loading>
--- ---+Loading... -- Increment - -Counter: {{ counter }}
-+ + ++ + Increment + +Counter: {{ counter }}
+In this example we're loading -
<mwc-button>
using the -*axLazyElement
directive but we're also adding -<ng-template #loading>Loading...<ng-template>
which - will be displayed before the element was loaded (depending on your +<mwc-button>
+ using the +*axLazyElement
+ directive but we're also adding +<ng-template #loading>Loading...<ng-template>
+ which will be displayed before the element was loaded (depending on your connection speed)Use error
+<ng-template #error>
+ Use error +
<ng-template #error>
+-- ---+Loading... -Loading failed... -- Submit - -+ + +Loading... +Loading failed... ++ Submit + +In this example we're loading -
<mwc-button>
using the -*axLazyElement
directive but we're also adding +<mwc-button>
+ using the +*axLazyElement
+ directive but we're also adding<ng-template #error>Loading failed...<ng-template>
which will be displayed because the loading request will fail (wrong url) @@ -122,62 +120,62 @@Advanced binding (objects instead of primitives)
-- ---+--Adjust the +xAxis
array values -{{ xAxis | json }}
+ +++ Adjust the + +xAxis
+ array values ++ {{ xAxis | json }} +
++ + +Loading... +Loading failed... +- - - -Loading... -Loading failed... -- -- + + + + @@ -185,34 +183,35 @@In this example we're loading - Ink chart element! - Ink comes with all elements in single bundle so we can use multiple Ink - elements while using
*axLazyElement
directive only on the - outmost element. Besides that in our Angular component we have defined -xAxix
array with the[-6.28, 6.28]
values - which are passed in the element using standard Angular property binding -[xlim]="xAxis"
. + Ink + chart element! Ink comes with all elements in single bundle so we can + use multiple Ink elements while using +*axLazyElement
+ directive only on the outmost element. Besides that in our Angular + component we have defined +xAxix
+ array with the +[-6.28, 6.28]
+ values which are passed in the element using standard Angular property + binding +[xlim]="xAxis"
+ .
Using import maps (SystemJS is required)
-- ---+- + + ++ @@ -22,60 +22,65 @@In this example we are using axLazyElement with Import Maps to load -
<mwc-switch>
. Any key can be used in the URL that is - mapped to a import map. For this feature to work properly a peer - dependency of having - SystemJS +<mwc-switch>
+ . Any key can be used in the URL that is mapped to a import map. For + this feature to work properly a peer dependency of having + + SystemJS + globally loaded needs to be satisfied. For instance your index.html may have the following code:- So when adding
<mwc-switch>
with -*axLazyElement
with theimportMap
flag set to + So when adding +<mwc-switch>
+ with +*axLazyElement
+ with the +importMap
+ flag set totrue
the directive will look for the key stored in the import map and load the correct script. diff --git a/projects/elements-demo/src/app/features/examples/basic/basic.component.scss b/projects/elements-demo/src/app/features/examples/basic/basic.component.scss index 840a7c05..68916aa1 100644 --- a/projects/elements-demo/src/app/features/examples/basic/basic.component.scss +++ b/projects/elements-demo/src/app/features/examples/basic/basic.component.scss @@ -38,32 +38,6 @@ flex-direction: column; margin: 0 0 50px 0; - .implementation { - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - flex: 1; - margin: 0 20px 20px 0; - border: 2px solid #eee; - padding: 20px; - - p { - margin: 10px 0 0 0; - } - - .controls { - display: flex; - align-items: center; - - .values { - display: inline-block; - width: 100px; - text-align: center; - } - } - } - .description { flex: 1.5; } diff --git a/projects/elements-demo/src/app/features/examples/basic/basic.component.ts b/projects/elements-demo/src/app/features/examples/basic/basic.component.ts index 756e63ce..3349e97d 100644 --- a/projects/elements-demo/src/app/features/examples/basic/basic.component.ts +++ b/projects/elements-demo/src/app/features/examples/basic/basic.component.ts @@ -1,4 +1,4 @@ -import { Component, CUSTOM_ELEMENTS_SCHEMA, OnInit } from '@angular/core'; +import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { RouterLink } from '@angular/router'; import { JsonPipe, NgIf } from '@angular/common'; import { MatSliderModule } from '@angular/material/slider'; @@ -9,6 +9,9 @@ import { HighlightModule } from 'ngx-highlightjs'; import { LazyElementDirective } from '@angular-extensions/elements'; +import { ExampleComponent } from '../../../shared/example/example.component'; +import { SpinnerComponent } from '../../../shared/spinner/spinner.component'; + @Component({ selector: 'demo-basic', standalone: true, @@ -25,16 +28,11 @@ import { LazyElementDirective } from '@angular-extensions/elements'; MatFormFieldModule, HighlightModule, LazyElementDirective, + ExampleComponent, + SpinnerComponent, ], }) -export class BasicComponent implements OnInit { - // example toggles - example1 = false; - example2 = false; - example3 = false; - example4 = false; - example5 = false; - +export class BasicComponent { // example code examples codeExample1 = CODE_EXAMPLE_1; codeExample2 = CODE_EXAMPLE_2; @@ -48,8 +46,6 @@ export class BasicComponent implements OnInit { xAxis = [-6.28, 6.28]; yAxis = [-1, 1]; - ngOnInit() {} - increment() { this.counter++; } diff --git a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html index 8ee4e319..a85dd590 100644 --- a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html +++ b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html @@ -5,14 +5,14 @@Quick navigation
Dynamic element tag- - Dynamic element tag with module pre-configuration + + Dynamic element tag with module pre-configuration +
- - Multiple different dynamic elements + + Multiple different dynamic elements +
Dynamic
These example explain how to configure element tag dynamically using -
*axLazyElementDynamic
directive. This can be very useful if we - want to retrieve element configuration from somewhere else (for example a - backend or a json config file). Such a setup can enable us to develop fully - dynamic and configurable microfrontend applications! +*axLazyElementDynamic
+ directive. This can be very useful if we want to retrieve element + configuration from somewhere else (for example a backend or a json config + file). Such a setup can enable us to develop fully dynamic and configurable + microfrontend applications!Dynamic element tag
-@@ -84,31 +89,31 @@- ---+Loading... -- Increment - -Counter: {{ counter1 }}
-- Raised button --- Outline button - + + +Loading... ++ Increment + +Counter: {{ counter1 }}
++ Raised button +++ Outline button + In this example we're loading -
<mwc-button>
using the -*axLazyElementDynamic
directive. Please notice that the - original HTML element used in the template is -<ax-lazy-element>
and we're specifying the desired tag - as the first parameter of -*axLazyElementDynamic='mwc-button'
. Please, feel free to - open your browsers dev tools to check resulting element tag! +<mwc-button>
+ using the +*axLazyElementDynamic
+ directive. Please notice that the original HTML element used in the + template is +<ax-lazy-element>
+ and we're specifying the desired tag as the first parameter of +*axLazyElementDynamic='mwc-button'
+ . Please, feel free to open your browsers dev tools to check resulting + element tag!- You might be asking why do we use
<ax-lazy-element>
. - This is not a real custom element and that's exactly the point. We can + You might be asking why do we use +<ax-lazy-element>
+ . This is not a real custom element and that's exactly the point. We can provide any valid and unused custom element name. That way Angular won't complain if we use custom property and event binding which would be the - case if we tried to bind<div [custom]="value">
... + case if we tried to bind +<div [custom]="value">
+ ...Dynamic element tag with module pre-configuration
-- ---+Loading... -- Increment - -Counter: {{ counter2 }}
-+ + +Loading... ++ Increment + +Counter: {{ counter2 }}
+- The
*axLazyElementDynamic
directive works also with the - pre-configuredLazyElementsModule
. That way we don't have - to specify url of the element in the template. Please notice that the - original HTML element used in the template is<div>
and - we're specifying the desired tag as the first parameter of -*axLazyElementDynamic='wired-button'
. Please, feel free to - open your browsers dev tools to check resulting element tag! + The +*axLazyElementDynamic
+ directive works also with the pre-configured +LazyElementsModule
+ . That way we don't have to specify url of the element in the template. + Please notice that the original HTML element used in the template is +<div>
+ and we're specifying the desired tag as the first parameter of +*axLazyElementDynamic='wired-button'
+ . Please, feel free to open your browsers dev tools to check resulting + element tag!Multiple different dynamic elements-- ---+--- -Loading <{{ c.tag }}>... -- {{ c.content }} - -Counter: {{ counter3 }}
-+ + ++++ ++ Loading <{{ c.tag }}>... + ++ {{ c.content }} + +Counter: {{ counter3 }}
+- The
*axLazyElementDynamic
directive works also with + The +*axLazyElementDynamic
+ directive works also with*ngFor
directive to render fully dynamic configuration which could have been retrieved from backend during the runtime in comparison to hard-coded diff --git a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.scss b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.scss index cd0ee5c8..d841cc6a 100644 --- a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.scss +++ b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.scss @@ -38,34 +38,6 @@ flex-direction: column; margin: 0 0 50px 0; - .implementation { - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - flex: 1; - margin: 0 20px 20px 0; - border: 2px solid #eee; - padding: 20px; - - p { - margin: 10px 0 0 0; - } - - wired-button { - padding: 10px 15px; - } - - .actions { - display: flex; - align-items: center; - - > * { - margin: 10px; - } - } - } - .description { flex: 1.5; } diff --git a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.ts b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.ts index b2546393..d1f11a09 100644 --- a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.ts +++ b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.ts @@ -7,6 +7,7 @@ import { MatButtonModule } from '@angular/material/button'; import { MatSlideToggleModule } from '@angular/material/slide-toggle'; import { HighlightModule } from 'ngx-highlightjs'; import { LazyElementDynamicDirective } from '@angular-extensions/elements'; +import { ExampleComponent } from '../../../shared/example/example.component'; @Component({ selector: 'demo-dynamic', @@ -23,15 +24,11 @@ import { LazyElementDynamicDirective } from '@angular-extensions/elements'; FormsModule, LazyElementDynamicDirective, HighlightModule, + ExampleComponent, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], }) export class DynamicComponent implements OnInit { - // example toggles - example1 = false; - example2 = false; - example3 = false; - // example code examples codeExample1 = CODE_EXAMPLE_1; codeExample2module = CODE_EXAMPLE_2_MODULE; diff --git a/projects/elements-demo/src/app/shared/example/example.component.ts b/projects/elements-demo/src/app/shared/example/example.component.ts new file mode 100644 index 00000000..8e443b1f --- /dev/null +++ b/projects/elements-demo/src/app/shared/example/example.component.ts @@ -0,0 +1,46 @@ +import { + ChangeDetectionStrategy, + Component, + contentChild, + signal, + TemplateRef, +} from '@angular/core'; +import { MatIcon } from '@angular/material/icon'; +import { MatButton } from '@angular/material/button'; +import { NgTemplateOutlet } from '@angular/common'; + +@Component({ + selector: 'demo-example', + standalone: true, + imports: [MatButton, MatIcon, NgTemplateOutlet], + changeDetection: ChangeDetectionStrategy.OnPush, + template: ` ++ @if (!showExample()) { + + } @else { + + } + `, + styles: ` + :host { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + flex: 1; + margin: 0 20px 20px 0; + border: 2px solid #eee; + padding: 20px; + border-radius: 10px; + } + `, +}) +export class ExampleComponent { + showExample = signal(false); + + example = contentChild(TemplateRef); +} diff --git a/projects/elements-demo/src/app/shared/spinner/spinner.component.html b/projects/elements-demo/src/app/shared/spinner/spinner.component.html deleted file mode 100644 index ec2efcec..00000000 --- a/projects/elements-demo/src/app/shared/spinner/spinner.component.html +++ /dev/null @@ -1 +0,0 @@ - Loading (spinner)...
diff --git a/projects/elements-demo/src/app/shared/spinner/spinner.component.scss b/projects/elements-demo/src/app/shared/spinner/spinner.component.scss deleted file mode 100644 index e69de29b..00000000 diff --git a/projects/elements-demo/src/app/shared/spinner/spinner.component.ts b/projects/elements-demo/src/app/shared/spinner/spinner.component.ts index f5251ee9..9a8dcafb 100644 --- a/projects/elements-demo/src/app/shared/spinner/spinner.component.ts +++ b/projects/elements-demo/src/app/shared/spinner/spinner.component.ts @@ -1,10 +1,13 @@ import { Component, OnInit } from '@angular/core'; +import { MatProgressSpinner } from '@angular/material/progress-spinner'; @Component({ selector: 'demo-spinner', - templateUrl: './spinner.component.html', - styleUrls: ['./spinner.component.scss'], standalone: true, + imports: [MatProgressSpinner], + template: ` ++ `, }) export class SpinnerComponent implements OnInit { ngOnInit() {} diff --git a/projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.spec.ts b/projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.spec.ts index 9c5e2851..0afd91ce 100644 --- a/projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.spec.ts +++ b/projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.spec.ts @@ -8,7 +8,9 @@ import { LazyElementDirective } from './lazy-element.directive'; @Component({ standalone: true, - template: ` Spinner...
`, + template: ` +Spinner...
+ `, }) class SpinnerTestComponent {}