SlideShare a Scribd company logo
Stencil: The Time for Vanilla
Web Components has Arrived
Gil Fink
sparXys CEO
@gilfink / www.gilfink.net
Typical Application Web Page Design
From Design to Implementation
Track List
track
<track-list />
<track />
<track-header />
<track-content />
Component
Child component
<grand-child />
<grand-child />
header
content
How would you build that page?
Do we really need all these
frameworks/libraries?
What if we could teach the
browser new elements?
Each Element Instance
• Will be a DOM element
• Creates its own DOM tree
• Can be accessed and manipulated using DOM functions or its own
API
• Is a JavaScript object
• Is this possible?
This is where our journey begins
About Me
• sparXys CEO and senior consultant
• Microsoft MVP in the last 9 years
• Pro Single Page Application Development (Apress) co-author
• 4 Microsoft Official Courses (MOCs) co-author
• GDG Rishon and AngularUP co-organizer
Agenda
• The Problems We Faced
• Web Components APIs
• Stencil
Undescriptive Markup
Markup Example
Poor Separation of Concerns
You want HTML, CSS and
JavaScript to work together
You end up with a mess
Bundling is Hard
• You want to bundle a complex component
The component includes HTML, CSS and JavaScript
how would you do that?
• Use a server side mechanism?
• Bundler? (Webpack/Browserify)
Web Components Standard to The Rescue
• Natively-supported, standardized JavaScript components
• Some general goals:
Code Reuse Encapsulation
Separation of
Concerns
Composition Theming Expressive Semantic
The Web Components Standard
• Reusable DOM fragmentsTemplates
• Load HTML declarativelyImports
• DOM encapsulationShadow DOM
• Create your own elements
Custom
Elements
Custom Elements
• Enable to extend or create custom HTML elements
• Defined using the customElements.define function:
or extend an existing element:
var myInput = window.customElements.define(‘my-input’,
class x extends HTMLElement {…});
var myInput = window.customElements.define(‘my-input’,
class y extends HTMLInputElement {...});
Custom Elements – Usage
• Use the element in your DOM:
or use the createElement function:
<my-input></my-input>
var elm = document.createElement(‘my-input’);
Custom Element Life Cycle Events
• connectedCallback
• disconnectedCallback
• attributeChangedCallback
• adoptedCallback
class MyInput extends HTMLElement {
constructor() {
super();
// your initialization code goes here
}
connectedCallback() {…}
disconnectedCallback() {…}
attributeChangedCallback() {…}
adoptedCallback() {…}
}
Demo
Custom Elements
A Problem with Web Development Today
• Catholic wedding with frameworks/libraries
• Infrastructure is based on a
framework/library
• Infrastructure isn’t reusable if other company
projects use another framework/library
Problem with Web Development Today –
Cont.
• Custom Elements can remove the barrier of framework/library
coupling
• Can be used by any framework/library
• Encapsulate their functionality and style
• Suitable for component infrastructure development
But there are problems with
custom elements
Problems with Custom Elements
• We are used to runtime framework/library goodies such as:
• Virtual DOM
• Data binding
• Performance
• Server side rendering
• And etc.
Problems with Custom Elements – Cont.
• Verbose syntax
• Too much boilerplate
• We need to craft everything by ourselves
Problems with Custom Elements – Cont.
• Still W3C working draft
• Need Polyfills in some browsers
Is there a better way?
Stencil the time for vanilla web components has arrived
What is Stencil?
• A compiler that generates Custom Elements
• Not a framework/library
• Output is 100% standards-compliant web components
• Adds powerful framework features to Web Components
• Virtual DOM
• Reactivity
• JSX
• TypeScript
• And etc.
• Created and used by Ionic Framework
Stencil Component Example
import { Component, Prop } from '@stencil/core';
@Component({
tag: 'my-name',
styleUrl: 'my-name.scss'
})
export class MyName {
@Prop() name: string;
render() {
return (
<p>
Hello, my name is {this.name}
</p>
);
}
}
From Stencil to Custom Elements
import { Component, Prop } from
'@stencil/core';
@Component({
…
})
export class CollapsiblePanel {
…
}
Stencil Code JavaScript Code
Stencil
Compiler
var CollapsiblePanel = (function ()
{
function CollapsiblePanel() {
}
… // implementation
return CollapsiblePanel;
}());
Stencil
Compiler
Stencil the time for vanilla web components has arrived
Getting Started with Stencil
git clone https://github.com/ionic-team/stencil-component-starter.git my-component
cd my-component
git remote rm origin
npm install
npm start
Demo
Hello Stencil
Stencil API
• Based on JavaScript decorators
• Written with TypeScript
• You can use the following decorators:
• @Component()
• @Prop()
• @State()
• @Event()
• @Listen()
• @Element()
• @Method()
@Component Decorator
• The main Stencil decorator
• Configures the entire component including
• Tag
• Style
• Shadow DOM
• Host
• Assets
import { Component } from '@stencil/core';
@Component({
tag: 'st-comp',
styleUrl: 'comp.scss',
shadow: true
})
export class Comp {
...
}
@Prop and @State Decorators
• The Prop decorator is used to indicate that a member is exposed as
component attribute
• The State decorator is used to indicate that a member is part of the
component state
• Reactivity
import {Component, Prop, State} from '@stencil/core';
@Component({
tag: 'collapsible-panel',
styleUrl: 'collapsible-panel.css'
})
export class CollapsiblePanel {
@Prop() title: string;
@State() collapsed: boolean;
...
}
@Method Decorator
• The Method decorator is used to expose component API
import { Component, Element, Method } from '@stencil/core';
@Component({
...
})
export class Toaster {
@Element() toasterDiv: HTMLElement;
@Method()
showToast() {
this.toasterDiv.style.display = 'block';
};
}
Demo
Creating a Stencil Component
Deploying a Stencil Component
• Update the stencil.config.js file, if needed
• stencil.config.js in Stencil starter already has these things configured
exports.config = {
namespace: 'myname',
generateDistribution: true,
generateWWW: false,
...
};
How Stencil helps with the Frameworks
Problem?
• Stencil works primarily in build time
• Any framework/library (such as React or Angular) can consume the
generated component
• As a script tag
• As a node module
• Using the stencil-starter-app
• Stencil is suitable for infrastructure components
Demo
Stencil and React
A Word About Micro Frontends
Shared Components and Behaviors
Generate
Micro-app 1 Micro-app 2 Micro-app 3
Summary
• Web Component standard is very powerful
• But… still in development
• Stencil compiler can ease the pain of creating custom elements
• Includes a lot of advantages such as JSX, TypeScript and more
• Generates standard-compliant web components
Resources
• Stencil website: https://stenciljs.com/
• Custom Elements: https://mzl.la/2L6jyP7
• My Website – http://www.gilfink.net
• Follow me on Twitter – @gilfink
#UseThePlatform
Thank You!

More Related Content

PDF
Stencil the time for vanilla web components has arrived
PPTX
#SPSLondon - Session 2 JSLink for IT Pros
PDF
Build Reusable Web Components using HTML5 Web cComponents
PPTX
Web Components
PDF
Custom Elements with Polymer Web Components #econfpsu16
PPTX
Web Components
PDF
Building search app with ElasticSearch
Stencil the time for vanilla web components has arrived
#SPSLondon - Session 2 JSLink for IT Pros
Build Reusable Web Components using HTML5 Web cComponents
Web Components
Custom Elements with Polymer Web Components #econfpsu16
Web Components
Building search app with ElasticSearch

What's hot (20)

PPTX
The Truth About Your Web App's Performance
PDF
Introduction to React Native
PPTX
RapidApp - YAPC::NA 2014
PDF
The Time for Vanilla Web Components has Arrived
PDF
Web components the future is here
PDF
Sightly - AEM6 UI Development using JS and JAVA
PDF
Web components
PDF
Beyond Domino Designer
PDF
Shootout! Template engines for the JVM
PPTX
Rapi::Blog talk - TPC 2017
PDF
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
PPTX
SPTechCon 2014 How to develop and debug client side code in SharePoint
PDF
Web Components Everywhere
PDF
Introduction to Web Components
PPT
Reaching for the Future with Web Components and Polymer
PDF
Shootout! template engines on the jvm
PPTX
Polymer and web component
PDF
Component-Oriented Web Development with Dart
PDF
Introduction to Web Components
PPTX
Google Developer Group(GDG) DevFest Event 2012 Android talk
The Truth About Your Web App's Performance
Introduction to React Native
RapidApp - YAPC::NA 2014
The Time for Vanilla Web Components has Arrived
Web components the future is here
Sightly - AEM6 UI Development using JS and JAVA
Web components
Beyond Domino Designer
Shootout! Template engines for the JVM
Rapi::Blog talk - TPC 2017
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
SPTechCon 2014 How to develop and debug client side code in SharePoint
Web Components Everywhere
Introduction to Web Components
Reaching for the Future with Web Components and Polymer
Shootout! template engines on the jvm
Polymer and web component
Component-Oriented Web Development with Dart
Introduction to Web Components
Google Developer Group(GDG) DevFest Event 2012 Android talk
Ad

Similar to Stencil the time for vanilla web components has arrived (20)

PDF
Stencil: The Time for Vanilla Web Components has Arrived
PDF
Stencil the time for vanilla web components has arrived
PDF
Web component driven development
PDF
Web Components v1
PDF
Webcomponents are your frameworks best friend
PPT
Java EE Revisits Design Patterns
PPT
Java EE revisits design patterns
PPT
Java EE revisits design patterns
PDF
Advanced Site Studio Class, June 18, 2012
PPTX
Web components, so close!
PDF
Agile sites311training
PDF
Web Development with Delphi and React - ITDevCon 2016
PPT
SE2016 - Java EE revisits design patterns 2016
PPTX
Lightning web components - Introduction, component Lifecycle, Events, decorat...
PPTX
WEB DEVELOPMENT.pptx
PPTX
Html,CSS & UI/UX design
PPTX
web development
PPTX
Introduction to HTML language Web design.pptx
PPTX
JS Essence
PDF
Web Components - The Future is Here
Stencil: The Time for Vanilla Web Components has Arrived
Stencil the time for vanilla web components has arrived
Web component driven development
Web Components v1
Webcomponents are your frameworks best friend
Java EE Revisits Design Patterns
Java EE revisits design patterns
Java EE revisits design patterns
Advanced Site Studio Class, June 18, 2012
Web components, so close!
Agile sites311training
Web Development with Delphi and React - ITDevCon 2016
SE2016 - Java EE revisits design patterns 2016
Lightning web components - Introduction, component Lifecycle, Events, decorat...
WEB DEVELOPMENT.pptx
Html,CSS & UI/UX design
web development
Introduction to HTML language Web design.pptx
JS Essence
Web Components - The Future is Here
Ad

More from Gil Fink (20)

PDF
Becoming a Tech Speaker
PPTX
Web animation on steroids web animation api
PDF
Being a tech speaker
PDF
Working with Data in Service Workers
PDF
Demystifying Angular Animations
PDF
Redux data flow with angular
PDF
Redux data flow with angular
PDF
Who's afraid of front end databases?
PDF
One language to rule them all type script
PDF
End to-end apps with type script
PDF
Redux data flow with angular 2
PDF
Biological Modeling, Powered by AngularJS
PDF
Who's afraid of front end databases
PDF
Biological modeling, powered by angular js
PDF
Whos afraid of front end databases?
PDF
One language to rule them all type script
PDF
Getting Started with TypeScript
PDF
Biological Modeling, Powered by AngularJS
PDF
Biological Modelling, Powered by AngularJS
PDF
Brand New JavaScript - ECMAScript 2015
Becoming a Tech Speaker
Web animation on steroids web animation api
Being a tech speaker
Working with Data in Service Workers
Demystifying Angular Animations
Redux data flow with angular
Redux data flow with angular
Who's afraid of front end databases?
One language to rule them all type script
End to-end apps with type script
Redux data flow with angular 2
Biological Modeling, Powered by AngularJS
Who's afraid of front end databases
Biological modeling, powered by angular js
Whos afraid of front end databases?
One language to rule them all type script
Getting Started with TypeScript
Biological Modeling, Powered by AngularJS
Biological Modelling, Powered by AngularJS
Brand New JavaScript - ECMAScript 2015

Recently uploaded (20)

PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PPTX
"Secure File Sharing Solutions on AWS".pptx
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
assetexplorer- product-overview - presentation
PPTX
Trending Python Topics for Data Visualization in 2025
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PDF
Time Tracking Features That Teams and Organizations Actually Need
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PPTX
Cybersecurity: Protecting the Digital World
PPTX
Custom Software Development Services.pptx.pptx
PDF
Types of Token_ From Utility to Security.pdf
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
MCP Security Tutorial - Beginner to Advanced
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Complete Guide to Website Development in Malaysia for SMEs
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
"Secure File Sharing Solutions on AWS".pptx
Digital Systems & Binary Numbers (comprehensive )
assetexplorer- product-overview - presentation
Trending Python Topics for Data Visualization in 2025
Designing Intelligence for the Shop Floor.pdf
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Salesforce Agentforce AI Implementation.pdf
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Time Tracking Features That Teams and Organizations Actually Need
Why Generative AI is the Future of Content, Code & Creativity?
Cybersecurity: Protecting the Digital World
Custom Software Development Services.pptx.pptx
Types of Token_ From Utility to Security.pdf
Advanced SystemCare Ultimate Crack + Portable (2025)
wealthsignaloriginal-com-DS-text-... (1).pdf
MCP Security Tutorial - Beginner to Advanced
How to Use SharePoint as an ISO-Compliant Document Management System

Stencil the time for vanilla web components has arrived

  • 1. Stencil: The Time for Vanilla Web Components has Arrived Gil Fink sparXys CEO @gilfink / www.gilfink.net
  • 3. From Design to Implementation Track List track <track-list /> <track /> <track-header /> <track-content /> Component Child component <grand-child /> <grand-child /> header content
  • 4. How would you build that page?
  • 5. Do we really need all these frameworks/libraries?
  • 6. What if we could teach the browser new elements?
  • 7. Each Element Instance • Will be a DOM element • Creates its own DOM tree • Can be accessed and manipulated using DOM functions or its own API • Is a JavaScript object • Is this possible?
  • 8. This is where our journey begins
  • 9. About Me • sparXys CEO and senior consultant • Microsoft MVP in the last 9 years • Pro Single Page Application Development (Apress) co-author • 4 Microsoft Official Courses (MOCs) co-author • GDG Rishon and AngularUP co-organizer
  • 10. Agenda • The Problems We Faced • Web Components APIs • Stencil
  • 12. Poor Separation of Concerns You want HTML, CSS and JavaScript to work together You end up with a mess
  • 13. Bundling is Hard • You want to bundle a complex component The component includes HTML, CSS and JavaScript how would you do that? • Use a server side mechanism? • Bundler? (Webpack/Browserify)
  • 14. Web Components Standard to The Rescue • Natively-supported, standardized JavaScript components • Some general goals: Code Reuse Encapsulation Separation of Concerns Composition Theming Expressive Semantic
  • 15. The Web Components Standard • Reusable DOM fragmentsTemplates • Load HTML declarativelyImports • DOM encapsulationShadow DOM • Create your own elements Custom Elements
  • 16. Custom Elements • Enable to extend or create custom HTML elements • Defined using the customElements.define function: or extend an existing element: var myInput = window.customElements.define(‘my-input’, class x extends HTMLElement {…}); var myInput = window.customElements.define(‘my-input’, class y extends HTMLInputElement {...});
  • 17. Custom Elements – Usage • Use the element in your DOM: or use the createElement function: <my-input></my-input> var elm = document.createElement(‘my-input’);
  • 18. Custom Element Life Cycle Events • connectedCallback • disconnectedCallback • attributeChangedCallback • adoptedCallback class MyInput extends HTMLElement { constructor() { super(); // your initialization code goes here } connectedCallback() {…} disconnectedCallback() {…} attributeChangedCallback() {…} adoptedCallback() {…} }
  • 20. A Problem with Web Development Today • Catholic wedding with frameworks/libraries • Infrastructure is based on a framework/library • Infrastructure isn’t reusable if other company projects use another framework/library
  • 21. Problem with Web Development Today – Cont. • Custom Elements can remove the barrier of framework/library coupling • Can be used by any framework/library • Encapsulate their functionality and style • Suitable for component infrastructure development
  • 22. But there are problems with custom elements
  • 23. Problems with Custom Elements • We are used to runtime framework/library goodies such as: • Virtual DOM • Data binding • Performance • Server side rendering • And etc.
  • 24. Problems with Custom Elements – Cont. • Verbose syntax • Too much boilerplate • We need to craft everything by ourselves
  • 25. Problems with Custom Elements – Cont. • Still W3C working draft • Need Polyfills in some browsers
  • 26. Is there a better way?
  • 28. What is Stencil? • A compiler that generates Custom Elements • Not a framework/library • Output is 100% standards-compliant web components • Adds powerful framework features to Web Components • Virtual DOM • Reactivity • JSX • TypeScript • And etc. • Created and used by Ionic Framework
  • 29. Stencil Component Example import { Component, Prop } from '@stencil/core'; @Component({ tag: 'my-name', styleUrl: 'my-name.scss' }) export class MyName { @Prop() name: string; render() { return ( <p> Hello, my name is {this.name} </p> ); } }
  • 30. From Stencil to Custom Elements import { Component, Prop } from '@stencil/core'; @Component({ … }) export class CollapsiblePanel { … } Stencil Code JavaScript Code Stencil Compiler var CollapsiblePanel = (function () { function CollapsiblePanel() { } … // implementation return CollapsiblePanel; }()); Stencil Compiler
  • 32. Getting Started with Stencil git clone https://github.com/ionic-team/stencil-component-starter.git my-component cd my-component git remote rm origin npm install npm start
  • 34. Stencil API • Based on JavaScript decorators • Written with TypeScript • You can use the following decorators: • @Component() • @Prop() • @State() • @Event() • @Listen() • @Element() • @Method()
  • 35. @Component Decorator • The main Stencil decorator • Configures the entire component including • Tag • Style • Shadow DOM • Host • Assets import { Component } from '@stencil/core'; @Component({ tag: 'st-comp', styleUrl: 'comp.scss', shadow: true }) export class Comp { ... }
  • 36. @Prop and @State Decorators • The Prop decorator is used to indicate that a member is exposed as component attribute • The State decorator is used to indicate that a member is part of the component state • Reactivity import {Component, Prop, State} from '@stencil/core'; @Component({ tag: 'collapsible-panel', styleUrl: 'collapsible-panel.css' }) export class CollapsiblePanel { @Prop() title: string; @State() collapsed: boolean; ... }
  • 37. @Method Decorator • The Method decorator is used to expose component API import { Component, Element, Method } from '@stencil/core'; @Component({ ... }) export class Toaster { @Element() toasterDiv: HTMLElement; @Method() showToast() { this.toasterDiv.style.display = 'block'; }; }
  • 39. Deploying a Stencil Component • Update the stencil.config.js file, if needed • stencil.config.js in Stencil starter already has these things configured exports.config = { namespace: 'myname', generateDistribution: true, generateWWW: false, ... };
  • 40. How Stencil helps with the Frameworks Problem? • Stencil works primarily in build time • Any framework/library (such as React or Angular) can consume the generated component • As a script tag • As a node module • Using the stencil-starter-app • Stencil is suitable for infrastructure components
  • 42. A Word About Micro Frontends Shared Components and Behaviors Generate Micro-app 1 Micro-app 2 Micro-app 3
  • 43. Summary • Web Component standard is very powerful • But… still in development • Stencil compiler can ease the pain of creating custom elements • Includes a lot of advantages such as JSX, TypeScript and more • Generates standard-compliant web components
  • 44. Resources • Stencil website: https://stenciljs.com/ • Custom Elements: https://mzl.la/2L6jyP7 • My Website – http://www.gilfink.net • Follow me on Twitter – @gilfink