<?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: Alberto Forni</title>
    <description>The latest articles on Forem by Alberto Forni (@albertoforni).</description>
    <link>https://forem.com/albertoforni</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%2F277762%2F66f4f108-79e1-49e9-8562-491038063433.jpeg</url>
      <title>Forem: Alberto Forni</title>
      <link>https://forem.com/albertoforni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/albertoforni"/>
    <language>en</language>
    <item>
      <title>Text Color Gradient</title>
      <dc:creator>Alberto Forni</dc:creator>
      <pubDate>Mon, 18 Dec 2023 10:05:29 +0000</pubDate>
      <link>https://forem.com/albertoforni/text-color-gradient-9hl</link>
      <guid>https://forem.com/albertoforni/text-color-gradient-9hl</guid>
      <description>&lt;p&gt;This is the simplest way I found to create a beautiful text gradient effect like the one you can find on Apple Products&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iANyGTB4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l7ak8t8ongwqgegptd9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iANyGTB4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l7ak8t8ongwqgegptd9.png" alt="Apple version of text colour gradient" width="800" height="186"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"title gradient"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Text Gradient&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;width CSS&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.gradient&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;/* add the colour effect as background */&lt;/span&gt;
    &lt;span class="nl"&gt;background-image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="m"&gt;90deg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#1e3791&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#2948b1&lt;/span&gt; &lt;span class="m"&gt;40%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#3153c6&lt;/span&gt; &lt;span class="m"&gt;55%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#385fda&lt;/span&gt; &lt;span class="m"&gt;60%&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c"&gt;/* clip the background so only the text is visible */&lt;/span&gt;
    &lt;span class="nl"&gt;background-clip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;-webkit-background-clip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c"&gt;/* set the text color to transparent so the background colour is visible */&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;transparent&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;and here is the result&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iwsAXekD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo12lc65e7jm1wwv7rvr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iwsAXekD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo12lc65e7jm1wwv7rvr.png" alt="The result of applying a text gradient" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;simple right?!&lt;/p&gt;

&lt;p&gt;happy coding.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>beginners</category>
    </item>
    <item>
      <title>CSS-in-JS vs CSS in CSS</title>
      <dc:creator>Alberto Forni</dc:creator>
      <pubDate>Fri, 03 Nov 2023 11:14:21 +0000</pubDate>
      <link>https://forem.com/albertoforni/css-in-js-vs-css-in-css-19i1</link>
      <guid>https://forem.com/albertoforni/css-in-js-vs-css-in-css-19i1</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Why the question?&lt;/li&gt;
&lt;li&gt;🛠️ Comparison between CSS Modules and CSS-in-JS 🛠️&lt;/li&gt;
&lt;li&gt;🚀 CSS-in-JS: The Trending Path with Vue, Svelte, and Astro 🚀&lt;/li&gt;
&lt;li&gt;🔧 Ok, I’m Convinced About CSS-in-JS. Which Library Should I Pick? 🔧&lt;/li&gt;
&lt;li&gt;Content Security Policy (CSP)&lt;/li&gt;
&lt;li&gt;Have you come across Tailwind CSS?&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Resources&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Why the question?
&lt;/h2&gt;

&lt;p&gt;Picture this: You've got a classic bicycle. It's beautiful, sturdy, and perfect for leisurely rides through the park. But one day, you decide to enter the Giro di Italia. 🚴‍♂️💨 That trusty bike, as lovely as it is, just isn't cut out for the intense, mountainous terrains of the race. Similarly, when the World Wide Web was born, HTML was our trusty bicycle. Designed for simple document structuring, it was perfect for its time. And then came CSS, the stylish accessories to make our HTML bicycle dazzle. But here's the twist: the web evolved! Now, we're not just riding through parks; we're racing in the big leagues.&lt;/p&gt;

&lt;p&gt;As the web landscape shifted towards more interactive and dynamic content, the traditional ways of styling started to show their limits.&lt;/p&gt;

&lt;p&gt;Relying on manual naming, like BEM, while beneficial, can become cumbersome. Imagine having to remember or reference naming conventions for every component you create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* BEM Style */&lt;/span&gt;
&lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.button__icon&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.button--disabled&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The margin for error increases and consistency can waver.&lt;/p&gt;

&lt;p&gt;With the introduction of component-based frameworks, each component acted as a self-contained unit, housing both its logic and style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;/* app.svelte */
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;$state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;on:click=&lt;/span&gt;&lt;span class="s"&gt;{increment}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    clicks: {count}
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, as applications grow, the need for shared styles and variables becomes paramount. Consider the scenario where a primary colour needs to be referenced in multiple components. Instead of hardcoding this in each component, we need to use a scalable solution that can reference code written in other components or parts of the application.&lt;/p&gt;

&lt;p&gt;In conclusion, "Why the question?" It's because the web isn't static. As we strive for more efficient, maintainable, and scalable solutions, we must evaluate and adapt our tools accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  🛠️ Comparison between CSS Modules and CSS-in-JS 🛠️
&lt;/h2&gt;

&lt;p&gt;As we pedal further into our web development journey, the landscape of styling demands a closer look. If we're all in agreement about the need to modularise styles, let's dive deep into comparing two of the most popular techniques: CSS Modules and CSS-in-JS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using CSS Modules:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Separation of Concerns:&lt;/strong&gt; With CSS Modules, styling resides in its own dedicated file. It's like having a separate gear for uphill climbs on our bike journey – dedicated and efficient.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* styles.module.css */&lt;/span&gt;
&lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity:&lt;/strong&gt; No extra baggage! There's no need for additional plugins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hot-Reload:&lt;/strong&gt; A smooth ride with easy and instant updates as you make changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Developer Tools:&lt;/strong&gt; Interacting with dev tools is straightforward since the syntax is the genuine CSS we all know and love.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;File Juggling:&lt;/strong&gt; The separation means you're constantly switching between your component and style files. It's like having to switch bikes mid-race.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unused Styles:&lt;/strong&gt; Identifying and removing styles that are no longer in use can be challenging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variable Usages:&lt;/strong&gt; Spotting typos and leveraging autocomplete becomes trickier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Navigation:&lt;/strong&gt; You miss out on the convenience of jumping directly from your component to the associated style.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using CSS-in-JS:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Close Proximity to View:&lt;/strong&gt; Styles are right next to where they're used. It's like having your water bottle attached to your bike – easily accessible when you need it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&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;css&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;solid-styled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="s2"&gt;`
    h1 {
      color: red;
    }
  `&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Full Power of TypeScript/JavaScript:&lt;/strong&gt; Imagine being able to adjust your bike's settings on-the-go. That's the flexibility you get here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type Checking:&lt;/strong&gt; With TypeScript, you get an extra pair of eyes ensuring you're using the right properties and values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Navigation:&lt;/strong&gt; The "go to definition" feature works seamlessly, especially if the style isn’t directly attached to the HTML element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bulky View Files:&lt;/strong&gt; Since everything is in one place, your component file can get crowded, making it harder to parse at a glance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Additional Tooling:&lt;/strong&gt; Your editor might need some extra setup to understand and highlight styles within JavaScript properly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Overheads:&lt;/strong&gt; To ensure your app runs smoothly, you might need to incorporate specific libraries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bundle Size:&lt;/strong&gt; Due to vendor prefixes not always adhering to &lt;code&gt;.browserlistrc&lt;/code&gt;, your bundle might end up heavier, and your developer experience could suffer with unnecessary styles in the CSS panel.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  But What About Inline &lt;code&gt;style&lt;/code&gt; Attributes?
&lt;/h3&gt;

&lt;p&gt;While it might be tempting to style directly using the &lt;code&gt;style&lt;/code&gt; attribute of HTML elements, there are some pitfalls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lack of Global Styles:&lt;/strong&gt; You miss out on overarching design consistency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited Styling Options:&lt;/strong&gt; Pseudo-classes, keyframes, and states like hover or media queries are off the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Class names are simply faster than inline styles. It's the difference between a road bike and a mountain bike – each has its purpose!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;Both CSS Modules and CSS-in-JS come with their set of advantages and challenges. Your choice will largely depend on the nature of your project, your team's familiarity with the tools, and personal preference. While I won't outright advocate for one over the other, I lean slightly towards CSS-in-JS for TypeScript-heavy projects due to the added benefits of type checking and direct navigation. But remember, the best tool is the one that gets the job done and feels right for your journey!&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 CSS-in-JS: The Trending Path with Vue, Svelte, and Astro 🚀
&lt;/h2&gt;

&lt;p&gt;The ever-changing terrain of web development constantly introduces us to newer tools and techniques. A trend that's hard to miss is the surge in the popularity of CSS-in-JS, especially evident in modern frameworks like Vue, Svelte, and Astro. But what's fuelling this trend?&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the Shift to CSS-in-Component?
&lt;/h3&gt;

&lt;p&gt;Component-Based Architecture: The modular, reusable, and efficient nature of component-based web development naturally complements the CSS-in-JS approach, where styles are directly tied to their respective components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;**Dynamic Styling: **Traditional CSS can feel static. CSS-in-JS breaks that mould, allowing styles to adapt based on props, state, or live data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scoped Styles:&lt;/strong&gt; Global CSS can sometimes lead to style conflicts. With CSS-in-JS, styles are strictly scoped to the component, preventing unintended overrides.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enhanced Developer Experience:&lt;/strong&gt; Features like hot-reloading, immediate feedback, and leveraging JavaScript's capabilities elevate the development process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Spotlight on Vue, Svelte, and Astro:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Vue:&lt;/strong&gt; Vue's single-file components are a developer's delight, integrating template, script, and style in one place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;/* app.vue */
&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyButton&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;scoped&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nc"&gt;.btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The scoped attribute ensures this component's styles won't interfere with others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Svelte:&lt;/strong&gt; Svelte offers a clean and intuitive way to structure components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;/* app.svelte */
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
  &lt;span class="nc"&gt;.btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;white&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="err"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're using a variable directly within our styles, showcasing the dynamic capabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Astro:&lt;/strong&gt; Astro, a newcomer to the scene, emphasises delivering the least amount of JavaScript possible. Here's a glimpse of styling in Astro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;/* app.astro */
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;className=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nc"&gt;.btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;The adoption of CSS-in-JS in frameworks like Vue, Svelte, and Astro is indicative of the web development community's desire for more integrated, efficient, and dynamic styling methods. These frameworks, by melding styles with components, offer an approach that's both intuitive and powerful, ensuring web experiences are not only functional but also aesthetically pleasing.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔧 Ok, I’m Convinced About CSS-in-JS. Which Library Should I Pick? 🔧
&lt;/h2&gt;

&lt;p&gt;Diving into the world of CSS-in-JS, it's evident that many contemporary frameworks come with styling solutions out of the box. But, if you're like me, with a React project in hand, you might be thinking, "Alright, I'm convinced. Hand me the right library!" But wait, it's not that straightforward.&lt;/p&gt;

&lt;p&gt;Just as the web development landscape is vast and varied, so are the choices for CSS-in-JS libraries. It's reminiscent of the myriad state management libraries available – each with its unique features and quirks. But fear not! Let's break down the essential features to consider, helping you navigate this space with clarity:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;&lt;/code&gt; (Tagged Templates)&lt;/strong&gt;&lt;br&gt;
Using ES Tagged Templates, styles are defined as strings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;📝 Syntax:&lt;/strong&gt; Adheres to the familiar kebab-case, akin to plain CSS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🔄 Migration:&lt;/strong&gt; Transitioning from plain CSS to CSS-in-JS becomes smoother, saving you from a total rewrite.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🔧 Tooling:&lt;/strong&gt; To beautify your code with syntax highlighting and code completion, you'll need extra plugins. Without them, it's just plain strings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🔄 Parsing:&lt;/strong&gt; An additional step is needed to morph the string into JS.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. { } (Object Styles)&lt;/strong&gt;&lt;br&gt;
Employing plain JavaScript objects, styles are defined as... well, objects!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;**📝 Syntax: **Leverages camelCase for property names, echoing React Native conventions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🔄 Migration:&lt;/strong&gt; Brace yourself for a full rewrite if you're moving existing CSS.&lt;br&gt;
for instance, &lt;code&gt;backgroundColor: "transparent"&lt;/code&gt;, in JS becomes &lt;code&gt;background-color: transparent;&lt;/code&gt; in CSS and Developer Tools. Note the&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;camelCase → kebab case,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;,&lt;/code&gt; instead of &lt;code&gt;;&lt;/code&gt;,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;""&lt;/code&gt; go away.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;- 🔧 Tooling:&lt;/strong&gt; The beauty of JS objects means you get out-of-the-box syntax highlighting, sans any extra tooling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. .css (Static CSS Extraction)&lt;/strong&gt;&lt;br&gt;
Your styles get a new life as static .css files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;⚖️ Bundle Size:&lt;/strong&gt; Say goodbye to additional runtime libraries, trimming your bundle/page size.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🚀 Performance:&lt;/strong&gt; With an empty cache, this might be a tad slower for your users (affecting FCP/FMP metrics). But with a full cache? Lightning-fast page loads.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🎨 Dynamic Styling:&lt;/strong&gt; The generated file could bulk up since all style combos need pre-generation at build time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🖥️ Use Case:&lt;/strong&gt; Ideal for scenarios like e-commerce sites or blogs where cached styles play a pivotal role.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag&lt;/strong&gt;&lt;br&gt;
Your styles find their home inside &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tags within the document's &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🎨 Dynamic Styling:&lt;/strong&gt; Tailor-making styles on-the-fly becomes a breeze.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;⚖️ Payload:&lt;/strong&gt; A tad bulkier, given that a runtime library is required for dynamic style handling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🔄 SSR Considerations:&lt;/strong&gt; Styles necessary for the initial render are shipped twice: during SSR and hydration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;🖥️ Use Case:&lt;/strong&gt; Perfect for SPAs that are dynamic and interactive.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Content Security Policy (CSP)
&lt;/h2&gt;

&lt;p&gt;A common problem that libraries that create a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag at runtime could face is the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP"&gt;Content Security Policy&lt;/a&gt;. Styles might be blocked if the HTML page contains &lt;code&gt;Content-Security-Policy: style-src&lt;/code&gt;. For that, you can use a &lt;code&gt;nonce&lt;/code&gt; that is dynamically produced from the server for every HTTP request and injected into your &lt;code&gt;style&lt;/code&gt; tag. This is how &lt;a href="https://emotion.sh/docs/@emotion/cache#nonce"&gt;Emotion&lt;/a&gt; does it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Have you come across Tailwind CSS?
&lt;/h2&gt;

&lt;p&gt;It's a notable player that has popularised the Atomic CSS approach. While it simplifies styling by allowing users to directly add CSS classes to HTML elements and offering sensible defaults, it does introduce a new "language" to learn. It could be a good option if you start from scratch, but it might not be the most practical choice for existing projects that have extensively utilised traditional CSS or CSS-in-JS.&lt;/p&gt;

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

&lt;p&gt;As you set out to pick your ideal CSS-in-JS library, keep in mind the specific needs and nuances of your project. While there's no one-size-fits-all solution, understanding the features and trade-offs of each approach will ensure you make an informed choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://css-tricks.com/a-thorough-analysis-of-css-in-js/?unapproved=1805860&amp;amp;moderation-hash=40e922499ef117063610ca604588240e#comment-1805860"&gt;A Thorough Analysis of CSS-in-JS | CSS-Tricks&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/airbnb-engineering/airbnbs-trip-to-linaria-dc169230bd12"&gt;https://medium.com/airbnb-engineering/airbnbs-trip-to-linaria-dc169230bd12&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/andreipfeiffer/css-in-js/blob/main/README.md"&gt;https://github.com/andreipfeiffer/css-in-js/blob/main/README.md&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Make Your Code Self-Documenting with Descriptive Event Handler Names</title>
      <dc:creator>Alberto Forni</dc:creator>
      <pubDate>Mon, 09 Jan 2023 15:52:49 +0000</pubDate>
      <link>https://forem.com/albertoforni/make-your-code-self-documenting-with-descriptive-event-handler-names-210i</link>
      <guid>https://forem.com/albertoforni/make-your-code-self-documenting-with-descriptive-event-handler-names-210i</guid>
      <description>&lt;p&gt;In software development, one of the key principles is to make code that is &lt;strong&gt;easy to read and understand&lt;/strong&gt;. This is especially important in &lt;strong&gt;declarative systems&lt;/strong&gt; like &lt;strong&gt;React&lt;/strong&gt;, Vue or Svelte where the code describes what the system is supposed to do rather than the specific steps to do it. When you give your event handlers meaningful names, it becomes much easier for other developers (and yourself, when you come back to the code later) to understand the purpose of the handler just by reading the name.&lt;/p&gt;

&lt;p&gt;For example, consider the following two event handlers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// meaningful event handlers&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Touch&lt;/span&gt;
  &lt;span class="na"&gt;onTouchStart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;saveOffsetForMoveAndFocus&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;onTouchMove&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;moveWindow&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
...
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Touch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// generic event handlers&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Touch&lt;/span&gt;
  &lt;span class="na"&gt;onTouchStart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleTouchStart&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;onTouchMove&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleTouchMove&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
...
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Touch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which of these handlers is more meaningful? The first example clearly communicates what is supposed to happen, whereas the second one does not so, you need to browse the rest of the code to understand its purpose.&lt;/p&gt;

&lt;p&gt;Using descriptive names for your event handlers makes your code more &lt;strong&gt;self-documenting&lt;/strong&gt;, which can save time and reduce the need for detailed documentation. It can also make it easier to &lt;strong&gt;find and fix bugs&lt;/strong&gt; in your code, as the names can provide clues about the purpose of the event handler and where it is being used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wait, this way my function names are gonna be too long!!!
&lt;/h2&gt;

&lt;p&gt;When it comes to the length of method names, &lt;strong&gt;Uncle Bob&lt;/strong&gt; suggests that it is generally &lt;em&gt;better to err on the side of longer and more descriptive names, rather than shorter and more vague names&lt;/em&gt;. In "Clean Code" he writes:&lt;/p&gt;

&lt;p&gt;"Don’t be afraid to make a name long. A long descriptive name is better than a short enigmatic name. A long descriptive name is better than a long descriptive comment. Use a naming convention that allows multiple words to be easily read in the function names, and then make use of those multiple words to give the function a name that says what it does."&lt;/p&gt;

&lt;p&gt;To add to that, I recently came across a concept: the &lt;strong&gt;"Principle of Least Astonishment" or POLA&lt;/strong&gt;, which states that code should be written in a way that minimises the surprise of the reader. I think that by giving your event handlers descriptive names, you can make it clear what they do and reduce the potential for a surprise when reading the code.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
