<?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: skalable-dev</title>
    <description>The latest articles on DEV Community by skalable-dev (@skalabledev).</description>
    <link>https://dev.to/skalabledev</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%2F584252%2Fd29da66d-dc3e-48f4-9c7c-64f4c44cbf3a.png</url>
      <title>DEV Community: skalable-dev</title>
      <link>https://dev.to/skalabledev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/skalabledev"/>
    <language>en</language>
    <item>
      <title>Managing Gaps in Rows, Columns (or even both!) with KotlinJS</title>
      <dc:creator>skalable-dev</dc:creator>
      <pubDate>Sat, 24 Apr 2021 12:35:02 +0000</pubDate>
      <link>https://dev.to/skalabledev/managing-gaps-in-rows-columns-or-even-both-with-kotlinjs-2fc1</link>
      <guid>https://dev.to/skalabledev/managing-gaps-in-rows-columns-or-even-both-with-kotlinjs-2fc1</guid>
      <description>&lt;p&gt;At sKalable we are on a mission to make KotlinJS websites mainstream. We strive to make them easy to code, fun to build and, of course, skalable! 😊 😛&lt;/p&gt;

&lt;p&gt;We are aware that sometimes when building the design of our website, working with column and row gaps can be initially tricky. Who hasn't struggled with this before? We certainly have...&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's explore how we can work effectively with gaps in rows and columns to smash our designs!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mind the Gap
&lt;/h2&gt;

&lt;p&gt;So what actually is the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/gap" rel="noopener noreferrer"&gt;gap&lt;/a&gt; property?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;gap&lt;/code&gt; property refers to the space between &lt;code&gt;columns&lt;/code&gt; and &lt;code&gt;rows&lt;/code&gt; by specifying the size of their gutters within flex, multi-columns and grid layouts. We use the &lt;code&gt;gap&lt;/code&gt; property to add the required spacing between the items in our website so that they comply with the design and look great! :D&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FUnderstanding%2520Flexbox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FUnderstanding%2520Flexbox.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In terms of the syntax it can be confusing to grasp at first. Essentially, &lt;strong&gt;&lt;em&gt;Columns&lt;/em&gt;&lt;/strong&gt; have &lt;code&gt;rowGaps&lt;/code&gt; and &lt;strong&gt;&lt;em&gt;Rows&lt;/em&gt;&lt;/strong&gt; have &lt;code&gt;columnGaps&lt;/code&gt;. Each of these takes a &lt;code&gt;Gap&lt;/code&gt; value in the form of a size property.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gaps and Flexbox
&lt;/h2&gt;

&lt;p&gt;When should we use gaps? Usually gaps are used in cells that repeat or to justify items on screen when a particular distance is required between them. In this example project we have used &lt;code&gt;rem&lt;/code&gt; to set the sizing of gaps but other sizes such as &lt;code&gt;pct&lt;/code&gt; or &lt;code&gt;px&lt;/code&gt; work too.&lt;/p&gt;

&lt;p&gt;It is worth mentioning that there are three gap variants that we can use with flexbox. Let's explore them in more detail below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gap for Rows only
&lt;/h3&gt;

&lt;p&gt;When we want to add spaces to items in a &lt;code&gt;flexDirection&lt;/code&gt; column we need to include &lt;code&gt;rowGap&lt;/code&gt; as per the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Style for rowGap in a flex column
 */&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;container&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flex&lt;/span&gt;
    &lt;span class="n"&gt;flexDirection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FlexDirection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;column&lt;/span&gt;
    &lt;span class="c1"&gt;//when flex direction is column we use rowGap as spacing&lt;/span&gt;
    &lt;span class="n"&gt;rowGap&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Gap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;YOUR_VALUE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// this can be other sizes&lt;/span&gt;
    &lt;span class="o"&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;As we can see, using a column layout with &lt;code&gt;rowGap&lt;/code&gt; applies spacing between the row cells.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FrowGap.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FrowGap.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Gap for Columns only
&lt;/h3&gt;

&lt;p&gt;Adding spaces to items in a flexDirection row requires &lt;code&gt;rowColumn&lt;/code&gt; like in this example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Style for columnGap in a flex row
 */&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;container&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flex&lt;/span&gt;
    &lt;span class="n"&gt;flexDirection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FlexDirection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;
    &lt;span class="c1"&gt;//when flex direction is row we use columnGap as spacing&lt;/span&gt;
    &lt;span class="n"&gt;columnGap&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Gap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;YOUR_VALUE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// this can be other sizes&lt;/span&gt;
    &lt;span class="o"&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;Here we are applying columnGap to a row layout. Modifying the spacing size horizontally between cells. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FcolumnGapFinal.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FcolumnGapFinal.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Gap for both Rows and Columns
&lt;/h3&gt;

&lt;p&gt;What happens when we need both row and column gaps?! Luckily this is built in using just &lt;code&gt;gap&lt;/code&gt; as the key...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Style for gap in both flexRow and flexColumn
 */&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;container&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flex&lt;/span&gt;
    &lt;span class="n"&gt;flexDirection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FlexDirection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;
    &lt;span class="c1"&gt;//gap allows for both rows and columns to be sized as one&lt;/span&gt;
    &lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Gap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;YOUR_VALUE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// this can be other sizes&lt;/span&gt;
    &lt;span class="o"&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;as we can see, both row and column spacing is adjusted in tandem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FDualGapFinal2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Understanding%2520Gap%2FDualGapFinal2.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Gap Object?!
&lt;/h2&gt;

&lt;p&gt;In KotlinJS and Styled-Components there is a wrapper around how we set the value of the spacing called &lt;code&gt;Gap(value: String)&lt;/code&gt; which requires a string value as the parameter. &lt;/p&gt;

&lt;p&gt;Because of this we must convert our size by using &lt;code&gt;value&lt;/code&gt; which is of type &lt;code&gt;String&lt;/code&gt;. This is provided for free thanks to the &lt;code&gt;LinearDimensions&lt;/code&gt; class. Ensuring all our conversions are web safe!&lt;/p&gt;

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

&lt;p&gt;Using row and column gaps correctly is incredibly beneficial to keeping CSS clean and will speed up your development process if you can apply them correctly. We hope that this article has helped resolved your confusion around this topic.&lt;/p&gt;

&lt;p&gt;Please feel free to post any questions below, and we will be happy to support you :)&lt;/p&gt;

&lt;p&gt;You can check out the sample project &lt;a href="https://github.com/skalable-samples/KotlinJS-Row-Column-Gap" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;@sKalable we are Full Stack Kotlin-centric agency that creates code that is always flexible, maintainable and of course, &lt;strong&gt;&lt;em&gt;sKalable&lt;/em&gt;&lt;/strong&gt; :D&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/skalable_dev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/skalabledev"&gt;Dev.to&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/company/skalable-dev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow us on for the latest updates and strategies in Kotlin, Multiplatform and much more, catering for your business or personal needs. &lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>css</category>
      <category>html</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Making your KotlinJS Website Mobile-Friendly</title>
      <dc:creator>skalable-dev</dc:creator>
      <pubDate>Wed, 07 Apr 2021 11:59:06 +0000</pubDate>
      <link>https://dev.to/skalabledev/making-your-kotlinjs-website-mobile-friendly-223g</link>
      <guid>https://dev.to/skalabledev/making-your-kotlinjs-website-mobile-friendly-223g</guid>
      <description>&lt;p&gt;At sKalable we are Kotlin driven, so we are in a mission to make KotlinJS websites mainstream. We are advocates of clean code and strive for the best quality in everything we build! &lt;/p&gt;

&lt;p&gt;How many times have you visited a site that is not mobile responsive and thus renders terribly on your device? From my experience, I would say too many times! &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's explore how we can make our KotlinJS site responsive, so we can delight our users&lt;/em&gt; 🙂😛&lt;/p&gt;

&lt;h1&gt;
  
  
  Making your KotlinJS Website Mobile-Friendly
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Mobile%2520Responsive%2520Website%2520%2FKotlinJS%2520Media.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Mobile%2520Responsive%2520Website%2520%2FKotlinJS%2520Media.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is mobile responsiveness?
&lt;/h3&gt;

&lt;p&gt;When a website is responsive, its layout and content automatically adapt to fit and render correctly on the device its being run on. Basically, if the screen gets smaller, then the content and layout rearranges accordingly, ensuring the visuals and functionality are not compromised.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is it so important?
&lt;/h3&gt;

&lt;p&gt;Google's recommendation is to adopt a &lt;a href="https://developers.google.com/search/mobile-sites/mobile-seo/responsive-design" rel="noopener noreferrer"&gt;responsive development&lt;/a&gt; for our websites. The benefits of doing this are many:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It provides the best quality and user experience as a non-mobile friendly website is very hard to view on devices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A good website will have better SEO, perform better in rankings and make your brand distill quality!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Users find non-mobile friendly websites frustrating and will certainly abandon your site if it is hard to navigate.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How are we going to achieve this?
&lt;/h3&gt;

&lt;p&gt;We are going to adopt a 'mobile first' approach by using &lt;code&gt;Meta Tags&lt;/code&gt; and &lt;code&gt;Media Queries&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Let's get started by building a quick website with a &lt;em&gt;Navigation Bar&lt;/em&gt; that changes the layout into a convenient &lt;em&gt;Navigation Drawer&lt;/em&gt; depending on the device's dimensions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Meta Viewport Tag
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;ViewPort&lt;/code&gt; tag is basically needed to signal browsers that the site adapts to all devices. It provides them with instructions on how to scale and adjust the dimensions to match the screen's size.&lt;/p&gt;

&lt;p&gt;The first thing we need to do is to add the &lt;a href="https://developers.google.com/search/mobile-sites/mobile-seo/responsive-design" rel="noopener noreferrer"&gt;Meta Viewport Tag&lt;/a&gt; below in your &lt;code&gt;index.html&lt;/code&gt; file.&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;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Media Queries
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/cssref/css3_pr_mediaquery.asp" rel="noopener noreferrer"&gt;Media Queries&lt;/a&gt; are used to style websites for different sized screens (phones, tablets, desktop). Essentially, by using Media Queries, we can target a specific screen size or pixel range and make changes to the CSS ONLY when the particular screen size that we have set is hit. &lt;/p&gt;

&lt;p&gt;So, how do we use them? The syntax of Media Queries for mobile responsiveness looks like this in KotlinJS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*Specify the minimum screen width that you want to target in pixels*/&lt;/span&gt;
&lt;span class="nf"&gt;media&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"only screen and (min-width: 768px)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*Add the CSS changes you want to display here*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To clarify, the code above will only execute on screens that are 768px or larger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*Specify the maximum screen width that you want to target in pixels*/&lt;/span&gt;
&lt;span class="nf"&gt;media&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"only screen and (max-width: 768px)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/*Add the CSS changes you want to display here*/&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will adapt our CSS for screen sizes 768px or lower.&lt;/p&gt;

&lt;h3&gt;
  
  
  Safety in Functions
&lt;/h3&gt;

&lt;p&gt;As we begin to add more and more queries it becomes more and more hardcoded. A misspelling can cause all sorts of havoc in these cases.&lt;/p&gt;

&lt;p&gt;Using utility functions to generate these can save us from all types of headaches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;mediaOnlyScreenMinWidth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LinearDimension&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"only screen and (min-width: ${width})"&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;mediaOnlyScreenMaxWidth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LinearDimension&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"only screen and (max-width: ${width})"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using these functions we can create responsive sizes that will match across our codebase.&lt;/p&gt;

&lt;p&gt;Applying the utility functions our code now feels less hardcoded&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*Specify the minimum screen width that you want to target in pixels*/&lt;/span&gt;
&lt;span class="nf"&gt;media&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mediaOnlyScreenMinWidth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*Add the CSS changes you want to display here*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see above, we use the &lt;code&gt;(mediaOnlyScreenMaxWidth())&lt;/code&gt; or the &lt;br&gt;
&lt;code&gt;(mediaOnlyScreenMinWidth())&lt;/code&gt; to specify when the changes to the CSS should happen. &lt;/p&gt;

&lt;p&gt;Let's explore the code above in more detail with specific examples from our website. &lt;/p&gt;
&lt;h3&gt;
  
  
  Media in Action
&lt;/h3&gt;

&lt;p&gt;Our top navigation bar includes a logo and three Call to action (CTA) buttons. When the Webpage is rendered on Desktop, we want the navbar to display fully. If the screen size drops below the desired limit, the margin should be removed. To achieve this we use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;primaryNav&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;marginTop&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;em&lt;/span&gt;

        &lt;span class="nf"&gt;media&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mediaOnlyScreenMinWidth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;margin&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="n"&gt;px&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;Rendering on smaller devices, such as phones and tablets, the navbar should be replaced with a drawer navigation containing the CTAs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;mobileMenu&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;

        &lt;span class="nf"&gt;media&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mediaOnlyScreenMinWidth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;none&lt;/span&gt;
            &lt;span class="nf"&gt;margin&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="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;LinearDimension&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blueViolet&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;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Mobile%2520Responsive%2520Website%2520%2Fmobile-responsiveness.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Mobile%2520Responsive%2520Website%2520%2Fmobile-responsiveness.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Using media tags in KotlinJS is quite simple. It is definitely worth addressing mobile responsiveness when developing your KotlinJS website. Making it render correctly on a desktop is not enough, especially  factoring in that mobile traffic has overtaken desktop and now accounts for the majority of website traffic.&lt;/p&gt;

&lt;p&gt;You can check out the sample project &lt;a href="https://github.com/skalable-samples/KotlinJS-Responsive-Web" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to leave any questions or comments below. We would love hear your thoughts.&lt;/p&gt;

&lt;p&gt;@sKalable we are a Full Stack Kotlin-centric agency that create code to ensure it is consistently Maintainable, Flexible and of course,  &lt;strong&gt;&lt;em&gt;sKalable&lt;/em&gt;&lt;/strong&gt;. 😎&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/skalable_dev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/skalabledev"&gt;Dev.to&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/company/skalable-dev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;for the latest updates and strategies in Kotlin, Multiplatform and much more, catering for your business or personal needs.&lt;/p&gt;

</description>
      <category>react</category>
      <category>kotlin</category>
      <category>html</category>
      <category>codequality</category>
    </item>
    <item>
      <title>KotlinJS and MULTIPLE State Hooks (Part two)</title>
      <dc:creator>skalable-dev</dc:creator>
      <pubDate>Mon, 29 Mar 2021 14:38:13 +0000</pubDate>
      <link>https://dev.to/skalabledev/kotlinjs-and-multiple-state-hooks-part-two-3k69</link>
      <guid>https://dev.to/skalabledev/kotlinjs-and-multiple-state-hooks-part-two-3k69</guid>
      <description>&lt;p&gt;At sKalable we are just in love with Kotlin! We really strive to make all things Kotlin simple, fun, and a breeze to work with :D &amp;lt;3 KotlinJS is no exception to our mission. 😃 ❤️&lt;/p&gt;

&lt;p&gt;Following on from our &lt;a href="https://dev.to/skalabledev/kotlinjs-and-state-hooks-2426"&gt;Part 1 tutorial of KotlinJS and State Hooks&lt;/a&gt; ,  that covers State as a singular, Hooks and the best practices for working with functional components, we want to take things further and  delve into using multiple State Hooks or State values in our code. Using multiple State Hooks can be advantageous as you can split them for different uses, better manage properties that change independently of each other... but with certain caveats... &lt;/p&gt;

&lt;p&gt;Helping to define the structure and improve the readability of our code so much more!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Before delving deeper into multiple State Hooks, feel free to take a look at &lt;a href="https://dev.to/skalabledev/kotlinjs-and-state-hooks-2426"&gt;Part 1&lt;/a&gt; of this article as a refresher 😊 )&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's give it a go!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple States in action
&lt;/h2&gt;

&lt;p&gt;Check out this diagram where we can see multiple states in action!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FKotlinJS%2520Multi%2520State.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FKotlinJS%2520Multi%2520State.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with Fixed State interfaces
&lt;/h2&gt;

&lt;p&gt;Below is an example looking at some issues of setting an interface object as a &lt;code&gt;useState&lt;/code&gt; type value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**  
 * EnterWordStateOriginal is used as a State Object * 
 * 
 * @property word is the word that is updated when the input changes.  
 * @property updateClicked is the property that is updated when the button gets clicked.  
 * @property updatedWord the new word that has been updated.  
 */&lt;/span&gt;
 &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EnterWordStateOriginal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;  
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updateClicked&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;  
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedWord&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**  
 * enterWord is a functional component that renders an input, a button and a label. 
 */&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;enterWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;functionalComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RProps&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
&lt;span class="cm"&gt;/**  
 * When we first declare the useState, the default value is set in the parenthesis. 
 * This will be held in enterWordState. 
 * 
 * To modify this we use the setEnterWord function, delegated with the [by] key. 
 * To clarify enterWord is treated as a var with a getter and a setter. 
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;enterWordState&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EnterWordStateOriginal&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;EnterWordStateOriginal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updateClicked&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//... functional / render code .&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Setting a state object requires the full object to be set in functional
 * components. This can become very verbose, incredibly quickly.     
 */&lt;/span&gt;  
    &lt;span class="c1"&gt;//... HTML Input handler &lt;/span&gt;
    &lt;span class="n"&gt;onChangeFunction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; 
         &lt;span class="n"&gt;enterWordState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;EnterWordState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
             &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;  
             &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updateClicked&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;updateClicked&lt;/span&gt; 
             &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;updatedWord&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;It might not be the most elegant looking code, but it works. When using state objects in functional components, you'll see there is no requirement to set the &lt;code&gt;RState&lt;/code&gt; type on the component itself. This is different to how &lt;code&gt;Class Components&lt;/code&gt; work for instance.&lt;/p&gt;

&lt;p&gt;Unlike with &lt;code&gt;Class Components&lt;/code&gt;, &lt;code&gt;Functional Components&lt;/code&gt; do not have a &lt;code&gt;setState {}&lt;/code&gt; function to map old state to new state &lt;em&gt;(This is not the case for Props though)&lt;/em&gt; . Nor do they require knowledge of the state in their construction either.&lt;/p&gt;

&lt;p&gt;We apply the concept of &lt;code&gt;state&lt;/code&gt; to a functional component through &lt;code&gt;React Hooks&lt;/code&gt;. Using hooks, the component now has the capability to handle &lt;code&gt;state&lt;/code&gt; changes. There is a readability issue regarding this though...&lt;/p&gt;

&lt;p&gt;Code should be clean, easy to write and read. Unfortunately, using &lt;code&gt;state&lt;/code&gt; objects in functional components doesn't help us achieve that with the approach above.&lt;/p&gt;

&lt;p&gt;Below, we see that in order to set &lt;code&gt;state&lt;/code&gt; we must initialise the full object every time. This requires us to manually set the values of the previous states that don't change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** 
 * Setting a state object requires the full object to be set in functional 
 * components. 
 * This can become very verbose, incredibly quickly.     
 */&lt;/span&gt;  
 &lt;span class="n"&gt;onChangeFunction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;   
     &lt;span class="n"&gt;enterWordState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;EnterWordState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
         &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;  
         &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updateClicked&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;updateClicked&lt;/span&gt; 
         &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;updatedWord&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;Ughhh.... We can't be adding this everywhere each time we update the state. Ok, time to clean this up a little.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dividing state strategies
&lt;/h2&gt;

&lt;p&gt;There is no real &lt;em&gt;"right"&lt;/em&gt; or &lt;em&gt;"wrong"&lt;/em&gt; method of tackling division of state, its mostly down to personal preference and use case for each component &lt;em&gt;(although some strategies can look ridiculous such as above)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Larger states have a different challenge than smaller states. Below we outline various strategies and how to decide which is best approach for the components needs and number of states you require.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dividing by individual values — Multi-State component
&lt;/h3&gt;

&lt;p&gt;For small state interfaces that can be described as having no more than three vars in a State, prefer an individual state for each value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**  
 * Primitive State based on a String and an Int. The types are inferred. 
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;wordState&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// inferred String &lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedClickedState&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;useState&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="c1"&gt;// inferred Int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows for clean and easy methods to update and read the required state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;updatedClickedState&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// update the value by 1  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What about larger states? How should we handle those?&lt;/p&gt;

&lt;h3&gt;
  
  
  Keeping composition / context as a Single State
&lt;/h3&gt;

&lt;p&gt;If you find yourself writing a lot of repetitive code, always think of &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="noopener noreferrer"&gt;DRY Principles&lt;/a&gt;. We tend do repeat a lot of the &lt;code&gt;state&lt;/code&gt; construction just to update a single value when using &lt;code&gt;state&lt;/code&gt; as a single object. A separate function within the &lt;code&gt;functional component&lt;/code&gt; can help resolve this issue.&lt;/p&gt;

&lt;p&gt;Builder functions can be used to create new objects and handle the mapping of values. Kotlin has a feature called &lt;a href="https://kotlinlang.org/docs/functions.html#default-arguments" rel="noopener noreferrer"&gt;default arguments&lt;/a&gt; allowing parameter values to have the default value to the corresponding states value. Automatically the parameters will have the value if one has not been provided by the caller.&lt;/p&gt;

&lt;p&gt;Applying this approach allows for cleaner code. It does require "boilerplate" in the form of a separate function for each state interface in functional components with interface states.&lt;/p&gt;

&lt;p&gt;Though it's a better approach to mapping, it's still not ideal nor efficient when writing components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**  
 * When we first declare the useState, the default value is set in the parenthesis. 
 * This will be held in enterWordState. 
 * 
 * To modify this we use the setEnterWord function, delegated with the [by] key.
 * To clarify enterWord is treated as a var with a getter and a setter. 
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;enterWordState&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EnterWordStateWithBuilder&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;EnterWordStateWithBuilder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updateClicked&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**  
 * In this approach we use utility builders within the functional component to set state as a single 
 * line when interfaces are used as state holders.
 * Using default params pointed at [enterWordState] allows for cleaner setters. 
 * 
 * @param word — Has a default of the current state word  
 * @param updateClicked — Has a default of the current state updateClicked  
 * @param updatedWord — Has a default of the current state updatedWord  
 */&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setWordState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
    &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="n"&gt;updateClicked&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;updateClicked&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="n"&gt;updatedWord&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;updatedWord&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="n"&gt;enterWordState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;EnterWordStateWithBuilder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updateClicked&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updateClicked&lt;/span&gt;  
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updatedWord&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 result of creating a utility builder for the function state is a clean setter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**  
 * Setting a state object requires the full object to be set in functional 
 * components. This can become very verbose, incredibly quickly. 
 */&lt;/span&gt;  
&lt;span class="n"&gt;onChangeFunction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; 
    &lt;span class="nf"&gt;setWordState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&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;There must be another option...&lt;/p&gt;

&lt;p&gt;As the number of &lt;code&gt;state&lt;/code&gt; values grow, they become more and more cumbersome to maintain. If we need to create large &lt;code&gt;builder functions&lt;/code&gt; for each State object, our &lt;code&gt;functional components&lt;/code&gt; will become more and more polluted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Utility function to the rescue!
&lt;/h3&gt;

&lt;p&gt;The thought of writing different builders for each state object is daunting. Removing the need for this and providing a clean method of updating &lt;code&gt;state&lt;/code&gt; objects without writing builders would be perfect. Even better if it meant changing the component from &lt;code&gt;functional&lt;/code&gt; to a &lt;code&gt;class&lt;/code&gt; didn't require the interface to change.&lt;/p&gt;

&lt;p&gt;To solve this we look at Kotlin itself and the incredible &lt;a href="https://kotlinlang.org/docs/scope-functions.html#apply" rel="noopener noreferrer"&gt;apply&lt;/a&gt; function. Using our old state and new state values together provides all the ingredients to create a new object by copying the existing values of the old state and applying the new state values atop.&lt;/p&gt;

&lt;p&gt;Let us start by changing the state holder interface slightly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**  
 * EnterWordStateOriginal is used as a State Object * 
 *  
 * @property word is the word that is updated when the input changes.  
 * @property updateClicked is the property that is updated when the button gets clicked.  
 * @property updatedWord the new word that has been updated.  
 */&lt;/span&gt;
&lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;SetStateExampleState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;  
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updateClicked&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;  
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;updatedWord&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I know what you're all thinking, &lt;em&gt;"What's &lt;code&gt;RState&lt;/code&gt; doing there?!"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is a genuine reason: earlier we mentioned maintaining cooperation of &lt;code&gt;state&lt;/code&gt; if we change the component from &lt;code&gt;functional&lt;/code&gt; into a &lt;code&gt;class&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Extending &lt;code&gt;RState&lt;/code&gt; achieves this, but also plays a secret second purpose.👇&lt;/p&gt;

&lt;h4&gt;
  
  
  Functional setState
&lt;/h4&gt;

&lt;p&gt;To prevent any regular interface being used as a &lt;code&gt;state&lt;/code&gt; we can extend our state interface from &lt;code&gt;RState&lt;/code&gt;. Using this as the type for our &lt;code&gt;setState&lt;/code&gt; ensures only &lt;code&gt;state&lt;/code&gt; objects can be used. Forcing better readability and cleaner code across our codebase naturally.&lt;/p&gt;

&lt;p&gt;no more &lt;em&gt;"What is this badly named interface for?!"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Our new utility function to handle this mapping will now provide us not just the clean setState we want, but the setState we &lt;em&gt;deserve&lt;/em&gt;!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**  
 * By creating a utility function to map the current state with 
 * the updated variables, it removes the need to create multiple 
 * builder functions for larger states across the project. 
 * Using this function we can keep code clean and efficient. 
 * 
 * @see T — The purpose of extending RState is to keep uniformity across the code. 
 *          If we look to change the type of component we can * be guaranteed the state will work for free.
 * 
 * @param oldState — The current state values  
 * @param newState — The new values we would like to apply to the state  
 * 
 * @return T — The values of old state plus the updated values of new state.  
 */&lt;/span&gt;
&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
    &lt;span class="n"&gt;oldState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="n"&gt;newState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;  
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oldState&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time to break it down a little:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;internal&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Prevents the &lt;code&gt;setState&lt;/code&gt; function being exposed as part of the overall modules API.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;inline&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://kotlinlang.org/docs/inline-functions.html#non-local-returns" rel="noopener noreferrer"&gt;&lt;code&gt;inline&lt;/code&gt;&lt;/a&gt; optimises functions by inlining the lambda expressions for a reduction in runtime overhead.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;T : RState&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;This defines the type of &lt;code&gt;oldState&lt;/code&gt; and &lt;code&gt;newState&lt;/code&gt;. Extending &lt;code&gt;RState&lt;/code&gt; gives us the certainty this will be a &lt;code&gt;state&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;oldState: T&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;The value of the existing state. Kotlin uses &lt;em&gt;"Copy by Value"&lt;/em&gt; for function parameters. The &lt;code&gt;oldState&lt;/code&gt; param will then be a copy of the state we want to set. (&lt;em&gt;There is some discrepancy in this statement to the values inside, as only the outlining object is copied, but that's for another time.&lt;/em&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;newState: T.() -&amp;gt; Unit&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;For those of you who don't know, this has got to be one of the most amazing features of Kotlin. It's known as a &lt;a href="https://kotlinlang.org/docs/lambdas.html#lambda-expressions%20and-anonymous-functions" rel="noopener noreferrer"&gt;&lt;em&gt;Function literals with receiver&lt;/em&gt;&lt;/a&gt;. We can set parameters of the &lt;code&gt;receiver T&lt;/code&gt; and apply them to our clone.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;clone&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;_Ok, this might not be &lt;em&gt;exactly&lt;/em&gt; part of the Kotlin language, but it is part of KotlinJS! It allows us to copy &lt;code&gt;oldState&lt;/code&gt; into a new jsObject.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;apply(newState)&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;We want to return the value of the &lt;code&gt;oldState&lt;/code&gt; with the updates from &lt;code&gt;newState&lt;/code&gt;. Using &lt;code&gt;apply&lt;/code&gt; allows for this. &lt;code&gt;apply&lt;/code&gt; returns an instance of &lt;code&gt;this&lt;/code&gt; so is ideal for returning a new copy after adding &lt;code&gt;newState&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Result
&lt;/h4&gt;

&lt;p&gt;Adding our brand new &lt;code&gt;setState&lt;/code&gt; to the &lt;code&gt;functional component&lt;/code&gt; we get a clean, readable state management.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;enterWordState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enterWordState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="n"&gt;updateClicked&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  
    &lt;span class="n"&gt;updatedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The best part of this approach is autocomplete and no need to define each value to set &lt;code&gt;state&lt;/code&gt;. Our generic function infers the type of the &lt;code&gt;state&lt;/code&gt; and gives us auto complete within the body of the lambda block while also mapping existing values that haven't changed to the new &lt;code&gt;state&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Awesome right?!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The outcome is a clean &lt;code&gt;state&lt;/code&gt; setter within a &lt;code&gt;functional component&lt;/code&gt; that can have its interface values extended without requiring refactoring everywhere the state is set. &lt;br&gt;
&lt;em&gt;(As we would with the initial approach)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing remarks
&lt;/h2&gt;

&lt;p&gt;Using large sets of values in a &lt;code&gt;state&lt;/code&gt; object can be the most efficient way of keeping code clean and maintainable. Especially when dealing with larger state sets within components (such as forms).&lt;/p&gt;

&lt;p&gt;As a rule of thumb, with smaller &lt;code&gt;state&lt;/code&gt; values individual states can be used. These can lose context of "what they're for" as logic grows. &lt;/p&gt;

&lt;p&gt;Object states address this by grouping these values into a single value. Important when improving the clarity of code, also providing a "Context" to what state is.&lt;br&gt;&lt;br&gt;
e.g "&lt;code&gt;formValuesState&lt;/code&gt;' would hold the state of fields in a form.&lt;/p&gt;

&lt;p&gt;One last tip to help avoid confusion is to make sure you include the actual word &lt;code&gt;State&lt;/code&gt; as part of the state variable name, this is especially true with single states. i.e &lt;code&gt;nameState&lt;/code&gt;, &lt;code&gt;emailState&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To help differentiate we have grouped each approach into separate examples in the project below, so you can get an overall understanding of each approach and its advantages.&lt;/p&gt;

&lt;p&gt;Checkout it out &lt;a href="https://github.com/skalable-samples/KotlinJS-and-React-State" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;@sKalable we are a Full Stack Kotlin-centric agency that create code to ensure it is consistently Maintainable, Flexible and of course,  &lt;strong&gt;&lt;em&gt;sKalable&lt;/em&gt;&lt;/strong&gt;. 😎&lt;/p&gt;

&lt;p&gt;We love to hear from the community, so if this helped feel free to get in touch or follow us on&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/skalable_dev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/skalabledev"&gt;Dev.to&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/company/skalable-dev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;to get the latest updates and strategies with Kotlin and Multiplatform for your business or personal needs.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>react</category>
      <category>javascript</category>
      <category>codequality</category>
    </item>
    <item>
      <title>KotlinJS and State Hooks</title>
      <dc:creator>skalable-dev</dc:creator>
      <pubDate>Tue, 09 Mar 2021 15:31:00 +0000</pubDate>
      <link>https://dev.to/skalabledev/kotlinjs-and-state-hooks-2426</link>
      <guid>https://dev.to/skalabledev/kotlinjs-and-state-hooks-2426</guid>
      <description>&lt;p&gt;At sKalable we are Kotlin Obsessed! Making the environment better is part of our daily mission. We want to make all things KotlinJS amazingly easy to work with too.&lt;/p&gt;

&lt;p&gt;As part of our pursuit to clean up code, we will be delving into state management in this two part tutorial. 😎&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; part of React Hooks for &lt;code&gt;state&lt;/code&gt; management is something that even &lt;a href="https://www.javascript.com/" rel="noopener noreferrer"&gt;&lt;code&gt;Javascript&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;&lt;code&gt;Typescript&lt;/code&gt;&lt;/a&gt; engineers struggle with from time to time. We are going to reduce this struggle within the React ecosystem using KotlinJS and the ever incredible &lt;a href="https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-react/README.md" rel="noopener noreferrer"&gt;Kotlin-React&lt;/a&gt; library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding state
&lt;/h2&gt;

&lt;p&gt;To get an idea of what we are trying to do we need to grasp what &lt;code&gt;state&lt;/code&gt; is in &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;react&lt;/a&gt; programming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;So lets start!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is state
&lt;/h3&gt;

&lt;p&gt;The React library provides components with a built-in &lt;code&gt;state&lt;/code&gt; management object. In this &lt;code&gt;state&lt;/code&gt; object we can store and manipulate the state of the React component on the fly. If the state object changes, then the component will re-render with the updated state and any UI changes will be reflected.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does it work
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FKotlinJS%2520State.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FKotlinJS%2520State.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Keeping things reactive
&lt;/h3&gt;

&lt;p&gt;We can describe &lt;code&gt;state&lt;/code&gt; as being reactive since it stores dynamic data for the component. The &lt;code&gt;state&lt;/code&gt; object itself allows the component to keep track of changes and updates to the data and render views accordingly. It works similarly to the &lt;a href="https://www.tutorialspoint.com/design_pattern/observer_pattern.htm" rel="noopener noreferrer"&gt;Observer Pattern&lt;/a&gt; given that it defines a subscription mechanism to notify observers of the data about any events that happen to the payload they are observing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Moving on we will cover &lt;code&gt;state&lt;/code&gt; in both Class and Functional components.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  State in Class Components
&lt;/h3&gt;

&lt;p&gt;A Stateful Class component is &lt;a href="https://reactjs.org/docs/state-and-lifecycle.html#converting-a-function-to-a-class" rel="noopener noreferrer"&gt;lifecycle aware&lt;/a&gt; and has its &lt;code&gt;state&lt;/code&gt; defined in an &lt;code&gt;external interface&lt;/code&gt;. They can also initialise &lt;code&gt;state&lt;/code&gt; as a class property &lt;em&gt;(which we will cover later in The &lt;code&gt;useState&lt;/code&gt; Hook)&lt;/em&gt; or in a constructor function — both approaches achieve the same result.&lt;/p&gt;

&lt;p&gt;When we first initialise our custom &lt;code&gt;state&lt;/code&gt;, it creates a &lt;code&gt;getter&lt;/code&gt; and &lt;code&gt;setter&lt;/code&gt; for the value of the property we want to have state aware. The getter is named similarly to a property variable in Kotlin &lt;a href="https://kotlinlang.org/docs/coding-conventions.html#property-names" rel="noopener noreferrer"&gt;(see naming properties in Kotlin)&lt;/a&gt; such as &lt;code&gt;count&lt;/code&gt; or &lt;code&gt;word&lt;/code&gt;, i.e. descriptive of the data it holds.&lt;/p&gt;

&lt;p&gt;To update the data in this &lt;code&gt;getter&lt;/code&gt; we use the function defined as &lt;code&gt;setState&lt;/code&gt;. Inside the Lambda block from this function we have access the variable we want to update.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * A class component extends from [RComponent]. There is no requirement for
 * an external prop or state. The values of [RProps] and / or [RState]
 * can be provided without the need to declare external interfaces reflecting these.
 */&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IndentWithDot&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;IndentState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/**
     * To initialise the `state` when the class component is created we
     * must override the `RState.init()` function corresponding to the external
     * interface we provided to the component. In our case its `IndentState.init()`
     *
     * @see RComponent&amp;lt;IndentProps, IndentState&amp;gt; — (We're interested in IndentState)
     * @see IndentState
     */&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;IndentState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;indentAmount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;indentationValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"."&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * The render function gets called when the component mounts or is updated.
     * Code inside the render function gets rendered when called.
     *
     * In our render function we have a single [button] that updates
     * the [indent] each time its pressed and displays the current update to the user.
     *
     * In order to read the `indentationValue` and `indentAmount` we need to reference the `state` from our class
     * and get the `indent` values from it.
     * @see IndentState
     *
     */&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;RBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;div&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Update the string using the values from state.indentationValue and state.ident&lt;/span&gt;
                &lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"press me to add another dot indent ${state.indentationValue} ${state.indentAmount}"&lt;/span&gt;
                &lt;span class="nf"&gt;attrs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;onClickFunction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="nf"&gt;setState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="cm"&gt;/**
                             * Reference the value of the `state.indent` and add 1.
                             * This will become the new value of `indent`.
                             */&lt;/span&gt;
                            &lt;span class="n"&gt;indentAmount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indentAmount&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                            &lt;span class="n"&gt;indentationValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indentAmount&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;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="cm"&gt;/**
 * ReactBuilder function used to construct the React Component IndentWithDot.
 */&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;RBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indentByDots&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;child&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IndentWithDot&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&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;Let's see the code in action!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FIndentByDot.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FIndentByDot.gif" alt="here"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though there is nothing wrong with class components, they can be quite verbose and heavyweight so let's compare how this code looks when optimised with the &lt;code&gt;useState&lt;/code&gt; hook and Functional Components!&lt;/p&gt;

&lt;h3&gt;
  
  
  The useState Hook!
&lt;/h3&gt;

&lt;p&gt;Prior to &lt;a href="https://reactjs.org/blog/2019/02/06/react-v16.8.0.html" rel="noopener noreferrer"&gt;React 16.8&lt;/a&gt;, functional components could not hold a &lt;code&gt;state&lt;/code&gt;. Luckily, this is no longer the case as we can now use &lt;a href="https://reactjs.org/docs/hooks-state.html" rel="noopener noreferrer"&gt;React Hooks&lt;/a&gt; that include the power of &lt;code&gt;useState&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Before this, one of the key differences between them was that functional components lacked the capability to hold an abstracted &lt;code&gt;state&lt;/code&gt; property. With the introduction of the &lt;code&gt;useState&lt;/code&gt; hook there is now an alternative to this. :)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="py"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="py"&gt;setWord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&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 example above shows a simple &lt;code&gt;useState&lt;/code&gt; variable of type &lt;code&gt;String&lt;/code&gt;. The default value is initialised in the parameters of the &lt;code&gt;useState&lt;/code&gt; function — i.e &lt;code&gt;useState("hello")&lt;/code&gt;, this would declare the &lt;code&gt;getter&lt;/code&gt; value as &lt;code&gt;"hello"&lt;/code&gt;. To update the value of the &lt;code&gt;word&lt;/code&gt; we use the function &lt;code&gt;setWord("World")&lt;/code&gt;. Essentially, &lt;code&gt;word&lt;/code&gt; is the getter and &lt;code&gt;setWord&lt;/code&gt; is the setter.&lt;/p&gt;

&lt;p&gt;We can actually tidy up this logic further with delegation using the &lt;a href="https://kotlinlang.org/docs/delegated-properties.html" rel="noopener noreferrer"&gt;by&lt;/a&gt; keyword to delegate the &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; of &lt;code&gt;useState&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;wordState&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To benefit from delegation, we need to convert the way we instantiate the state variable. To have &lt;code&gt;state&lt;/code&gt; capability, the type of the property needs to become mutable — i.e. &lt;code&gt;val&lt;/code&gt; to &lt;code&gt;var&lt;/code&gt;. There is also no need to keep two properties for &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; either. Renaming the variable is important as it has a hidden superpower.&lt;/p&gt;

&lt;p&gt;Here @sKalable our preference is to give it a suffix named &lt;code&gt;State&lt;/code&gt; for more clarity around our code and hidden functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  State in Functional Components
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;lets refactor our Class Component to a Functional Component!&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * [indentWithDot] is a react [functionalComponent]. This type of component is not
 * lifecycle aware and is more lightweight than a class component [RComponent].
 */&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;indentWithDot&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;functionalComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RProps&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/**
         *  To initialise the state within the function component we need to
         *  declare the [useState]s as the first variables in the function. Doing
         *  so ensures the variables are available for the rest of the code within
         *  the function.
         *
         *  Using the `by` keyword allows for delegation of the get and set of [useState]
         *  into the indentState var.
         *
         *  @see IndentState for state values
         */&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;indentState&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;IndentState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;IndentState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;indentAmount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;indentationValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"."&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="cm"&gt;/**
         *  In a [functionalComponent] (FC) the last code block should always be the HTML to
         *  render. Compared to a class component, there is no RBuilder.render() as the HTML
         *  at the end of the function is what gets rendered. A FCs first param is a lambda extending
         *  from [RBuilder] itself so RBuilder.render() is not required.
         *
         *  As we can see, the [button] we render within [div] has an [onClickFunction] attribute that
         *  handles click events.
         *
         *  Here, when handled, we update the [IndentState.indentAmount] by adding one.
         *  [IndentState.indentationValue] is then updated by adding a number of "."s equal
         *  to the amount of [IndentState.indentAmount].
         *
         *  This value is then reflected in the text of the button.
         */&lt;/span&gt;
        &lt;span class="nf"&gt;div&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="cm"&gt;/**
                 * Update the string using the values from [IndentState.indentationValue] and [IndentState.indentAmount]
                 */&lt;/span&gt;
                &lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"press me to add another dot indent from FC ${indentState.indentationValue} ${indentState.indentAmount}"&lt;/span&gt;
                &lt;span class="nf"&gt;attrs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;onClickFunction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;indentState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;IndentState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="cm"&gt;/**
                             * reference the value of the [IndentState.indentAmount] and increment by one.
                             * The value of [IndentState.indentationValue] is then updated with a number of "."s
                             * equal to the new amount of [IndentState.indentAmount]
                             */&lt;/span&gt;
                            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;indentAmount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;indentState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indentAmount&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;indentationValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indentAmount&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;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * ReactBuilder function used to construct the React Component IndentWithDot.
 */&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;RBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indentByDotsFC&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;child&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indentWithDot&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;Running the code again we can see it works exactly the same as before, except with much less boilerplate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FIndentByDotFC.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520State%2520%2FIndentByDotFC.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There we have it, two approaches to using state in both a Class and Functional Component!&lt;/p&gt;

&lt;h2&gt;
  
  
  To Summarise
&lt;/h2&gt;

&lt;p&gt;Effective code is clean and readable code. Also, you might be wondering how to handle multiple states? We cover this in Part 2 of KotlinJS and State Hooks!&lt;/p&gt;

&lt;p&gt;As always, you can find the sample project for the above &lt;a href="https://github.com/skalable-samples/KotlinJS-and-React-State" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for taking your time to learn with us! Feel free to reach out and say hello. &lt;/p&gt;

&lt;p&gt;@sKalable we are a Kotlin-centric agency that builds code to ensure it is &lt;em&gt;Maintainable&lt;/em&gt;, &lt;em&gt;Flexible&lt;/em&gt; and of course, &lt;em&gt;sKalable&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Follow us on &lt;a href="https://twitter.com/skalable_dev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and &lt;a href="https://dev.to/skalabledev"&gt;Dev.to&lt;/a&gt; and &lt;a href="https://www.linkedin.com/company/skalable-dev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; to get the latest on Kotlin Multiplatform for your business or personal needs. &lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>react</category>
      <category>codequality</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Global Applying Kotlin Styled CSS</title>
      <dc:creator>skalable-dev</dc:creator>
      <pubDate>Fri, 26 Feb 2021 11:35:56 +0000</pubDate>
      <link>https://dev.to/skalabledev/global-applying-kotlin-styled-css-2i89</link>
      <guid>https://dev.to/skalabledev/global-applying-kotlin-styled-css-2i89</guid>
      <description>&lt;p&gt;Here at sKalable we love all things Kotlin. We use it for working on the FrontEnd, BackEnd and everywhere in between.&lt;/p&gt;

&lt;p&gt;We are particularly excited about making code as clean and easy to use as possible within the Kotlin ecosystem. Enabling the formation of an environment that encourages engineers to develop fast, all while applying effective and understandable patterns. We are very passionate about that @sKalable.&lt;/p&gt;

&lt;p&gt;Today we will be covering Global Styles in CSS in a KotlinJS project.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To begin we need to see what the outlook of what we would like to achieve feels like.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS%2520Overview_Global%2520CSS%2520KotlinJS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS%2520Overview_Global%2520CSS%2520KotlinJS.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When looking at the above diagram it shows that we are looking to apply the &lt;code&gt;Img CSS&lt;/code&gt; to every image living in the body of the project. &lt;/p&gt;

&lt;h2&gt;
  
  
  Getting a base Setup
&lt;/h2&gt;

&lt;p&gt;Sometimes the best place to start is with the basics. For this example we will create a base component of just an &lt;code&gt;img&lt;/code&gt; within our document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onload&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&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="s"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="cm"&gt;/**
             * Load our image in the body without any CSS applied directly
             */&lt;/span&gt;
            &lt;span class="nf"&gt;img&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;alt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Skalable Logo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"YOUR IMAGE URL"&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;Running the above the following is rendered. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS-01%2520.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS-01%2520.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A little on the large side to say the least. We can see it takes up the entirety of the main page. While this might be what you're looking for in some cases it definitely shouldn't be the default. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;So how do we address this for all images?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Going Global
&lt;/h2&gt;

&lt;p&gt;Applying global styles can have a huge range of benefits for your project. You can decide default styles and fonts for your basic tags such as &lt;code&gt;p&lt;/code&gt; or &lt;code&gt;h1&lt;/code&gt; &lt;code&gt;h2&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;To add a global style for your project there are a few approaches. What we tend to prefer @sKalable is &lt;a href="https://en.wikipedia.org/wiki/Single-responsibility_principle" rel="noopener noreferrer"&gt;SRP&lt;/a&gt; - &lt;em&gt;"A class should have only one reason to change"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The message here is to group your logic into separate areas of concern. To achieve this with styles we can create a &lt;code&gt;GlobalStyles&lt;/code&gt; object that manages all our global styles in the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;GlobalStyles&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GlobalStyles"&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;While currently bare, we need to look to the ever brilliant &lt;a href="https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-styled" rel="noopener noreferrer"&gt;Kotlin-Styled&lt;/a&gt; library for KotlinJS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;at the time of writing the global style approach is non-functional with the listed approach within the README.md&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Populating our stylesheet with our global style is easy as &lt;a href="https://en.wikipedia.org/wiki/Pi" rel="noopener noreferrer"&gt;&lt;code&gt;3.14&lt;/code&gt;&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;We need to begin by declaring our initial style for the body of our page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;GlobalStyles&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GlobalStyles"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;styles&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CSSBuilder&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="cm"&gt;/**
             * Gives the body the following css globally (there is only ever one body)
             *
             *  - Sets the maxWidth with to the size available for the content.
             *  - Adds Auto Margin to the body allowing everything to be centered.
             *  - Creates a padding of 100px all around the body
             */&lt;/span&gt;
            &lt;span class="n"&gt;maxWidth&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LinearDimension&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxContent&lt;/span&gt;
            &lt;span class="nf"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LinearDimension&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="cm"&gt;/**
             * We can visualise this much better if we give the background a grey colour.
             */&lt;/span&gt;
            &lt;span class="n"&gt;backgroundColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;darkGray&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;This won't apply the style &lt;em&gt;just yet&lt;/em&gt;, first we need to inject it into the &lt;a href="https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-styled" rel="noopener noreferrer"&gt;Kotlin-Styled&lt;/a&gt; global setter. Without causing confusion and to abide by &lt;a href="https://en.wikipedia.org/wiki/Single-responsibility_principle" rel="noopener noreferrer"&gt;SRP&lt;/a&gt; we can create the handler function for this directly in the stylesheet. Doing so will allow for all the styles that are in the &lt;code&gt;GlobalStyles&lt;/code&gt; to remain private.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Inside GlobalStyles at the bottom &lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * By creating a wrapper function in our Global Styles, it gives
     * the global style sheet the capability to have the styles set as private.
     */&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;applyGlobalStyle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;injectGlobal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;So when do we use this new &lt;code&gt;applyGlobalStyle&lt;/code&gt; function? &lt;/p&gt;

&lt;p&gt;Directly in our &lt;code&gt;client&lt;/code&gt; class is where!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/**
     * Using our global styles object we
     * can call our wrapper function to apply
     * the global styles to our project.
     */&lt;/span&gt;
    &lt;span class="nc"&gt;GlobalStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;applyGlobalStyle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onload&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&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="s"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="cm"&gt;/**
             * Load our image in the body without any CSS applied directly
             */&lt;/span&gt;
            &lt;span class="nf"&gt;img&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;alt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Skalable Logo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"YOUR IMAGE URL"&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;Applying and running the above gives us the following result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS-02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS-02.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know, yuck. What the hell is going on here. There is one huge positive in all of this, the style was &lt;em&gt;applied&lt;/em&gt;. We haven't done anything with the image in particular in all of this. Time to get specific. &lt;/p&gt;

&lt;h2&gt;
  
  
  Applying Global CSS to individual components
&lt;/h2&gt;

&lt;p&gt;Back in our &lt;code&gt;GlobalStyles&lt;/code&gt; object we are going to add an &lt;code&gt;img&lt;/code&gt; tag directly in the &lt;code&gt;body&lt;/code&gt; and apply some &lt;code&gt;css&lt;/code&gt; to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="c1"&gt;// CSS for Body Tag&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * When you want to apply a base CSS style to components within the
     * body its as simple as nesting that component within and setting
     * the CSS that you like.
     */&lt;/span&gt;
    &lt;span class="nf"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;maxWidth&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;px&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 where getting somewhere. We have applied the image css to all &lt;code&gt;img&lt;/code&gt; tags within our project. Awesome! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS-03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlin%2520JS%2520Global%2520Styles%2FGlobal%2520CSS-03.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimise for scalability
&lt;/h2&gt;

&lt;p&gt;Every web engineer knows how important it is to keep your &lt;code&gt;css&lt;/code&gt; organised and clean. The best way to apply this philosophy is by &lt;em&gt;starting&lt;/em&gt; with it. Since we have everything working correctly lets abstract the individual &lt;code&gt;css&lt;/code&gt; for each component to keep readability high and keep things &lt;code&gt;sKalable&lt;/code&gt; &lt;em&gt;(see what I did there?)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Back in our &lt;code&gt;GlobalStyles&lt;/code&gt; we are going to abstract the image style into its own &lt;code&gt;RuleSet&lt;/code&gt;. After we will apply it back into the &lt;code&gt;img&lt;/code&gt; tag within the &lt;code&gt;body&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Inside GlobalStyles below styles val.&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Im order to not overwhelm your fellow engineers, we can
     * extract styles into separate CSS components.
     *
     * This is particularly useful when you have quite a few different styles.
     */&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;baseImg&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;maxWidth&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;px&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've abstracted the &lt;code&gt;img&lt;/code&gt; css we can add it to the &lt;code&gt;body&lt;/code&gt; with a simple &lt;code&gt;+&lt;/code&gt; operator also known as &lt;a href="https://kotlinlang.org/docs/operator-overloading.html#unary-prefix-operators" rel="noopener noreferrer"&gt;UnaryPlus&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="c1"&gt;// CSS for Body Tag&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * When you want to apply a base CSS style to components within the
     * body its as simple as nesting that component within and setting
     * the CSS that you like.
     */&lt;/span&gt;
    &lt;span class="nf"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;baseImg&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;There we are, a global stylesheet that will grow with your projects needs.&lt;/p&gt;

&lt;p&gt;Feel free to comment or get in touch. @sKalable we are a Kotlin agency that builds code the way it should be built to ensure it is &lt;em&gt;Maintainable&lt;/em&gt;, &lt;em&gt;Customisable&lt;/em&gt;, &lt;em&gt;Flexible&lt;/em&gt; and &lt;code&gt;sKalable&lt;/code&gt; &lt;em&gt;(I did it again.)&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Check out the sample project &lt;a href="https://github.com/skalable-samples/KotlinJS-Global-Stylesheet-with-Kotlin-Styled" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow us on &lt;a href="https://twitter.com/skalable_dev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or &lt;a href="https://dev.to/skalabledev"&gt;Dev.to&lt;/a&gt; to get the latest on Kotlin Multi Platform for your businesses needs. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Injectable Kotlin Styles</title>
      <dc:creator>skalable-dev</dc:creator>
      <pubDate>Tue, 23 Feb 2021 23:01:25 +0000</pubDate>
      <link>https://dev.to/skalabledev/injectable-kotlin-styles-3h0i</link>
      <guid>https://dev.to/skalabledev/injectable-kotlin-styles-3h0i</guid>
      <description>&lt;h1&gt;
  
  
  Custom Component with Injected Kotlin Styled CSS
&lt;/h1&gt;

&lt;p&gt;At sKalable we love Kotlin! We work with it at both the frontend and backend.&lt;/p&gt;

&lt;p&gt;One aspect of Kotlin we are most excited by in terms of potential is Kotlin Multiplatform (KMM). We have recently been doing some work on improving the usability of KMM in a way that creates a friendly environment for engineers working on building web apps using Kotlin. This article helps to explain what we have built.&lt;/p&gt;

&lt;p&gt;Reusing components is no easy feat for newcomers, it can take a deep understanding of various frameworks and patterns of development. When building a generic style systems that can change the look and feel of a component in most web frameworks, they are normally created in a separate &lt;code&gt;.ccs&lt;/code&gt; or &lt;code&gt;.scss&lt;/code&gt; file. These styles are then imported where needed, but what if you wanted to keep it all in the same language? While there are quite a lot of examples around this scenario, the same cannot be said for the incredible &lt;a href="https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-styled" rel="noopener noreferrer"&gt;Kotlin-Styled&lt;/a&gt; framework.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's outline what we would like to achieve:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Custom%2520Style%2520Injection%2FCustom%2520Injected%2520CSS-01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Custom%2520Style%2520Injection%2FCustom%2520Injected%2520CSS-01.png" alt="Injectable Kotlin CSS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While in theory we can just inject the name of the style in our &lt;code&gt;.css&lt;/code&gt; file that we wish to apply to the custom &lt;code&gt;KotlinJS&lt;/code&gt; component as a &lt;code&gt;String&lt;/code&gt;. We don't really get the same benefit or capability we would by directly accessing the object itself. Following this approach, it also creates more boilerplate from managing constant strings of the style names and reducing re-usability.&lt;/p&gt;

&lt;p&gt;Let us look at how our Kotlin Stylesheet is constructed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentStyles&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"CustomComponentStyles"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/**
    * A custom style declared directly in Kotlin using styled!
    */&lt;/span&gt;
   &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;cool&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//Custom style&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;To first understand what we need to do we need to look at what &lt;code&gt;css&lt;/code&gt; does itself in Kotlin Styled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;vararg&lt;/span&gt; &lt;span class="n"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RuleSet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RuleSet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CssHolder&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="p"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While it looks like this just returns a &lt;code&gt;CssHolder&lt;/code&gt;, in reality what we get is a &lt;code&gt;RuleSet&lt;/code&gt;. The delegation happens when using the &lt;a href="https://kotlinlang.org/docs/delegated%20properties.html" rel="noopener noreferrer"&gt;by&lt;/a&gt; key in our Kotlin Stylesheet. This allows us to use the &lt;code&gt;css&lt;/code&gt; getter when applying the values to our &lt;code&gt;cool&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;A deeper look into the &lt;code&gt;CssHolder&lt;/code&gt; reveals the truth of the situation. In overloaded operator on the &lt;code&gt;getValue&lt;/code&gt; function we can see it returns a &lt;code&gt;RuleSet&lt;/code&gt;. A funny situation to be in when you expected a &lt;code&gt;CssHolder&lt;/code&gt; unknowingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;RuleSet&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;
  
  
  Extension Functions to the rescue!
&lt;/h2&gt;

&lt;p&gt;With this newfound knowledge we can begin to make our code scale in a way that reads well and causes less confusion for everyone.&lt;/p&gt;

&lt;p&gt;To begin we need to analyse what type does a &lt;code&gt;CustomStyledProps&lt;/code&gt; expect for &lt;code&gt;css&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CustomStyledProps&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RuleSet&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get an &lt;code&gt;ArrayList&lt;/code&gt; of type &lt;code&gt;RuleSet&lt;/code&gt; which is nullable, our issue is that it's nullable. While some might say, why is that an issue? Readability. That's why.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's get building!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Personally I tend to keep all my &lt;a href="https://kotlinlang.org/docs/extensions.html" rel="noopener noreferrer"&gt;extension functions&lt;/a&gt; in an &lt;code&gt;util&lt;/code&gt; directory. So let us create a file in there called &lt;code&gt;CssExt.kt&lt;/code&gt;. This is where our custom CSS extension functions will live.&lt;/p&gt;

&lt;p&gt;Within this file create a function extending from the &lt;code&gt;RuleSet&lt;/code&gt; itself. This function will translate the &lt;code&gt;RuleSet&lt;/code&gt; itself into an &lt;code&gt;ArrayList&amp;lt;Ruleset&amp;gt;&lt;/code&gt; object that is non nullable as to use it RuleSet must always exist. In this instance we called the function &lt;code&gt;toCss()&lt;/code&gt; to make reading the code more intuitive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;RuleSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toCss&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RuleSet&lt;/span&gt;&lt;span class="p"&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="nf"&gt;arrayListOf&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While we have a way of adding the &lt;code&gt;ArrayList&amp;lt;RuleSet&amp;gt;&lt;/code&gt; to the property as a list, we need to be able to also convert it back into a single &lt;code&gt;RuleSet&lt;/code&gt;. This brings us back to our Nullability issue. Before we create the second extension to our &lt;code&gt;CssExt.kt&lt;/code&gt; file, create a &lt;code&gt;GlobalStyles&lt;/code&gt; file with a single empty style within.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;GlobalStyles&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GlobalStyles"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="cm"&gt;/**
    * Rather than setting null we will use an empty style.
    */&lt;/span&gt;
   &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;empty&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;css&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 we can use the power of the &lt;a href="http://kotlin-quick-reference.com/156-R-elvis-operator.html" rel="noopener noreferrer"&gt;Elvis operator&lt;/a&gt;&lt;code&gt;?:&lt;/code&gt; &lt;em&gt;(Turn your head sideways to see elvis)&lt;/em&gt; we can add another extension function to give us a guarantee the &lt;code&gt;RuleSet&lt;/code&gt; will have a valid value in our extension. This will be based on the &lt;code&gt;CustomStyledProps&lt;/code&gt; interface itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;CustomStyledProps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;RuleSet&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;css&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;GlobalStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a Custom Component
&lt;/h2&gt;

&lt;p&gt;Before we use our new extensions and &lt;code&gt;CustomSyledProps&lt;/code&gt; though, we need to create a Custom KotlinJS component. &lt;em&gt;(For this example &lt;code&gt;state&lt;/code&gt; is here for extensibility purposes in the future)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;react.*&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;styled.CustomStyledProps&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;styled.css&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;styled.styledDiv&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;util.css&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * We use an interface to inject in props. It allows us to create
 * clean DSL builders for our custom components.
 *
 * By extending form CustomStyledProps we can benefit from adding CSS
 * directly through our props.
 */&lt;/span&gt;
&lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentProps&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CustomStyledProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * An interface is also used as a state
 * so we can maintain the state of a component.
 */&lt;/span&gt;
&lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentState&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * We extend from RComponent and tell it the types of Props and State to expect internally.
 *
 * This is our custom component.
 */&lt;/span&gt;
&lt;span class="nd"&gt;@JsExport&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CustomComponentProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="cm"&gt;/**
    * To begin, we set the initial state to the name in the prop we injected.
    */&lt;/span&gt;
   &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;RBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;styledDiv&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nf"&gt;css&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="cm"&gt;/**
             * We make use of our CustomStyledProps extension function by
             * setting the from the returned value RuleSet.
             */&lt;/span&gt;
            &lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&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="s"&gt;"Hello there ${state.name} from your very own custom component!"&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="cm"&gt;/**
 * Using an extension function on RBuilder we can construct our DSL.
 *
 * Here we apply each variable within the props to the child class of our Custom component,
 * Setting each as an attribute of the component.
 *
 */&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;RBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;customComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentProps&lt;/span&gt;&lt;span class="p"&gt;.()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;child&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CustomComponent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;attrs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handler&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;Within the &lt;code&gt;styledDiv&lt;/code&gt; there is a &lt;code&gt;css&lt;/code&gt; section. In here we can embed any custom styles we desire. Using our extension &lt;code&gt;css()&lt;/code&gt; we can also call upon the props to fetch the required &lt;code&gt;RuleSet&lt;/code&gt; that we have injected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;The extension function at the bottom of the file provides a clean way of building our custom component. Let's see our new utility functions in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Our custom DSL to create the CustomComponent.
 * Here we use the cool style
 * @see CustomComponentStyles.cool
 */&lt;/span&gt;
&lt;span class="nf"&gt;customComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/**
    * Using our extension function we can create the desired list
    * that the CustomStyledProps requires.
    * @see CustomComponentProps.css
    */&lt;/span&gt;
   &lt;span class="n"&gt;css&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toCss&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Sean"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * The same component but this time
 * we use the cooler style
 * @see CustomComponentStyles.cooler
 */&lt;/span&gt;
&lt;span class="nf"&gt;customComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;css&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CustomComponentStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cooler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toCss&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Seb"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;.toCss()&lt;/code&gt; utilises our extension to create the desired output for the &lt;code&gt;CustomStyledProps&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When rendered, we get the following result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Custom%2520Style%2520Injection%2FCustom%2520Injected%2520CSS-02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Fskalable.appspot.com%2FKotlinJS%2520Custom%2520Style%2520Injection%2FCustom%2520Injected%2520CSS-02.png" alt="result output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One component, two different styles, no strings, all linked directly with the Power of Kotlin!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Find the sample project for this approach hosted &lt;a href="https://github.com/skalable-samples/KotlinJS-Custom-Component-with-Kotlin-Styled-Injection" rel="noopener noreferrer"&gt;@here&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Best of luck from all @ the sKalable team 😎&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>css</category>
      <category>javascript</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
