Stencil 101 Introduction to a web components library like no other
Horacio Gonzalez @LostInBrittany
Slide 2
Who are we? Introducing myself and introducing OVH OVHcloud
Slide 3
Horacio Gonzalez @LostInBrittany Spaniard lost in Brittany, developer, dreamer and all-around geek
Flutter
Slide 4
OVHcloud: A Global Leader 200k Private cloud VMs running
1
Dedicated IaaS Europe
30 Datacenters
Own 20Tbps
Hosting capacity : 1.3M Physical Servers 360k Servers already deployed
Netwok with 35 PoPs
1.3M Customers in 138 Countries
Slide 5
OVHcloud: Our solutions
Cloud
Web Hosting
Mobile Hosting
Telecom
VPS
Containers ▪ Dedicated Server
Domain names
VoIP
Public Cloud
Compute ▪ Data Storage
Email
SMS/Fax
Private Cloud
▪ Network and Database
CDN
Virtual desktop
Serveur dédié
Security Object Storage
Web hosting
Cloud Storage Over the Box
▪ Licences
Cloud Desktop
Securities
MS Office
Hybrid Cloud
Messaging
MS solutions
Slide 6
The 3 minutes context What the heck are web component?
Slide 7
Web Components
Web standard W3C
Slide 8
Web Components
Available in all modern browsers: Firefox, Safari, Chrome
Slide 9
Web Components
Create your own HTML tags Encapsulating look and behavior
Slide 10
Web Components
Fully interoperable With other web components, with any framework
Slide 11
Web Components
CUSTOM ELEMENTS
SHADOW DOM
TEMPLATES
Slide 12
Custom Element To define your own HTML tag <body> … <script> window.customElements.define(‘my-element’, class extends HTMLElement {…}); </script> <my-element></my-element> </body>
Slide 13
Shadow DOM To encapsulate subtree and style in an element <button>Hello, world!</button> <script> var host = document.querySelector(‘button’); const shadowRoot = host.attachShadow({mode:’open’}); shadowRoot.textContent = ‘こんにちは、影の世界!’; </script>
Slide 14
Template To have clonable document template <template id=”mytemplate”> <img src=”” alt=”great image”> <div class=”comment”></div> </template> var t = document.querySelector(‘#mytemplate’); // Populate the src at runtime. t.content.querySelector(‘img’).src = ‘logo.png’; var clone = document.importNode(t.content, true); document.body.appendChild(clone);
Slide 15
But in fact, it’s just an element…
● ● ● ●
Attributes Properties Methods Events
Slide 16
Aren’t the multiple Web Components libs a sign of failure? If the standard worked, people would use Vanilla, wouldn’t them?
Slide 17
Web component standard is low level
At it should be!
Slide 18
Standard == basic bricks Standard exposes an API to: ○ Define elements ○ Encapsulate DOM
Slide 19
Libraries are helpers
They give you higher-level primitives
Slide 20
Different high-level primitives
Each one tailored to a use
Slide 21
Sharing the same base
High-performant, low-level, in-the-platform web components standard
Slide 22
Libraries aren’t a failure of standard
They happen by design
Slide 23
Stencil Powering Ionic 4+
Slide 24
Not another library
A Web Component toolchain
Slide 25
A mature technology
Ionic 4 released on year ago, powered by Stencil!
Slide 26
A build time tool
To generate standard web components
Slide 27
Fully featured ●
Web Component-based
●
Component pre-rendering
●
Asynchronous rendering pipeline
●
Simple component lazy-loading
●
TypeScript support
●
JSX support
●
Reactive Data Binding
●
Dependency-free components
Slide 28
And the cherry on the cake
Server-Side Rendering
Slide 29
Stencil leverages the web platform
Working with the web, not against it
Slide 30
The Stencil story A company tired of putting good code in the bin
Slide 31
Once upon a time there was a fight
Between native apps and web app on mobile
Slide 32
A quest to the perfect solution
Hybrid apps, leveraging on web technologies
Slide 33
A company wanted to do it well
The perfect technology for mobile web and hybrid apps
Slide 34
The time is 2013
So what technology would you use?
Slide 35
Really soon after launch…
Hey folks, we are killing AngularJS!
Slide 36
What did Ionic people do?
Let’s put everything in the trash bin and begin anew
Slide 37
But timed have changed…
In 2013 Angular JS was the prom queen
Slide 38
Times have changed…
In 2017 Angular is only one more in the clique
Slide 39
Angular limits adoption of Ionic
Devs and companies are very vocal about JS Frameworks
Slide 40
What did Ionic people do?
Let’s put everything in the trash bin and begin anew… But on which framework?
Slide 41
What about web components?
A nice solution for Ionic problems: Any framework, even no framework at all!
Slide 42
But what Web Component library?
SkateJS
There were so many of them!
Slide 43
Let’s do something different
A fully featured web component toolchain With all the bells and whistles!
Slide 44
Ionic rewrote all their code again
Ionic 4 is fully based on Ionic
Slide 45
Now Ionic works on any framework
Or without framework at all
Slide 46
And we have Stencil
To use it in any of our projects
Slide 47
Hey dude, enough stories! We are here to see some code!
Slide 48
Hands on Stencil Simply use npm init npm init stencil
Choose the type of project to start ? Pick a starter › - Use arrow-keys. Return to submit. ❯
ionic-pwa Everything you need to build fast, production ready PWAs app Minimal starter for building a Stencil app or website component Collection of web components that can be used anywhere Updating Stencil
Slide 49
Hands on Stencil And the project is initialized in some seconds! ✔ Pick a starter › component ✔ Project name › sthlm-j ✔ All setup in 17 ms $ npm start Starts the development server. $ npm run build Builds your components/app in production mode. $ npm test Starts the test runner. We suggest that you begin by typing: $ cd sthlm-js $ npm start Happy coding! 🎈
Slide 50
Starting the development server npm start
Slide 51
Let’s look at the code
Slide 52
Some concepts import { Component, Prop, h } from ‘@stencil/core’; import { format } from ‘../../utils/utils’; @Component({ tag: ‘my-component’, styleUrl: ‘my-component.css’, shadow: true }) export class MyComponent { @Prop() first: string;
Decorators
Slide 53
Some concepts @Prop() first: string; @Prop() middle: string; @Prop() last: string; @State() nickname: string;
Properties and States
Slide 54
Some concepts render() { return <div>Hello, World! I’m {this.getText()}</div>; }
Asynchronous rendering using JSX
Slide 55
Some concepts @Prop() value: number; @Watch(value) valueChanged(newValue: boolean, oldValue: boolean) { console.log(The new value is ${newValue}, it was ${oldValue} before); }
Watch
Slide 56
Some concepts @Event() actionCompleted: EventEmitter; someAction(message: String) { this.actionCompleted.emit(message); }
Emitting events @Listen(‘actionCompleted’) actionCompletedHandler(event: CustomEvent) { console.log(‘Received the custom actionCompleted event: ‘, event.detail); }
Listening to events