<?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>Forem: Jorge Suarez</title>
    <description>The latest articles on Forem by Jorge Suarez (@jorgerafaelsj).</description>
    <link>https://forem.com/jorgerafaelsj</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%2F1692%2F681dadf0-ebd6-4002-9ec8-e30bed3fe495.JPG</url>
      <title>Forem: Jorge Suarez</title>
      <link>https://forem.com/jorgerafaelsj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jorgerafaelsj"/>
    <language>en</language>
    <item>
      <title>Creating Web Components with Stencil.js</title>
      <dc:creator>Jorge Suarez</dc:creator>
      <pubDate>Sat, 27 Jan 2018 00:28:07 +0000</pubDate>
      <link>https://forem.com/jorgerafaelsj/creating-web-components-with-stenciljs-5gpk</link>
      <guid>https://forem.com/jorgerafaelsj/creating-web-components-with-stenciljs-5gpk</guid>
      <description>&lt;p&gt;Raise your hand if you have ever inherited 3000, 5000, 8000+ lines of CSS in a single file, then seen some variation of the same file across many projects. That random submit button with a different hover state is pretty annoying. You want to create a component library but will have to go importing it in all the applications, with varying the stacks, and changing css selectors all over. When you move to a different framework, redo it all over again. Solution: Web Components. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Web Components is a suite of different technologies allowing you to create reusable custom user interface components — with their functionality encapsulated away from the rest of your code — and utilize them in your web apps."&lt;/em&gt; - &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components"&gt;MDN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A friend recently introduced me to &lt;a href="https://stenciljs.com/"&gt;Stencil.js&lt;/a&gt;. Blessed be his soul. Stencil, created by the Ionic Framework team, is a compiler that transform your JSX, and Sass to create a Web Components bundled into an NPM package that can be imported into all your projects. We can have one source that will supply consistent branding and behavior through all your applications. Framework agnostic!&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating a Component
&lt;/h4&gt;

&lt;p&gt;With JavaScript class syntax you can name 'MyComponent'. Specify the HTML tag name and your Sass file with the @Component decorator. Pass props with the @Prop decorator and use them with JSX syntax. &lt;/p&gt;

&lt;p&gt;/components&lt;br&gt;
   /component-name&lt;br&gt;
      /component-name.tsx&lt;br&gt;
      /component-name.scss&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Prop&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@stencil/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-first-component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-first-component.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Indicate that name should be a public property on the component&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Prop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&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;h4&gt;
  
  
  Distribution
&lt;/h4&gt;

&lt;p&gt;Their &lt;a href="https://github.com/ionic-team/stencil-component-starter"&gt;starter template&lt;/a&gt; is setup for super easy publishing of your components as an NPM package. Then you can npm install or use the unpkg CDN. Add a script tag in your index.html with the src to your dist file. Voila! Your components are now registered/defined and ready to be used. &lt;a href="https://stenciljs.com/docs/distribution"&gt;distribution docs&lt;/a&gt;.   &lt;/p&gt;

&lt;h4&gt;
  
  
  Usage
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;my-first-component name="Max"&amp;gt;&amp;lt;/my-first-component&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  It Gets Better
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It lazy loads components as they become present in the DOM. If I am understanding correctly, the HTML tags are registered but the rest of the custom content loads once the browser is about to paint each component.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://stenciljs.com/docs/shadow-dom"&gt;Shadow DOM&lt;/a&gt;. Your component is scoped. The styles will not conflict!&lt;/li&gt;
&lt;li&gt;Components have a React-like life cycle. [componentWillLoad, componentDidLoad, componentWillUpdate, componentDidUpdate, componentDidUnload]&lt;/li&gt;
&lt;li&gt;There is a @State decorator to handle component state. &lt;/li&gt;
&lt;li&gt;And more...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'm still learning more about this tool, and looking for other technologies to take into consideration. Let me know what you think and if you have any suggestions. &lt;/p&gt;

&lt;p&gt;Check out the &lt;a href="https://stenciljs.com/docs/intro"&gt;docs&lt;/a&gt; to try it out. &lt;/p&gt;

&lt;h3&gt;
  
  
  Launch Video
&lt;/h3&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/UfD-k7aHkQE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>webcomponents</category>
      <category>react</category>
      <category>vue</category>
      <category>stencil</category>
    </item>
  </channel>
</rss>
