KEMBAR78
Web components with Angular | PDF
Web
Components
with
Angular
Hello!
I’m Sherry List
Azure Developer Technical Lead, Microsoft
Women Techmaker Lead
You can find me at @SherrryLst
Hi!
I’m Ana Cidre
Developer Advocate, Ultimate Courses
Women Techmaker Lead
You can find me at @AnaCidre_
Web Components
A component that is platform agnostic
Their main goal is to encapsulate the code for the components into a nice, reusable package
for maximum interoperability.
Check out: https://custom-elements-everywhere.com/
Web components consist of three main
technologies:
● HTML template
● Custom Elements
● Shadow DOM
● HTML imports
html
<sa-button
text="Click here"
style="--background-color: navy; --color: aqua; --font-weight: 600; --padding: 12px;">
</sa-button>
js
const template = `
<style>
.btn {
font-weight: var(--font-weight, 200);
background-color: var(--background-color, indianred);
border: 0;
border-radius: 5px;
color: var(--color, #fff);
padding: var(--padding, 10px);
text-transform: uppercase;
}
</style>
<button id="text" class="btn"></button>
`;
js
class SAButtonElement extends HTMLElement {
constructor() {
super();
const template = document.createElement("template");
// Shadow DOM
this.attachShadow({ "mode": "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
js
class SAButtonElement extends HTMLElement {
constructor() {
super();
const template = document.createElement("template");
// Shadow DOM
this.attachShadow({ "mode": "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
…
}
// Define custom element
customElements.define("sa-button", SAButtonElement);
http://bit.ly/angular-with-web-components-jfokus
Creating Web
Components with
Angular
Adding a Web
Component to an
Angular project
Creating Web
Components with
Angular
Adding a Web
Component to an
Angular project
Creating Web
Components with
Angular
How does that web
component
communicate to the
Angular project
Adding a Web
Component to an
Angular project
A look into the
future
Creating Web
Components with
Angular
How does that web
component
communicate to the
Angular project
Angular Elements
npm i -g @angular/cli
terminal
npm i -g @angular/cli
ng new sa-button --prefix sa --inline-template --style=scss
terminal
npm i -g @angular/cli
ng new sa-button --prefix sa --inline-template --style=scss
cd sa-button
terminal
npm i -g @angular/cli
ng new sa-button --prefix sa --inline-template --style=scss
cd sa-button
ng add @angular/elements
terminal
npm i -g @angular/cli
ng new sa-button --prefix sa --inline-template --style=scss
cd sa-button
ng add @angular/elements
npm install @webcomponents/custom-elements --save
terminal
import '@webcomponents/custom-elements/custom-elements.min';
polyfills.ts
{
"compileOnSave": false,
"compilerOptions": {
…
"target": "es2015",
….
}
}
}
tsconfig.json
ng generate component button
terminal
button.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'sa-button',
template: ` ...`,
styleUrls: ['./button.component.scss'],
})
export class ButtonComponent implements OnInit {
constructor() { }
ngOnInit() {}
}
button.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'sa-button',
template: ` <button id="text" class="btn">{{ text }}</button>`,
styleUrls: ['./button.component.scss'],
})
export class ButtonComponent implements OnInit {
@Input() text= 'Click me!';
constructor() { }
ngOnInit() {}
}
Web components consist of three main
technologies:
● HTML template
● Shadow DOM
● Custom Elements
ViewEncapsulation.ShadowDom
This encapsulation mode uses the Shadow DOM to scope styles only to this specific
component.
button.component.ts
import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
@Component({
selector: 'sa-button',
template: ` ...`,
styleUrls: ['./'sa-button'.scss'],
encapsulation: ViewEncapsulation.ShadowDom
})
Web components consist of three main
technologies:
● HTML template
● Shadow DOM
● Custom Elements
app.module.ts
import { NgModule, Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
@NgModule({
declarations: [ ButtonComponent ],
imports: [ BrowserModule ],
entryComponents: [ ButtonComponent ]
})
app.module.ts
@NgModule({
declarations: [ButtonComponent],
imports: [BrowserModule],
entryComponents: [ButtonComponent],
})
export class AppModule {
constructor(private injector: Injector) {
const saButton = createCustomElement(ButtonComponent, { injector });
customElements.define('sa-button', saButton);
}
app.module.ts
@NgModule({
declarations: [ButtonComponent],
imports: [BrowserModule],
entryComponents: [ButtonComponent],
})
export class AppModule {
constructor(private injector: Injector) {
const saButton = createCustomElement(ButtonComponent, { injector });
customElements.define('sa-button', saButton);
}
ngDoBootstrap() {}
}
Wait a min
build
● Huge bundle size
● No support for one bundle
● -> Eject
● Complicated!
It’s not that easy!
ngx-build-plus
@ManfredSteyer
It’s that easy!
● Extends Cli
● No Eject
● Build a single bundle
● No need to add Angular multiple times
to your project
● It’s simple!
How do we implement it?
ng add ngx-build-plus
terminal
[...]
"architect": {
"build": {
"builder": "ngx-build-plus:build",
[...]
}
}
[...]
angular.json
module.exports = {
externals: {
rxjs: 'rxjs',
'@angular/core': 'ng.core',
'@angular/common': 'ng.common',
'@angular/platform-browser': 'ng.platformBrowser',
'@angular/elements': 'ng.elements',
},
};
webpack.extra.js
ng build --prod
--extraWebpackConfig webpack.extra.js
--single-bundle true
terminal
Now our component is ready!
Ivy
● Speed improvements
● Bundle size reduction
● Increasing flexibility
The new backwards-compatible Angular renderer:
How do we use our web component?
npm install @webcomponents/custom-elements --save
terminal
import '@webcomponents/custom-elements/custom-elements.min';
polyfills.ts
app.module.ts
@NgModule({
[…],
schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
import * as saButton from '../webcomponents/sa-button/sa-button.js';
@Component({
selector: 'sa-root',
template: `
<sa-button text="hi" ></sa-button>
`',
styleUrls: ['./app.component.css']
})
export class AppComponent {...}
How do we communicate with the WC?
One way data flow
</>
</></> </>
</> </>
One way data flow
</>
</></> </>
</> </>
One way data flow
</>
</></> </>
</> </>
One way data flow
</>
</></> </>
</> </>
✓ Create a web component with Angular
✓ Add a web component to an existing Angular project
✓ Use the web component in an Angular project
We are almost done!
Do Web Components
rock?
● Maximum interoperability
● Support from Frameworks (Tooling)
● Many success stories
● Browsers are ready
Web Components DO Rock!
*https://webcomponents.org
What’s next?
lit-html
Check out: https://youtu.be/Io6JjgckHbg
● <template> in JS with template literals
● Extremely fast (Partial update)
● Customizable and extensible
(Directives)
What is lit-html?
lit-element
Check out: https://youtu.be/ypPRdtjGooc
● Base class for creating web components
● Uses lit-html to render into the element’s
Shadow DOM
● React to changes
● Extremely fast & light
What is lit-element?
Check out: https://youtu.be/ypPRdtjGooc
Theming
CSS Shadow Parts
::part()
Check out: https://meowni.ca/posts/part-theme-explainer/
<x-foo>
#shadow-root
<div part="some-box"><span>...</span></div>
<input part="some-input">
<div>...</div> /* not styleable
</x-foo>
::part
Source: https://meowni.ca/posts/part-theme-explainer/
At a document which has <x-foo>:
x-foo::part(some-box) { … }
x-foo::part(some-box):hover { … }
x-foo::part(some-input)::placeholder { … }
::part
Source: https://meowni.ca/posts/part-theme-explainer/
Angular ♥ Web Components
Sherry List
@SherrryLst
Ana Cidre
@AnaCidre_
Thank you!
http://bit.ly/front-end-love-web-components
Best practices
● https://w3ctag.github.io/webcomponents-design-guidelines/
● https://github.com/webcomponents/gold-standard/wiki
● https://developers.google.com/web/fundamentals/web-components/best-practices
Resources
● https://www.softwarearchitekt.at/post/2018/07/13/angular-elements-part-i-a-dynami
c-dashboard-in-four-steps-with-web-components.aspx
● https://meowni.ca/posts/part-theme-explainer/
● https://medium.com/google-developer-experts/are-web-components-a-thing-5a116
b1da7e4
● https://www.telerik.com/blogs/web-components-101-an-introduction-to-web-comp
onents
● https://www.softwarearchitekt.at/post/2019/01/27/building-angular-elements-with-t
he-cli.aspx

Web components with Angular