<?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: Kudakwashe Gore</title>
    <description>The latest articles on DEV Community by Kudakwashe Gore (@kudakwashegore).</description>
    <link>https://dev.to/kudakwashegore</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%2F274403%2F8af47e5d-a153-4e06-9fbd-c79b7634983d.png</url>
      <title>DEV Community: Kudakwashe Gore</title>
      <link>https://dev.to/kudakwashegore</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kudakwashegore"/>
    <language>en</language>
    <item>
      <title>How to customize Semantic UI with React, MiniCssExtractPlugin, LESS and Webpack 4? </title>
      <dc:creator>Kudakwashe Gore</dc:creator>
      <pubDate>Tue, 11 Feb 2020 08:29:03 +0000</pubDate>
      <link>https://dev.to/kudakwashegore/how-to-customize-semantic-ui-with-react-minicssextractplugin-less-and-webpack-4-498j</link>
      <guid>https://dev.to/kudakwashegore/how-to-customize-semantic-ui-with-react-minicssextractplugin-less-and-webpack-4-498j</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;This tutorial assumes that you already know how to set up a basic react app and webpack. &lt;/p&gt;

&lt;p&gt;Setting up &lt;a href="https://react.semantic-ui.com/"&gt;Semantic UI&lt;/a&gt; and extending/overriding the default themes with your custom styles in a React app can give you a bad day if not done carefully.&lt;/p&gt;

&lt;p&gt;On the webpack side, after &lt;code&gt;ExtractTextPlugin&lt;/code&gt; was deprecated and replaced with &lt;code&gt;MiniCssExtractPlugin&lt;/code&gt;, configuring your webpack to recognise your Semantic UI less and fonts can also be a pain if you haven't done that before. &lt;/p&gt;

&lt;p&gt;This tutorial can help you get started within a few minutes, it can also work with &lt;a href="https://github.com/fomantic/Fomantic-UI-LESS"&gt;Fomantic UI Less&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Code for this tutorial can be found on &lt;a href="https://github.com/kudakwashegore/custom-semanticui-react"&gt;https://github.com/kudakwashegore/custom-semanticui-react&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependencies
&lt;/h3&gt;

&lt;p&gt;First things first, a basic setup will require installation of the following dependencies (I use npm, yarn can also work). &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -D mini-css-extract-plugin semantic-ui-less less@2.7.3 less-loader css-loader url-loader&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -S semantic-ui-react&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please note the version of &lt;code&gt;less&lt;/code&gt; used (2.7.3). Later versions will give you endless nightmares.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I also deliberately used &lt;code&gt;url-loader&lt;/code&gt; instead of &lt;code&gt;file-loader&lt;/code&gt;, &lt;code&gt;file-loader&lt;/code&gt; will give you issues when you want to use your favourite Semantic UI &lt;code&gt;&amp;lt;Icon /&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Folder Structure
&lt;/h3&gt;

&lt;p&gt;Assuming the following project structure, you can always update your code to match your folder structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules/
app/
  app.js
  index.html
my-custom-semantic-theme/
webpack.config.js
package.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Webpack
&lt;/h3&gt;

&lt;p&gt;Below are some additional webpack configuration that you need to add in your webpack configuration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');

module.exports = {
    resolve: {
      alias: {
        '../../theme.config$': path.join(
          __dirname,
          '../my-custom-semantic-theme/theme.config',
        ),
      },
    },
    module: {
      rules: [
        {
          test: /\.less$/,
          use: [
            {
              loader: MiniCssExtractPlugin.loader,
            },
            'css-loader',
            'less-loader',
          ],
        },
        {
          test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
          use: 'url-loader',
        }
      ],
    },
    plugins: [
      new MiniCssExtractPlugin({
        filename: 'styles/[name].[contenthash].css',
      }),
    ],
  };

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



&lt;h3&gt;
  
  
  Semantic UI
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Go on &lt;a href="https://github.com/Semantic-Org/Semantic-UI-LESS"&gt;https://github.com/Semantic-Org/Semantic-UI-LESS&lt;/a&gt; and download the repo into your &lt;code&gt;my-custom-semantic-theme/&lt;/code&gt; folder. &lt;/li&gt;
&lt;li&gt;Rename file &lt;code&gt;theme.config.example&lt;/code&gt; to &lt;code&gt;theme.config&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Rename _site folder to site&lt;/li&gt;
&lt;li&gt;Delete other folders and files in your &lt;code&gt;my-custom-semantic-theme/&lt;/code&gt; folder and remain with
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  my-custom-semantic-theme/
      site/
      theme.config
      theme.less
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open &lt;code&gt;theme.config&lt;/code&gt; and have the last lines match the following
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   /*******************************
            Folders
   *******************************/

   /* Path to theme packages */
   @themesFolder : 'themes';

   /* Path to site override folder */
   @siteFolder  : '../../my-custom-semantic-theme/site';


   /*******************************
         Import Theme
   *******************************/

   @import (multiple) "~semantic-ui-less/theme.less";

   @fontPath : "../../../themes/@{theme}/assets/fonts";

   /* End Config */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  App
&lt;/h3&gt;

&lt;p&gt;We are almost ready. Below is an example of a basic app.js using &lt;code&gt;semantic-ui-react&lt;/code&gt; components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  import React from 'react';
  import ReactDOM from 'react-dom';
  import { Icon, Button, Segment, Label } from 'semantic-ui-react';
  import 'semantic-ui-less/semantic.less'; // if you do this once in your entry point file, you don't have to do it again in other files.

  ReactDOM.render(
  &amp;lt;Segment&amp;gt;
    &amp;lt;p&amp;gt;Hello semantic-ui button with icon&amp;lt;/p&amp;gt;
    &amp;lt;Button as='div' labelPosition='right'&amp;gt;
      &amp;lt;Button color='red'&amp;gt;
        &amp;lt;Icon name='heart' /&amp;gt;
        Like
      &amp;lt;/Button&amp;gt;
      &amp;lt;Label as='a' basic color='red' pointing='left'&amp;gt;
        2,048
      &amp;lt;/Label&amp;gt;
    &amp;lt;/Button&amp;gt;
  &amp;lt;/Segment&amp;gt;,
  document.getElementById('appRoot'),
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing our app
&lt;/h3&gt;

&lt;p&gt;We are now ready to test if semantic-ui-less is correctly setup. Do the &lt;code&gt;npm start&lt;/code&gt; thing. &lt;/p&gt;

&lt;p&gt;To check if you can now override the default semantic-ui styles with your own, open &lt;code&gt;/my-custom-semantic-theme/site/elements/button.variables&lt;/code&gt; and add the following line&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@backgroundColor: green;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Refresh your app and you should see your button background color changing to brown.&lt;/p&gt;

&lt;p&gt;You can download example code on &lt;a href="https://github.com/kudakwashegore/custom-semanticui-react"&gt;https://github.com/kudakwashegore/custom-semanticui-react&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
