<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Scipion</title>
    <description>The latest articles on DEV Community by Scipion (@scipion).</description>
    <link>https://dev.to/scipion</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F178122%2Fd88fbb11-6c01-4c1d-b668-6ac551103ae1.png</url>
      <title>DEV Community: Scipion</title>
      <link>https://dev.to/scipion</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/scipion"/>
    <language>en</language>
    <item>
      <title>WebComponents as the standard should be. @github/catalyst</title>
      <dc:creator>Scipion</dc:creator>
      <pubDate>Mon, 07 Dec 2020 16:54:20 +0000</pubDate>
      <link>https://dev.to/scipion/webcomponents-as-the-standard-should-be-github-catalyst-27i8</link>
      <guid>https://dev.to/scipion/webcomponents-as-the-standard-should-be-github-catalyst-27i8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Don't confuse @github/catalyst with the perl framework Catalyst (also hosted in github).&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Good approach, No magic, Production ready.
&lt;/h2&gt;

&lt;p&gt;First of all, don't look for Virtual DOM or Two-way binding in here. But some existing libraries are probably very good to doing this kind of things.&lt;/p&gt;

&lt;p&gt;Many developers feel that &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components"&gt;Web Components&lt;/a&gt; still have to improve or the standard should contain those features. IMHO it would be very cool have what React, Vue or Angular already do but with just native custom elements, but the scene is not jet that.&lt;/p&gt;

&lt;p&gt;Anyway every day more companies are choosing web components to build/improve their modern web pages. And Github is one of these companies&lt;/p&gt;

&lt;p&gt;To make the gap smaller &lt;a href="https://github.com/github/catalyst"&gt;github&lt;/a&gt; have just shipped a nice set of helpers using Typescript (mainly the decorators). It's only on the version 1.0.2, but everything shipped is already working in github.com.&lt;/p&gt;

&lt;h2&gt;
  
  
  Less Boilerplate
&lt;/h2&gt;

&lt;p&gt;Catalyst makes use of decorators to save some repetitive code that every custom element needs. The main one is &lt;code&gt;@controller&lt;/code&gt; to decorate the whole class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;controller&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;TodoAppElement&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;HTMLElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nx"&gt;connectedCallback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Automatically the class is register by calling &lt;code&gt;customElement.register&lt;/code&gt; with the proper name, in the case above it would be &lt;code&gt;todo-app&lt;/code&gt; removing the sufix "Element" from the name.&lt;/p&gt;

&lt;p&gt;To make us the live easier &lt;code&gt;@controller&lt;/code&gt; also injects a call to &lt;code&gt;bind(this)&lt;/code&gt; in the &lt;code&gt;connectedCallback&lt;/code&gt;. This makes the actions work in the current scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Actions and Targets
&lt;/h2&gt;

&lt;p&gt;The main interaction between the dom and the web component class is made by delegating actions (callbacks), that will be public methods in the current class.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;data-action&lt;/code&gt; we have to specify the dom event, webcomponent name and callback name and the binding will be done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click:todo-item#handleOnRemoved&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A class field decorated with &lt;code&gt;@target&lt;/code&gt; will receive the respective dom element. So the field &lt;code&gt;@target elem;&lt;/code&gt; will receive a element marked as &lt;code&gt;data-target="component-name.elem"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The HTMLElemenet can be a target and also the source of actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;new-todo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todo-input.text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;keyup:todo-input#handleInputChange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What needs to be done?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Custom Events
&lt;/h2&gt;

&lt;p&gt;Let's don't forget that we are working with just HTMLElments, so in the end we can receive a DOM Event as well as a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent"&gt;Custom Event&lt;/a&gt; from a nested web coponent. Actions can handle any kind of event and fire any callback defined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;
        removed:todo-list#handleOnRemoved
        checked:todo-list#handleOnChecked
    &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/todo-item&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just by adding extra lines in the &lt;code&gt;data-action&lt;/code&gt; attribute the callbacks will be binded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thanks for reading up to here, this was just a quick summary of what I learned by messing around with &lt;a href="https://www.npmjs.com/package/@github/catalyst"&gt;@github/catalyst&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can check the full code of the &lt;a href="https://github.com/Scipion/catalyst-todo"&gt;ToDo List app here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As I said at the begining, I don't think catalyst is a replacement for modern frameworks but It can be an alternative in the future for other similar tools (today much more matures) like &lt;a href="https://lit-element.polymer-project.org/"&gt;LitElement&lt;/a&gt; or &lt;a href="https://svelte.dev/"&gt;Svelte&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It's still a very young package but let's keep an eye on what Github can ship in the future.&lt;/p&gt;

</description>
      <category>github</category>
      <category>catalyst</category>
      <category>webcomponents</category>
      <category>customelements</category>
    </item>
  </channel>
</rss>
