SlideShare una empresa de Scribd logo
1 de 82
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 
Angular Notes.pdf
Angular Notes.pdfAngular Notes.pdf
Angular Notes.pdf
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
jQuery
jQueryjQuery
jQuery
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced Routing
 
Angular 6 Form Validation with Material
Angular 6 Form Validation with MaterialAngular 6 Form Validation with Material
Angular 6 Form Validation with Material
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
 
Angular material
Angular materialAngular material
Angular material
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2
 
Angular routing
Angular routingAngular routing
Angular routing
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginners
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Flask Basics
Flask BasicsFlask Basics
Flask Basics
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
 
Angular
AngularAngular
Angular
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 

Similar a Web components with Angular

Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Jeado Ko
 

Similar a Web components with Angular (20)

Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next Framework
 
mean stack
mean stackmean stack
mean stack
 
Kendoui
KendouiKendoui
Kendoui
 
Jan 2017 - a web of applications (angular 2)
Jan 2017 - a web of applications (angular 2)Jan 2017 - a web of applications (angular 2)
Jan 2017 - a web of applications (angular 2)
 
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
 
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점 Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
 
Building a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web ServicesBuilding a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web Services
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
 
Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
 
8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!
 
Angular 2 for Java Developers
Angular 2 for Java DevelopersAngular 2 for Java Developers
Angular 2 for Java Developers
 
Web components - An Introduction
Web components - An IntroductionWeb components - An Introduction
Web components - An Introduction
 
Angular Web Components
Angular Web ComponentsAngular Web Components
Angular Web Components
 
Angular Web Components
Angular Web ComponentsAngular Web Components
Angular Web Components
 
Data Flow Patterns in Angular 2 - Sebastian Müller
Data Flow Patterns in Angular 2 -  Sebastian MüllerData Flow Patterns in Angular 2 -  Sebastian Müller
Data Flow Patterns in Angular 2 - Sebastian Müller
 
angular-concepts-introduction-slides.pptx
angular-concepts-introduction-slides.pptxangular-concepts-introduction-slides.pptx
angular-concepts-introduction-slides.pptx
 
Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2
 
はじめてのAngular2
はじめてのAngular2はじめてのAngular2
はじめてのAngular2
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Web components with Angular