<?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: drkcode</title>
    <description>The latest articles on DEV Community by drkcode (@drkcode).</description>
    <link>https://dev.to/drkcode</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%2F684316%2Fff87b233-ee13-4dec-bd20-0d346b83ad0c.png</url>
      <title>DEV Community: drkcode</title>
      <link>https://dev.to/drkcode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/drkcode"/>
    <language>en</language>
    <item>
      <title>Using Linaria with React/TS and Vite 🚀</title>
      <dc:creator>drkcode</dc:creator>
      <pubDate>Wed, 13 Aug 2025 13:56:43 +0000</pubDate>
      <link>https://dev.to/drkcode/adding-linaria-to-a-react-typescript-and-vite-project-j1k</link>
      <guid>https://dev.to/drkcode/adding-linaria-to-a-react-typescript-and-vite-project-j1k</guid>
      <description>&lt;p&gt;This guide will walk you through integrating &lt;strong&gt;Linaria&lt;/strong&gt;, a zero-runtime CSS-in-JS library, into your React project built with TypeScript and Vite. Linaria extracts CSS at build time, resulting in smaller bundle sizes and better performance compared to traditional CSS-in-JS solutions that execute in the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Linaria? 🤔
&lt;/h3&gt;

&lt;p&gt;Linaria offers several advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Zero-runtime CSS:&lt;/strong&gt; CSS is extracted during the build process, meaning no extra CSS processing happens in the user's browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Performance:&lt;/strong&gt; Smaller JavaScript bundles and no runtime CSS overhead lead to faster loading times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Developer Experience:&lt;/strong&gt; Write CSS directly in your JavaScript/TypeScript files using template literals with syntax highlighting and autocompletion (with appropriate editor extensions).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Familiar CSS Syntax:&lt;/strong&gt; You write standard CSS, and Linaria takes care of generating the corresponding CSS files.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisites ✅
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Node.js and npm (or yarn/pnpm) installed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;An existing React project set up with TypeScript and Vite. If you don't have one, you can quickly create one by running:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm create vite@latest my-app &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;--template&lt;/span&gt; react-ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Install Dependencies 📦
&lt;/h3&gt;

&lt;p&gt;Open your terminal in the project directory and run the following command to install the necessary packages as development dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; @linaria/react @wyw-in-js/vite @babel/typescript @babel/react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's briefly understand what each of these dependencies does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@linaria/react&lt;/code&gt;:&lt;/strong&gt; Provides the React-specific API for using Linaria, including the &lt;code&gt;css&lt;/code&gt; prop and &lt;code&gt;styled&lt;/code&gt; component.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@wyw-in-js/vite&lt;/code&gt;:&lt;/strong&gt; A Vite plugin that enables Linaria to process your CSS within JavaScript/TypeScript files during the build. &lt;code&gt;@wyw-in-js&lt;/code&gt; (what-you-write-is-what-you-get) is the core library that powers Linaria and other zero-runtime CSS-in-JS solutions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@babel/typescript&lt;/code&gt;:&lt;/strong&gt; Allows Babel to understand and compile TypeScript syntax. While Vite has built-in TypeScript support, Linaria's build process often leverages Babel for transformations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@babel/react&lt;/code&gt;:&lt;/strong&gt; A Babel preset that provides support for React JSX syntax.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 2: Configure Vite ⚙️
&lt;/h3&gt;

&lt;p&gt;Next, you need to configure the Vite build process to include the &lt;code&gt;@wyw-in-js/vite&lt;/code&gt; plugin. Open your &lt;code&gt;vite.config.ts&lt;/code&gt; file (or &lt;code&gt;vite.config.js&lt;/code&gt; if you haven't switched to TypeScript yet). If you don't have one, create it in the root of your project.&lt;/p&gt;

&lt;p&gt;Your &lt;code&gt;vite.config.ts&lt;/code&gt; should look similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vite.config.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@vitejs/plugin-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;wyw&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@wyw-in-js/vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;wyw&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="p"&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;./src/**/*.{ts,tsx}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;babelOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;presets&lt;/span&gt;&lt;span class="p"&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;@babel/preset-typescript&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;@babel/preset-react&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="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;We import the necessary functions from &lt;code&gt;vite&lt;/code&gt;, &lt;code&gt;@vitejs/plugin-react&lt;/code&gt;, and &lt;code&gt;@wyw-in-js/vite&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;plugins&lt;/code&gt; array, we include both the &lt;code&gt;react()&lt;/code&gt; plugin (for React support) and the &lt;code&gt;wyw()&lt;/code&gt; plugin (for Linaria).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;wyw()&lt;/code&gt; plugin is configured with an &lt;code&gt;include&lt;/code&gt; option to specify the file patterns that Linaria should process (in this case, all &lt;code&gt;.ts&lt;/code&gt; and &lt;code&gt;.tsx&lt;/code&gt; files).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;babelOptions&lt;/code&gt; tell Linaria to use the &lt;code&gt;@babel/preset-typescript&lt;/code&gt; and &lt;code&gt;@babel/preset-react&lt;/code&gt; presets to correctly handle TypeScript and JSX syntax within the styled components.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Start Using Linaria ✨
&lt;/h3&gt;

&lt;p&gt;Now you can start using Linaria in your React components! You have two main ways to define styles: the &lt;code&gt;css&lt;/code&gt; prop and the &lt;code&gt;styled&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the &lt;code&gt;css&lt;/code&gt; prop:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can import the &lt;code&gt;css&lt;/code&gt; function from &lt;code&gt;@linaria/react&lt;/code&gt; and use it within the &lt;code&gt;className&lt;/code&gt; prop of your HTML elements or React components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/components/MyComponent.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@linaria/react&lt;/span&gt;&lt;span class="dl"&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;myStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="s2"&gt;`
  color: blue;
  font-size: 16px;
  padding: 10px;
  border: 1px solid lightblue;

  &amp;amp;:hover {
    background-color: lightblue;
    color: white;
  }
`&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;MyComponent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;myStyle&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;Linaria&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;css&lt;/code&gt; is a tagged template literal. The CSS defined within it will be extracted at build time and a unique class name (&lt;code&gt;myStyle&lt;/code&gt;) will be generated and applied to the &lt;code&gt;div&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the &lt;code&gt;styled&lt;/code&gt; component:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Linaria also provides a &lt;code&gt;styled&lt;/code&gt; factory function that allows you to create styled components, similar to other CSS-in-JS libraries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/components/AnotherComponent.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@linaria/react&lt;/span&gt;&lt;span class="dl"&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;StyledButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="s2"&gt;`
  background-color: green;
  color: white;
  padding: 8px 15px;
  border: none;
  border-radius: 4px;
  cursor: pointer;

  &amp;amp;:hover {
    opacity: 0.8;
  }
`&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;AnotherComponent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StyledButton&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StyledButton&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;AnotherComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;styled.button&lt;/code&gt; creates a &lt;code&gt;StyledButton&lt;/code&gt; component that renders a &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; element with the defined styles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Run Your Application 🚀
&lt;/h3&gt;

&lt;p&gt;Now, start your Vite development server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or build your project for production:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During the build process, Linaria will extract the CSS from your components and generate separate &lt;code&gt;.css&lt;/code&gt; files, which will be automatically linked in your HTML. You can inspect your browser's developer tools to see that the styles are applied through regular CSS classes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion 🎉
&lt;/h3&gt;

&lt;p&gt;You have successfully integrated Linaria into your React, TypeScript, and Vite project! This setup allows you to enjoy the benefits of zero-runtime CSS-in-JS, leading to optimized performance and a streamlined development experience. Explore the Linaria documentation for more advanced features like theming and more! Happy styling! 🎨&lt;/p&gt;

</description>
      <category>react</category>
      <category>linaria</category>
      <category>styledcomponents</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
