<?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: Wendell Adriel</title>
    <description>The latest articles on DEV Community by Wendell Adriel (@wendell_adriel).</description>
    <link>https://dev.to/wendell_adriel</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%2F51286%2Fd989ec38-edfb-4bb0-80af-7e252eb464d0.jpeg</url>
      <title>DEV Community: Wendell Adriel</title>
      <link>https://dev.to/wendell_adriel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wendell_adriel"/>
    <language>en</language>
    <item>
      <title>Structuring a Server-Side Application</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Fri, 05 Aug 2022 11:39:17 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/structuring-a-server-side-application-94f</link>
      <guid>https://dev.to/wendell_adriel/structuring-a-server-side-application-94f</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is the English version of an article that I wrote in Portuguese &lt;a href="https://dev.to/wendell_adriel/estruturando-uma-aplicacao-server-side-548b"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, I'll talk about how can we structure a server-side application in a simple way, while using a clean and efficient architecture that could be used for small to large applications.&lt;/p&gt;

&lt;p&gt;This will be a "theoric" article, I won't show code and it won't be tied to a specific language, it's going to be an approach based on the design/architecture of the application, this way you can apply it to any language that you may know.&lt;/p&gt;

&lt;p&gt;I hope that this article can help you!&lt;/p&gt;

&lt;h2&gt;
  
  
  Once Upon a Time the MVC
&lt;/h2&gt;

&lt;p&gt;Probably you heard about the &lt;strong&gt;famous MVC&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;M - Model - Data Layer&lt;/li&gt;
&lt;li&gt;V - View - Visual Layer&lt;/li&gt;
&lt;li&gt;C - Controller - Communication Layer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a software architecture heavily used some time ago and probably still used nowadays for simpler software.&lt;/p&gt;

&lt;p&gt;In this architecture we have three different layers where the access to the data (Database) is on the &lt;strong&gt;Data Layer&lt;/strong&gt;, that's accessed by the &lt;strong&gt;Communication Layer&lt;/strong&gt; that's the layer responsible for receiving the requests from the &lt;strong&gt;Visual Layer&lt;/strong&gt; where the user interacts with the software, doing the needed processing and returning the response back to the &lt;strong&gt;Visual Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is a simple and practical architecture, but with the advance of the software that started to grow in complexity, this architecture started to be obsolete because of the coupling of the code and the layers starting to have a lot of responsibilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Evolving the Architecture of the Applications
&lt;/h2&gt;

&lt;p&gt;Nowadays the Client-Side (Front-end) applications are complex as the Server-Side (Back-end) applications and can have different architectures and patterns to be created on a way to support small and simple, but also large and complex applications. Because of that, the &lt;strong&gt;MVC&lt;/strong&gt; architecture can be broken down, having the &lt;strong&gt;Visual Layer&lt;/strong&gt; as a totally different application with its own architecture. The focus of this article will be the architecture of a Server-Side application.&lt;/p&gt;

&lt;p&gt;Removing the &lt;strong&gt;Visual Layer&lt;/strong&gt; of our architecture, if we were going to follow the &lt;strong&gt;MVC&lt;/strong&gt; architecture we would be left with only two layers: the &lt;strong&gt;Data Layer&lt;/strong&gt; and the &lt;strong&gt;Communication Layer&lt;/strong&gt;, but as I said before this type of architecture results in a coupled code, where we can have code duplication and a lot of responsibilities in a single layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The New Server-Side Architecture
&lt;/h2&gt;

&lt;p&gt;Since the two layers that we talked about early aren't enough to create a clean and easy-to-maintain application, the solution is to recreate the architecture of our server-side application in a way that gets cleaner and decoupled.&lt;/p&gt;

&lt;p&gt;There are many books, courses and tutorials out there and also N types of architectures that are possible to follow, here I'll explain only one of those architectures but that has been working good for me and the projects that I've been working on in the last years.&lt;/p&gt;

&lt;p&gt;The architecture that I've been using is composed of four layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Communication Layer&lt;/li&gt;
&lt;li&gt;Validation Layer&lt;/li&gt;
&lt;li&gt;Service/Business Layer&lt;/li&gt;
&lt;li&gt;Data Layer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below I'll explain briefly each one of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Communication Layer
&lt;/h3&gt;

&lt;p&gt;This is the simplest layer of the application and the one with fewer responsibilities, as the name already says, this is the communication layer between the user and the logic of our application. It's responsible only for the IO (Input &amp;amp; Output) of our application, this layer receives the data sent by the user (it could be through a CLI tool or a Web page or a Mobile App) and returns the result back to the user after going through the other layers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Validation Layer
&lt;/h3&gt;

&lt;p&gt;This layer is responsible for validating the data received by the &lt;strong&gt;Communication Layer&lt;/strong&gt;. And if you're asking the reason for separating those two layers, the answer is simple. Imagine that currently your application communicates with a Web page and all the validation is coupled with the HTTP request. Now you or your company decide to create a CLI tool for your application. Being your validation logic coupled with the HTTP request, now you'll have to recreate all the validation logic again. If you had separated that into a specific layer, you just needed to create a new &lt;strong&gt;Communication Layer&lt;/strong&gt; that would be your CLI tool and the whole application would be working fine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Service/Business Layer
&lt;/h3&gt;

&lt;p&gt;This is the most critical layer of our application, here lies all the business logic, the core of our application. This layer is responsible for getting the already validated data from the &lt;strong&gt;Validation Layer&lt;/strong&gt;, getting the needed data, applying the needed logic and formatting the data to send back to the &lt;strong&gt;Communication Layer&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Layer
&lt;/h3&gt;

&lt;p&gt;This is the layer responsible for getting and saving data into the application's database (s). To have the separation between the business logic and the data itself, this should be the &lt;strong&gt;ONLY&lt;/strong&gt; layer that accesses the Database(s), this way if any other layer needs to access this data, it should be done through this layer.&lt;/p&gt;

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

&lt;p&gt;In this article, we learned what's the &lt;strong&gt;MVC&lt;/strong&gt; architecture, the reason that this architecture is obsolete nowadays and we say an example of an architecture for modern Server-Side applications that's simple but efficient to create clean and easy-to-maintain applications. This architecture can be used with any language and it's not only for Web applications but as well for APIs, CLIs, etc.&lt;/p&gt;

&lt;p&gt;I hope that this article helped you to have a better understanding of how the architecture and the design of an application can impact A LOT in the final result of our software in order to create a clean, easy to maintain and evolve software.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>architecture</category>
    </item>
    <item>
      <title>#Descomplica - DTO</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Tue, 21 Jun 2022 11:40:56 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/descomplica-dto-39h4</link>
      <guid>https://dev.to/wendell_adriel/descomplica-dto-39h4</guid>
      <description>&lt;h2&gt;
  
  
  Introdução
&lt;/h2&gt;

&lt;p&gt;E aí pessoas, tudo bem?&lt;br&gt;
Esse será o primeiro artigo do &lt;strong&gt;#Descomplica&lt;/strong&gt;, uma série de pequenos artigos que irão sempre focar em um tema específico, com uma abordagem simples e direta sobre o assunto e o nosso primeiro assunto será sobre &lt;strong&gt;DTOs&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  O que é um DTO
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DTO (Data Transfer Object/Objeto de Transferência de Dados)&lt;/strong&gt; é um(a) &lt;strong&gt;Design Pattern/Padrão de Projeto&lt;/strong&gt; simples, porém eficiente para se aplicar na arquitetura um software.&lt;/p&gt;

&lt;p&gt;Um &lt;strong&gt;DTO&lt;/strong&gt; nada mais é que um objeto utilizado para encapsular e mapear dados primitivos e que podem ser serializados.&lt;/p&gt;
&lt;h2&gt;
  
  
  Como criar e utilizar um DTO
&lt;/h2&gt;

&lt;p&gt;Como dito anteriormente, um &lt;strong&gt;DTO&lt;/strong&gt; nada mais é que um objeto simples para encapsular e mapear dados que serão enviados entre as camadas da nossa aplicação. Então vamos ver um exemplo bem simples em &lt;strong&gt;PHP&lt;/strong&gt; de um &lt;strong&gt;DTO&lt;/strong&gt; utilizado para encapsular dados para um endpoint/serviço que irá salvar um usuário:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SaveUserDTO&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="c1"&gt;// GETTERS AND SETTERS&lt;/span&gt;
&lt;span class="p"&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 php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$userDTO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SaveUserDTO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Wendell'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'me@wendelladriel.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'S3cR3t'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Como podem ver acima, criar e utilizar um &lt;strong&gt;DTO&lt;/strong&gt; é extremamente simples e fácil. Agora vamos aprender o porquê utilizar esse padrão.&lt;/p&gt;

&lt;h2&gt;
  
  
  Porquê utilizar DTOs
&lt;/h2&gt;

&lt;p&gt;Para mim, a utilização de &lt;strong&gt;DTOs&lt;/strong&gt; no código gera um código simples, limpo e de fácil manutenção. Para quem trabalha com &lt;strong&gt;Laravel&lt;/strong&gt; e outros frameworks &lt;strong&gt;PHP&lt;/strong&gt; já devem ter visto como eu vi em várias aplicações algo como abaixo nos controllers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;JsonResponse&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;all&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;No código acima o código pega todos os parâmetros do &lt;strong&gt;Request&lt;/strong&gt; e os envia para a &lt;strong&gt;Camada de Serviço&lt;/strong&gt;. Alguns dos problemas criados por isso são:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Não temos a definição de quais valores estão a ser enviados&lt;/li&gt;
&lt;li&gt;Caso o Request seja atualizado, não vamos saber de forma fácil o que foi alterado&lt;/li&gt;
&lt;li&gt;Para alguém que não conhece o código ou se ficar um tempo sem mexer no código, dificilmente saberá os possíveis valores desse Request&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Se ao invés de fazer isso, usarmos o &lt;strong&gt;DTO&lt;/strong&gt; que criamos acima, qualquer dev que entrar na equipe ou caso alguém precise fazer alguma alteração ou manutenção no código, será muito mais fácil de saber quais são os dados esperados pelo serviço, o que podemos utilizar e como alterar algo se necessário.&lt;/p&gt;

&lt;p&gt;O código fica muito mais limpo, fica muito mais fácil de dar manutenção ou alterar algo e oferece uma excelente DX (Developer Experience/Experiência para o Desenvolvedor) para toda a equipe!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusão
&lt;/h2&gt;

&lt;p&gt;Vimos nesse artigo o que é, como utilizar e o porquê utilizar DTOs para melhorar a arquitetura da nossa aplicação! Espero que tenham gostado e aproveitem pra comentar aqui se conheciam esse padrão, se utilizam e como utilizam!&lt;/p&gt;

&lt;p&gt;Um grande abraço e até o próximo &lt;strong&gt;#Descomplica&lt;/strong&gt;!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>architecture</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Estruturando uma Aplicação Server-Side</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Mon, 20 Jun 2022 12:01:19 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/estruturando-uma-aplicacao-server-side-548b</link>
      <guid>https://dev.to/wendell_adriel/estruturando-uma-aplicacao-server-side-548b</guid>
      <description>&lt;h2&gt;
  
  
  Introdução
&lt;/h2&gt;

&lt;p&gt;E aí pessoal, depois de um hiato de dois anos sem escrever nenhum artigo, esse será o meu primeiro artigo pra voltar a escrever e tentar ajudar a comunidade de alguma forma.&lt;/p&gt;

&lt;p&gt;Nesse artigo irei falar de como estruturar uma aplicação server-side de forma simples, mas com uma arquitetura limpa e eficiente para pequenas e grandes aplicações.&lt;/p&gt;

&lt;p&gt;Esse será um artigo "teórico", não irei mostrar código e não estará ligado a uma linguagem específica, será uma abordagem no ponto de vista do design e arquitetura da aplicação, dessa forma vocês poderão utilizar em qualquer linguagem que conheçam.&lt;/p&gt;

&lt;p&gt;Espero que esse artigo venha a ser útil para alguém!&lt;/p&gt;

&lt;h2&gt;
  
  
  Era uma vez o MVC
&lt;/h2&gt;

&lt;p&gt;Provavelmente vocês já devem ter ouvido falar do &lt;strong&gt;famoso MVC&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;M - Model (Modelo) - Camada de Dados&lt;/li&gt;
&lt;li&gt;V - View (Visualização) - Camada de Visualização&lt;/li&gt;
&lt;li&gt;C - Controller (Controlador) - Camada de Comunicação&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Essa é uma arquitetura de software muito utilizada há alguns tempos atrás e provavelmente ainda usada hoje em dia para softwares mais simples.&lt;/p&gt;

&lt;p&gt;Nessa arquitetura, temos três camadas distintas onde o acesso aos dados (Base de Dados) fica na &lt;strong&gt;Camada de Dados&lt;/strong&gt;, que por sua vez é acessada pela &lt;strong&gt;Camada de Comunicação&lt;/strong&gt; que é a camada responsável por receber os pedidos da &lt;strong&gt;Camada de Visualização&lt;/strong&gt; onde o usuário interage com o software, fazer o processamento necessário e retornar a resposta novamente para a &lt;strong&gt;Camada de Visualização&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Essa é uma arquitetura simples e prática, porém com o avanço dos softwares que foram ficando mais complexos essa arquitetura começou a ficar um pouco obsoleta pelo acoplamento do código e as camadas foram ficando com muitas responsabilidades.&lt;/p&gt;

&lt;h2&gt;
  
  
  Evoluindo a Arquitetura das Aplicações
&lt;/h2&gt;

&lt;p&gt;Hoje em dia as aplicações Client-Side (Front-end) são tão complexas quanto as aplicações Server-Side (Back-end) e possuem arquiteturas e padrões para serem criadas de forma a suportar pequenas e simples, mas também grandes e complexas aplicações. Por isso a arquitetura &lt;strong&gt;MVC&lt;/strong&gt; pode ser quebrada, tendo a &lt;strong&gt;Camada de Visualização&lt;/strong&gt; se tornado uma aplicação com sua própria arquitetura. O foco desse artigo será a arquitetura da aplicação Server-Side.&lt;/p&gt;

&lt;p&gt;Tirando a &lt;strong&gt;Camada de Visualização&lt;/strong&gt; da nossa arquitetura, se fóssemos seguir a arquitetura &lt;strong&gt;MVC&lt;/strong&gt; iríamos sobrar apenas com duas camadas: a &lt;strong&gt;Camada de Dados&lt;/strong&gt; e a &lt;strong&gt;Camada de Comunicação&lt;/strong&gt;, mas como eu disse anteriormente esse tipo de arquitetura resulta em um código muito acoplado, onde podemos ter duplicação de código e muitas responsabilidades em uma única camada.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Nova Arquitetura Server-Side
&lt;/h2&gt;

&lt;p&gt;Se as duas camadas que falamos anteriormente não são suficientes para criar uma aplicação mais limpa e fácil de ser mantida, a solução é recriar a arquitetura da nossa aplicação server-side para que ela fique mais desacoplada e mais limpa.&lt;/p&gt;

&lt;p&gt;Existem diversos livros, cursos e tutoriais falando sobre e também existem N tipos de arquitetura que são possíveis, aqui eu vou explicar apenas uma dessas arquiteturas, mas que funciona para mim e os projetos que tenho trabalhado nos últimos anos.&lt;/p&gt;

&lt;p&gt;A arquitetura que venho utilizando é composta por quatro camadas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Camada de Comunicação&lt;/li&gt;
&lt;li&gt;Camada de Validação&lt;/li&gt;
&lt;li&gt;Camada de Serviço/Negócio&lt;/li&gt;
&lt;li&gt;Camada de Dados&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Abaixo irei explicar resumidamente cada camada.&lt;/p&gt;

&lt;h3&gt;
  
  
  Camada de Comunicação
&lt;/h3&gt;

&lt;p&gt;Essa é a camada mais simples da aplicação e com menos responsabilidades, como o próprio nome diz, ela é a camada de comunicação entre o usuário e a lógica da nossa aplicação. Ela é responsável apenas pelo IO (Input &amp;amp; Output) da nossa aplicação, ou seja, essa aplicação recebe os dados enviados pelo usuário (pode ser através de uma ferramenta CLI ou uma página Web ou uma App de dispositivos móveis) e retorna o resultado de volta ao usuário depois de passar pelas outras camadas.&lt;/p&gt;

&lt;h3&gt;
  
  
  Camada de Validação
&lt;/h3&gt;

&lt;p&gt;Essa camada é responsável por validar os dados recebidos pela &lt;strong&gt;Camada de Comunicação&lt;/strong&gt;. E se você está perguntando o motivo de separar essas duas camadas a resposta é simples. Imagine que hoje sua aplicação comunique com uma página Web e toda validação esteja acoplada com a requisição HTTP. Agora você ou sua empresa decidem criar uma ferramenta CLI para a aplicação. Estando sua lógica de validação acoplada com a requisição HTTP, você terá de recriar toda a lógica de validação novamente. Se você tivesse separado isso em uma camada específica, bastava recriar uma nova &lt;strong&gt;Camada de Comunicação&lt;/strong&gt; que seria para a ferramenta CLI e toda sua aplicação estaria a funcionar normalmente.&lt;/p&gt;

&lt;h3&gt;
  
  
  Camada de Serviço/Negócio
&lt;/h3&gt;

&lt;p&gt;Essa é a camada mais importante da nossa aplicação, aqui é onde fica toda a lógica de negócio, todo o núcleo da aplicação. Essa camada é responsável por receber os dados já validados da &lt;strong&gt;Camada de Validação&lt;/strong&gt;, buscar os dados necessários, aplicar a lógica necessária e formatar os dados de forma a enviar de volta para a &lt;strong&gt;Camada de Comunicação&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Camada de Dados
&lt;/h3&gt;

&lt;p&gt;Essa é a camada responsável por buscar e salvar os dados na(s) Base(s) de Dados da aplicação. Para se ter a separação da regra de negócio dos dados, essa deve ser a &lt;strong&gt;ÚNICA&lt;/strong&gt; camada que faz acesso à(s) Base(s) de Dados, dessa forma se qualquer outra camada necessitar de acessar esses dados, deve ser feito através dessa camada.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusão
&lt;/h2&gt;

&lt;p&gt;Com esse artigo aprendemos o que é a arquitetura &lt;strong&gt;MVC&lt;/strong&gt;, os motivos dessa arquitetura estar obsoleta nos dias de hoje e vimos o exemplo de uma arquitetura para as aplicações Server-Side atuais que apesar de simples, é eficaz para se criar aplicações limpas e com código menos acoplado. Essa arquitetura pode ser aplicada em qualquer linguagem e não apenas para aplicações Web, como também APIs, CLIs, etc. &lt;/p&gt;

&lt;p&gt;Espero que esse artigo possa ajudar vocês a terem uma visão de como a arquitetura e o design da aplicação pode influenciar e MUITO no resultado final do software, para conseguir criar um software limpo e fácil de ser mantido e evoluído.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>architecture</category>
    </item>
    <item>
      <title>PurgeCSS gotchas</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Wed, 13 May 2020 14:10:59 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/purgecss-gotchas-4ee4</link>
      <guid>https://dev.to/wendell_adriel/purgecss-gotchas-4ee4</guid>
      <description>&lt;p&gt;Some time ago I bought a &lt;a href="https://tailwindui.com"&gt;Tailwind UI&lt;/a&gt; license and I wanted to test it alongside Vue.&lt;/p&gt;

&lt;p&gt;Following the example of someone who created a website to sell pixels from the website: &lt;a href="http://milliondollarhomepage.com"&gt;http://milliondollarhomepage.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the "I'm Rich" app: &lt;a href="https://en.wikipedia.org/wiki/I_Am_Rich"&gt;https://en.wikipedia.org/wiki/I_Am_Rich&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I came up with something funny to work on and test it. I created &lt;strong&gt;&lt;a href="https://url-list.com"&gt;URList&lt;/a&gt;&lt;/strong&gt;, a site to archive a limited amount of URLs that can be tagged!!! 🥳🥳🥳&lt;/p&gt;

&lt;p&gt;I used Laravel, Laravel Forge, Vue CLI and Tailwind UI to create it and it was a wonderful experience! If you already worked with &lt;a href="https://tailwindcss.com"&gt;TailwindCSS&lt;/a&gt; you know that it offers a wonderful experience to create UIs, so you know what I'm talking about.&lt;/p&gt;

&lt;p&gt;The first time I created a build I didn't set the PurgeCSS so I got a 3MB+ css file. Then I configured PurgeCSS and ran again and it was less than 20Kb, this is amazing!!! But when I took a look on the app, some things were broken and I didn't understand why, but it was because of the PurgeCSS somehow.&lt;/p&gt;

&lt;p&gt;After some minutes I remembered that I had some classes that were applied dynamically using Vue, so I set these on PurgeCSS whitelist and it was all good.&lt;/p&gt;

&lt;p&gt;I know that this is something really basic and simple, but I thought about writing about this because since I had this issue, maybe it will help someone else.&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>vue</category>
    </item>
    <item>
      <title>Creating Modularized Stateless APIs with Laravel 7.x</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Wed, 25 Mar 2020 19:08:15 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/creating-modularized-stateless-apis-with-laravel-7-x-40e9</link>
      <guid>https://dev.to/wendell_adriel/creating-modularized-stateless-apis-with-laravel-7-x-40e9</guid>
      <description>&lt;p&gt;I work with the Laravel framework since it was on version 4.x and I really love the Framework and the things that it provides. But when I started to work with more complex applications and creating APIs with it, I missed some things. So I saw myself doing the same configurations and changing the default Laravel installation structure quite some times. So I decided to create a Skeleton structure for myself and also to use as the base structure for the new API for my company, that’s when I created LarAPI and made it as an Open Source project this week:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/WendellAdriel/larapi"&gt;https://github.com/WendellAdriel/larapi&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This Skeleton has a lot of things already configured out-of-the-box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JWT Authentication&lt;/li&gt;
&lt;li&gt;User Roles&lt;/li&gt;
&lt;li&gt;API documentation with Swagger&lt;/li&gt;
&lt;li&gt;Exception Handler configured to return only JSON responses&lt;/li&gt;
&lt;li&gt;Base classes with common features for projects&lt;/li&gt;
&lt;li&gt;Git Hooks to lint the code, run tests and update the API documentation automatically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’m still planning to add more things, but you can start using it right now!!! Check out the repository for the complete documentation and to know how to use it.&lt;br&gt;
If you want to help, give it a star and share with your friends!&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>api</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A Walk on the Svelte Side: Introduction</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Mon, 13 May 2019 17:29:39 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/a-walk-on-the-svelte-side-introduction-3m64</link>
      <guid>https://dev.to/wendell_adriel/a-walk-on-the-svelte-side-introduction-3m64</guid>
      <description>&lt;h2&gt;
  
  
  CodeShare
&lt;/h2&gt;

&lt;p&gt;I have a project called &lt;strong&gt;&lt;a href="https://codeshare.com.br"&gt;CodeShare&lt;/a&gt;&lt;/strong&gt;, to create free and high-quality content in Portuguese, to reach more people in Brazil that didn’t have the opportunity to learn English yet.&lt;/p&gt;

&lt;p&gt;So when I write a post there, I’ll post here as well, but in English (of course), so if you want to check the original version of this article (in Portuguese) you can check it &lt;strong&gt;&lt;a href="https://codeshare.com.br/passeio-pelo-svelte-introducao"&gt;here&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You may have entered this article because you heard this name somewhere and don't know what it is, or you already know, but you want to know more about it or you can be just asking yourself something like &lt;strong&gt;"Why do I need another reactive UI lib if I already have React and Vue?"&lt;/strong&gt;. So, whatever reason that brought you here, prepare yourself because we're going on an adventure: &lt;strong&gt;A Walk on the Svelte Side&lt;/strong&gt; is the new series of articles from &lt;strong&gt;CodeShare&lt;/strong&gt; where I'll show what Svelte is, its main features and we will build examples along with this series and in the end you'll be ready to start using this lib in your projects!&lt;/p&gt;

&lt;p&gt;This first article will be more theoretical, but that's really important to know how &lt;strong&gt;Svelte&lt;/strong&gt; differs from other libs. I suggest that you read with attention so we can dive in the code after.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Svelte
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Svelte&lt;/strong&gt;, like other UI libs, is based in components, but with an essential and very important difference: it doesn't use the &lt;strong&gt;Virtual DOM differentiation&lt;/strong&gt; like &lt;strong&gt;&lt;a href="https://reactjs.org"&gt;React&lt;/a&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;a href="https://vuejs.org"&gt;Vue.js&lt;/a&gt;&lt;/strong&gt; because this libs use declarative structures that are converted into DOM operations that can overload a little the frames of our apps and also the &lt;strong&gt;&lt;a href="https://codeshare.com.br/garbage-collector-no-javascript"&gt;Garbage Collector (in Portuguese)&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But how &lt;strong&gt;Svelte&lt;/strong&gt; works then? All the work is done in the build time, that way it converts all of your components to a high-performance imperative code that applies the fewest possible DOM operations, making that &lt;strong&gt;Svelte&lt;/strong&gt; "disappears" completely of your final code. You'll have a pure, high-performance imperative JavaScript code. We can highlight some important aspects of &lt;strong&gt;Svelte&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extraordinary performance&lt;/li&gt;
&lt;li&gt;Small bundles&lt;/li&gt;
&lt;li&gt;Accessibility aspects included in the lib&lt;/li&gt;
&lt;li&gt;Styles encapsulation/isolation by default&lt;/li&gt;
&lt;li&gt;Declarative transitions included in the lib&lt;/li&gt;
&lt;li&gt;Learning curve&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And in the end, the answer to our question: &lt;strong&gt;"What is Svelte?"&lt;/strong&gt; can be answered saying that it's a compiler because its job is to compile the components into an imperative JavaScript code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reactivity on Svelte
&lt;/h2&gt;

&lt;p&gt;Svelte version 3 was released on April 21 and brought huge changes to the lib, making that the written code is cleaner and improving how it works with the reactivity of the components. For example, before this version, to update the state of our components we needed something like:&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;clicks&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;clicks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;clicks&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you already worked with &lt;strong&gt;React&lt;/strong&gt;, you'll note a huge resemblance on how we handle the state on a class based component (before the launch of the famous &lt;strong&gt;&lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;Hooks&lt;/a&gt;&lt;/strong&gt;):&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;clicks&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&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;state&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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;clicks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;clicks&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the launch of the &lt;strong&gt;Hooks&lt;/strong&gt;, the way that &lt;strong&gt;React&lt;/strong&gt; works with the state of the components has changed significantly and some other libs started to create their own version of the &lt;strong&gt;Hooks&lt;/strong&gt;. &lt;strong&gt;Svelte&lt;/strong&gt; didn't want to follow this path, because behind the curtains they create some overload to the &lt;strong&gt;&lt;a href="https://codeshare.com.br/garbage-collector-no-javascript"&gt;Garbage Collector (in Portuguese)&lt;/a&gt;&lt;/strong&gt; and if you need to run this code on an &lt;strong&gt;embedded device&lt;/strong&gt; or if your app relies on many animation-based interactions this can lead to some issues.&lt;/p&gt;

&lt;p&gt;So how &lt;strong&gt;Svelte 3&lt;/strong&gt; works with reactivity and updating a component state? In a very simple way, without using &lt;strong&gt;proxies&lt;/strong&gt; or nothing like that. When we want to change for example a state variable named &lt;code&gt;clicks&lt;/code&gt;, we just need to update it:&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="nx"&gt;clicks&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;strong&gt;Svelte's&lt;/strong&gt; job is done in the app build time, for being a compiler, it can only do the instrumentation of these updates without any additional complexity. Behind the curtains what he does is:&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="nx"&gt;clicks&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nx"&gt;$$invalidate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;count&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Performance on Svelte
&lt;/h2&gt;

&lt;p&gt;One of the main diff between &lt;strong&gt;Svelte&lt;/strong&gt; and other libs like &lt;strong&gt;React&lt;/strong&gt; and &lt;strong&gt;Vue&lt;/strong&gt; is that it doesn't use the &lt;strong&gt;Virtual DOM&lt;/strong&gt;, but you must be thinking: how can it be so fast without using the &lt;strong&gt;Virtual DOM&lt;/strong&gt;? If you work with &lt;strong&gt;Front-end&lt;/strong&gt; (or even with Back-end, but like to read about), probably you already heard about the &lt;strong&gt;Virtual DOM&lt;/strong&gt; and that working with it is more performant than with the &lt;strong&gt;Real DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But what's the &lt;strong&gt;Virtual DOM&lt;/strong&gt; and how the libs work with it? In a very simple way, the &lt;strong&gt;Virtual DOM&lt;/strong&gt; is only a JavaScript object that defines the basic structure of your page. When a change occurs in the state of your application, for example, the value of some property is updated, it's created a new object and the lib work is to find the diff between the old and the new object and apply the fewest number of updates in the &lt;strong&gt;Real DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In practice, there is no way to make any changes to the &lt;strong&gt;Real DOM&lt;/strong&gt; without first comparing the two states of the &lt;strong&gt;Virtual DOM&lt;/strong&gt;, but this can lead to some unnecessary steps. &lt;strong&gt;Svelte&lt;/strong&gt; works as a compiler, in the app build time it already knows how the state of the application can be changed, so it generates the fewest possible steps to manage this possible changes without having any work during application execution. &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating our first Svelte component
&lt;/h2&gt;

&lt;p&gt;One of the main features of &lt;strong&gt;Svelte&lt;/strong&gt; is to let us build apps in a simple way and writing less code. The longest the code, greater will be the effort to understand it and the chance of having bugs increases, so when we write less code, we have the benefit of being able to understand it faster and introducing fewer bugs.&lt;/p&gt;

&lt;p&gt;Let's create our first component, it will be something really simple. We will have two text fields where we can provide our first and last name respectively and it will be shown a welcome message on the screen with our full name. &lt;strong&gt;Svelte&lt;/strong&gt; components use the &lt;code&gt;.svelte&lt;/code&gt; extension where we declare:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the behaviour of our component with JavaScript inside a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the style of our component with CSS inside a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag, the styles that we declare on a component have a restrict scope to that component, that means that if you create a rule to change the style of the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tags of your component, it won't affect any other &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag outside your component&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the structure of our component with HTML, it is not necessary to encapsulate this structure in a &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt; tag as we do on &lt;strong&gt;Vue&lt;/strong&gt;, and also may have several root elements, other than the &lt;strong&gt;React&lt;/strong&gt; that we can return only one element or use the &lt;strong&gt;&lt;a href="https://reactjs.org/docs/react-api.html#fragments"&gt; Fragments&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, we have the behaviour part of our component that will be a totally normal and basic JavaScript code. We will create two variables and define a function that we will use when we define the structure of our component:&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;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;showGreeting&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below we will also define the style of our component that is also a normal CSS code, the only difference is that the styles declared here will affect only the elements of this component:&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;style&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;700&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;.warning&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="m"&gt;#ff2b56&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 last part of our component is the structure of our component, that's done with HTML, with only some little details that we will check after:&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;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"first_name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;First name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"first_name"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;bind:value=&lt;/span&gt;&lt;span class="s"&gt;{firstName}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"last_name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Last name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"last_name"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;bind:value=&lt;/span&gt;&lt;span class="s"&gt;{lastName}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

{#if firstName.length &amp;gt; 0 &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; lastName.length &amp;gt; 0}
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello, {`${firstName} ${lastName}`}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt; &lt;span class="na"&gt;on:click=&lt;/span&gt;&lt;span class="s"&gt;{showGreeting}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Show alert greeting!&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
{:else}
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"warning"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Type your first and last name above...&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
{/if}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we have some details that aren't an HTML code in our structure, but that is used to connect the structure of our component with its behaviour. In our &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; elements the attribute &lt;code&gt;value&lt;/code&gt; is changed to &lt;code&gt;bind:value={variable}&lt;/code&gt;, where we link a state variable of our component so that when this variable is changed the change will be reflected in the &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; element and vice versa.&lt;/p&gt;

&lt;p&gt;Just as it is easy to link state variables, invoking functions that we define when an event occurs in the DOM is also very easy, we use &lt;code&gt;on:event={function}&lt;/code&gt;. In our code, when the button receives a click it will invoke our &lt;code&gt;showGreeting&lt;/code&gt; function defined above.&lt;/p&gt;

&lt;p&gt;Often we need to show or hide some content according to the state of our component, &lt;strong&gt;Svelte&lt;/strong&gt; gives us a simple way to do this with the blocks: &lt;code&gt;{#if} {:else if} {:else} {/if}&lt;/code&gt;, this way we can control in a simple and clean way what and when to display and hide some content.&lt;/p&gt;

&lt;p&gt;Our example can be seen in the &lt;strong&gt;CodeSandbox&lt;/strong&gt; below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/84ljk782w0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you can see in the &lt;strong&gt;CodeSandbox&lt;/strong&gt; above, we have a &lt;code&gt;index.js&lt;/code&gt; file that imports our component and renders it in the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;. This file alongside with the files &lt;code&gt;package.json&lt;/code&gt; and the config for &lt;strong&gt;&lt;a href="https://rollupjs.org"&gt;Rollup&lt;/a&gt;&lt;/strong&gt;, the default bundler that &lt;strong&gt;Svelte&lt;/strong&gt; uses, &lt;code&gt;rollup.config.js&lt;/code&gt; are already included in the default template for &lt;strong&gt;Svelte&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can play on the &lt;strong&gt;CodeSandbox&lt;/strong&gt; to not need to create the projects locally, but if you want to is really easy as well. We can use &lt;strong&gt;NPX&lt;/strong&gt; to create our project in a simple way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx degit sveltejs/template my-app &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;my-app &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That way you'll create a project using a tool called &lt;strong&gt;Degit&lt;/strong&gt; that will create a copy of &lt;strong&gt;&lt;a href="https://github.com/sveltejs/template"&gt;this repository&lt;/a&gt;&lt;/strong&gt; that's the minimum default template for an app, with all the needed config. If you prefer or want to make any changes to this default template you can &lt;strong&gt;fork&lt;/strong&gt; &lt;strong&gt;&lt;a href="https://github.com/sveltejs/template"&gt;this repository&lt;/a&gt;&lt;/strong&gt; and change the command above to use your Github user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx degit your-github-user/template my-app &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;my-app &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;README.md&lt;/code&gt; file of this template you'll find instructions on how to run your project locally and also instructions on how to deploy your app.&lt;/p&gt;

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

&lt;p&gt;In this first article we saw what &lt;strong&gt;Svelte&lt;/strong&gt; is, its main features and we built our first component to see how it works. In the next articles we will go deeper on other features of this lib while we create examples to put in practice the concepts we will learn.&lt;/p&gt;

&lt;p&gt;I hope that you liked this article and if you do, don't forget about commenting and sharing this article with your friends!!! See ya! 😎&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>svelte</category>
      <category>ui</category>
    </item>
    <item>
      <title>Working with multiple CSS themes</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Fri, 26 Apr 2019 19:17:41 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/working-with-multiple-css-themes-5aej</link>
      <guid>https://dev.to/wendell_adriel/working-with-multiple-css-themes-5aej</guid>
      <description>&lt;h2&gt;
  
  
  CodeShare
&lt;/h2&gt;

&lt;p&gt;I have a project called &lt;strong&gt;&lt;a href="https://codeshare.com.br" rel="noopener noreferrer"&gt;CodeShare&lt;/a&gt;&lt;/strong&gt;, to create free and high-quality content in Portuguese, to reach more people in Brazil that didn’t have the opportunity to learn English yet.&lt;/p&gt;

&lt;p&gt;So when I write a post there, I’ll post here as well, but in English (of course), so if you want to check the original version of this article (in Portuguese) you can check it &lt;strong&gt;&lt;a href="https://codeshare.com.br/trabalhando-com-multiplos-temas-css" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;&lt;a href="https://dev.to/wendell_adriel/theming-with-css-variables-1o56"&gt;the first article of this series&lt;/a&gt;&lt;/strong&gt; we saw how variables work on CSS and how we can use them to create themes for our projects. In this article, we will put this into practice creating some simple themes and seeing how to change between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Default Theme
&lt;/h2&gt;

&lt;p&gt;As we saw before, the &lt;code&gt;:root&lt;/code&gt; selector can be used to store the global variables of our themes. To create a default theme for our app, we will define some variables in this selector that will be used by our elements:&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="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--global-bg-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Verdana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-text-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#222&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-link-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="py"&gt;--global-link-hover&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;lightblue&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;In the code above we defined some variables that will be used as the base values for our theme. That way we can use these values to style our elements. Since we're only working on an example, the number of variables we have are just a few, but in a large project, this list can be much bigger. Now, let's use our variables:&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="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-bg-color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-font-family&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="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-text-color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;a&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="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-link-color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;700&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:hover&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="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-link-hover&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;In the code above we used our variables to control the background and text colors, the font and also the color of the links. If in the middle of our project we want to customize any of these values, we just need to change the value of the variable that we want and all the elements that use it will update, pretty simple. Now let's create some elements to check our theme in action:&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="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Tematização com CSS&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;STYLE&lt;/span&gt; &lt;span class="nt"&gt;HERE&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Tematização com variáveis CSS&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
            Exemplo para ser usado em um artigo na
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://codeshare.com.br"&lt;/span&gt; &lt;span class="na"&gt;target=&lt;/span&gt;&lt;span class="s"&gt;"_blank"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;CodeShare&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/wyr0q30wzk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a Night Mode Theme
&lt;/h2&gt;

&lt;p&gt;Now that we already have our default theme, we can work in a new theme for our app. A lot of sites have a &lt;strong&gt;"Night Mode"&lt;/strong&gt; to make it easier to read during the night. Let's create a theme for this.&lt;/p&gt;

&lt;p&gt;There are some ways of working with themes, but my favorite is to use the &lt;code&gt;data-&lt;/code&gt; attributes on &lt;strong&gt;HTML&lt;/strong&gt;. These &lt;code&gt;data-&lt;/code&gt; attributes are a convention used to define custom attributes for tags and to identify which theme we're using we will create a &lt;code&gt;data-theme&lt;/code&gt; attribute. All the elements with this attribute and their children will be modified. Since our new theme will be for our Night Mode we will call it &lt;strong&gt;&lt;em&gt;dark&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since our elements are already using the variables that we created, to create a new theme we just need to update the values of the ones that we need to modify, that way the values that we change will overwrite the others and the ones we don't change will be inherited from the &lt;code&gt;:root&lt;/code&gt; element. We will use the attribute selector on &lt;strong&gt;CSS&lt;/strong&gt; to apply our theme. Since our theme is a &lt;strong&gt;Global Theme&lt;/strong&gt;, we will link it to the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; tag:&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="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;data-theme&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;'dark'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--global-bg-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#444&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--global-text-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ddd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--global-link-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--global-link-hover&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;lightyellow&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;In the code above we changed the colors for the background, text and links. Now we just need to put the &lt;code&gt;data-theme="dark"&lt;/code&gt; attribute in our &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; tag to see the result:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/mp42xzmnp"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  Letting the user switch themes
&lt;/h2&gt;

&lt;p&gt;Now that we created two themes for our app, it would be interesting if we let the user switch between them right? It's not something hard to do, let's create this option. First, let's remove the &lt;code&gt;data-theme="dark"&lt;/code&gt; attribute from our &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; tag and create the element that will let the user switch themes when clicked.&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="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    ...
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"theme-toggle"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"theme-toggle"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
        ...
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now let's style this element, for that we will create two new variables. We will create this element as a fixed element, so the user can switch themes whenever he wants to. We will also assign different values for these variables when our &lt;strong&gt;&lt;em&gt;dark&lt;/em&gt;&lt;/strong&gt; theme is enabled, making that this element will also update its appearance:&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="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="py"&gt;--global-theme-toggle-bg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ccc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-theme-toggle-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'🌞 THEME'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;data-theme&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;'dark'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="py"&gt;--global-theme-toggle-bg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-theme-toggle-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'🌝 THEME'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.theme-toggle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;fixed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-theme-toggle-bg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.8rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;700&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.theme-toggle&lt;/span&gt;&lt;span class="nd"&gt;::after&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-theme-toggle-content&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;Now that we have finished setting up the visual part of our component, let's add the needed action to make that our theme will switch when the element is clicked:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toggleTheme&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;htmlTag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;html&lt;/span&gt;&lt;span class="dl"&gt;"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;htmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data-theme&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="nx"&gt;htmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data-theme&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;htmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data-theme&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark&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="nb"&gt;document&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;theme-toggle&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="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggleTheme&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the code above we created a function that will check if the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; tag has a &lt;code&gt;data-theme&lt;/code&gt; attribute and if so, it will remove it and if doesn't, it will add this attribute with the value &lt;strong&gt;&lt;em&gt;dark&lt;/em&gt;&lt;/strong&gt; to activate our Night Mode Theme. After that we create a listener in our element that when it gets clicked it will run the function that we just created:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/m45wj6xz68"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  Improving the Theme Switching
&lt;/h2&gt;

&lt;p&gt;Congrats for us! We already let that our users can switch the theme of our app, but we still have some little details that we can improve. For example, when we switch the theme, the update is made in a very sudden way, it would be interesting to make this update in a smoother way and we can do that easily using the &lt;code&gt;transition&lt;/code&gt; rule from &lt;strong&gt;CSS&lt;/strong&gt;, check how simple it is:&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="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="m"&gt;1s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="m"&gt;1s&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 it's done!!! See how now the transition between themes is being made much smoother and pleasing to the eye!!! But we can still improve something else. When the user enters in our app and change the theme and leave, we want that when he returns, the site will load the last chosen theme, for that we will use the &lt;code&gt;localStorage&lt;/code&gt; to store this info. We will need to make a little change in our &lt;code&gt;toggleTheme&lt;/code&gt; function and create one more function to help us:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toggleTheme&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;htmlTag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html&lt;/span&gt;&lt;span class="dl"&gt;'&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;htmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-theme&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="nx"&gt;htmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;site-theme&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="nx"&gt;htmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;site-theme&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;applyInitialTheme&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;site-theme&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;htmlTag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;html&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;htmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data-theme&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;theme&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;span class="nf"&gt;applyInitialTheme&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nb"&gt;document&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;theme-toggle&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="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggleTheme&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That's it, now we can offer a special experience for our users!!!&lt;/p&gt;
&lt;h2&gt;
  
  
  Applying Themes in Specific Zones
&lt;/h2&gt;

&lt;p&gt;We saw above how to create and switch between two &lt;strong&gt;Global Themes&lt;/strong&gt;, that means that they're applied in our application as a whole, but we can also apply themes to specific zones in our app. To show that, let's create a theme that will change only the font and let's apply that to some elements:&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="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;data-theme&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;'comic'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--global-font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'Comic Sans MS'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-font-family&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;data-theme=&lt;/span&gt;&lt;span class="s"&gt;"comic"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"theme-toggle"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"theme-toggle"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Tematização com variáveis CSS&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Exemplo para ser usado em um artigo na
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://codeshare.com.br"&lt;/span&gt; &lt;span class="na"&gt;target=&lt;/span&gt;&lt;span class="s"&gt;"_blank"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;CodeShare&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;data-theme=&lt;/span&gt;&lt;span class="s"&gt;"comic"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        Exemplo de um tema aplicado em partes específicas da página! Esse texto
        tem o tema "comic"!
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/nnqvpvmx1m"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In our examples above, we worked with three simple themes, but in larger projects, the complexity of the themes can be much bigger. Thinking about that, I created a lib called &lt;strong&gt;CSS Theme Manager&lt;/strong&gt; that helps in the management of themes based in CSS variables, check it out and if you find it interesting, use it in your projects!&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/WendellAdriel" rel="noopener noreferrer"&gt;
        WendellAdriel
      &lt;/a&gt; / &lt;a href="https://github.com/WendellAdriel/css-theme-manager" rel="noopener noreferrer"&gt;
        css-theme-manager
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Zero dependency lib to manage CSS themes easily for your app
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;CSS Theme Manager&lt;/h1&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="https://github.com/WendellAdriel/css-theme-manager/blob/master/LICENSE" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/a98f98d59ba704d65d101a24d7455025265e84600d6dd8f88837a46a0d48a4bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6173686170652f6170697374617475732e7376673f7374796c653d666c61742d737175617265" alt="license"&gt;&lt;/a&gt;
&lt;a href="https://github.com/WendellAdriel/css-theme-manager#contributors" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/d319ea9b47fbfa41e3336926df5bdae31bf2d6b01b7fd120d064cb86b5f506f3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f616c6c5f636f6e7472696275746f72732d312d6f72616e67652e7376673f7374796c653d666c61742d737175617265" alt="All Contributors"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.npmjs.com/package/css-theme-manager" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/2c60a3394034ae30f828027cd1e545bcc90f9ca690c2d980d3e884138bcf1c4b/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f6373732d7468656d652d6d616e616765722e7376673f7374796c653d666c61742d737175617265" alt="version"&gt;&lt;/a&gt;
&lt;a href="http://npmcharts.com/compare/css-theme-manager" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/805572c1e7c113751241502368e92ba61b10d84152835f2fe4168bf46981d361/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f6373732d7468656d652d6d616e616765722e7376673f7374796c653d666c61742d737175617265" alt="downloads"&gt;&lt;/a&gt;
&lt;a href="https://unpkg.com/css-theme-manager/dist/css-theme-manager.js" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/31b1a4a1e1aa7dcfe96c3889637f90293d8adeea4ec69e78c08858dab38f6c6d/687474703a2f2f696d672e626164676573697a652e696f2f68747470733a2f2f756e706b672e636f6d2f6373732d7468656d652d6d616e616765722f646973742f6373732d7468656d652d6d616e616765722e6a733f7374796c653d666c61742d737175617265266c6162656c3d73697a65" alt="size"&gt;&lt;/a&gt; &lt;a href="https://unpkg.com/css-theme-manager/dist/css-theme-manager.js" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/2dad396a3b440210234dadf2b0179b6107e9c9997a9502c41e729c73e16d976b/687474703a2f2f696d672e626164676573697a652e696f2f68747470733a2f2f756e706b672e636f6d2f6373732d7468656d652d6d616e616765722f646973742f6373732d7468656d652d6d616e616765722e6a733f6c6162656c3d677a697025323073697a65267374796c653d666c61742d73717561726526636f6d7072657373696f6e3d677a6970" alt="gzip size"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/WendellAdriel/css-theme-manager/watchers" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/9d88cd0d740399269e22277594448d14e279fd44d316b22852ab6a318cbf3975/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f77617463686572732f57656e64656c6c41647269656c2f6373732d7468656d652d6d616e616765722e7376673f7374796c653d736f6369616c" alt="Watch on GitHub"&gt;&lt;/a&gt;
&lt;a href="https://github.com/WendellAdriel/css-theme-manager/stargazers" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/a43eee7d200d00693e608b3ce093b2460b5a0578931135d0cc2ef416d5a99aa6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f57656e64656c6c41647269656c2f6373732d7468656d652d6d616e616765722e7376673f7374796c653d736f6369616c" alt="Star on GitHub"&gt;&lt;/a&gt;
&lt;a href="https://twitter.com/intent/tweet?text=Check%20out%20css-theme-manager!%20https://github.com/WendellAdriel/css-theme-manager%20%F0%9F%91%8D" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/53035760a10ac97f7fdf696e2736604cc28a32c84d1636910b952bce375534b8/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f68747470732f6769746875622e636f6d2f57656e64656c6c41647269656c2f6373732d7468656d652d6d616e616765722e7376673f7374796c653d736f6369616c" alt="Tweet"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Zero dependency lib to manage CSS themes easily for your app&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;How to use&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Install the package&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;npm install css-theme-manager --save
// or with yarn
yarn add css-theme-manager
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Import it and init the &lt;strong&gt;CSS Theme Manager&lt;/strong&gt; with a default theme
A &lt;code&gt;theme&lt;/code&gt; is an object with the name of your variables as keys and the value of the variables as the values.&lt;/p&gt;
&lt;div class="highlight highlight-source-js notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-v"&gt;CSSThemeManager&lt;/span&gt; &lt;span class="pl-k"&gt;from&lt;/span&gt; &lt;span class="pl-s"&gt;'css-theme-manager'&lt;/span&gt;

&lt;span class="pl-k"&gt;const&lt;/span&gt; &lt;span class="pl-s1"&gt;themeManager&lt;/span&gt; &lt;span class="pl-c1"&gt;=&lt;/span&gt; &lt;span class="pl-k"&gt;new&lt;/span&gt; &lt;span class="pl-v"&gt;CSSThemeManager&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-kos"&gt;{&lt;/span&gt;
  &lt;span class="pl-s"&gt;'bg-color'&lt;/span&gt;: &lt;span class="pl-s"&gt;'#fff'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s"&gt;'text-color'&lt;/span&gt;: &lt;span class="pl-s"&gt;'darkblue'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s"&gt;'featured-font'&lt;/span&gt;: &lt;span class="pl-s"&gt;'Verdana, sans-serif'&lt;/span&gt;
&lt;span class="pl-kos"&gt;}&lt;/span&gt;&lt;span class="pl-kos"&gt;)&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will create and insert the given variables in the &lt;code&gt;:root&lt;/code&gt; selector.
All the variables created with this &lt;strong&gt;CSS Theme Manager&lt;/strong&gt; will have a &lt;code&gt;csstm-&lt;/code&gt; prefix.
The code above will result in:&lt;/p&gt;
&lt;div class="highlight highlight-source-css notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-kos"&gt;:&lt;/span&gt;&lt;span class="pl-c1"&gt;root&lt;/span&gt; {
    &lt;span class="pl-c1"&gt;--csstm-bg-color&lt;/span&gt;&lt;span class="pl-kos"&gt;:&lt;/span&gt; &lt;span class="pl-s"&gt;'#fff'&lt;/span&gt;;
    &lt;span class="pl-c1"&gt;--csstm-text-color&lt;/span&gt;&lt;span class="pl-kos"&gt;:&lt;/span&gt; &lt;span class="pl-s"&gt;'darkblue'&lt;/span&gt;;
    &lt;span class="pl-c1"&gt;--csstm-featured-font&lt;/span&gt;&lt;span class="pl-kos"&gt;:&lt;/span&gt; &lt;span class="pl-s"&gt;'Verdana, sans-serif'&lt;/span&gt;;
}&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Check the &lt;strong&gt;API Reference&lt;/strong&gt; below to check all that you can…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/WendellAdriel/css-theme-manager" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;I hope that you liked this article and if you do, don't forget about commenting and sharing this article with your friends!!! See ya! 😎&lt;/p&gt;

</description>
      <category>css</category>
      <category>theme</category>
      <category>theming</category>
    </item>
    <item>
      <title>Theming with CSS variables</title>
      <dc:creator>Wendell Adriel</dc:creator>
      <pubDate>Wed, 24 Apr 2019 14:51:51 +0000</pubDate>
      <link>https://dev.to/wendell_adriel/theming-with-css-variables-1o56</link>
      <guid>https://dev.to/wendell_adriel/theming-with-css-variables-1o56</guid>
      <description>&lt;h2&gt;
  
  
  CodeShare
&lt;/h2&gt;

&lt;p&gt;I have a project called &lt;strong&gt;&lt;a href="https://codeshare.com.br"&gt;CodeShare&lt;/a&gt;&lt;/strong&gt;, to create free and high-quality content in Portuguese, to reach more people in Brazil that didn't have the opportunity to learn English yet.&lt;/p&gt;

&lt;p&gt;So when I write a post there, I'll use &lt;strong&gt;DEV.to&lt;/strong&gt; to post here as well, but in English (of course).&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;It doesn't matter if we're creating a website or an admin page or an app, we need to deliver a solid and consistent design always. We can't have a software where on each page we use a different font or buttons of all colours and shapes. We need to define a pattern, not only because of the visual aspect but when we create a pattern of colours and layout, the users can learn and get used quickly with our product.&lt;/p&gt;

&lt;p&gt;To make this process easier, some time ago we had to ask for the help of the CSS pre-processors to use features like variables that make our lives easier when it comes to theming. Gladly we don't need that anymore because of CSS already let us work with variables natively and in this article, we will see how we can use this feature to create a simple, yet efficient design system for our projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declaring and using variables
&lt;/h2&gt;

&lt;p&gt;To declare a CSS variable it's super simple. The name of the variables &lt;strong&gt;MUST&lt;/strong&gt; start with &lt;code&gt;--&lt;/code&gt; and they are &lt;em&gt;case-sensitive&lt;/em&gt;.&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="nc"&gt;.my-class&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&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;The same way that's easy to declare a variable, using it is also super simple. We just need to use the &lt;code&gt;var()&lt;/code&gt; function. This function takes two parameters: the first one is the name of the variable that we want to get its value and the second one is an optional parameter with a fallback value if the given variable passed as the first parameter isn't found.&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="nc"&gt;.my-class&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--font-size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.8rem&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;The variables on CSS inherit values, that means that if a value is not found in a given element, the parent element value will be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two layers theming
&lt;/h2&gt;

&lt;p&gt;If in our project we group all variables in a single place (Global Variables), we create a single source of truth, but we lose the ability to modularize our theme and always when a dev needs to customize a component he will need to overwrite the CSS rules.&lt;/p&gt;

&lt;p&gt;In the other hand, if we leave the variables scattered all around, we will create a modularized theme, but we lose the consistency between our components and if we need to make a general change, we will have a lot of work.&lt;/p&gt;

&lt;p&gt;To solve this issue we will think about our theme having two layers, we will have the &lt;strong&gt;Global Variables&lt;/strong&gt; and the &lt;strong&gt;Modules Variables&lt;/strong&gt;. That way we can get the best of the two worlds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global Variables
&lt;/h2&gt;

&lt;p&gt;Global variables are generic variables that will be used to keep the consistency between all our components. Some examples of global variables are font, default font-size and color palette. It's super simple to define global variables on CSS, we use the &lt;code&gt;:root&lt;/code&gt; selector and inside it, we define our variables. It's not mandatory, but to make it simple to identify that a variable is global or not, I like to use the &lt;code&gt;global-&lt;/code&gt; prefix, that way when I'm reading the code I can already know that certain value comes from a global variable.&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="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--global-font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Verdana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.8rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-text-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#222&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-primary-title-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-color-primary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#88498f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-color-secondary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#779fa1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-color-warning&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#e28413&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-color-danger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ff6542&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;In the code above, we define some variables that will be used as base values in all our project: font, font size for texts and for main titles, font color for texts and a color palette with four different colors. Now we will use the global variables that we just defined:&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="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-font-family&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-font-size&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="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-text-color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-primary-title-size&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;In the code above we define the default font, font color and font size for all our product and we also define the font size for the main titles. If we need to change any of these values in the middle of our project, we just need to change the variables declaration with the new values and it's done! That way we can create some sort of consistency in our product and also create a common reference point for devs and designers to work together in a quick and easy way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modules Variables
&lt;/h2&gt;

&lt;p&gt;These are variables with a restricted scope where it's defined. Every module variable &lt;strong&gt;MUST&lt;/strong&gt; be defined using the value from a global variable and we also need to provide a fallback value for the case that the module will be used in an environment that doesn't provide global variables, it doesn't break. It's not mandatory, but to make it simple to identify that a variable is a module variable, I like to use the &lt;code&gt;module-&lt;/code&gt; prefix, that way when I'm reading the code I can already know that certain value comes from a variable of the &lt;strong&gt;X&lt;/strong&gt; module. Let's take as an example a module for a button:&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="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;...&lt;/span&gt;
    &lt;span class="py"&gt;--global-border-radius-sm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;--global-text-color-light&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--btn-border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-border-radius-sm&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="py"&gt;--btn-text-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-text-color-light&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--btn-border-radius&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5px&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="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--btn-text-color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#ddd&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.btn-primary&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--btn-primary-bg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-color-primary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="py"&gt;--btn-primary-border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-color-primary&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="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--btn-primary-bg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#605770&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;border-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--btn-primary-bg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#605770&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.btn-secondary&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--btn-secondary-bg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-color-secondary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="py"&gt;--btn-secondart-border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--global-color-secondary&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="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--btn-secondary-bg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#7fc29b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;border-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--btn-secondary-bg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;#7fc29b&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;h2&gt;
  
  
  Understanding the two layers theming
&lt;/h2&gt;

&lt;p&gt;Ok, now that we already saw how to structure our theme, we need to understand the reason why this Two Layers Theming works. The way that we built our theme, we make independent modules, because if they are being used on a context where we have the global variables, they will inherit the values from these variables, but if it doesn't find these variables, we also defined fallback values that will be used.&lt;/p&gt;

&lt;p&gt;We also made that changes can be really easy to be done. For example if we want to change the primary color everywhere in our product, we just need to change the &lt;code&gt;--global-color-primary&lt;/code&gt; global variable and all our modules, including the &lt;strong&gt;btn&lt;/strong&gt; module will be affected. Now imagine that we want to change the font color of all our buttons, we can change the &lt;code&gt;--global-text-color-light&lt;/code&gt; variable, but that will affect all the other modules, so we just need to change the &lt;code&gt;--btn-text-color&lt;/code&gt; module variable that will affect only the &lt;strong&gt;btn&lt;/strong&gt; module.&lt;/p&gt;

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

&lt;p&gt;We saw how to create a consistent and adaptable theme for our projects using only CSS. Using this Two Layers Theming approach, we created a better environment and experience for our teams of devs and designers. You can check an example of the code we created in this article in this Codepen that I created:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/WendellAdriel/embed/QPxRjN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I hope that you liked this article and if you do, don't forget about commenting and sharing this article with your friends!!! See ya! 😎&lt;/p&gt;

</description>
      <category>css</category>
      <category>theme</category>
      <category>theming</category>
    </item>
  </channel>
</rss>
