WEBCOMPONENTS ARE YOUR
FRAMEWORKS BEST FRIEND
@filipbech
colibo.com
WRITE ONCE - USE
ANYWHERE
the promise of webcomponents
THIS TALK WILL BE
• Webcomponents - what and why
• How (basic tech intro)
• Using Webcomponents in your framework
• Your framework-component as a Web-component
WHY
• Interoperability - portability
• Native components brings us native performance
• Framework == implementation detail
ITS (FINALLY) HAPPENING…
SPECS
• custom elements
• shadow dom
• <template>
• es-modules
ATTRIBUTES VS PROPERTIES
CONTENT PROJECTION WITH SLOT
NAMED SLOTS
STYLING
• scoped to shadowRoot
• custom-properties
• special selectors - eg. :host, :slotted etc.
• ::parts on the horizon
WITH FRAMEWORKS
HOW TO USE FROM WITHIN FRAMEWORKS
• angular
• vue
• react (preact)
COMPARING FEATURE-COMPLETENESS
• The tests checks that a framework supports the usage of custom elements
• Each test has an associated weight, based on how critical it is. The final tally of
pass/fails is combined with these weights to create a weighted average score.
• Testing in browsers with native support AND in old browser with polyfill
• All credit goes to @rob_dodson
https://custom-elements-everywhere.com/
ANGULAR
https://custom-elements-everywhere.com/
VUE
https://custom-elements-everywhere.com/
REACT
https://custom-elements-everywhere.com/
REACT 17 MAYBE?
https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html
https://twitter.com/dan_abramov/status/1135440680375873536
https://github.com/facebook/react/issues/11347
PREACT TO THE RESCUE..?
https://custom-elements-everywhere.com/
SO…
• You can do it yourself
• Get a reference to the custom-element, and pass properties to it
• get a reference to the custom-element and addEventListener
YOUR COMPONENT
AS A CUSTOM-ELEMENT
the why, the how and all the details
WHY
• Interoperability
• Framework is an implementation detail
• The component-developer chooses their preferred tools
• Write a datepicker in React - use it in an Angular form
HOW
• Native Webcomponent framework
• Wrap in custom-element
WRAPPING
• Element-properties for data
• Dom-events for events
• Slots
• Style encapsulation w. shadowDOM
THE COMPARISON
• features
• css encapsulation
• slots
• properties (inputs)
• events (outputs)
• ease of use (for dev)
• size of bundle
• NOT framework integration (too hard to compare)
EXPORT TO WEBCOMPONENTS
• Angular (official)
• Vue (official)
• React (community)
WHAT ARE WE BUILDING
• a custom-button component
• takes a label as a property
• Has a Slot for content
• counts clicks
• emits a custom event (“action”) when clicked
• contains an h1, and styles applied to h1
@ANGULAR/ELEMENTS
NG-CONTENT OR <SLOT>
DISABLE NGZONE
RESULTS
ivy
ANGULAR 9 NEXT 16
IVY ELEMENTS
• Maybe with 9? Later?
• smaller, simpler, faster (~15kb)
• lazyNgElement
https://www.youtube.com/watch?v=JX5GGu_7JKc
@VUE/WEB-COMPONENT-
WRAPPER
BUILDING
WITH VUE
• feels elegant and light
• e.detail is an array of additional arguments to $emit()
• Doesn’t handle styles => points to css-modules as the solution
• The bundle is tiny - but requires a global vue (cannot be tree-shaken)
RESULTS
REACT
REACT IS DIFFERENT
• Passing callbacks as props instead of custom events
• CSS-in-JS
REACT…
• no official wrapper
• react-web-component
• web-react-components
• @adobe/react-webcomponent
• skateJS
WEB-REACT-COMPONENTS
• Looks like the real deal - but also not maintained
• Register()-method to wrap and register
• props.children is a <slot>
• Hack styles with <style dangerouslySetInnerHTML…>
• Requires the polyfill as it ships es5 😒
https://github.com/ChristophP/web-react-components
RESULTS
THE RESULTS ARE IN…
Angular Vue React
Consume 100 % 100 % 71 %
Wrap - features 😀 🙂 😕
Wrap - DX 🙂 🙂 😀
Wrap - bundlesize ~49kb ~36kb ~42kb
DO WE NEED FRAMEWORKS
can we use webcomponents alone?
USING WEBCOMPONENTS ALONE
• Sugar on top
• LitElement, Stencil, Svelte,
• Helpers below
• Utility-libraries for http, state-management, etc…
https://developers.google.com/web/fundamentals/web-components/best-practices
PLATFORM RESULTS
LitElement
Consume 100 %
All features 😀
Developer experience 😀
Wrap - bundlesize ~7kb
THE PLATFORM STILL HAVE SOME GAPS
• ::parts
• scoped registries
• import maps
• polyfilling is non-trivial
CONCLUSION
CONCLUSION
• Webcomponents and your framework complement each other.
• Doable today, but still a little complicated and some missing features
• Vue and Angular are good in using webcomponents. React not so much atm
• The wrappers are pretty cool, but incomplete, large and/or complex
(and no official wrapper for react)
• <slot>’s don’t mix/match so well with framework-features
• Lets make this happen…
https://github.com/filipbech/framework-webcomponents
THANK YOU!
This was “webcomponents are your frameworks best friend”
Im @filipbech
Follow for slides

Webcomponents are your frameworks best friend