<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Donovan's Blog - Hop.ie</title>
    <description>Front-end development blog</description>
    <link>http://hop.ie</link>
    <atom:link href="http://hop.ie/feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>SwiftUI Styling for Design Systems</title>
        <description>&lt;p&gt;When working with design systems as engineers, we want to create styles that match the design resources while being composable and easy to use across many variations in our UI. In this talk &lt;a href=&quot;https://danieltull.co.uk&quot;&gt;Daniel Tull&lt;/a&gt; explains how we can make use of a SwiftUI approach to build extensible design systems without creating a load of difficult-to-manage custom views.&lt;/p&gt;

&lt;p&gt;More talks &lt;a href=&quot;https://www.youtube.com/@danielctull&quot;&gt;are available from Daniel Tull here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/embed/Z7rJKr6Jlho&quot;&gt;&lt;img src=&quot;https://img.youtube.com/vi/Z7rJKr6Jlho/hqdefault.jpg&quot; alt=&quot;Watch the talk&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can &lt;a href=&quot;https://www.youtube.com/embed/Z7rJKr6Jlho&quot;&gt;watch the talk on Youtube here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Check out the useful post on Moving Parts also, &lt;a href=&quot;https://movingparts.io/styling-components-in-swiftui&quot;&gt;Styling Components in SwiftUI&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;code&quot;&gt;Code&lt;/h2&gt;

&lt;p&gt;You can &lt;a href=&quot;https://github.com/donovanh/swift-design-system/tree/main&quot;&gt;download an Xcode project with the code&lt;/a&gt; from this talk.&lt;/p&gt;

&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;/h2&gt;

&lt;p&gt;This post is mostly my notes from the talk, as well as code examples shown during the presentation.&lt;/p&gt;

&lt;h2 id=&quot;what-is-a-design-system&quot;&gt;What is a Design System&lt;/h2&gt;

&lt;p&gt;Design systems are a combination of processes, documentation, common resources and functional libraries.&lt;/p&gt;

&lt;p&gt;A challenge is keeping these resources aligned with the way we develop in Swift apps, but thankfully Swift offers approaches that can make it easier to create shared vocabularity and components.&lt;/p&gt;

&lt;h2 id=&quot;a-common-approach-custom-button&quot;&gt;A common approach: Custom button&lt;/h2&gt;

&lt;p&gt;In Swift we can might want to create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primary&lt;/code&gt; button by creating a custom component such as:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryButton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;RoundedRectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cornerRadius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/simple-button-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This produces a button with a blue background as expected. However, there’s a downside to this approach. By creating custom components for each use-case, we end up with a lot of components to manage and keep track of.&lt;/p&gt;

&lt;p&gt;The above code also doesn’t cover some issues, such as tap state, accessibility and more.&lt;/p&gt;

&lt;p&gt;Another weakness of the above is when someone then decides we need a button with an image. We would create a second component, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrimaryImageButton&lt;/code&gt;. Over time this becomes tricky to scale and maintain and is a barrier to discovery.&lt;/p&gt;

&lt;h2 id=&quot;using-buttonstyle&quot;&gt;Using buttonStyle&lt;/h2&gt;

&lt;p&gt;Instead of making custom components, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStyle&lt;/code&gt; is availble as a way of adding the styling we need to existing button components, rather than creating new components.&lt;/p&gt;

&lt;p&gt;An example of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primary&lt;/code&gt; button style similar to the above might look like this:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryButtonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ButtonStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;RoundedRectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cornerRadius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This makes use of the same styling but instead of applying it to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt;, we make use of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;label&lt;/code&gt; that is passed to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makeBody&lt;/code&gt; method in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configuration&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configuration&lt;/code&gt; also includes other angles such as tap events which can be configured here too.&lt;/p&gt;

&lt;p&gt;This could then be applied to a button using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStyle&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;Button Text&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PrimaryButtonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;improving-discoverability&quot;&gt;Improving discoverability&lt;/h3&gt;

&lt;p&gt;To make it easier to discover this style, it’s possible to define static variable on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStyle&lt;/code&gt; that extend it like so:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ButtonStyle&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryButtonStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above sets a static variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primary&lt;/code&gt; that we can then apply to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; by pressing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; and it’ll suggest our static variable as an option:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;Button Text&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above also supports any other &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; usage out of the box, such as using it with an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt; label or text alongside images.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/button-with-image-and-text-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;extending-label-styling&quot;&gt;Extending Label styling&lt;/h2&gt;

&lt;p&gt;It’s possible to customise the styling of buttons further, such as adding space between the text and image on a button with both.&lt;/p&gt;

&lt;p&gt;We would create such a button using both text and an image:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;Button Text&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;systemImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/button-with-image-and-text-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this case, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; component is making use of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Label&lt;/code&gt; version that accepts both text and image inputs corresponding to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Title&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Icon&lt;/code&gt; view.&lt;/p&gt;

&lt;p&gt;An example of such a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Label&lt;/code&gt; used directly looks like:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Text&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;labelstyle&quot;&gt;labelStyle&lt;/h3&gt;

&lt;p&gt;Similar to creating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStyle&lt;/code&gt; we can implement a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;labelStyle&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryButtonLabelStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LabelStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;HStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;spacing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;icon&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This styles the text and image to have a large amount of space between them.&lt;/p&gt;

&lt;p&gt;We can use this as above to create an extension for a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primary&lt;/code&gt; label style, or we can even use this directly within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrimaryButtonStyle&lt;/code&gt;. This would mean any button using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.primary&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStyle&lt;/code&gt; could make use of the above when using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;label&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryButtonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ButtonStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;RoundedRectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cornerRadius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;labelStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PrimaryButtonLabelStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above we add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.labelStyle&lt;/code&gt; line and this will apply to any label that appears in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.primary&lt;/code&gt; buttonStyle button.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Text&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/button-with-spacing-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;applying-our-style-to-custom-components&quot;&gt;Applying our style to custom components&lt;/h2&gt;

&lt;p&gt;So we can add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;labelStyle&lt;/code&gt; to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrimaryButtonStyle&lt;/code&gt; but what if we have our own custom components to be used within a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; and we want to have them handled by our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrimaryButtonStyle&lt;/code&gt; too?&lt;/p&gt;

&lt;p&gt;An example could be a button styled to have three different lines of text.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/detail-button-styled-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A less-ideal way to achieve this is to create a custom &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VerticalTextButton&lt;/code&gt; view containing a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VStack&lt;/code&gt; of text. But as above, making new buttons is not the best approach. It lacks discoverability but also makes it difficult to customise the content.&lt;/p&gt;

&lt;p&gt;An approach would be to create our own approach, which would have it’s own default style that can be extended in the same way as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStyle&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;labelStyle&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;semantic-container&quot;&gt;Semantic container&lt;/h3&gt;

&lt;p&gt;Much as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Label&lt;/code&gt; could be described as a semantic container - a component that takes an icon and text and lets &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;labelStyle&lt;/code&gt; decide how it looks, we can create our own container within which to create thie 3-line text button design.&lt;/p&gt;

&lt;p&gt;For the sake of this example, the semantic container being created is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt;, and it accepts a set of three views: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Title&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Subtitle&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Caption&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code for this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; is like this:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;

  &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;VStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view takes the three value views, and contains a simple initialiser and gives us back the three views as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VStack&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To set this up to receive our styling, we create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;protocol&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;protocol&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DynamicProperty&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;typealias&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyleConfiguration&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;associatedtype&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Body&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Body&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;configuring-with-anyview&quot;&gt;Configuring with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyView&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;We then set up configuration to pass each of the child views out to the caller for use when styling. It looks like this:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyleConfiguration&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AnyView&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AnyView&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AnyView&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;fileprivate&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AnyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AnyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AnyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This configuration is wrapping each of the given views in &lt;a href=&quot;https://developer.apple.com/documentation/swiftui/anyview&quot;&gt;AnyView&lt;/a&gt;, which allows the view’s type to be dynamically updated when the configuration is used for styling.&lt;/p&gt;

&lt;p&gt;This helps protect the component’s data from being exposed to the styling but mainly is set up this way to work with how generics in Swift are handled through protocols.&lt;/p&gt;

&lt;h4 id=&quot;adding-to-the-environment&quot;&gt;Adding to the environment&lt;/h4&gt;

&lt;p&gt;Next, to prepare for applying the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;detailStyle&lt;/code&gt; across the environment, the following code sets up a default “plain” style &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EnvironmentKey&lt;/code&gt; and extends &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EnvironmentValues&lt;/code&gt; to be able to get and set this key, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;View&lt;/code&gt; component to make use of it:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyleKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;EnvironmentKey&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;defaultValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PlainDetailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;EnvironmentValues&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;fileprivate&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;detailStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;DetailStyleKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;DetailStyleKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newValue&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;detailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(\&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;detailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;provide-a-default-style&quot;&gt;Provide a default style&lt;/h4&gt;

&lt;p&gt;With the mechanism in place to apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;detailStyle&lt;/code&gt; across views, we then define the default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlainDetailStyle&lt;/code&gt; mentioned above:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PlainDetailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;VStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;light&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This default exists mostly as a template for other users of this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view so that others can then create their own styles.&lt;/p&gt;

&lt;p&gt;It’ll also allow us to remove the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VStack&lt;/code&gt; we used above when initially setting up the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view. But first we need to create a way of properly applying the style across views.&lt;/p&gt;

&lt;h4 id=&quot;resolve-style&quot;&gt;Resolve style&lt;/h4&gt;

&lt;p&gt;In the blog post &lt;a href=&quot;https://movingparts.io/styling-components-in-swiftui&quot;&gt;Styling Components in SwiftUI&lt;/a&gt; by &lt;a href=&quot;https://movingparts.io&quot;&gt;Moving Parts&lt;/a&gt;, an approach to resolving styles in order to apply them to components is set out. A similar method can be set up to resolve the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DetailStyle&lt;/code&gt; here:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;fileprivate&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;ResolvedDetailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ResolvedDetailStyle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Style&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This lets use use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;environment&lt;/code&gt; properties, such as when a button is enabled or disabled. By extending our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DetailStyle&lt;/code&gt; property we can ensure these environment properties are available within to be used our styles.&lt;/p&gt;

&lt;p&gt;The above approach, which calls resolve in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DetailStyle&lt;/code&gt;, can resolve it’s type at the point at which it is used so that the right style can be applied alongside keeping these environment values available.&lt;/p&gt;

&lt;h4 id=&quot;applying-style-to-detail&quot;&gt;Applying style to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;With these build blocks in place we can revisit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view code from earlier and update it to make use of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;detailStyle&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;@Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(\&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;detailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;style&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;

  &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Caption&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyleConfiguration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;AnyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case we have added a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Environment&lt;/code&gt; variable, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;style&lt;/code&gt;, then within the body, used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DetailStyleConfiguration&lt;/code&gt; to create the configuration, and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;style.resolve&lt;/code&gt; to resolve this style into an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyView&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;default-style&quot;&gt;Default style&lt;/h4&gt;

&lt;p&gt;We can now see all this in action:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/detail-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;creating-primarybuttondetailstyle&quot;&gt;Creating PrimaryButtonDetailStyle&lt;/h4&gt;

&lt;p&gt;Now that we have a default style being applied, we can align it with the previous &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ButtonStyle&lt;/code&gt; approach by creating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Primary&lt;/code&gt; style:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryButtonDetailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DetailStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;VStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtitle&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;light&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is similar to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlainDetailStyle&lt;/code&gt; above but contains an additional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alignment&lt;/code&gt; to make the text items left-align.&lt;/p&gt;

&lt;p&gt;We can then add this to the ` PrimaryButtonStyle``, to apply any time we use a  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt;inside a&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryButtonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ButtonStyle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeBody&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;RoundedRectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cornerRadius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;labelStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PrimaryButtonLabelStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;detailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PrimaryButtonDetailStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;using-detail-in-a-button&quot;&gt;Using Detail in a Button&lt;/h4&gt;

&lt;p&gt;We can then use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; and have it make use of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.primary&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buttonStyle&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/detail-button-styled-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;convenience-initialiser&quot;&gt;Convenience initialiser&lt;/h3&gt;

&lt;p&gt;We don’t want to make developers of the design system remember to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view each time. A convenience initialiser can be used that accepts the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;title&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subtitle&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caption&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LocalizedStringKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LocalizedStringKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LocalizedStringKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Void&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
              &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
              &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
              &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This would then be simpler to use:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;nesting-styles&quot;&gt;Nesting styles&lt;/h3&gt;

&lt;p&gt;Using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;label&lt;/code&gt; within the styled &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; even works, applying the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;labelStyle&lt;/code&gt; to apply space between the image and the “text” view, where the “text” is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This can be also wrapped in a convenience initialiser as above, so avoid having to specify the usage of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Detail&lt;/code&gt; view:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SwiftUI&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LocalizedStringKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LocalizedStringKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LocalizedStringKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;systemImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Void&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Detail&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;systemImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This could result in a simpler usage such as this:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;systemImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;making-views-optional&quot;&gt;Making views optional&lt;/h2&gt;

&lt;p&gt;If you want to have flexibility to not have, for example, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caption&lt;/code&gt;, you can pass in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EmptyView()&lt;/code&gt; to have it not show. Adding this inside a convenience initializer that simply doesn’t have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caption&lt;/code&gt; in it, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caption&lt;/code&gt; can be omitted from the above and it will just work as expected.&lt;/p&gt;

&lt;h2 id=&quot;examples-of-the-button-in-use&quot;&gt;Examples of the Button in use&lt;/h2&gt;

&lt;p&gt;With these initialisers and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.primary&lt;/code&gt; Button style, some examples of use could look like this:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Simple Button&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Button with image&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Button with text and image&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Label&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Text&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Button with title and subtitle&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Button with title and subtitle and caption&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Button with title and subtitle and caption and image&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;subtitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Subtitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Caption&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;systemImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square.and.arrow.up&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;buttonStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/design-system-swift/all-examples-min.png&quot; alt=&quot;Primary Button example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can &lt;a href=&quot;https://github.com/donovanh/swift-design-system/tree/main&quot;&gt;download an Xcode project with the code&lt;/a&gt; from this talk.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Make use of custom styles rather than create views for each use cases. As the styles are composable, the styles can be nested for different contexts.&lt;/p&gt;

&lt;p&gt;Then make use of semantic containers for custom components as they can be seamlessly combined with the composable styles.&lt;/p&gt;

&lt;p&gt;Then make the standard types easier to use by using extensions to provide simpler APIs which handle the custom components.&lt;/p&gt;

&lt;h2 id=&quot;well-thats-enough-about-me-your-turn&quot;&gt;Well that’s enough about me. Your turn!&lt;/h2&gt;

&lt;p&gt;Got any useful SwiftUI tips to share or questions about this post? You can message me &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;, I’d love to hear from you.&lt;/p&gt;
</description>
        <pubDate>Mon, 09 Oct 2023 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/design-system-swift/</link>
        <guid isPermaLink="true">http://hop.ie/blog/design-system-swift/</guid>
      </item>
    
      <item>
        <title>TypeScript: Course notes</title>
        <description>&lt;p&gt;I decided to take a look at &lt;a href=&quot;https://www.typescriptlang.org&quot;&gt;TypeScript&lt;/a&gt; this year. As I often do, I turn to Udemy for helpful courses and so found &lt;a href=&quot;https://www.udemy.com/share/1013yQA0EedV5RTH4=/&quot;&gt;this great TypeScript course&lt;/a&gt; from the wonderful &lt;a href=&quot;https://academind.com/&quot;&gt;Maximilian Schwarzmüller&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The following is a summary of some notes I took during the course. To assist my learning I have created my own examples where possible. This is not a replacement for taking the course as the full course includes more practical tips and advice along with in-depth examples showing TypeScript used on a larger project.&lt;/p&gt;

&lt;p&gt;The following should be a helpful resource to refer to after taking the course.&lt;/p&gt;

&lt;h2 id=&quot;table-of-contents&quot;&gt;Table of contents&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#getting-started&quot;&gt;Getting started&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#typescript-in-action&quot;&gt;TypeScript in action&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#types&quot;&gt;Types&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#defining-types-in-functions&quot;&gt;Defining types in functions&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#type-inference&quot;&gt;Type inference&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#using--or-as-htmlinputelement-type-casting&quot;&gt;Using “!” or “as HTMLInputElement” (type casting)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#object-type&quot;&gt;Object type&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#custom-object-definition&quot;&gt;Custom object definition&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#array-types&quot;&gt;Array types&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-tuples&quot;&gt;Other types: Tuples&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-enum&quot;&gt;Other types: enum&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-any&quot;&gt;Other types: any&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-union&quot;&gt;Other types: union&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-literal&quot;&gt;Other types: literal&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#type-aliases&quot;&gt;Type aliases&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#functions&quot;&gt;Functions&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#return-void&quot;&gt;Return: void&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-function&quot;&gt;Other types: Function&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-unknown&quot;&gt;Other types: unknown&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-types-never&quot;&gt;Other types: never&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#configurating-typescript-compiler&quot;&gt;Configurating TypeScript compiler&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#watch-mode&quot;&gt;watch mode&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#watching-a-project&quot;&gt;Watching a project&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tsconfigjson&quot;&gt;tsconfig.json&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#adding-options-to-tsconfigjson&quot;&gt;Adding options to tsconfig.json&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#strict-options&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strict&lt;/code&gt; options&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#additional-checks&quot;&gt;Additional checks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#full-reference&quot;&gt;Full reference&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#classes--interfaces&quot;&gt;Classes &amp;amp; Interfaces&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#public-and-private-modifiers&quot;&gt;public and private modifiers&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#shorthand-initialisation&quot;&gt;Shorthand initialisation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#read-only&quot;&gt;Read-only&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#inheritance&quot;&gt;Inheritance&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#overriding-properties-and-protected&quot;&gt;Overriding properties and protected&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#getters-and-setters&quot;&gt;Getters and setters&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#static-properties-and-methods&quot;&gt;Static properties and methods&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#singletons&quot;&gt;Singletons&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#interfaces&quot;&gt;Interfaces&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#readonly-interface-properties&quot;&gt;Readonly interface properties&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#extending-interfaces&quot;&gt;Extending interfaces&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#interfaces-as-function-types&quot;&gt;Interfaces as function types&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#optional-properties&quot;&gt;Optional properties&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#advanced-types-and-typescript-features&quot;&gt;Advanced Types and TypeScript Features&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#intersection-types&quot;&gt;Intersection Types&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#type-guards&quot;&gt;Type guards&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#discriminated-unions&quot;&gt;Discriminated Unions&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#type-casting&quot;&gt;Type Casting&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#index-types&quot;&gt;Index types&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#function-overloads&quot;&gt;Function Overloads&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#optional-chaining&quot;&gt;Optional chaining&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nullish-coalescing&quot;&gt;Nullish Coalescing&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#generics&quot;&gt;Generics&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#using-generic-types&quot;&gt;Using generic types&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#constraints&quot;&gt;Constraints&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#keyof-constraint&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keyof&lt;/code&gt; constraint&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#generic-classes&quot;&gt;Generic classes&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#generic-utility-types&quot;&gt;Generic utility types&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#decorators&quot;&gt;Decorators&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#decorator-factory&quot;&gt;Decorator factory&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#adding-multiple-decorators&quot;&gt;Adding multiple decorators&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#accessor-method-and-property-decorators&quot;&gt;Accessor, method and property decorators&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#accessor-and-parameter-decorators&quot;&gt;Accessor and Parameter decorators&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#adding-custom-class-logic-to-decorators&quot;&gt;Adding custom class logic to decorators&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#returning-from-decorators&quot;&gt;Returning from decorators&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#decorator-example---autobind&quot;&gt;Decorator example - autobind&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-notes&quot;&gt;Other notes&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#next-steps&quot;&gt;Next steps&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;getting-started&quot;&gt;Getting started&lt;/h2&gt;

&lt;p&gt;TypeScript is an &lt;a href=&quot;https://www.typescriptlang.org&quot;&gt;open-source language which builds on JavaScript&lt;/a&gt;. You can write JavaScript in TypeScript files, as TypeScript builds on top of the existing functionality of JavaScript. You cannot however write TypeScript inside a JavaScript (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.js&lt;/code&gt;) file. This is because TypeScript requires a compiler, which takes given TypeScript code and compiles it to JavaScript so that it can be run in the browser and elsewhere.&lt;/p&gt;

&lt;p&gt;You can &lt;a href=&quot;https://www.typescriptlang.org/download&quot;&gt;install TypeScript&lt;/a&gt; a number of ways. I installed it globally using:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm install typescript -g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With TypeScript installed you can create TypeScript files as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filename.ts&lt;/code&gt; files and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsc filename&lt;/code&gt; to compile to JS locally.&lt;/p&gt;

&lt;h2 id=&quot;typescript-in-action&quot;&gt;TypeScript in action&lt;/h2&gt;

&lt;p&gt;TypeScript’s main use is to declare the type a given property may have when used. It can be done so by declaring it within a function definition, when declaring variables, or within classes. The types can apply to variables, functions, classes and can apply to input data or the expected output of a function.&lt;/p&gt;

&lt;p&gt;To start simply, we can define the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt; of given inputs to a function like so.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(num1: number, num2: number) {
  ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This tells the compiler that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;num1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;num2&lt;/code&gt; should be given a value of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;. It will display an error (when editing, depending on editor, or during compile time) if we are passing in a value that is not a number.&lt;/p&gt;

&lt;p&gt;Editors such as VSCode will show errors before compilation but you can also run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsc finename.ts&lt;/code&gt; to generate the JavaScript and it will display any errors.&lt;/p&gt;

&lt;h2 id=&quot;types&quot;&gt;Types&lt;/h2&gt;

&lt;p&gt;Core data types:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;number&lt;/li&gt;
  &lt;li&gt;string&lt;/li&gt;
  &lt;li&gt;boolean&lt;/li&gt;
  &lt;li&gt;object&lt;/li&gt;
  &lt;li&gt;Arrays (string[], number[] etc)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;defining-types-in-functions&quot;&gt;Defining types in functions&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(n1: number, n2: number, show: boolean, phrase: string) {
  const result = n1 + n2;
  if (show) {
    console.log(`${phrase} ${result}`);
  }
  return result;
}

const number1 = 5;
const number2 = 2.8;
const printResult = true;
const resultPhrase = 'Result is: ';

add(number1, number2, printResult, resultPhrase);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;type-inference&quot;&gt;Type inference&lt;/h3&gt;

&lt;p&gt;TypeScript infers types when they are initialised:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let number1 = 5; // type number automatically inferred
number1 = 'ok'; // error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If not specifying a value, you can specify it manually:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let number1: number;
number1 = 'ok'; // error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;using--or-as-htmlinputelement-type-casting&quot;&gt;Using “!” or “as HTMLInputElement” (type casting)&lt;/h3&gt;

&lt;p&gt;When using HTML selectors you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; to say “this will not be null” or we can be more specific and say “as HTMLInputElement so that TypeScript knows that the resulting elements will have certain properties.&lt;/p&gt;

&lt;h3 id=&quot;object-type&quot;&gt;Object type&lt;/h3&gt;

&lt;p&gt;An inferred object will let the editor highlight issues with missing properties:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const person = {
  name: 'Person',
  age: 100
};

console.log(person.foo) // error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When defining an object type, an explicit declaration blocks TypeScript from analysing object further which could cause unexpected errors:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const person: object = {
  name: 'Person',
  age: 100
};

console.log(person.name) // error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; is a property but TypeScript only considers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;person&lt;/code&gt; an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt; without delving deeper. Letting the object be inferred allows TypeScript to parse the properties.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const person = {
  name: 'Person',
  age: 100
};

console.log(person.name) // no error now
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;custom-object-definition&quot;&gt;Custom object definition&lt;/h3&gt;

&lt;p&gt;We can explicitly define the object when setting it up:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const person: {
  name: string;
  age: number;
} = {
  name: 'Person',
  age: 100
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While not something you’d do in practice (the types can be inferred).&lt;/p&gt;

&lt;h3 id=&quot;array-types&quot;&gt;Array types&lt;/h3&gt;

&lt;p&gt;An array containing strings is described as type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string[]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;TypeScript will infer this type if an object is given an array of strings. You can also explicity define it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let myArray: string[];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Multiple types of items in the array can be specified, such as if it has strings or numbers:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let myArray: (string | number)[];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If any values allowed in the array:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let myArray: any[];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One side effect of knowing the type is that when looping through arrays, editors can suggest string methods automatically such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toUpperCase&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for (const hobby of person.hobbies) {
  console.log(hobby.toUpperCase()); // No error
  console.log(hobby.map()); // Does not exist on type string
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Methods unavailable to the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; would then show an error when writing the code.&lt;/p&gt;

&lt;h3 id=&quot;other-types-tuples&quot;&gt;Other types: Tuples&lt;/h3&gt;

&lt;p&gt;An example of a tuple where we can specify the specific order of types in an array, for example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const person = {
  name: string;
  age: number;
  hobbies: string[];
  role: [number, string]; // Specifies that the role array has a number then a string in 2 elements
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By defining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;role&lt;/code&gt; array, it allows us to specify a specific array length and the types of data in it.&lt;/p&gt;

&lt;p&gt;Note: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;array.push&lt;/code&gt; is a special case though, and on a defined tuple array would not show in the editor as an array.&lt;/p&gt;

&lt;h3 id=&quot;other-types-enum&quot;&gt;Other types: enum&lt;/h3&gt;

&lt;p&gt;A defined set of values that a variable can use. As a custom type, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt;s start with a capital letter. An example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;enum Role { ADMIN, READ_ONLY, AUTHOR };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The compiler then produces code in which the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Role&lt;/code&gt; array uses these keys and assigns numberic values to them. In TypeScript that can continue to be used like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Role.ADMIN&lt;/code&gt; so that the raw numberic value does not need to be managed.&lt;/p&gt;

&lt;p&gt;You can assign specific values:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;enum Role { ADMIN = 5, READ_ONLY, AUTHOR }; // Increments from 5
enum Role { ADMIN = 5, READ_ONLY = 100, AUTHOR = 200 }; // Specific numbering
enum Role { ADMIN = 'ADMIN', READ_ONLY, AUTHOR }; // Text alternative
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;other-types-any&quot;&gt;Other types: any&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; is not prescriptive and allows a variable to contain anything:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const whatever: any;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’s important to avoid this if possible as it removes all the benefits of using TypeScript such as checking for errors at writing / compile time.&lt;/p&gt;

&lt;p&gt;It should only be used if you cannot tell before runtime what data might be in place, but should be otherwise avoided. Always try to use a built-in type or a custom type if more appropriate.&lt;/p&gt;

&lt;h3 id=&quot;other-types-union&quot;&gt;Other types: union&lt;/h3&gt;

&lt;p&gt;Accepting multiple types means joining them with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt; pipe symbol. These are union types:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let input1: number | string;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You might need to do runtime checks to avoid compiler errors by manually checking the type of value given and adjusting the result. For example, you might need to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toString&lt;/code&gt; on a value that can be a number or a string and it’s not a number, which makes it more explicit to the compiler what you’re doing.&lt;/p&gt;

&lt;h3 id=&quot;other-types-literal&quot;&gt;Other types: literal&lt;/h3&gt;

&lt;p&gt;If we want to make sure a given value is an expected value we can set it to a literal type, often used as a union type such as:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let val: 'thing-1' | 'thing-2';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will then limit the potential values to those two strings and will show an error if it does match the one of the expected values.&lt;/p&gt;

&lt;h3 id=&quot;type-aliases&quot;&gt;Type aliases&lt;/h3&gt;

&lt;p&gt;We can build type aliases to make creating more complex types easier. For example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type NumOrString = number | string;
type ThingTypes = 'thing-1' | 'thing-2';
type ObjType = { prop1: string; prop2: number, thing: ThingTypes };
let value: NumOrString;
let thing: ThingTypes;
const obj: ObjType = { prop1: 'Foo', prop2: 12, thing: 'thing-1' };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This can make types that are more complex and composable at the same time.&lt;/p&gt;

&lt;h2 id=&quot;functions&quot;&gt;Functions&lt;/h2&gt;

&lt;p&gt;The return value of functions can be inferred by TypeScript. The following will infer that the expected result is of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(n1: number, n2: number) {
  return n1 + n1;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can manually specify a function’s return type by passing it with the function definition:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(n1: number, n2: number):string {
  return n1 + n1; // Error, this will be a number
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;return-void&quot;&gt;Return: void&lt;/h3&gt;

&lt;p&gt;If a function doesn’t return anything, TypeScript will report that the function returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is not to be confused with type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;, which is a different and valid type in TypeScript. A function that doesn’t not return anything doesn’t return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; and should be instead be written:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(n1: number, n2: number):void {
  console.log(n1 + n1); // No returning
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However if the function does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; without returning a value, you could write:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(n1: number, n2: number):undefined {
  console.log(n1 + n1);
  return;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;other-types-function&quot;&gt;Other types: Function&lt;/h3&gt;

&lt;p&gt;Function types describe a function including the parameters and return values of the desired function.&lt;/p&gt;

&lt;p&gt;We can delare the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function&lt;/code&gt; but that simply allows a variable to store any function at all.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let myAdd: Function;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can be more specific by defining our own function type:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(n1: number, n2: number) {
  return n1 + n2;
}

let myAdd: (a: number, b: number) =&amp;gt; number;

myAdd = (a, b) =&amp;gt; add(a, b);

console.log(myAdd(2, 2)); // Outputs 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;other-types-unknown&quot;&gt;Other types: unknown&lt;/h3&gt;

&lt;p&gt;Unknown works a little like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt;, except that once set, if you then apply a variable of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt; to a variable of another type it will error. It is only assignable to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt; types of variables.&lt;/p&gt;

&lt;p&gt;TypeScript will also error if we try to try to run arbitrary operations on variables of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let value: unknown;
value.trim();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this way &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt; has some type checking whereas &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; has none. It’s in some ways the opposite of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; - as the latter allows any operations whereas &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt; is more restrictive.&lt;/p&gt;

&lt;p&gt;In general you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt; when you do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typeof&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;instanceof&lt;/code&gt; checks before carrying out operations on the variable.&lt;/p&gt;

&lt;p&gt;Tip: Try to use more specific types when possible, but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt; in preference of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;other-types-never&quot;&gt;Other types: never&lt;/h3&gt;

&lt;p&gt;If a function does not produce a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; value of any sort, such as if a function throws an error, it is a function of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;never&lt;/code&gt; as it doesn’t even produce &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// Inferred function type: void
function throwCustomError(message: string, errorCode: number) {
  throw {message, errorCode};
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can make it more explicit that the function never returns:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function throwCustomError(message: string, errorCode: number):never {
  throw {message, errorCode};
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;configurating-typescript-compiler&quot;&gt;Configurating TypeScript compiler&lt;/h2&gt;

&lt;p&gt;To run TypeScript locally, &lt;a href=&quot;https://nodejs.org/en/&quot;&gt;Node/NPM&lt;/a&gt; is required so that we can install it with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm i typescript -g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This allows us to run the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsc myScript.ts&lt;/code&gt; and it will compile the given script. However there are ways to do more than just run the compilation manually.&lt;/p&gt;

&lt;h3 id=&quot;watch-mode&quot;&gt;watch mode&lt;/h3&gt;

&lt;p&gt;The command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsc&lt;/code&gt; can be run with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--watch&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-w&lt;/code&gt; to catch and re-compile then a target file changes.&lt;/p&gt;

&lt;p&gt;This is handy for small use-cases. When working on an entire project we make use of configuration.&lt;/p&gt;

&lt;h3 id=&quot;watching-a-project&quot;&gt;Watching a project&lt;/h3&gt;

&lt;p&gt;Set up a TypeScript project using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsc --init&lt;/code&gt; to create a config file.&lt;/p&gt;

&lt;p&gt;With this file in place we can run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsc -w&lt;/code&gt; and it will then compile and watch any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ts&lt;/code&gt; files in a project.&lt;/p&gt;

&lt;h3 id=&quot;tsconfigjson&quot;&gt;tsconfig.json&lt;/h3&gt;

&lt;p&gt;We use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; to specify which version of output JavaScript to generate. It defaults to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib&lt;/code&gt; is used to specify which libraries are included in the global scope. By default it includes everything needed to run in the browser such as the DOM APIs, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;document&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;console&lt;/code&gt; etc. Uncommenting it removes these defaults.&lt;/p&gt;

&lt;p&gt;We could specify this using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;dom&quot;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;es6&quot;&lt;/code&gt; along with things like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;dom.iterable&quot;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;scripthost&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;allowJs&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;checkJs&lt;/code&gt; can be used to compile or check JavaScript files using the TypeScript compiler.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sourceMap&lt;/code&gt; tells the compiler to build sourceMap files to help map compiled code back to the source to help in debugging.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rootDir&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;outDir&lt;/code&gt; help organising the files into source and output folders.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;downlevelIteration&lt;/code&gt; can help in specific cases where some kinds of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loops cause issues in older browsers.&lt;/p&gt;

&lt;h3 id=&quot;adding-options-to-tsconfigjson&quot;&gt;Adding options to tsconfig.json&lt;/h3&gt;

&lt;p&gt;An option worth considering is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noEmitOnError&lt;/code&gt; setting. By default it is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;. This means that even when errors are encountered, the compiled JavaScript files are still generated.&lt;/p&gt;

&lt;p&gt;If set to true, this will stop any files being written when errors are encountered.&lt;/p&gt;

&lt;p&gt;We can also add extra options to the end of the generated config file, such as if we want to exclude certain files:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  ...
    &quot;forceConsistentCasingInFileNames&quot;: true        /* Disallow inconsistently-cased references to the same file. */
  },
  &quot;exclude&quot;: [
    &quot;foo.ts&quot;,
    &quot;**/*.dev.ts&quot;,
    &quot;node_modules&quot; // This is ignored by default if &quot;exclude&quot; not used
  ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Alternately you can choose to only compile specific files or folders:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  ...
  },
  &quot;include&quot;: [ &quot;my_specific_file_only.ts&quot;, &quot;./specialFolder/* ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;files&lt;/code&gt; to specify specific files only and not folders.&lt;/p&gt;

&lt;h3 id=&quot;strict-options&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strict&lt;/code&gt; options&lt;/h3&gt;

&lt;p&gt;Setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strict&lt;/code&gt; to true sets all the following options to true.&lt;/p&gt;

&lt;p&gt;This means applying rules such as not allowing implicit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; (where functions are given unspecified type variables).&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strictNullChecks&lt;/code&gt; makes TypeScript strict when checking the potential values of elements such as those from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;querySelector&lt;/code&gt;. This can be overridden using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const button = document.querySelector(&quot;button&quot;)!;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The purpose is to avoid running into “cannot find property of null” error. Using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; workaround is one approach but we could also avoid the error by adding an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; check around a situation that could potentially fail, as in:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if (button) {
  button.addEventListener('click', () =&amp;gt; {
    ...
  });
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strictFunctionTypes&lt;/code&gt; checks any function on which you apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; to change what is being passed in to the function. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; causes an issue, passing in the correct properties within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt; should work.&lt;/p&gt;

&lt;h3 id=&quot;additional-checks&quot;&gt;Additional checks&lt;/h3&gt;

&lt;p&gt;Turning on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noUnusedLocals&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noUnusedParameters&lt;/code&gt; are useful ways to highlight when unused local variables or parameters find their way into your code.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noImplicitReturns&lt;/code&gt; will complain if we have functions that return in some places but not in others.&lt;/p&gt;

&lt;h3 id=&quot;full-reference&quot;&gt;Full reference&lt;/h3&gt;

&lt;p&gt;More information on the options available can be found at the &lt;a href=&quot;https://www.typescriptlang.org/tsconfig&quot;&gt;official TSConfig Reference documentation page&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;classes--interfaces&quot;&gt;Classes &amp;amp; Interfaces&lt;/h2&gt;

&lt;p&gt;When declaring classes we can specify the types for given properties, like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Example {
  myProp: string; // Type of string expected

  constructor(p: string) {
    this.myProp = p; // Expects a string in p
  }

  logTheProp(this: Example) {
    console.log(this.myProp)
  }
}

const newThing = new Example('New thing');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that we define the given &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; property in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logTheProp&lt;/code&gt; as being of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Example&lt;/code&gt;. By doing so we ensure that calling the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logTheProp&lt;/code&gt; method out of the expected context will show up as an error when coding or compiling:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const randomOtherThing = { logTheProp: newThing.logTheProp };
randomOtherThing.logTheProp();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above we could fix the object by giving it a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myProp&lt;/code&gt; property like the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Example&lt;/code&gt; type.&lt;/p&gt;

&lt;h3 id=&quot;public-and-private-modifiers&quot;&gt;public and private modifiers&lt;/h3&gt;

&lt;p&gt;Adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt; before the type definition in the class will ensure it cannot be accessed from outside the class.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Example() {
  private things: string[] = [];

  addThing(thing: string) {
    this.things.push(thing);
  }
}

const newThing = new Example('New thing');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this way the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;newThing.addThing&lt;/code&gt; method is the only way to modify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;things&lt;/code&gt; array as it could otherwise be modified using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;newThing.things&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Private&lt;/code&gt; can also be applied to methods within a class and will be enforced by TypeScript.&lt;/p&gt;

&lt;h3 id=&quot;shorthand-initialisation&quot;&gt;Shorthand initialisation&lt;/h3&gt;

&lt;p&gt;We can use a shorthand to define properties with types in the constuctor:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Example {
  constructor(public myProp: string) {

  }

  logTheProp(this: Example) {
    console.log(this.myProp)
  }
}

const newThing = new Example('New thing');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This would work similarly to the first example above but save some code.&lt;/p&gt;

&lt;h3 id=&quot;read-only&quot;&gt;Read-only&lt;/h3&gt;

&lt;p&gt;Adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readonly&lt;/code&gt; when defining class properties can prevent them being accidentally changed by other methods or from outside the class. For example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Example {
  constructor(readonly myProp: string) {

  }

  changeThing() {
    this.myProp = 'changed'; // Error
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;inheritance&quot;&gt;Inheritance&lt;/h3&gt;

&lt;p&gt;You can inherit and extend classes like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class SpecialExample extends Example {
  // Use super in the inheriting class to call the base class constructor
  constructor(p: string, public specialThings: string[]) {
    super('Special ' + p);
  }
}

const newThing = new SpecialExample('New thing', ['More things']);

console.log(newThing);
// logs {things: Array(0), myProp: &quot;Special New thing&quot;, specialThings: Array(1)}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;overriding-properties-and-protected&quot;&gt;Overriding properties and protected&lt;/h3&gt;

&lt;p&gt;By changing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt; property on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;things&lt;/code&gt; above to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;protected&lt;/code&gt;, it can be overridden in the inherited class:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class SpecialExample extends Example {
  constructor(p: string, public specialThings: string[]) {
    super('Special ' + p);
  }

  logThings() {
    console.log('Special things: ', this.things);
    // This.things shows error if &quot;private&quot; in base class
    // Change to &quot;protected&quot; for this to work
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;getters-and-setters&quot;&gt;Getters and setters&lt;/h3&gt;

&lt;p&gt;In a class getters and setters are written like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  // Inside class
  private thing: string;
  private things: string[];

  get getMyThing() {
    return this.thing;
  }

  set setMyThing(value: string) {
    this.things.push(value);
  }

  ...
  console.log(newThing.getMyThing) // Note no parens
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This allows access to getting and setting private properties of a class.&lt;/p&gt;

&lt;h3 id=&quot;static-properties-and-methods&quot;&gt;Static properties and methods&lt;/h3&gt;

&lt;p&gt;We can access static methods without instantiating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; version of a class but instead access them directly within the class definition.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Example {
  static myProp: string = 'A thing';

  static staticMethod(p: string) {
    console.log(p)
  }
}

Example.staticMethod('hello');
console.log(Example.myProp)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;singletons&quot;&gt;Singletons&lt;/h3&gt;

&lt;p&gt;When following the singleton pattern, TypeScript makes the process easier by allowing you to specify a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt; constructor.&lt;/p&gt;

&lt;h2 id=&quot;interfaces&quot;&gt;Interfaces&lt;/h2&gt;

&lt;p&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;interface&lt;/code&gt; describes the structure of an object and can be used as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt; definition to check that a type matches the expected pattern.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface Person {
  name: string;
  age: number;

  greet(phrase: string): void;
}

let user1: Person;

user1 = {
  name: 'Bill',
  age: 50,
  greet(phrase: string) {
    console.log(phrase + ' ' + this.name);
  }
}

user1.greet('Hey I am');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While it’s similar to using a custom &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;interface&lt;/code&gt; can only be used to describe an object where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt; can be used for different structures such as unions etc.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type Person = { ... } // Similar result as above
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The benefit is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;interface&lt;/code&gt; is generally clearer. Interfaces can be used to define the way classes are implemented:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class PersonClass implements Person {
  ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This then enforces the above structure on the class. It would enforce a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;age&lt;/code&gt; property along with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;greet&lt;/code&gt; method.&lt;/p&gt;

&lt;h3 id=&quot;readonly-interface-properties&quot;&gt;Readonly interface properties&lt;/h3&gt;

&lt;p&gt;Adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readonly&lt;/code&gt; to the definition shows an error if we then try to change that property later.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface Person {
  readonly name: string;
  age: number;

  greet(phrase: string): void;
}

let user1: Person;

user1 = {
  name: 'Bill',
  age: 50,
  greet(phrase: string) {
    console.log(phrase + ' ' + this.name);
  }
}

user1.name = 'Bob'; // Error: name is read-only
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will also then apply within any classes that implement an interface.&lt;/p&gt;

&lt;p&gt;Note: You can not add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private&lt;/code&gt; to interface properties.&lt;/p&gt;

&lt;h3 id=&quot;extending-interfaces&quot;&gt;Extending interfaces&lt;/h3&gt;

&lt;p&gt;We can compose interfaces using extends and even involve multiple interfaces:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface Named {
  readonly name: string;
}

interface Aged {
  age: number;
}

interface Person extends Named, Aged {
  greet(phrase: string): void;
}

let user1: Person;

user1 = {
  name: 'Bill',
  age: 50,
  greet(phrase: string) {
    console.log(phrase + ' ' + this.name);
  }
}

user1.greet('Hey I am');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;interfaces-as-function-types&quot;&gt;Interfaces as function types&lt;/h3&gt;

&lt;p&gt;As an alternative to defining custom types, you can define a custom function type within an interface:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface AddFn {
  (a: number, b: number): number
}

let add: AddFn;

add = (n1: number, n2: number) =&amp;gt; {
  return n1 + n2;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;optional-properties&quot;&gt;Optional properties&lt;/h3&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt; defines any type as optional. This can also be handled by using a default value. In this example we add an optional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nickname&lt;/code&gt; property which will show if it is set:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface Named {
  readonly name: string;
  nickname?: string;
}

interface Aged {
  age: number;
}

interface Person extends Named, Aged {
  greet(phrase: string): void;
}

let user1: Person;
let user2: Person;

user1 = {
  name: 'Bill',
  age: 50,
  greet(phrase: string) {
    console.log(phrase + ' ' + this.name);
  }
}

user2 = {
  name: 'Bob',
  nickname: 'Dave',
  age: 30,
  greet(phrase: string) {
    console.log(phrase + ' ' + (this.nickname || this.name));
  }
}

user2.greet('Hey I am'); // Dave
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;advanced-types-and-typescript-features&quot;&gt;Advanced Types and TypeScript Features&lt;/h2&gt;

&lt;h3 id=&quot;intersection-types&quot;&gt;Intersection Types&lt;/h3&gt;

&lt;p&gt;We can define multiple types then compose them into intersection types.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type Admin = {
  name: string;
  privileges: string[];
};

type Employee = {
  name: string;
  startDate: Date; // a built-in interface
};

type ElevatedEmployee = Admin &amp;amp; Employee;

const e1: ElevatedEmployee = {
  name: 'Jen',
  privileges: ['admin'],
  startDate: new Date()
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we were using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;interface&lt;/code&gt; for the type definitions we could use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends&lt;/code&gt; as in the previous section.&lt;/p&gt;

&lt;p&gt;Types can cancel each other out when intersecting, so the following will resolve to simply be of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type a = string | number;
type b = number | boolean;
type Intersection = a &amp;amp; b; // Type: number
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;type-guards&quot;&gt;Type guards&lt;/h3&gt;

&lt;p&gt;In the following, TypeScript will complain as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a + b&lt;/code&gt; part could be either a string or number for each input:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type myType = string | number;

function add(a: myType, b: myType) {
  return a + b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We add a type guard to show that we are taking this into consideration, removing the error:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;type myType = string | number;

function add(a: myType, b: myType) {
  if (typeof a === 'string' || typeof b === 'string') {
    return a.toString() + b.toString();
  }
  return a + b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Sometimes we might want to use an optional property like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;obj.propertyName&lt;/code&gt;. This will error as we do not know whether the object will have that property.&lt;/p&gt;

&lt;p&gt;We can also look for the existence of optional properties using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;in&lt;/code&gt;, such as:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if ('propertyName' in obj) {
  // ...act on it
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For classes, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;instanceof&lt;/code&gt; type guard can be used to determine if a class has the expected method.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if (vehicle instanceof Truck) {
  // ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These guards allow for flexible type definitions while still ensuring we are confident that the code will be robust.&lt;/p&gt;

&lt;h3 id=&quot;discriminated-unions&quot;&gt;Discriminated Unions&lt;/h3&gt;

&lt;p&gt;When creating a union-based type we might want to have some way to determine which of the given types in the union are bring used. In the following example, the types &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Horse&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bird&lt;/code&gt; make use of the shared &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt; property that can be one of two values to determine which interface applies to the given object:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface Bird {
  type: 'bird';
  flyingSpeed: number;
}

interface Horse {
  type: 'horse';
  runningSpeed: number;
}

type Animal = Bird | Horse;

function moveAnimal(animal: Animal) {
  let speed;
  switch (animal.type) {
    case 'bird':
      speed = animal.flyingSpeed;
      break;
    case 'horse':
      speed = animal.runningSpeed;
  }
  console.log('Moving at speed: ' + speed);
}

moveAnimal({ type: 'bird', flyingSpeed: 100 });
moveAnimal({ type: 'horse', runningSpeed: 30 });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This gives us a safe way to ensure that we don’t accidentally use the incorrect object definition and avoids the previous type guards using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typeof&lt;/code&gt; or checking for specific properties in the object.&lt;/p&gt;

&lt;h3 id=&quot;type-casting&quot;&gt;Type Casting&lt;/h3&gt;

&lt;p&gt;We can manually direct TypeScript to expect certain values to be of a specific type as it may not always be able to infer the type.&lt;/p&gt;

&lt;p&gt;For example a document selector will be considered of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTMLElement&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;, and so TypeScript will complain if we try to access a specific property of an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;input&lt;/code&gt; type for example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const inputEl = document.getElementById('user-input');

inputEl.value = 'Hello'; // inputEl could be null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we avoid the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; type by adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; to the query we get a different error:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const inputEl = document.getElementById('user-input')!;

inputEl.value = 'Hello'; // value not a known property on HTMLElement
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So instead we can cast the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;HTMLInputElement&amp;gt;&lt;/code&gt; directly on the query. This tells TypeScript that the result of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getElementById&lt;/code&gt; will be of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTMLInputElement&lt;/code&gt; and not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTMLElement&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const inputEl = &amp;lt;HTMLInputElement&amp;gt;document.getElementById('user-input');

inputEl.value = 'Hello'; // No error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Alternatively you can avoid clashes with JSX formatting by using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;as&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const inputEl = document.getElementById('user-input') as HTMLInputElement;

inputEl.value = 'Hello';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that the above casting implicitly removes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; type as an option so we might want to instead check that manually if we are not sure whether it might be null.&lt;/p&gt;

&lt;h3 id=&quot;index-types&quot;&gt;Index types&lt;/h3&gt;

&lt;p&gt;When defining a custom type interfaces we can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index&lt;/code&gt; types to set out a shape for the expected object.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface ErrorContainer {
  [prop: string]: string;
}

const errors: ErrorContainer = {
  email: 'The key is a string here',
  100: 'A number can also converted to a string'
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above says that an object of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ErrorContainer&lt;/code&gt; can have any number of keys but the key should be a string (or be able to be converted to a string) and have a string as the value for each.&lt;/p&gt;

&lt;h3 id=&quot;function-overloads&quot;&gt;Function Overloads&lt;/h3&gt;

&lt;p&gt;Function overloads are useful when you want to avoid issues where trying to run methods on results of functions where the result could be moe than one type. For example in the add function, this results in an output that could either be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; so errors if we apply a string method:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function add(a: myType, b: myType) {
  if (typeof a === 'string' || typeof b === 'string') {
    return a.toString() + b.toString();
  }
  return a + b;
}

const result = add('String', 'Values'); // Always a string
result.split(' '); // Error as it still thinks it could be number
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can type case using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add('String', 'Values') as String;&lt;/code&gt; or overload the function:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// overloads for add function
function add(a: number, b: number): number;
function add(a: string, b: string | number): string;
function add(a: string | number, b: string): string;

function add(a: myType, b: myType) {
  if (typeof a === 'string' || typeof b === 'string') {
    return a.toString() + b.toString();
  }
  return a + b;
}

const result = add('String', 'Values') as String;
result.split(' ')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This documents the possible combinations of types that can be given to a function and specifies the expected return type for each.&lt;/p&gt;

&lt;h3 id=&quot;optional-chaining&quot;&gt;Optional chaining&lt;/h3&gt;

&lt;p&gt;TypeScript includes built-in support for adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt; to objects and their properties if they may be missing.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;myObject?.property?.foo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;nullish-coalescing&quot;&gt;Nullish Coalescing&lt;/h3&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;??&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt; when determining if a variable is null or undefined will ensure that situations such as empty strings or zero values aren’t considered null.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const emptyVar = '';

const newThing = emptyVar || 'DEFAULT'; // Will consider empty string as falsey
const newThing2 = emptyVar ?? 'DEFAULT'; // Will allow empty string

console.log({newThing, newThing2})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;generics&quot;&gt;Generics&lt;/h2&gt;

&lt;p&gt;In TypeScript, some built-in types are generic such as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; type, as they describe a structure within which it expects a certain type.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const myArray: Array = [1, 2, 3]; // Error
const myArray: Array&amp;lt;number&amp;gt; = [1, 2, 3]; // No error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By defining the type of items in the array, TypeScript then knows which methods are available on the given data, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toFixed&lt;/code&gt; or used within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Math&lt;/code&gt; methods. It could then error if a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; type of array used those features.&lt;/p&gt;

&lt;p&gt;A similar example is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&amp;lt;string&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;using-generic-types&quot;&gt;Using generic types&lt;/h3&gt;

&lt;p&gt;If we have the following function, we see an error when accessing a property on the merged objects:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function merge(objA: object, objB: object) {
  return Object.assign(objA, objB);
}

const merged = merge({ name: 'Marge' }, { age: 30 });
console.log(merged.name);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;TypeScript doesn’t know that the vaguely inferred &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt; output of the above function will contain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can fix this by defining the two given objects as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generic&lt;/code&gt; types so that TypeScript can infer that the output is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;merge&lt;/code&gt; is in fact the combination of two generic input objects:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function merge&amp;lt;T, U&amp;gt;(objA: T, objB: U) {
  return Object.assign(objA, objB);
}

const merged = merge({ name: 'Marge' }, { age: 30 });
console.log(merged.name);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; as symbols for these generic types as a convention but they could be any string.&lt;/p&gt;

&lt;p&gt;TypeScript will infer the more specific types within these generic types based on how they are used.&lt;/p&gt;

&lt;h3 id=&quot;constraints&quot;&gt;Constraints&lt;/h3&gt;

&lt;p&gt;In the above generic example, the types &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; can be anything at all. While this works, it would be better to constrain them to ensure they are objects.&lt;/p&gt;

&lt;p&gt;We set constraints using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends&lt;/code&gt; within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&amp;gt;&lt;/code&gt; part of the function definition.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function merge&amp;lt;T extends object, U extends object&amp;gt;(objA: T, objB: U) {
  return Object.assign(objA, objB);
}

const merged = merge({ name: 'Marge' }, { age: 30 });
console.log(merged.name);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This ensures that TypeScript will expect an object for each of the generic types, while also ensuring that the function outputs the merged objects &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;, which allows us to then confidently access properties such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another example of a generic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; with a custom defined type (interface) as a constraint:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface Lengthy {
  length: number;
}

function countAndDescribe&amp;lt;T extends Lengthy&amp;gt;(element: T): [T, string] {
  let desc = 'Got no elements';
  if (element.length &amp;gt; 0) {
    desc = `Got ${element.length} element${element.length === 1 ? '' : 's'}`;
  }
  return [element, desc];
}

console.log(countAndDescribe(['one', 'two']))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;keyof-constraint&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keyof&lt;/code&gt; constraint&lt;/h3&gt;

&lt;p&gt;Extending using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keyof&lt;/code&gt; can be used to describe a type that represents a key of another type:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function extractAndConvert&amp;lt;T extends object, U extends keyof T&amp;gt;(obj: T, key: U) {
  return obj[key];
}

console.log(extractAndConvert({ name: 'Max' }, 'name'));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;generic-classes&quot;&gt;Generic classes&lt;/h3&gt;

&lt;p&gt;Flexible reusable classes with strong typing can be created using generic types. For example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class DataStorage&amp;lt;T extends string | number | boolean&amp;gt; {
  private data: T[] = [];

  addItem(item: T) {
    this.data.push(item);
  }

  removeItem(item: T) {
    this.data.splice(this.data.indexOf(item), 1);
  }

  getItems() {
    return [...this.data];
  }
}

const textStorage = new DataStorage&amp;lt;string&amp;gt;();
textStorage.addItem('TypeScript using generic classes');
textStorage.addItem('Remove me');
textStorage.removeItem('Remove me');
console.log(textStorage.getItems());

const numberStorage = new DataStorage&amp;lt;number&amp;gt;();
numberStorage.addItem(2);
numberStorage.addItem(3);
numberStorage.removeItem(3);
console.log(numberStorage.getItems());
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above works well enough with primatives as it relies on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;indexOf&lt;/code&gt;. To protect it we specify &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string | number | boolean&lt;/code&gt; as it would need a different approach for objects.&lt;/p&gt;

&lt;p&gt;In the above, more than one generic can be used if needed, along with constraints.&lt;/p&gt;

&lt;h3 id=&quot;generic-utility-types&quot;&gt;Generic utility types&lt;/h3&gt;

&lt;p&gt;Some &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/utility-types.html&quot;&gt;generic utility types&lt;/a&gt; are available including:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Partial&lt;/code&gt; wraps a type to make any properties on it temporarily optional.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;interface Thing {
  title: string;
  description: string
}

function createThing(title: string, description: string): Thing {
  let thing: Partial&amp;lt;Thing&amp;gt; = {};
  thing.title = title;
  thing.description = description;
  return thing as Thing;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Required&lt;/code&gt; does the opposite.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReadOnly&lt;/code&gt; can be used on arrays or objects to stop new items being added or properties being added to an object:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const names: Readonly&amp;lt;string[]&amp;gt; = ['Superman'];
names.push('Clark Kent'); // Error as it is read only
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Find &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/utility-types.html&quot;&gt;more generic utility types here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;decorators&quot;&gt;Decorators&lt;/h2&gt;

&lt;p&gt;Decorators are useful for meta-programming. By that, it makes it easier to write code that is easier for other developers to work with.&lt;/p&gt;

&lt;p&gt;We can create and apply a simple decorator like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function Logger(constructor: Function) { // Convention: capital letter
  console.log('Logging...');
  console.log(constructor);
}

@Logger
class Person {
  name = 'Jo';

  constructor() {
    console.log('Creating person object');
  }
}

const pers = new Person();

console.log(pers);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;decorator-factory&quot;&gt;Decorator factory&lt;/h3&gt;

&lt;p&gt;A decorator can be run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt; to then return a function.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function Logger(logString: string) { // Convention: capital letter
  return (constructor: Function) =&amp;gt; {
    console.log(logString);
    console.log(constructor);
  }
}

@Logger('Logging - Person')
class Person {
  name = 'Jo';

  constructor() {
    console.log('Creating person object');
  }
}

const pers = new Person();

console.log(pers);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Another example to handle more complex logic and wrap it up into a decorator that offers specific functionality:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function WithTemplate(template: string, hookId: string) {
  return (_: Function) =&amp;gt; {
    const element = document.getElementById(hookId);
    if (element) {
      element.innerHTML = template;
    }
  };
}

@WithTemplate('&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;', 'app')
class Person {
  name = 'Jo';

  constructor() {
    console.log('Creating person object');
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This could then be adjusted to make use of the given constructor from the class being decorated:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function WithTemplate(template: string, hookId: string) {
  return (constructor: any) =&amp;gt; {
    const element = document.getElementById(hookId);
    const p = new constructor(); // Constructor from Person
    if (element) {
      element.innerHTML = template;
      element.querySelector('h1')!.textContent = p.name;
    }
  };
}

@WithTemplate('&amp;lt;h1&amp;gt;Fallback&amp;lt;/h1&amp;gt;', 'app')
class Person {
  name = 'Jo';

  constructor() {
    console.log('Creating person object');
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;adding-multiple-decorators&quot;&gt;Adding multiple decorators&lt;/h3&gt;

&lt;p&gt;When running multiple decorators, they execute from the bottom up:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@Decorator1
@Decorator2
class MyClass...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above, the second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Decorator2&lt;/code&gt; executes first. However if they are factories, they execute top to bottom first, then their returned function is executed from last to first.&lt;/p&gt;

&lt;h3 id=&quot;accessor-method-and-property-decorators&quot;&gt;Accessor, method and property decorators&lt;/h3&gt;

&lt;p&gt;When using decorators in other parts of code, they receive different arguments.&lt;/p&gt;

&lt;p&gt;If used on a property, it receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;propertyName&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function Log(target: any, propertyName: string) {
  console.log('Property decorator');
  console.log(target, propertyName);
}

class Product {
  @Log
  title: string;
  private _price: number;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The property decorator is executed when the class definition is registered with JavaScript, before being instantiated.&lt;/p&gt;

&lt;h3 id=&quot;accessor-and-parameter-decorators&quot;&gt;Accessor and Parameter decorators&lt;/h3&gt;

&lt;p&gt;Applying a decorator to accessors (setters / getters) gives it a name and descriptor as so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function LogAccessor(target: any, name: string, descriptor: PropertyDescriptor) {
  console.log('Accessor decorator');
  console.log(target);
  console.log(name);
  console.log(descriptor);
}

class Product {
  title: string;
  private _price: number;

  constructor(t: string, p: number) {
    this.title = t;
    this._price = p;
  }

  @LogAccessor
  set price(val: number) {
    if (val &amp;gt; 0) {
      this._price = val;
    } else {
      throw new Error('Invalid price - should be positive')
    }
  }

  getPriceWithTax(tax: number) {
    return this._price * tax;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A method decorator is similar:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function LogMethod(target: any, name: string | Symbol, descriptor: PropertyDescriptor) {
  console.log('Method decorator');
  console.log(target);
  console.log(name);
  console.log(descriptor);
}

class Product {
  title: string;
  private _price: number;

  constructor(t: string, p: number) {
    this.title = t;
    this._price = p;
  }

  set price(val: number) {
    if (val &amp;gt; 0) {
      this._price = val;
    } else {
      throw new Error('Invalid price - should be positive')
    }
  }

  @LogMethod
  getPriceWithTax(tax: number) {
    return this._price * tax;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Logging a parameter is similar but instead of descriptor, provides a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;position&lt;/code&gt; argument containing a zero-indexed position for the parameter.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function LogParameter(target: any, name: string | Symbol, position: number) {
  console.log('Parameter decorator');
  console.log(target);
  console.log(name);
  console.log(position);
}

class Product {
  title: string;
  private _price: number;

  constructor(t: string, p: number) {
    this.title = t;
    this._price = p;
  }

  set price(val: number) {
    if (val &amp;gt; 0) {
      this._price = val;
    } else {
      throw new Error('Invalid price - should be positive')
    }
  }

  getPriceWithTax(@LogParameter tax: number) {
    return this._price * tax;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;adding-custom-class-logic-to-decorators&quot;&gt;Adding custom class logic to decorators&lt;/h3&gt;

&lt;p&gt;The following adjusts the previous decorator to make it only run when the class is instantiated, rather than when the class is defined:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function Logger(logString: string) { // Convention: capital letter
  return (_: Function) =&amp;gt; {
    console.log(logString);
  };
}

function WithTemplate(template: string, hookId: string) {
  return &amp;lt;T extends {new(...args: any[]): { name: string }}&amp;gt;(orig_constructor: T) =&amp;gt; {
    console.log('Rendering template');
    // The following class is only run when the class is instantiated
    // Rather than when the class is defined
    return class extends orig_constructor {
      constructor(..._: any[]) {
        super(); // Call the base constructor
        const element = document.getElementById(hookId);
        if (element) {
          element.innerHTML = template;
          element.querySelector('h1')!.textContent = this.name;
        }
      }
    }
  };
}

@Logger('Instantiating create person object')
@WithTemplate('&amp;lt;h1&amp;gt;Fallback&amp;lt;/h1&amp;gt;', 'app')
class Person {
  name = 'Jo';

  constructor() {
    console.log('Creating person object');
  }
}

// Activate the custom class that replaces the given class above
const pers = new Person();

console.log(pers);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;returning-from-decorators&quot;&gt;Returning from decorators&lt;/h3&gt;

&lt;p&gt;When returning values from decorators, you can return &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty&quot;&gt;property descriptors&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We can return these from method and parameter decorators. This allows us to override or set our own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getter&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setter&lt;/code&gt; descriptors for example, along with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enumarable&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configurable&lt;/code&gt; properties.&lt;/p&gt;

&lt;h3 id=&quot;decorator-example---autobind&quot;&gt;Decorator example - autobind&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function Autobind(_: any, _2: string, descriptor: PropertyDescriptor) {
  const originalMethod = descriptor.value;
  return {
    configurable: true,
    enumerable: false,
    get() {
      return originalMethod.bind(this);
    }
  }
}

class PrintMessage {
  message: string = 'This works!';

  @Autobind
  showMessage() {
    console.log(this.message);
  }
}

const button = document.getElementById('mybutton')!;

const p = new PrintMessage();

button.addEventListener('click', p.showMessage);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;other-notes&quot;&gt;Other notes&lt;/h3&gt;

&lt;p&gt;You can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt; in argument lists to mark an expected argument that will not be used:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function(_: string, _2: number) {
  // _  or _2 not used
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;next-steps&quot;&gt;Next steps&lt;/h2&gt;

&lt;p&gt;The &lt;a href=&quot;https://www.typescriptlang.org/&quot;&gt;TypeScript website&lt;/a&gt; is a good starting point to explore TypeScript further.&lt;/p&gt;

&lt;h2 id=&quot;well-thats-enough-about-me-your-turn&quot;&gt;Well that’s enough about me. Your turn!&lt;/h2&gt;

&lt;p&gt;Have you build a cool Svelte app you’d like to tell me about? You can message me &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;, I’d love to hear from you.&lt;/p&gt;
</description>
        <pubDate>Thu, 27 May 2021 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/learning-typescript/</link>
        <guid isPermaLink="true">http://hop.ie/blog/learning-typescript/</guid>
      </item>
    
      <item>
        <title>Lazy loading images in Svelte</title>
        <description>&lt;p&gt;When we build websites and apps we want them to load quickly and feel quick when navigating. One easy way to improve the speed is to only download visible images.&lt;/p&gt;

&lt;p&gt;In this article I’ll show how can make use of the Intersection Observer alongside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onLoad&lt;/code&gt; event to load only the necessary images as our visitors load and then scroll within our Svelte websites and apps.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was &lt;a href=&quot;https://css-tricks.com/lazy-loading-images-in-svelte/&quot;&gt;originally published on CSS Tricks&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;real-life-example&quot;&gt;Real-life example&lt;/h2&gt;

&lt;p&gt;I put this approach together while testing the speed on &lt;a href=&quot;https://www.shopireland.ie&quot;&gt;Shop Ireland&lt;/a&gt;. It is a Svelte and Sapper application designed to be as fast-loading as I can make it.&lt;/p&gt;

&lt;p&gt;During performance testing, the biggest issue was the home page loading many images at once, most of which were not even visible until the visitor scrolled.&lt;/p&gt;

&lt;h2 id=&quot;download-the-finished-code&quot;&gt;Download the finished code&lt;/h2&gt;

&lt;p&gt;If you’d like to save some time you can download the &lt;a href=&quot;https://github.com/donovanh/svelte-image-loading&quot;&gt;final code for this demo from Github&lt;/a&gt; and read along for an explanation of how it works.&lt;/p&gt;

&lt;h2 id=&quot;what-we-will-build&quot;&gt;What we will build&lt;/h2&gt;

&lt;p&gt;Here’s the result we will be aiming for today.&lt;/p&gt;

&lt;p class=&quot;codepen&quot; data-height=&quot;300&quot; data-theme-id=&quot;12592&quot; data-default-tab=&quot;js,result&quot; data-user=&quot;donovanh&quot; data-slug-hash=&quot;abdLERL&quot; style=&quot;height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;&quot; data-pen-title=&quot;Lazy Loading Images in Svelte&quot;&gt;
  &lt;span&gt;See the Pen &lt;a href=&quot;https://codepen.io/donovanh/pen/abdLERL&quot;&gt;
  Lazy Loading Images in Svelte&lt;/a&gt; by Donovan Hutchinson (&lt;a href=&quot;https://codepen.io/donovanh&quot;&gt;@donovanh&lt;/a&gt;)
  on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/span&gt;
&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;https://static.codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;starting-app&quot;&gt;Starting app&lt;/h2&gt;

&lt;p&gt;You might have an app in place where you’d like to apply these ideas, but if not you can start a new Svelte project and work on it locally. Start by initiating a new Svelte project and running it locally:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npx degit sveltejs/template my-svelte-project
cd my-svelte-project
npm install
npm run dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should now have a beginner app running on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://localhost:5000&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;adding-the-components-folder&quot;&gt;Adding the components folder&lt;/h2&gt;

&lt;p&gt;The initial Svelte demo has an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;App.svelte&lt;/code&gt; file but no components yet. Let’s set up the components we need for this demo. First, create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;components&lt;/code&gt; folder in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Within this create an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt; folder, which will hold our components.&lt;/p&gt;

&lt;p&gt;We’re going to have our components do two things. First we’ll want to check when our image is visible on the screen, and then if it is, we want to wait until the image file has loaded before then showing it.&lt;/p&gt;

&lt;p&gt;To do this we’ll wrap an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Intersection Observer&lt;/code&gt; around an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image Loader&lt;/code&gt;. This will allow both components to do one thing and create the effect we want. We’ll begin by setting up a wrapper to tell us when the component enters the viewport.&lt;/p&gt;

&lt;h2 id=&quot;intersection-observing&quot;&gt;Intersection Observing&lt;/h2&gt;

&lt;p&gt;To avoid getting to deep into how &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API&quot;&gt;Intersection Observer&lt;/a&gt; works, we’ll make use of a &lt;a href=&quot;https://github.com/sveltejs/svelte/blob/master/site/src/components/IntersectionObserver.svelte&quot;&gt;handy Svelte component&lt;/a&gt; that Rich Harris put together for the &lt;a href=&quot;https://svelte.dev&quot;&gt;Svelte&lt;/a&gt; website.&lt;/p&gt;

&lt;p&gt;We’ll set up this as a useful component first then look at what it does. Save the following into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;components/Image/IntersectionObserver.svelte&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;script&amp;gt;
	import { onMount } from 'svelte';

	export let once = false;
	export let top = 0;
	export let bottom = 0;
	export let left = 0;
	export let right = 0;

	let intersecting = false;
	let container;

	onMount(() =&amp;gt; {
		if (typeof IntersectionObserver !== 'undefined') {
			const rootMargin = `${bottom}px ${left}px ${top}px ${right}px`;

			const observer = new IntersectionObserver(entries =&amp;gt; {
				intersecting = entries[0].isIntersecting;
				if (intersecting &amp;amp;&amp;amp; once) {
					observer.unobserve(container);
				}
			}, {
				rootMargin
			});

			observer.observe(container);
			return () =&amp;gt; observer.unobserve(container);
		}

		function handler() {
			const bcr = container.getBoundingClientRect();

			intersecting = (
				(bcr.bottom + bottom) &amp;gt; 0 &amp;amp;&amp;amp;
				(bcr.right + right) &amp;gt; 0 &amp;amp;&amp;amp;
				(bcr.top - top) &amp;lt; window.innerHeight &amp;amp;&amp;amp;
				(bcr.left - left) &amp;lt; window.innerWidth
			);

			if (intersecting &amp;amp;&amp;amp; once) {
				window.removeEventListener('scroll', handler);
			}
		}

		window.addEventListener('scroll', handler);
		return () =&amp;gt; window.removeEventListener('scroll', handler);
	});
&amp;lt;/script&amp;gt;

&amp;lt;style&amp;gt;
	div {
		width: 100%;
		height: 100%;
	}
&amp;lt;/style&amp;gt;

&amp;lt;div bind:this={container}&amp;gt;
	&amp;lt;slot {intersecting}&amp;gt;&amp;lt;/slot&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This component will give us an ability to wrap other components, and it will determine for us whether the wrapped component is visible (intersecting) within the viewport.&lt;/p&gt;

&lt;p&gt;If you’re familiar with the structure of Svelte components, you’ll see it follows the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script / style / markdown&lt;/code&gt; pattern. To begin with, it sets some options that we can pass in. These are a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;once&lt;/code&gt; property, along with numeric values for the top, right, bottom and left distances from the edge of the screen.&lt;/p&gt;

&lt;p&gt;We’ll ignore the distances but instead make use of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;once&lt;/code&gt; property. This will ensure the images only load once, as they enter the viewport.&lt;/p&gt;

&lt;p&gt;The main logic of the component is within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onMount&lt;/code&gt; section. This sets up our observer, which is used to check our element to determine if it’s “intersecting” with the visible area of the screen.&lt;/p&gt;

&lt;p&gt;It also attached a scroll event to check whether the element is visible as we scroll, and then it’ll also remove this listener if we’ve determined that it is viable and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;once&lt;/code&gt; is true.&lt;/p&gt;

&lt;h2 id=&quot;loading-the-images&quot;&gt;Loading the images&lt;/h2&gt;

&lt;p&gt;Let’s use our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IntersectionObserver.svelte&lt;/code&gt; component to conditionally load our images by wrapping it around an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image Loader&lt;/code&gt; component. Within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;components/Image&lt;/code&gt; create a new file called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ImageLoader&lt;/code&gt; containing:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;script&amp;gt;
  export let src
  export let alt

  import IntersectionObserver from './IntersectionObserver.svelte'
  import Image from './Image.svelte'

&amp;lt;/script&amp;gt;

&amp;lt;IntersectionObserver once={true} let:intersecting={intersecting}&amp;gt;
  {#if intersecting}
    &amp;lt;Image {alt} {src} /&amp;gt;
  {/if}
&amp;lt;/IntersectionObserver&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This component takes some image-related props (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alt&lt;/code&gt;) which we will use to create our image tag. It them imports two other components, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IntersectionObserver&lt;/code&gt; which we created already, and a new component we’ll create in a moment called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We then make use of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IntersectionObserver&lt;/code&gt; component. This has a couple of interesting things going on. First we are setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;once&lt;/code&gt; to true, so the image only loads the first time we see it.&lt;/p&gt;

&lt;p&gt;Then we make use of Svelte’s slot props.&lt;/p&gt;

&lt;h2 id=&quot;slot-props&quot;&gt;Slot Props&lt;/h2&gt;

&lt;p&gt;When we make use of a wrapping component we sometimes want to pass properties to the childen of the wrapper. Svelte gives us a way to do this called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slot props&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IntersectionObserver&lt;/code&gt; component you may have noticed this line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;slot {intersecting}&amp;gt;&amp;lt;/slot&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is passing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;intersecting&lt;/code&gt; prop into whatever component we give it. In this case, our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ImageLoader&lt;/code&gt; component will receive this when it uses the wrapper.&lt;/p&gt;

&lt;p&gt;We access this prop using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let:intersecting={intersecting}&lt;/code&gt; like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;IntersectionObserver once={true} let:intersecting={intersecting}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can then use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;intersecting&lt;/code&gt; value to determine whether to show the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt; component. In this case we’re using an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; condition to decide whether the show the image:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;IntersectionObserver once={true} let:intersecting={intersecting}&amp;gt;
  {#if intersecting}
    &amp;lt;Image {alt} {src} /&amp;gt;
  {/if}
&amp;lt;/IntersectionObserver&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we do, we show the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt; component and pass in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alt&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt; props.&lt;/p&gt;

&lt;p&gt;You can learn a bit more about slot props in &lt;a href=&quot;https://svelte.dev/tutorial/slot-props&quot;&gt;this tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We now have the code in place to show an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt; component when it is scrolled onto the screen. Let’s build the component itself.&lt;/p&gt;

&lt;h2 id=&quot;showing-image-on-load&quot;&gt;Showing image on load&lt;/h2&gt;

&lt;p&gt;In the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;components/Image&lt;/code&gt; folder make a new file called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image.svelte&lt;/code&gt;. This component will receive our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alt&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt; props, it’ll then set up an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img&lt;/code&gt; tag, listen for when it loads and then fade it in.&lt;/p&gt;

&lt;p&gt;Set up the component like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;script&amp;gt;
  export let src
  export let alt

  import { onMount } from 'svelte'

  let loaded = false
  let thisImage

  onMount(() =&amp;gt; {
    thisImage.onload = () =&amp;gt; {
      loaded = true
    }
  })

&amp;lt;/script&amp;gt;

&amp;lt;style&amp;gt;
  img {
    height: 200px;
    opacity: 0;
    transition: opacity 1200ms ease-out;
  }
  img.loaded {
    opacity: 1;
  }
&amp;lt;/style&amp;gt;

&amp;lt;img {src} {alt} class:loaded bind:this={thisImage} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we are getting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alt&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt; props. We also set up a couple of other variables, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loaded&lt;/code&gt; to store whether it’s loaded yet, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thisImage&lt;/code&gt; to store a reference to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img&lt;/code&gt; DOM element itself.&lt;/p&gt;

&lt;p&gt;We also make use of a helpful Svelte method, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onMount&lt;/code&gt;. This gives us a way to call functions when our components have been mounted in the browser. In this case we’ll set up a callback for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thisImage.onload&lt;/code&gt;. This will be executed when the image has finished loading, and it will set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loaded&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To actually show the loaded image we’ll use CSS. The initial state of the image will be to have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;opacity&lt;/code&gt; of 0. Then when loaded, we’ll set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;opacity&lt;/code&gt; to 1. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transition&lt;/code&gt; takes care of animating this from transparent to visible. We’re using a very slow transition time of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1200ms&lt;/code&gt; in this demo, but you might want to set it to something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200ms&lt;/code&gt; to make it more subtle.&lt;/p&gt;

&lt;p&gt;With all that set up, we apply our image tag like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;img {src} {alt} class:loaded bind:this={thisImage} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class:loaded&lt;/code&gt; to conditionally apply a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loaded&lt;/code&gt; class if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loaded&lt;/code&gt; variable is true.&lt;/p&gt;

&lt;p&gt;It lastly uses the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind:this&lt;/code&gt; method to associate this DOM element with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thisImage&lt;/code&gt; variable.&lt;/p&gt;

&lt;h2 id=&quot;using-our-imageloader&quot;&gt;Using our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ImageLoader&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Let’s actually use our component. Back in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;App.svelte&lt;/code&gt; file we can import out component and use it like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;script&amp;gt;
  import ImageLoader from './components/Image/ImageLoader.svelte';
&amp;lt;/script&amp;gt;

&amp;lt;ImageLoader src=&quot;OUR_IMAGE_URL&quot; alt=&quot;Our image&quot;&amp;gt;&amp;lt;/ImageLoader&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;working-demo&quot;&gt;Working demo&lt;/h2&gt;

&lt;p&gt;You can download the &lt;a href=&quot;https://github.com/donovanh/svelte-image-loading&quot;&gt;complete code for this demo on Github&lt;/a&gt;. Here’s a live example:&lt;/p&gt;

&lt;p class=&quot;codepen&quot; data-height=&quot;300&quot; data-theme-id=&quot;12592&quot; data-default-tab=&quot;js,result&quot; data-user=&quot;donovanh&quot; data-slug-hash=&quot;abdLERL&quot; style=&quot;height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;&quot; data-pen-title=&quot;Lazy Loading Images in Svelte&quot;&gt;
  &lt;span&gt;See the Pen &lt;a href=&quot;https://codepen.io/donovanh/pen/abdLERL&quot;&gt;
  Lazy Loading Images in Svelte&lt;/a&gt; by Donovan Hutchinson (&lt;a href=&quot;https://codepen.io/donovanh&quot;&gt;@donovanh&lt;/a&gt;)
  on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/span&gt;
&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;https://static.codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;see-it-in-the-wild&quot;&gt;See it in the wild&lt;/h2&gt;

&lt;p&gt;This approach is being used on my &lt;a href=&quot;https://www.shopireland.ie&quot;&gt;Amazon Ireland&lt;/a&gt; project on the home page, category pages and search pages to help make every page view faster. I hope you find it useful!&lt;/p&gt;

&lt;h2 id=&quot;well-thats-enough-about-me-your-turn&quot;&gt;Well that’s enough about me. Your turn!&lt;/h2&gt;

&lt;p&gt;Have you build a cool Svelte app you’d like to tell me about? You can message me &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;, I’d love to hear from you.&lt;/p&gt;
</description>
        <pubDate>Mon, 29 Jun 2020 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/preload-images-svelte/</link>
        <guid isPermaLink="true">http://hop.ie/blog/preload-images-svelte/</guid>
      </item>
    
      <item>
        <title>Hosting your own Svelte / Sapper app</title>
        <description>&lt;p&gt;You’ve build an amazing app using Sapper and Svelte, but now what? Where do we host it to make it available to the world? This article will set out the steps involved in one possible approach, self-hosting your own Sapper application using &lt;a href=&quot;https://m.do.co/c/cfc7d387f018&quot;&gt;DigitalOcean&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I recently went through this process having build an application and hosting it using Vercel. They have a slick deployment process, but as they better suit serverless applications I quickly realised I needed more than what they offered. So I rented a virtual server and moved the app there.&lt;/p&gt;

&lt;p&gt;If you like you can see my &lt;a href=&quot;https://www.shopireland.ie&quot;&gt;Amazon Ireland&lt;/a&gt; Sapper / Svelte project in action. It’s running on a &lt;a href=&quot;https://m.do.co/c/cfc7d387f018&quot;&gt;DigitalOcean&lt;/a&gt; droplet as a Node app, with an Nginx reverse proxy. I also have a second app running alongside that acts as an API layer to get product information from Amazon.&lt;/p&gt;

&lt;h2 id=&quot;what-we-cover-in-this-article&quot;&gt;What we cover in this article&lt;/h2&gt;

&lt;p&gt;In this article I’ll walk through the steps I took to set up a server to run Node projects such as Sapper applications. I hope this can act as a good starting point for you if you’re interested in running your own Svelte / Sapper app.&lt;/p&gt;

&lt;p&gt;Note: this is written based on Ubuntu version &lt;em&gt;18.04&lt;/em&gt;. Some specifics might have changed with newer versions.&lt;/p&gt;

&lt;h2 id=&quot;topics&quot;&gt;Topics&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#setting-up-your-digital-ocean-droplet&quot;&gt;Setting up your Digital Ocean droplet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#node-and-nginx-proxy-server&quot;&gt;Node and Nginx proxy server&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pushing-your-app-code-using-git&quot;&gt;Pushing your app code using Git&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#running-your-apps-using-pm2&quot;&gt;Running your app using PM2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#setting-up-nginx-server-block-with-caching&quot;&gt;Setting up Nginx server block with caching&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#adding-a-domain-name&quot;&gt;Adding a domain name&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#testing-your-site&quot;&gt;Testing your site&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;setting-up-your-digital-ocean-droplet&quot;&gt;Setting up your Digital Ocean droplet&lt;/h2&gt;

&lt;p&gt;My site only serves around 500 page views per day, so it’s not in need of a powerful hosting plan. I’m also familiar with Ubuntu so went with that.&lt;/p&gt;

&lt;p&gt;For the plan I chose a tiny $5-per-month virtual server. It’s easy to upgrade if needed and not much of an outlay to get started.&lt;/p&gt;

&lt;p&gt;Digital Ocean lets you chose a data centre location. Since my site targets Ireland primarily I chose London. This will hopefully result in better performance for my audience.&lt;/p&gt;

&lt;p&gt;Under additional options you might want to enable monitoring. However, it can be &lt;a href=&quot;https://www.digitalocean.com/docs/monitoring/&quot;&gt;easily added later&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For authentication, I recommend setting up SSH key access rather than root password. This means your server is not vulnerable to possible password-based hacking attempts. We will add a firewall later.&lt;/p&gt;

&lt;p&gt;Make sure and add the content of your public SSH key. This can be found with (Mac/Linux):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat ~/.ssh/id_rsa.pub
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If not, &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1804#step-1-—-create-the-rsa-key-pair&quot;&gt;the Create RSA Key Pair step in this guide should help&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally give your droplet a name, and we’re off!&lt;/p&gt;

&lt;h3 id=&quot;log-in-and-set-up-ssh-sudo-and-firewall&quot;&gt;Log in and set up SSH, sudo and firewall&lt;/h3&gt;

&lt;p&gt;We need to get some initial setup out of the way. Begin by logging in using ssh (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YOUR-IP&lt;/code&gt; is the IP address given during setup):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh root@YOUR_IP_ADDRESS
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once logged in, set up a user by &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04&quot;&gt;following this short guide&lt;/a&gt;. This should give you your own login using the same SSH key. You will then be able to log in to your server using (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; is whatever name you chose during the above steps):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh USER_NAME@YOUR_IP_ADDRESS
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You’ll only be able to access the server via SSH as the firwall blocks other ports, but we will fix that when installing Nginx. You’ll also be able to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Next we set up Node and the Nginx reverse proxy.&lt;/p&gt;

&lt;h2 id=&quot;node-and-nginx-proxy-server&quot;&gt;Node and Nginx proxy server&lt;/h2&gt;

&lt;p&gt;Since I’m setting up a Sapper application which uses &lt;a href=&quot;https://expressjs.com&quot;&gt;Express&lt;/a&gt; we need Node. Begin by adding some repository information to our system. The following downloads and runs a script that adds the necessary repo URLs.&lt;/p&gt;

&lt;p&gt;You can find &lt;a href=&quot;https://github.com/nodesource/distributions/tree/master/deb&quot;&gt;the scripts here&lt;/a&gt;, if you want to see what they do.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With that set up, install Node:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt install nodejs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once finished you should be able to see your Node version with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodejs -v&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One last thing, be sure to add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;build-essential&lt;/code&gt; package also as some packages will need it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt install build-essential
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;nginx&quot;&gt;Nginx&lt;/h3&gt;

&lt;p&gt;Nginx (pronounced Engine-X) is a lightweight, fast web server well suited to acting as a gateway to our Node application. It’s very powerful and can do a lot but we’ll mostly be leaning on it for our caching.&lt;/p&gt;

&lt;p&gt;We begin by updating and then installing the package.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt update
sudo apt install nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Before we can run it, we need to let the firewall know. For this guide I’m not going to install SSL directly on Nginx, but if you want to do so &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04&quot;&gt;this guide has more in-depth information&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let’s add Nginx HTTP access to the firewall:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo ufw allow 'Nginx HTTP'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can check the status of Nginx by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemctl status nginx&lt;/code&gt;. If it says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Active&lt;/code&gt;, you should be able to navigate to your IP address and see the success page.&lt;/p&gt;

&lt;p&gt;Next, we’ll leave the Nginx configuration aside for a moment and set up our Sapper application and API code.&lt;/p&gt;

&lt;h2 id=&quot;pushing-your-app-code-using-git&quot;&gt;Pushing your app code using Git&lt;/h2&gt;

&lt;p&gt;There are many ways of building, testing and deploying projects. For this project I wanted something quite simple. I knew there would only be one person working on it at a time and I didn’t want to introduce lots of layers of complexity.&lt;/p&gt;

&lt;p&gt;Back in the day I’d have turned to a tool such as FTP to push files up to the server. Thankfully we have better means of deploying these days, and one such approach is to use a Git hook.&lt;/p&gt;

&lt;p&gt;With a Git hook we can have our code copy automatically from our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master&lt;/code&gt; branch to the server. In the following steps I am reusing some of the code from &lt;a href=&quot;https://macarthur.me/posts/deploying-code-with-a-git-hook&quot;&gt;this overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We get started by creating our directories to store the code. You can call them anything you like but I’m going to go with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;repo&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www&lt;/code&gt; in this case. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;repo&lt;/code&gt; represents the Git repository that we push our code to, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www&lt;/code&gt; directory contains our live code.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~/
mkdir -p repo/YOUR_APP
mkdir -p www/YOUR_APP
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YOUR_APP&lt;/code&gt; part is your app name. Call it what you like but be sure to replace further references to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YOUR_APP&lt;/code&gt; with the same name.&lt;/p&gt;

&lt;p&gt;We navigate to our repo folder, and set up a bare Git repo.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~/repo/YOUR_APP
git init --bare
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we set up a &lt;a href=&quot;https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks&quot;&gt;Git hook&lt;/a&gt; to carry out the commands we want to run after we push to this repo.&lt;/p&gt;

&lt;p&gt;Now we create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post-receive&lt;/code&gt; hook. I’m using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nano&lt;/code&gt; to edit files on the server, but replace the command as needed. I will create and save the hook using:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd hooks
nano post-receive
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And adding the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#!/bin/bash

GIT_DIR=&quot;/home/don/repo/YOUR_APP&quot;
TARGET=&quot;/home/don/www/YOUR_APP&quot;

while read oldrev newrev ref
do
    BRANCH=$(git rev-parse --symbolic --abbrev-ref $ref)

    if [[ $BRANCH == &quot;master&quot; ]];
    then
        # Send a nice message to the machine pushing to this remote repository.
        echo &quot;Push received! Deploying branch: ${BRANCH}...&quot;

        # &quot;Deploy&quot; the branch we just pushed to a specific directory.
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
    else
        echo &quot;Not master branch. Skipping.&quot;
    fi

  # We will add other steps here

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

&lt;p&gt;And save the file with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Control+X&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before we continue we need to make this file executable:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;chmod +x post-receive
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above code sets up some variables (adjust the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YOUR_APP&lt;/code&gt; part as needed!) and then within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;done&lt;/code&gt; lines it runs code to apply our hook.&lt;/p&gt;

&lt;p&gt;Currently all it’s set up to do is copy our code, but only if we’ve pushed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;Let’s try it. To use this we need to add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;remote&lt;/code&gt; to our project. In our project folder on our &lt;em&gt;local&lt;/em&gt; machine, add the following remote:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git remote add origin USER_NAME@YOUR_IP_ADDRESS:/home/USER_NAME/repo/YOUR_APP
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All the shouty bits above, be sure to replace them with the relevant username, IP address and the name of your app directory.&lt;/p&gt;

&lt;p&gt;Now when you push to master (you may need to make a change first) you should see something like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;remote: Push received! Deploying branch: master...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Along with some other Git noises. If you then return to your server, and check in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www/YOUR_APP&lt;/code&gt; directory, you should see your app files!&lt;/p&gt;

&lt;p&gt;Before we move on, let’s make the Git deploy process easier by adding multiple origins. Adjust the following commands to include your Git repo location.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git remote set-url origin git@github.com:username/your-app.git
git remote set-url --add --push origin USER_NAME@YOUR_IP_ADDRESS:/home/USER_NAME/repo/YOUR_APP
git remote set-url --add --push origin git@github.com:username/your-app.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Many thanks to Alex for &lt;a href=&quot;https://macarthur.me/posts/deploying-code-with-a-git-hook&quot;&gt;his helpful article on this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that we can deploy code, let’s run our application.&lt;/p&gt;

&lt;h2 id=&quot;running-your-app-using-pm2&quot;&gt;Running your app using PM2&lt;/h2&gt;

&lt;p&gt;I usually run my Node applications using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm start&lt;/code&gt; (or sometimes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm run dev&lt;/code&gt; when working locaslly). On the server we could certainly do the same, but unless we use some kind of service to monitor our application, it could crash and become unresponsive.&lt;/p&gt;

&lt;p&gt;It’s good to have some kind of tool to automatically restart our app, as well as starting up any apps when our server restarts. &lt;a href=&quot;https://github.com/Unitech/pm2&quot;&gt;PM2&lt;/a&gt; is a useful tool that manages our applications and ensures they stay up.&lt;/p&gt;

&lt;p&gt;Begin by installing it globally so that it can be used wherever.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo npm install pm2 -g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;running-a-sapper-app&quot;&gt;Running a Sapper app&lt;/h3&gt;

&lt;p&gt;Let’s get the app running. First we need to install dependencies.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~/www/YOUR_APP
npm install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Before we run our Sapper app we need to build it. While working in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev&lt;/code&gt; environment we don’t need the build step as it compiles for it, we do need this step when running in production. So before we start running our app, build it with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This should output a lot of lines of content as it builds all the scoped JavaScript fragments of your app. We can then run the app using PM2.&lt;/p&gt;

&lt;p&gt;While we can set up PM2 for most Node apps with a simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pm2 start app.js --name App&lt;/code&gt;, we need to use our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm start&lt;/code&gt; command. We can do it like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pm2 start npm --name &quot;AppName&quot; -- start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With that running, we save the current state of the PM2 system with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pm2 save
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also check the status of your running apps any time with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pm2 list&lt;/code&gt;. See &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pm2 examples&lt;/code&gt; for more.&lt;/p&gt;

&lt;p&gt;Lastly we want to make sure the app loads when the server restarts. Set this up with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo env PATH=$PATH:/usr/local/bin pm2 startup -u USER_NAME
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Be sure to replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USER_NAME&lt;/code&gt; with your actual chosen username as before.&lt;/p&gt;

&lt;p&gt;You can now test your app. If all went to plan, it should be running on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3000&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you get an error here, check your code is all in place and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pm2 restart AppName&lt;/code&gt; to restart it.&lt;/p&gt;

&lt;h3 id=&quot;improving-the-git-hook&quot;&gt;Improving the Git hook&lt;/h3&gt;

&lt;p&gt;With the app running, we can now improve our Git hook to have it handle the build step, run npm install, and restart PM2 on ever deploy. Update our hook by adding the following before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;done&lt;/code&gt; line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /home/don/www/YOUR_APP
npm install --production
npm run build
pm2 restart AppName
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again be careful to replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YOUR_APP&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppName&lt;/code&gt; with your values.&lt;/p&gt;

&lt;p&gt;Now when you push to master, it should install dependencies, build the app, and restart the app using PM2.&lt;/p&gt;

&lt;h2 id=&quot;setting-up-nginx-server-block-with-caching&quot;&gt;Setting up Nginx server block with caching&lt;/h2&gt;

&lt;p&gt;We have our app running and we get the expected output at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:3000&lt;/code&gt; so the next step is to let Nginx act as a reverse proxy. This will catch any web requests to port 80, and direct them to our app (or a cached version).&lt;/p&gt;

&lt;h3 id=&quot;caching&quot;&gt;Caching&lt;/h3&gt;

&lt;p&gt;Before we set up our server block to tell Nginx where to find our app, we will quickly set up caching. Nginx has a cache option that looks at the headers sent back from our app and saves a copy of the request to disk. It then returns this saved version to each new request until the cache expires.&lt;/p&gt;

&lt;p&gt;Begin by creating a directory for our cache to be stored:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo mkdir /var/cache/nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will then configure the cache settings in our server block.&lt;/p&gt;

&lt;h3 id=&quot;adding-the-server-block&quot;&gt;Adding the server block&lt;/h3&gt;

&lt;p&gt;We begin by setting up an entry in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sites-available&lt;/code&gt; directory.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/YOUR_SITE.vhost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this file we add some directives to tell Nginx how to serve up our app.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=480m use_temp_path=off;

server {
       listen 80;
       listen [::]:80;

       server_name example.com;

       location / {
          proxy_cache my_cache;
          proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
          proxy_cache_revalidate on;
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
       }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Exit and save using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Control + X, y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the start, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;proxy_cache_path&lt;/code&gt; sets a location for the cache along with how many &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;levels&lt;/code&gt; to store (this can be tweaked if you think you’ll be storing millions of requests). The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keys_zone&lt;/code&gt; part sets a named zone and how big it is, in memory, for storing metadata about the cached content.&lt;/p&gt;

&lt;p&gt;We set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_size&lt;/code&gt; as the maximum amount of space the cache can take up. In this case it’s a generous &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10g&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We set an optional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inactive&lt;/code&gt; value to 8 hours in this case, and set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_temp_path&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;off&lt;/code&gt; as this saves a little bit of performance as it no longer writes content twice.&lt;/p&gt;

&lt;p&gt;Next up is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server&lt;/code&gt; block itself. In this we set the port, set up a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;location&lt;/code&gt; and tell the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;proxy_pass&lt;/code&gt; to pass requests through to our app on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://localhost:3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We also tell the server where to find the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;proxy_cache&lt;/code&gt; that we defined above, and we use a very interesting optional setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;proxy_cache_use_stale&lt;/code&gt;. This tells the server to use old cached entries if for some reason the file returns an error.&lt;/p&gt;

&lt;p&gt;There are lots more settings available, and you can find out more &lt;a href=&quot;https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/&quot;&gt;in this article containing the full Nginx caching options&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;activating-the-server-block&quot;&gt;Activating the server block&lt;/h3&gt;

&lt;p&gt;We now have a file in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sites-available&lt;/code&gt; but we need to activate it by creating a link to it from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sites-enabled&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/YOUR_SITE.vhost /etc/nginx/sites-enabled/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There could be a “default” file or similar alread in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sites-enabled&lt;/code&gt; directory. We don’t need that so can delete it.&lt;/p&gt;

&lt;p&gt;Now that we have the file linked from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sites-enabled&lt;/code&gt; we can test to check our config is ok using:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If all is not well, check for typos or missing semi-colons! If it is, restart Nginx to activate the changes:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo service nginx restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We should now be able to access our content on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;80&lt;/code&gt; via this Nginx reverse proxy.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl http://localhost:80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If all is well, let’s move on to setting up our public URL and HTTPS.&lt;/p&gt;

&lt;h2 id=&quot;adding-a-domain-name&quot;&gt;Adding a domain name&lt;/h2&gt;

&lt;p&gt;With an app running on port 80 and an IP address, it’s time to give this app a home in the form of a URL.&lt;/p&gt;

&lt;p&gt;There are many ways to handle domain names but something I like to do is use &lt;a href=&quot;https://www.cloudflare.com&quot;&gt;CloudFlare&lt;/a&gt; and make use of their free “flexible” SSL service. It’s not 100% secure, but can be upgraded to provide end to end encryption for sites or apps that deal in sensitive data.&lt;/p&gt;

&lt;p&gt;In this case my app has no log in component and doesn’t send any sensitive data to or from the browser so I’m content with their most simple SSL setting. If you want to add more you can &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04&quot;&gt;set up your own SSL&lt;/a&gt; or use another service.&lt;/p&gt;

&lt;p&gt;So if we’re using CloudFlare, head over and register. Then add a new site and select the Flexible SSL option.&lt;/p&gt;

&lt;p&gt;If you have a domain already, it will prompt you to move the records. You might not want to keep the old records but instead take this opportunity to add in an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; record pointing to your server’s IP address, and a CNAME for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www&lt;/code&gt; pointing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YOUR_DOMAIN.rocks&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once set up you will be prompted to add two name servers to your domain. Take note of these.&lt;/p&gt;

&lt;h3 id=&quot;setting-up-your-nameservers&quot;&gt;Setting up your nameservers&lt;/h3&gt;

&lt;p&gt;Log in to your registrar and navigate to the section where you set the name servers. Update the list to contain just the two given by CloudFlare.&lt;/p&gt;

&lt;p&gt;Then, return to CloudFlare and press the button to continue. It may take a few hours for the updated nameservers to be detected. Once it detects the changes it will email you, and your site is live!&lt;/p&gt;

&lt;h2 id=&quot;testing-your-site&quot;&gt;Testing your site&lt;/h2&gt;

&lt;p&gt;You should now be able to access your app at your own domain name, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://YOUR_DOMAIN.rocks&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;troubleshooting&quot;&gt;Troubleshooting&lt;/h3&gt;

&lt;p&gt;So what should you do if you see the dreaded &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;520&lt;/code&gt; error? A few things to try:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Check your app has been built with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm run build&lt;/code&gt; and that it is successful&lt;/li&gt;
  &lt;li&gt;Check &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pm2 list&lt;/code&gt; to see the app is running&lt;/li&gt;
  &lt;li&gt;Look for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Actice&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo service nginx status&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Double-check the location server block to ensure it’s looking for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost:3000&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lastly, you can also check logs for both at their default locations using:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I hope this article is useful and helps you get your awesome Sapper projects onto the web!&lt;/p&gt;

&lt;h2 id=&quot;well-thats-enough-about-me-your-turn&quot;&gt;Well that’s enough about me. Your turn!&lt;/h2&gt;

&lt;p&gt;Have you build a cool Svelte app you’d like to tell me about? You can message me &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;, I’d love to hear from you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originaly &lt;a href=&quot;https://blog.logrocket.com/hosting-your-own-svelte-sapper-app/&quot;&gt;posted on LogRocket&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 26 Jun 2020 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/svelte-sapper-deploy/</link>
        <guid isPermaLink="true">http://hop.ie/blog/svelte-sapper-deploy/</guid>
      </item>
    
      <item>
        <title>Keeping it simple</title>
        <description>&lt;p&gt;Choosing a workout routine is a daunting process. They all seem complicated or difficult to stick to. Some plans set out in detail dozens of exercises, sets and weekly routines. Other plans make you sweat hard and give it all you have multiple times per week. Neither of these work for me.&lt;/p&gt;

&lt;p&gt;Last month I started a new workout plan that included 6 exercises every day, with the goal being that if I could keep it simple, and stick to it, it would be a success. I had a few requirements:&lt;/p&gt;

&lt;h3 id=&quot;no-gym&quot;&gt;No gym&lt;/h3&gt;

&lt;p&gt;I don’t want to travel each day to exercise.&lt;/p&gt;

&lt;h3 id=&quot;easy-to-understand&quot;&gt;Easy to understand&lt;/h3&gt;

&lt;p&gt;A bare minumum of exercises targeting all the main body parts.&lt;/p&gt;

&lt;h3 id=&quot;easy-to-progress&quot;&gt;Easy to progress&lt;/h3&gt;

&lt;p&gt;Doing the same exercises all the time is not only boring, it doesn’t have as good a result as when you make them gradually more difficult.&lt;/p&gt;

&lt;h3 id=&quot;the-right-intensity&quot;&gt;The right intensity&lt;/h3&gt;

&lt;p&gt;I burn out quickly with circuit training, and get tired of big 1-hour+ workouts.&lt;/p&gt;

&lt;h3 id=&quot;easy-to-log&quot;&gt;Easy to log&lt;/h3&gt;

&lt;p&gt;Being able to write down what I’ve done quickly will mean I actually write it down.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, how did it go?&lt;/strong&gt; After a full month it’s going great. Here’s a log of the month of March:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/exercise/workout-march-2018.jpg&quot; alt=&quot;March 2018 workout stats&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It looks like a lot of detail but this is an entire month of exercise. Along the top I’ve set up columns for the 7 things I do. This includes running, pushups, leg raises, squats, pullups, bridges and handstands.&lt;/p&gt;

&lt;p&gt;Each day I add in an extra row, and note the numbers as I do them. It’s easy to see how I progressed in each area, for the most part. The numbers don’t show how I improved in form in a couple of things, such as pushups and bridges, but you can see definite progress in pullups, bridges and squats.&lt;/p&gt;

&lt;p&gt;Most days I do one set of each. It’s not a very intense workout - I don’t even work up a sweat, and it’s done in about 20 minutes. The trickiest part about it is finding my pullup bar.&lt;/p&gt;

&lt;p&gt;I did do multiple sets on a few days, and I may try to do more over time but I don’t want to overdo it. This is also why I didn’t run every day. I enjoy running but there’s more prep and time needed for it than I can set aside each day. Plus I’m finding cardio is a bit overrated when it comes to strength and overall health. Two or three times per week, with moderate length runs (about 5k) works well.&lt;/p&gt;

&lt;p&gt;This plan seems to be working for me. I think this is why:&lt;/p&gt;

&lt;h2 id=&quot;easy-to-understand-1&quot;&gt;Easy to understand&lt;/h2&gt;

&lt;p&gt;There’s all sorts of exercises to choose from, as well as a crazy amount of choice of equipment. I find all that a bit distracting and tiriung and find myself paralysed by such choice. End result, I do none of the exercises. So to keep myself motivated I need to make things as simple as I can. In this case I have 6 very basic movements, and I run through them one at a time. In case you missed it, I go into &lt;a href=&quot;http://hop.ie/blog/exercise-plan/&quot;&gt;more detail on my exercise plan here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With these 6 exercises there’s no thinking, I just get onto the floor (or wall, or pullup bar) and do them.&lt;/p&gt;

&lt;p&gt;This helps me remain consistent. Consistency is important, as without it I’d be just doing random exercises and making no progress. With a consistent plan, I get two big advantages: I can monitor / adjust depending on how I feel to ensure I avoid injury, and I can progress the exercise to get better.&lt;/p&gt;

&lt;p&gt;It’s very difficult to make improvement and see gains in my fitness and strength without progression. Thankfully these 6 movements are very easy to progress.&lt;/p&gt;

&lt;h2 id=&quot;easy-to-progress-1&quot;&gt;Easy to progress&lt;/h2&gt;

&lt;p&gt;Did I mention how these exercises are easy to progress? One of the criticisms of callisthenics and bodyweight exercise is how it’s trickier to make the exercises harder over time. There’s some merit to this - if you’re using weights you can just add on another number or two to the bar and you see the progress. With my own bodyweight, I could in theory put on a backpack with weights in it. But there are better ways.&lt;/p&gt;

&lt;h3 id=&quot;more-reps-and-sets&quot;&gt;More reps and sets&lt;/h3&gt;

&lt;p&gt;This is the simplest way to progress. Just do more! Within a certain range this works well. For example you might go from being able to do 5 pushups to 12 over a few weeks. That’s progress! But the notion of diminishing returns applies. Going from 40 to 50 reps might not be the right approach.&lt;/p&gt;

&lt;p&gt;Another area to work on is the number of sets, and the “rest” time between sets. If you’re doing one set of 20 pushups - you could break that out into two sets of 12, with a short rest between. This means you increase your overall volume (24 pushups instead of 20) as well as give yourself another way to progress. You could then aim to progress that to 3 sets of 12 over time, or adjust the rest time between the sets.&lt;/p&gt;

&lt;h3 id=&quot;adjusting-technique--speed&quot;&gt;Adjusting technique / speed&lt;/h3&gt;

&lt;p&gt;Sometimes just slowing things down can make a movement so much harder. For example a 3-second pushup is much harder than a 1-second version. Try the same number of reps, but slow them down and you might find them much more effective.&lt;/p&gt;

&lt;p&gt;This can go hand-in-hand with technique. Paying attention to which muscles are being worked and making sure your form is good can make the exercises much more effective too. After a year of practicing pushups I’m still learning ways to make them better. Get onto Youtube, do some research on proper form, slow down and try to do every rep properly.&lt;/p&gt;

&lt;h3 id=&quot;variations&quot;&gt;Variations&lt;/h3&gt;

&lt;p&gt;Once you have the form right and you can do the movements in a slow, controlled way, and you have hit a high number of reps (I hear something like 12 - 15 is a good target), then it’s time to mix things up with variations.&lt;/p&gt;

&lt;p&gt;With pushups, you could start by bringing the hands closer to your centre line. This makes them harder and puts more pressure on the triceps. The target movement would be a “diamond” pushup with the two hands together in the centre.&lt;/p&gt;

&lt;p&gt;Then if this gets each, keep one hand in the centre and start moving the other hand out to the side (be sure to mirror this so that you stay balanced). This way you can work up toward “archer” pushups and eventually the fabled one-handed pushup.&lt;/p&gt;

&lt;p&gt;Similar approaches work for pullups, squats and other movements. Bring things closer to the centre initially then focus on one arm / leg at a time. There’s a load of progression that can happen and the difficulty increases each time.&lt;/p&gt;

&lt;p&gt;Speaking of time - if you’re like me you’ll find that this stuff takes longer than you expect. Think months, not days or weeks. It can be hard to stay motivated for months. At least that’s what I’ve found. I tend to burn out and lose interest if routines are too intense. So to make sure this doesn’t happen, it’s all about finding the right intensity.&lt;/p&gt;

&lt;h2 id=&quot;the-right-intensity-1&quot;&gt;The right intensity&lt;/h2&gt;

&lt;p&gt;When I start new routines I tend to start off very enthusiastic and then lose interest and burn out over time. With this in mind my current routine is deliberately &lt;em&gt;less&lt;/em&gt; intense than I’d usually go for. It’s enough to get my heart rate up a bit, and activates all muscle groups, but not intense enough to break a sweat.&lt;/p&gt;

&lt;p&gt;I want to be able to continue this over the long term. Plus, starting with a low intensity brings some useful benefits. There’s less chance of injury, as I’m giving my body time to adapt to the exercise routine. I’m happy to do it every day as I know it’s quite light and doesn’t take long.&lt;/p&gt;

&lt;p&gt;At the same time, it’s not &lt;em&gt;too&lt;/em&gt; easy. I push myself a little in each set, either with intensity or volume. I also try to gradually improve my technique each time. One tool I find very valuable in making sure I progress is keeping a log.&lt;/p&gt;

&lt;h2 id=&quot;easy-to-log-1&quot;&gt;Easy to log&lt;/h2&gt;

&lt;p&gt;This is an essential tool if I aim to keep up this routine long term. Without a log I’d be likely to lose interest, not progress or even regress in my workouts.&lt;/p&gt;

&lt;p&gt;It could be a nuisance to create and maintain a log so I try to keep it as simple as it needs to be. In this case I set up a row for each day, and log the number of reps and sets of each movement. If there’s something special to note I might add a little more detail, but generally it’s just the numbers.&lt;/p&gt;

&lt;p&gt;This puts the majority of the data onto the page and out of my head. I can then check over time and see what’s improving, or not.&lt;/p&gt;

&lt;p&gt;With it being quite easy to log it’s becoming a habit.&lt;/p&gt;

&lt;h2 id=&quot;take-it-slow&quot;&gt;Take it slow&lt;/h2&gt;

&lt;p&gt;This is a plan that’s working for me, and one I intend to tweak and change over time. You might want to do something similar, or maybe it’s really not for you at all. That’s fine. But if you do start regular exercise, listen to your body and take it slow. Progress in exercise is something you measure in months and years, not weeks.&lt;/p&gt;

&lt;p&gt;My goal is to be kind to myself, start slow, and gradually increase the intensity over time.&lt;/p&gt;

&lt;h2 id=&quot;how-about-you&quot;&gt;How about you?&lt;/h2&gt;

&lt;p&gt;What are you doing to stay in shape? You can message me &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;, I’d love to hear from you.&lt;/p&gt;
</description>
        <pubDate>Sun, 01 Apr 2018 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/keeping-it-simple/</link>
        <guid isPermaLink="true">http://hop.ie/blog/keeping-it-simple/</guid>
      </item>
    
      <item>
        <title>Creating an exercise plan</title>
        <description>&lt;p&gt;Last week I began a new routine to try to get back some of the strength and fitness I’d lost after 4 months of laziness. This post is an update on how it’s going, and I’d like to discuss my approach in a little more detail.&lt;/p&gt;

&lt;h2 id=&quot;my-goals&quot;&gt;My goals&lt;/h2&gt;

&lt;p&gt;Having been out of the bodyweight routine for quite a while my goal was to signal to my body that it needs to be ready to do all the old moves again. The last thing I wanted was to jump stright back into a heavy routine of workouts and risk injury or frustration, so my goal is to bring exercise back into my life in a sustainable way.&lt;/p&gt;

&lt;p&gt;We all have different goals. Your goals may not be similar to mine, and the plan set out here might not be for you. I’ve been experimenting with bodyweight exercise routines and gone through a few different variations over the past year. It’s good to try things and see what works, and be sure to check out the resources I’ll link below if you’re doing something like this for yourself.&lt;/p&gt;

&lt;h2 id=&quot;being-your-own-coach&quot;&gt;Being your own coach&lt;/h2&gt;

&lt;p&gt;I don’t have a gym membership or access to any coaches, so I’ve been doing my best with online research and experimenting with that I read and watch online. Being in charge of my own routine means being both a motivator as well as being my own physio. I need to watch out for signs of potential injury before they happen as well as look for opportunities to improve.&lt;/p&gt;

&lt;p&gt;To achieve this the best tool I’ve found is pen and paper. I keep a lot of each set and exercise I do, and occasionally add notes if something was unusual about a workout or day.&lt;/p&gt;

&lt;p&gt;Any plan is better than no plan. With that in mind I put together a routine that includes each of the main 6 exercises set out in Paul Wade’s &lt;a href=&quot;https://www.dragondoor.com/b41/&quot;&gt;Convict Conditioning&lt;/a&gt;. These exercises are doable with almost no equipment. The moves also have loads of room for progression, which is a big part of how I can maintain motivation and results.&lt;/p&gt;

&lt;h2 id=&quot;the-plan&quot;&gt;The plan&lt;/h2&gt;

&lt;p&gt;There is any number of ways to approach setting out a routine, but I wanted to set up a regular baseline of exercise I could perform daily without tiring myself out. For this it means 1 set of each exercise each day. Here’s how the last 10 days have gone:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/exercise/workout-feb-2018.jpg&quot; alt=&quot;February 2018 workout stats&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I started off a little enthusiastic, doing two sets each day but I quickly started to feel it was too much, and instead moved to one set per day. This has allowed more time for recovery and I don’t dread doing the exercises so much!&lt;/p&gt;

&lt;p&gt;As you can see, I started out quite well but regressed in a few areas in the first few days. Some of these have been improving, such as pushups, bridges and my most hated move, leg raises.&lt;/p&gt;

&lt;p&gt;This workout targets all parts of the body. It has push exercises, pull as well as balance.&lt;/p&gt;

&lt;p&gt;Here’s where I’m at with each and what I hope to progress to.&lt;/p&gt;

&lt;h3 id=&quot;push-ups&quot;&gt;Push ups&lt;/h3&gt;

&lt;p&gt;My push ups approach is back to the basic shoulder-width normal pushup form. I’m focusing on quality with each rep. The better I get at these the harder they get! If you’re trying to improve this area, &lt;a href=&quot;https://www.youtube.com/watch?v=IODxDxX7oi4&quot;&gt;checkout this video&lt;/a&gt; (keep the elbows in!). It’s amazing how much room for improvement I keep finding within such a simple movement.&lt;/p&gt;

&lt;p&gt;Progression: Rather than simply increase reps I’ll progress this move by bringing the hands closer together toward diamond push ups.&lt;/p&gt;

&lt;h3 id=&quot;leg-raises&quot;&gt;Leg raises&lt;/h3&gt;

&lt;p&gt;I hates these so much but they work amazing well for the abs and corset muscles around the waist. I &lt;a href=&quot;https://www.youtube.com/watch?v=pIvDbMEdAxI&quot;&gt;avoid situps&lt;/a&gt; as they don’t tend to be very effective and give me a sore back.&lt;/p&gt;

&lt;p&gt;Knee and leg raises are better. Currently I do leg raises lying down on my back, trying to keep the movement slow and steady and not letting my feet touch the ground between repetitions.&lt;/p&gt;

&lt;p&gt;I think a big part of what makes these difficult for me is the range of motion needed, I’m not as flexible as I need to be, so that’s something to work on! Keeping my legs straight all the way through is tough but is improving with practice.&lt;/p&gt;

&lt;p&gt;Progression: I’d like to move toward handing knee-raises as the next step.&lt;/p&gt;

&lt;h3 id=&quot;squats&quot;&gt;Squats&lt;/h3&gt;

&lt;p&gt;I used to have a lot of knee pain when running, but squats were a big part of fixing that. Squats are a very practical (functional?) exercise that helps walking, running, picking stuff up - loads of great day-to-day benefits. I love squats.&lt;/p&gt;

&lt;p&gt;I don’t use weights for the squats but instead make them a little harder by keeping my feet together. A “close” squat is more difficult than a wider stance, and after 30 or so reps I can really feel the burn. As with any other exercise, taking them slow is better than fast.&lt;/p&gt;

&lt;p&gt;You can &lt;a href=&quot;https://www.youtube.com/watch?v=X0qC1k0Zi6k&amp;amp;t=319s&quot;&gt;learn more about squats here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Progression: I’m aiming to work toward one-legged (pistol) squats. Will be a while before I get to that level!&lt;/p&gt;

&lt;h3 id=&quot;pull-ups&quot;&gt;Pull ups&lt;/h3&gt;

&lt;p&gt;Pull ups are a difficult exercise at the best of times, but with weighing 90kg and being over 6”3 tall, I’ve always struggled with them. It took many months of slowly practising each week to get to the point where I could do 5 good pull ups with a full range of motion.&lt;/p&gt;

&lt;p&gt;I started with half-pull ups and by jumping up and trying to bring myself down slowly (negatives). This is the only movement that requires any equipment, and I use a door-mounted pull up bar. I’m really keen to improve these as they have a massive impact to my back and upper half of my body so I’ve been putting the pull up bar on my home office door and doing sets through the day.&lt;/p&gt;

&lt;p&gt;If you’re looking to improve in this area, check out &lt;a href=&quot;https://www.youtube.com/watch?v=eGo4IYlbE5g&quot;&gt;this video first&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Progression: My goal for this is to get closer to 10 good reps in one set. Keeping the full range of motion and without kipping.&lt;/p&gt;

&lt;h3 id=&quot;bridges&quot;&gt;Bridges&lt;/h3&gt;

&lt;p&gt;Bridges are a massively underrated back exercise. I’m guilty of not giving one enough attention over the last year but since I got back into them I’m finding my back pain from sitting has massively reduced. Here’s &lt;a href=&quot;https://www.youtube.com/watch?v=WhYis4MMoak&quot;&gt;how to do bridges&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;My shoulder mobility is far from perfect so I’ve a long way to go with these but even within the last 10 days I’m seeing improvement. I’ve moved on from short bridges to an almost-full bridge.&lt;/p&gt;

&lt;p&gt;Progression: I’d like to get to a full bridge by building up the shoulder mobility and strength. This will help with handstands!&lt;/p&gt;

&lt;h3 id=&quot;handstands&quot;&gt;Handstands&lt;/h3&gt;

&lt;p&gt;Speaking of which, I am doing a handstand or two each day. I was inspired by &lt;a href=&quot;https://www.youtube.com/watch?v=ctunmnwbbSI&amp;amp;t=199s&quot;&gt;this overview&lt;/a&gt; to give this a go. Progress is very slow, I can hold a handstand against the wall for about 30 seconds before collapsing in a quivering heap.&lt;/p&gt;

&lt;p&gt;Progression: I’m currently facing the wall with my belly toward it. The next step will be to be able to kick into a handstand with my back to the wall. Progress is slow on this but it’s a goal for this year to do one free-standing handstand eventually.&lt;/p&gt;

&lt;h2 id=&quot;how-often-to-exercise&quot;&gt;How often to exercise?&lt;/h2&gt;

&lt;p&gt;So this is the exercises, the next decision was how often to exercise. My goal was to create a habit rather than try to do too much at once. Something I’m keen to avoid is burning out, or even worse, injury and so I decided to try to stick to a light routine I can do each day.&lt;/p&gt;

&lt;p&gt;One benefit of this routine is that it doesn’t leave me completely wrecked. I feel I have energy left over, which means I start the day energised rather than drained. I also look forward to it, and find I can get the routine finished up quite quickly.&lt;/p&gt;

&lt;p&gt;The progression from this routine would be to isolate one exercise to focus on each day, and maintain the single set in all exercises at the same time. I’ll see if that works, but it’s too early to start making that kind of plan.&lt;/p&gt;

&lt;p&gt;If you’re approaching making a workout plan like this yourself, always keep in mind that it’s ok to adjust and tweak it as you go. Only you know your own body and how it feels.&lt;/p&gt;

&lt;h2 id=&quot;how-about-you&quot;&gt;How about you?&lt;/h2&gt;

&lt;p&gt;How are your new years resolutions working out? Got any goals for the year? Mention me up &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;, I’d love to hear from you.&lt;/p&gt;
</description>
        <pubDate>Mon, 26 Feb 2018 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/exercise-plan/</link>
        <guid isPermaLink="true">http://hop.ie/blog/exercise-plan/</guid>
      </item>
    
      <item>
        <title>Resuming Exercise</title>
        <description>&lt;p&gt;It’s February and the time when most people’s new year resolutions have fallen away, so I’m getting started on this year’s. My goal this year is simple: do &lt;em&gt;one&lt;/em&gt; free-standing handstand.&lt;/p&gt;

&lt;p&gt;It’s a simple goal to state but takes a bit of preparation and work to get there. A big part of the challenge is that I’ve not been exercising much since last October and I need to get back into it.&lt;/p&gt;

&lt;h2 id=&quot;getting-back-into-exercise&quot;&gt;Getting back into exercise&lt;/h2&gt;

&lt;p&gt;It’s hard to begin exercise, and even harder to continue it long term. I found this last year when I had got myself into a good routine of bodyweight exercises and by the late summer was seeing great results. Then I got a bad head cold, followed by school holidays, then Christmas, along with all sorts of delicious unhealthy food and absolutely no exercise.&lt;/p&gt;

&lt;p&gt;One caveat - I’m still running and have been out most weeks at least once or twice for 5 to 7km of a run each time. When I say I’ve not been exercising, I don’t really count the running. What I stopped was the bodyweight exercise routine. Here’s how it worked.&lt;/p&gt;

&lt;h2 id=&quot;my-routine&quot;&gt;My routine&lt;/h2&gt;

&lt;p&gt;Before the multi-month gap, I had been following a weekly routine that involved a “focus” exercise each day along with a bunch of others. The “focus” exercise I would do multiple sets of, and then one set of each other exercise.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Monday: Pushups&lt;/li&gt;
  &lt;li&gt;Tuesday: Leg raises&lt;/li&gt;
  &lt;li&gt;Wednesday: Squats&lt;/li&gt;
  &lt;li&gt;Thursday: Bridges&lt;/li&gt;
  &lt;li&gt;Friday: Pull-ups&lt;/li&gt;
  &lt;li&gt;Saturday: Handstands&lt;/li&gt;
  &lt;li&gt;Sunday: One set of each&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It was fairly intense, doing multiple sets of one exercise but I had worked up to this point gradually and it felt good. As long as I could spare 30 minutes each day I could do all the routine.&lt;/p&gt;

&lt;h2 id=&quot;starting-again&quot;&gt;Starting again&lt;/h2&gt;

&lt;p&gt;Since I let things go, I’m now facing a bit of an uphill climb to get back to where I was. My approach begins this week. To get started I’m going to do one or two light sets of each exercise daily.&lt;/p&gt;

&lt;p&gt;The idea is that by doing a light routine each day, it’ll prompt the old muscles to wake up and realise that they’re going to be needed again.&lt;/p&gt;

&lt;p&gt;Last week I began by doing two sets of each of the above exercises. Being light sets, it was along the lines of 15 pushups, 10 leg raises, 5 pullups etc.&lt;/p&gt;

&lt;p&gt;It wasn’t easy. Even though the sets were quite light I did struggle toward the end of them. When I came back the next day I was also weaker and had to struggle more.&lt;/p&gt;

&lt;h2 id=&quot;progress-so-far&quot;&gt;Progress so far&lt;/h2&gt;

&lt;p&gt;It’s early days but I’m definitely feeling the result - a bit stiff but not quite sore. I’m hoping this workout routine will be a good way to prompt my body back into place and I’ll be able to catch up to where I was last summer.&lt;/p&gt;

&lt;p&gt;Finding time has been a challenge. With a couple of small kids and lots of work to do, it’s hard to really set aside the 30 to 45 mins I’d need to do this. I like to get up a little earlier than the kids in the morning and get a workout in before the day starts, so most days I take this approach. If that doesn’t work out, I can usually get a little time in the early evening. Motivation at that point of the day is tougher though, I feel like my reserves of motivation dry up as the day goes on so generally best to try to get the exercise done first.&lt;/p&gt;

&lt;p&gt;As a side effect it definitely lifts my mood for the rest of the day.&lt;/p&gt;

&lt;h2 id=&quot;diet&quot;&gt;Diet&lt;/h2&gt;

&lt;p&gt;Along with working out, it’s important to feed the body properly. I’ve been neglecting myself for a while, indulging in all the snacks and fast food. As a result I’m carrying more weight than I’d like (it makes the pullups harder!). To fix this I’ve put MyFitnessPal back on the home screen and I’m tracking my food, to make sure I’m getting enough protein and vitamins, along with taking it a little easier on the carbs and sugar.&lt;/p&gt;

&lt;p&gt;I’m not being all “diet” about it though, I still had a bowl of cheerios this morning for breakfast and I’ll look forward to a nice burger later, but in between I’ll reach for an apple before making a sandwich if I’m peckish. A sensible approach to eating shouldn’t feel unsatisfying.&lt;/p&gt;

&lt;h2 id=&quot;sleep&quot;&gt;Sleep&lt;/h2&gt;

&lt;p&gt;The last part, which is also important, is sleep. When adding in extra stress in the form of exercise it’s important to give time for the body to recover. Getting the right food is part of the recovery, and then getting an early night helps a lot too.&lt;/p&gt;

&lt;p&gt;One thing I’ve found that really helps my sleep is cutting back on the alcohol. Even one beer in an evening has a measurable impact on my sleeping heart rate, pushing it up from the mid-50s to mid-60s. I suspect this results in less useful sleep.&lt;/p&gt;

&lt;p&gt;Having cut that out of my evenings, and making a point of being in bed a little earlier, I’ve seen a definite improvement to both the quality and amount of sleep. I’m hoping this will be good both mentally as well as physically.&lt;/p&gt;

&lt;h2 id=&quot;one-handstand&quot;&gt;One handstand&lt;/h2&gt;

&lt;p&gt;The resolution this is all working toward is being able to do &lt;em&gt;one&lt;/em&gt; handstand. So far I still feel a long way away from that. I can hold myself up against a wall for 20 seconds, arms shaking and gasping the whole time, but if I get everything else in place I am sure I can do it.&lt;/p&gt;

&lt;h2 id=&quot;how-about-you&quot;&gt;How about you?&lt;/h2&gt;

&lt;p&gt;How are your new years resolutions working out? Got any goals for the year? Mention me up &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;, I’d love to hear from you.&lt;/p&gt;

&lt;h2 id=&quot;photo-credit&quot;&gt;Photo credit&lt;/h2&gt;

&lt;p&gt;Many thanks to &lt;a href=&quot;https://unsplash.com/photos/PVIbUkN_wCQ?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText&quot;&gt;Emma Frances Logan&lt;/a&gt; on Unsplash for the photo used in this post.&lt;/p&gt;
</description>
        <pubDate>Mon, 19 Feb 2018 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/exercise/</link>
        <guid isPermaLink="true">http://hop.ie/blog/exercise/</guid>
      </item>
    
      <item>
        <title>Learning React</title>
        <description>&lt;p&gt;This post is for front-end developers interested in learning new things. If you’re reading this and thinking “hey I wish this guy would make me an amazing website to make my business loads of money” - well, you’re in luck! &lt;a href=&quot;/hire/&quot;&gt;Let’s work together&lt;/a&gt; and make beautiful beautiful websites.&lt;/p&gt;

&lt;h2 id=&quot;why-react&quot;&gt;Why React?&lt;/h2&gt;

&lt;p&gt;React is a framework for building web applications using JavaScript. It’s designed to help manage the views of applications, with the views being driven by and rendered in response to changes in the data behind the app.&lt;/p&gt;

&lt;p&gt;It’s been around for a few years, originally open sourced in 2013 and I’ve seen it grow to become a very popular standard for fast, reactive web applications. As a fan of fast web apps, I’m keen to learn how to apply this to my projects.&lt;/p&gt;

&lt;p&gt;In the past I’ve seen it in use but have never really taken the time to properly learn it, and now seems a good time. The framework has matured to the point now where documentation is solid, and there are loads of great tutorials around.&lt;/p&gt;

&lt;p&gt;With the recent release of React Fibre (React 16), it’s a great time to learn some React! But where to start? Here’s what I’m doing.&lt;/p&gt;

&lt;h2 id=&quot;step-1-immersion&quot;&gt;Step 1: Immersion&lt;/h2&gt;

&lt;p&gt;The first thing I do when learning something new is to get a feel for it through whatever means possible. This means subscribing to React or JavaScript newsletters,podcasts, Youtube channels and basically getting a feel for the ecosystem around the framework.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://reactjsnewsletter.com&quot;&gt;React.js newsletter&lt;/a&gt; is good, as is &lt;a href=&quot;http://newsletter.fullstackreact.com&quot;&gt;Fullstack React&lt;/a&gt;, and I also recommend &lt;a href=&quot;https://devchat.tv/js-jabber&quot;&gt;JavaScript Jabber&lt;/a&gt; if you like listening to tech stuff.&lt;/p&gt;

&lt;p&gt;This is all very fun but I have no idea what most of the articles are really about yet, so the next step is to take to the &lt;a href=&quot;https://reactjs.org/docs/&quot;&gt;official documentation&lt;/a&gt;. A great starting point is &lt;a href=&quot;https://reactjs.org/docs/thinking-in-react.html&quot;&gt;Thinking in React&lt;/a&gt;. I worked through that, then looped back and studied the Quick Start sections more thoroughly. This helped me understand the concept.&lt;/p&gt;

&lt;p&gt;Understanding the basic concept is a good start, but it’s still a long way from applying it to the real world. There’s a big gap between understanding the basic idea and knowing how to apply it. For me this means I need to do some focused learning.&lt;/p&gt;

&lt;h2 id=&quot;step-2-lessons&quot;&gt;Step 2: Lessons&lt;/h2&gt;

&lt;p&gt;I took this &lt;a href=&quot;https://www.udemy.com/react-redux/learn/v4/&quot;&gt;Udemy React and Redux course&lt;/a&gt;. The instructor, Stephen Grider has a great way of explaining tricky concepts and makes use of loads of demos, which I really enjoyed following along.&lt;/p&gt;

&lt;p&gt;After a week or so of spare time I was able to work through this course and I feel I really understand now how React works alongside data management using Redux. The next step is to practise this new knowledge on a real app.&lt;/p&gt;

&lt;h2 id=&quot;step-3-application&quot;&gt;Step 3: Application&lt;/h2&gt;

&lt;p&gt;Immersion and book learning are all great, and I enjoy them very much but the process doesn’t really amount to much if I don’t apply it to a real project. This is how I really learn, by making mistakes and fixing them.&lt;/p&gt;

&lt;p&gt;Rather than hack together a to-do app, I decided to aim a little higher. I have an &lt;a href=&quot;https://shopireland.ie&quot;&gt;Irish shopping website&lt;/a&gt; that is a great place to try new approaches. The site’s very old now, and in need of a re-build so I’ll try the new skills on it.&lt;/p&gt;

&lt;p&gt;One of the great things about the &lt;a href=&quot;https://shopireland.ie&quot;&gt;ShopIreland.ie&lt;/a&gt; site has always been it’s speed. This is something I’d like to maintain or even improve, making the experience of searching for products, browsing and adding them to the basket as smooth as possible. I think React will be helpful with this.&lt;/p&gt;

&lt;p&gt;I’m just getting started with step 3 and will update once things progress. For now, I’ve build an API to provide the data for Redux, and the next step is to build out the routes within React to present the same URL structure as the old site.&lt;/p&gt;

&lt;p&gt;Wish me luck!&lt;/p&gt;

&lt;p&gt;I hope you find this helpful! If you have any questions, or are feeling inspired to take on a fitness challenge, hit me up &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;. Also, &lt;a href=&quot;/hire/&quot;&gt;you can hire me&lt;/a&gt;. I’d love to hear from you.&lt;/p&gt;
</description>
        <pubDate>Mon, 18 Dec 2017 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/learnng-react/</link>
        <guid isPermaLink="true">http://hop.ie/blog/learnng-react/</guid>
      </item>
    
      <item>
        <title>Eating better</title>
        <description>&lt;p&gt;Since giving up full-time work last year I’ve been able to take a look at how I use my time. Freelancing is going great and I’ve had more time for my family, but as well as that I’ve been able to improve my physical health too. This post is about some of the things I’ve tried and the results.&lt;/p&gt;

&lt;p&gt;In mid-2016 I started back at the gym and got back into running more regularly. I was doing weighted squats at the gym with the goal of improving my leg strength and avoid running injuries. It worked but didn’t change me much physically (apart from slightly thicker legs).&lt;/p&gt;

&lt;p&gt;In early 2017 I began adjusting my diet, and investigating other ways of exercising to encourage muscle growth. Along the way I’ve learned a lot about how to eat better, as well as using bodyweight exercises to build up a good weekly workout routine.&lt;/p&gt;

&lt;p&gt;As a result I’ve lost about 10 kilograms (22 pounds) overall, while gaining a significant amount of muscle in my legs, back, arms, shoulders and core.&lt;/p&gt;

&lt;p&gt;There are two parts to this, so I’ll begin with the diet changes, then share a bit about my workout routine. Hopefully this will be useful to some people - but do keep in mind that what works for me might not be right for you, your goals and physical needs. Be mindful and listen to your body!&lt;/p&gt;

&lt;h2 id=&quot;losing-weight--fat&quot;&gt;Losing weight / fat&lt;/h2&gt;

&lt;p&gt;For many years I weighed around 95 kilograms. Possibly more. I stopped measuring. Last month I weighed in at just under 85 kilograms and I’m looking better, and feeling a lot happier about myself. There are two things that caused this, the first being nutritional changes and the second being exercise.&lt;/p&gt;

&lt;p&gt;Over the last 6 months or so I’ve been making a lot of small changes to what I eat. It’s important to note that I didn’t do all this at once, I just changed things gradually, tried things and saw what worked and what didn’t. I even tried intermittent fasting - which was pretty good but left me feeling too drained so only lasted a few weeks.&lt;/p&gt;

&lt;p&gt;In the end, the bits that worked involved:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Less snacking between meals&lt;/li&gt;
  &lt;li&gt;Fewer treats like ice-cream, sweets etc&lt;/li&gt;
  &lt;li&gt;Replacing starchy carbs like rice, potatoes with green vegetables&lt;/li&gt;
  &lt;li&gt;Protein in every meal - lean meat, protein-rich vegetables&lt;/li&gt;
  &lt;li&gt;A small amount of whey protein supplementation&lt;/li&gt;
  &lt;li&gt;Drinking more water&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On top of this, I also took time to track everything in myFitnessPal, a phone app that helps track food and breaks it down into “macros” (how much protein, carbs and fats are in the diet).&lt;/p&gt;

&lt;p&gt;By taking the time to track each meal I could see that by the end of each day I was eating somewhere around 3,000 to 3,500 calories per day. Since my goal was to lose weight, I gave myself a target of about 2,500 or so. This helped me get control of my portions, as I knew I needed to aim for this target each day.&lt;/p&gt;

&lt;p&gt;I also validated this by &lt;a href=&quot;http://www.myfitnesspal.com/tools/bmr-calculator&quot;&gt;calculating my BMR&lt;/a&gt; and adjusting for my activity levels (more on those later!) to come up with the 2,500 calorie target. Yours may be different!&lt;/p&gt;

&lt;h3 id=&quot;macros&quot;&gt;Macros&lt;/h3&gt;

&lt;p&gt;While aiming for a specific calorie target, I needed to make sure I wasn’t losing too much muscle with the weight loss. To help this that meant eating enough protein. There’s a lot of information on this online but the best I can tell is that for gaining or maintaining muscle I needed to aim for 1 to 1.5 times my weight in kilograms per day. For me that meant about 120 to 150 or so grams of protein per day.&lt;/p&gt;

&lt;p&gt;Some days I’d go over that (I’m a big fan of chicken breast, egg-white omelettes and quinoa) but in the end that was no problem. “Spending” my calories on protein rather than processed carbohydrates resulted in less of the food being converted to fat, and having protein in my system each day meant I could make the most of any exercise.&lt;/p&gt;

&lt;h3 id=&quot;nutrients&quot;&gt;Nutrients&lt;/h3&gt;

&lt;p&gt;As well as getting protein (mostly from lean meats and vegetables) I replaced a lot of the “empty” carbs with better quality carbs such as leafy green vegetables, beetroot, peppers, tomatoes and peas. Keeping the freezer stocked with frozen spinach, peas and mixed vegetables makes that easier.&lt;/p&gt;

&lt;h3 id=&quot;a-typical-day-of-eating&quot;&gt;A typical day of eating&lt;/h3&gt;

&lt;p&gt;So while every day is different, I’ve found a few habits work for me. They include starting with a large bowl of overnight oats (made with water rather than milk), sweetened with a little honey. I also add in some whey protein powder to the oats to make sure there’s a bit of protein in that meal.&lt;/p&gt;

&lt;p&gt;For lunch I might steam some broccoli, or microwave a bowl of peas and spinach, and serve that with a couple of chicken breasts and quinoa. Some days I might chop up a bunch of peppers and put them into an egg-white omelette with some Frank’s hot sauce as a side to some roast beef or fried fish.&lt;/p&gt;

&lt;p&gt;Some days I might make a light lunch with boiled eggs, baby corn, mange-tout and chick peas. I guess it helps that I like simple, plain food!&lt;/p&gt;

&lt;p&gt;For dinner I would be more likely to be eating with the family, so whatever we’re having. Some days that might involve a bit of pizza or maybe even a burger but more often than not it would be something with a sauce and meat. I’d avoid eating rice or pasta but instead replace it with vegetables or grains.&lt;/p&gt;

&lt;p&gt;Most evenings I try to avoid grazing too much (drink water!) but I’d often have a few sweets (if myFitnessPal has me under the target), or some days I’d finish off leftover overnight oats. Having a little protein before bed can be good if you’re working on muscle growth but go light on the carbs!&lt;/p&gt;

&lt;p&gt;Basically each day I try to make sure I’m getting enough nutrient-rich carbs (veg), slow burning carbs (oats) and about 30% of my food being protein. Having targets like this means I tend to shop better and see less need to buy snacks and processed food as it doesn’t have what I need.&lt;/p&gt;

&lt;h3 id=&quot;make-it-sustainable&quot;&gt;Make it sustainable!&lt;/h3&gt;

&lt;p&gt;One thing I’ve tried to avoid is feeling like I’m depriving myself. I love baking, whether it’s crumbles, cookies, brownies or cakes, and I eat “unhealthy” foods when I want to. Having said that, I always track them on myFitnessPal. If I find I’ve gone over my target one day, I’ll make a point of staying under it for a few days to make up the difference. Or I might eat light during the day knowing I’m going out for a special meal that night.&lt;/p&gt;

&lt;p&gt;What I don’t do is cheat days. I might do “cheat” meals where I just eat 2,000 calories of carbs, but if I did that all day it would be too much. It’s more important to eat well most of the time and enjoy what you like, rather than torture yourself in a diet. That just leads fo giving up. Make changes with the aim of them becoming your new normal.&lt;/p&gt;

&lt;h2 id=&quot;building-muscle&quot;&gt;Building muscle&lt;/h2&gt;

&lt;p&gt;The second part to getting healthy has been to work out a decent training plan. This has evolved a lot since last year when I joined a gym, and I’m now in a very different place to where I started, but in the next post I’ll go into more detail about how this has progressed and what I hope to achieve.&lt;/p&gt;

&lt;p&gt;If you have any questions, hit me up &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Thu, 27 Jul 2017 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/eating-better/</link>
        <guid isPermaLink="true">http://hop.ie/blog/eating-better/</guid>
      </item>
    
      <item>
        <title>2015</title>
        <description>&lt;p&gt;It’s easy to forget how much we get done over the course of a year. Sometimes it’s good to take note of what we have achieved, and think about next year.&lt;/p&gt;

&lt;p&gt;2015 was busy. I learned a lot and published more than ever. Here are some highlights.&lt;/p&gt;

&lt;h2 id=&quot;cssanimationrocks&quot;&gt;CSSAnimation.rocks&lt;/h2&gt;

&lt;p&gt;In January I registered the domain &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;cssanimation.rocks&lt;/a&gt;, with the intention of writing a book. While that didn’t happen, I did manage to create a 4-week email course and write 15 in-depth CSS tutorials. The site has been well received, with over 500,000 views and more than 850 people taking part in the course.&lt;/p&gt;

&lt;p&gt;Along the way I also worked with &lt;a href=&quot;https://getnative.me&quot;&gt;Native&lt;/a&gt; to get the site translated into other languages. Thanks to over a dozen kind volunteers, articles have now been translated &lt;a href=&quot;https://cssanimation.rocks/translations&quot;&gt;into 10 languages&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;writing-and-speaking&quot;&gt;Writing and speaking&lt;/h2&gt;

&lt;p&gt;Outside of the CSS tutorials, I managed to write two &lt;a href=&quot;http://webdesign.tutsplus.com/tutorials/adding-appeal-to-your-animations-on-the-web--cms-23649&quot;&gt;articles for Tuts+&lt;/a&gt;, and the opening article &lt;a href=&quot;https://24ways.org/2015/animating-your-brand/&quot;&gt;for 24 Ways&lt;/a&gt;. I’ve also finished an article that should, fingers crossed, feature in Net Magazine in a couple of months.&lt;/p&gt;

&lt;p&gt;I was lucky enough also to be asked to speak at &lt;a href=&quot;http://www.defuse.ixd.ie&quot;&gt;Defuse Dublin&lt;/a&gt;. It was possibly the most nerve-wracking 5-minute talk I’ve ever done but well worth doing. It’s &lt;a href=&quot;https://www.youtube.com/watch?v=VYsJrPwI66M&quot;&gt;on Youtube here&lt;/a&gt;. I also got to take part in a &lt;a href=&quot;https://boagworld.com/season/12/episode/1206/&quot;&gt;Boagworld podcast&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I also started a meetup, &lt;a href=&quot;http://hop.ie/codepen-dublin/&quot;&gt;CodePen Dublin&lt;/a&gt;. We met 3 times and had some great chats about front-end development and working in Dublin.&lt;/p&gt;

&lt;h2 id=&quot;next-year&quot;&gt;Next Year&lt;/h2&gt;

&lt;p&gt;A problem I often face is trying to do everything at once. I’d like to try to continue to improve my focus this year, and put my energy into a small number of projects. Practicing mindfulness is something I’ve been trying recently and I think it’ll help.&lt;/p&gt;

&lt;p&gt;I learned a lot in producing &lt;a href=&quot;https://cssanimation.rocks/courses/animation-101/&quot;&gt;the CSS Animation course&lt;/a&gt;, and I’m keen to make another course or two this year and do it even better.&lt;/p&gt;

&lt;p&gt;This year will be a huge one for UX, front-end performance (including offline design principles and making single page apps faster), and it’ll be great seeing browsers continue to improve their support of service workers and other awesome tools.&lt;/p&gt;

&lt;p&gt;A good 2016 for me will be one in which I continue to learn, give back to the community, and find a balance between family time and working time that works for both.&lt;/p&gt;

&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;

&lt;p&gt;Articles on &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS Animation&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/twitter-fave/&quot;&gt;Twitter's &quot;fave&quot; animation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/pseudo-elements/&quot;&gt;Animating pseudo-elements&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/list-items/&quot;&gt;Animating List Items&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/baymax/&quot;&gt;Baymax in CSS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/clocks/&quot;&gt;Clocks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/transition-vs-animation/&quot;&gt;Transitions vs Animations&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/watch/&quot;&gt;Apple Watch Dials&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/principles/&quot;&gt;Animation Principles for the Web&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/macplus/&quot;&gt;Mac Plus CSS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/wwdc15/&quot;&gt;WWDC 2015&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/buffer/&quot;&gt;Buffer's loading animation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/portal/&quot;&gt;Portal CSS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/spheres/&quot;&gt;Spheres&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/stereoscopic/&quot;&gt;Stereoscopic CSS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/starwars/&quot;&gt;Star Wars&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/why-animate/&quot;&gt;Why Animate&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;International CSS Animation homepages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/ar/&quot;&gt;العربية (Arabic)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/cn/&quot;&gt;中国 (Chinese)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/da/&quot;&gt;Danske (Danish)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/de/&quot;&gt;Deutsch (German)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/fr/&quot;&gt;Français (French)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/jp/&quot;&gt;日本語 (Japanese)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/nl/&quot;&gt;Nederlands (Dutch)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/pt/&quot;&gt;Português (Portuguese)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/pl/&quot;&gt;Polski (Polish)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cssanimation.rocks/tr/&quot;&gt;Türkçe (Turkish)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Elsewhere:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://boagworld.com/season/12/episode/1206/&quot;&gt;Boagworld podcast: How to market and promote your digital business&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://24ways.org/2015/animating-your-brand/&quot;&gt;24 Ways: Animating your Brand&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://webdesign.tutsplus.com/tutorials/adding-appeal-to-your-animations-on-the-web--cms-23649&quot;&gt;Tuts+: Adding appeal&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://webdesign.tutsplus.com/articles/15-inspiring-examples-of-css-animation-on-codepen--cms-23937&quot;&gt;Tuts+: Inspiring CSS Animations on CodePen&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=VYsJrPwI66M&quot;&gt;Defuse Dublin: Animating your Brand (Talk)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/dan-oneill/&quot;&gt;Hop.ie: Dan O'Neill (Interview)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/launching-new-website/&quot;&gt;Hop.ie: Launching a new website&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/css-animation-101/&quot;&gt;Hop.ie: From Blog to Product&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/audience/&quot;&gt;Hop.ie: Building an audience&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Fri, 01 Jan 2016 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/2015/</link>
        <guid isPermaLink="true">http://hop.ie/blog/2015/</guid>
      </item>
    
      <item>
        <title>CodePen Meetup</title>
        <description>&lt;p&gt;On Thursday, 1st October I met up with 15 awesome front-end developers at the first CodePen Dublin meetup. It was a great evening, and I’m really looking forward to the next.&lt;/p&gt;

&lt;h2 id=&quot;a-codepen-meetup&quot;&gt;A CodePen meetup?&lt;/h2&gt;

&lt;p&gt;I’m a regular user of the &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen website&lt;/a&gt;, a service set up by Chris Coyier that lets front-end developers quickly build and share HTML, CSS and JavaScript demos. It can be used to mock up ideas, interactive experiments and much more. I’m a huge fan.&lt;/p&gt;

&lt;p&gt;So when I heard they were starting meetups at various locations, it seemed only right that we bring it to Dublin. So here it is. The plan for the meetup is simple, to create a relaxed, fun evening for developers (or non-developers) to chat, connect and get to know other people in our industry.&lt;/p&gt;

&lt;p&gt;For the moment there are no talks or other serious agenda items, the goal is begin simple and get some people together. We’ll see where it goes.&lt;/p&gt;

&lt;p&gt;The next meetup will be on Thursday 5th November, at the same location. I’ll look forward to &lt;a href=&quot;/codepen-dublin/&quot;&gt;seeing you there&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Sat, 03 Oct 2015 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/codepen-meetup/</link>
        <guid isPermaLink="true">http://hop.ie/blog/codepen-meetup/</guid>
      </item>
    
      <item>
        <title>Building an audience</title>
        <description>&lt;p&gt;I get a notification on my phone when someone follows &lt;a href=&quot;https://twitter.com/cssanimation&quot;&gt;@cssanimation&lt;/a&gt;. Every so often my phone will light up and show me that someone has pressed that “Follow” button, and it warms my heart. I do a little dance and think of what I need to do to make the &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS Animation&lt;/a&gt; site better.&lt;/p&gt;

&lt;p&gt;I also get a daily update from Mailchimp telling me how many subscribes and unsubscribes each day. On a busy day it might be as many as fifty new signups, but often it’s five or so. Which is fine, I’m always happy if it’s up and to the right. One day recently I had more unsubscribes than subscribes. I feel pretty bad when this happens.&lt;/p&gt;

&lt;p&gt;It’s a rollercoaster I tell you. Here’s what I’m doing to try to make sure the dances outweigh the downs.&lt;/p&gt;

&lt;h2 id=&quot;building-an-audience&quot;&gt;Building an audience&lt;/h2&gt;

&lt;p&gt;I’m not going to pretend I’m some genius at conceiving an approach and planning this all through, I’ve just gone with what felt right, tried things and repeated what seems to work. I have no doubt I’ve missed opportunities along the way but it’s an interesting journey. What’s right for you might be different, so try things. Let me know if it works!&lt;/p&gt;

&lt;p&gt;There are three steps to building an audience. Everybody loves things if they can be done in three steps.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Set up places to follow you&lt;/li&gt;
  &lt;li&gt;Create value&lt;/li&gt;
  &lt;li&gt;Get traffic&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;1-set-up-places&quot;&gt;1. Set up places&lt;/h2&gt;

&lt;p&gt;First, set up a mailing list. This may seem an obvious thing to do, but I didn’t set up an email list until late last year. Until then I’d been relying on Mastodon. Email is a whole other level, it is more direct, people are more likely to read an email than a tweet, and you can write a lot more. Definitely set up a mailing list and throw a signup form onto the footer of your site, or on a “subscribe” landing page.&lt;/p&gt;

&lt;p&gt;There could be some merit in offering something in exchange for signing up. I’ve heard it helps, but it’s not something I’ve done yet. I’ll have to get back to you on that one. But do make sure there’s some way for people to sign up.&lt;/p&gt;

&lt;h3 id=&quot;twitter-facebook-pinterest-instragram&quot;&gt;Twitter, Facebook, Pinterest, Instragram…&lt;/h3&gt;

&lt;p&gt;Get your usernames reserved on any appropriate services as soon as you can. For me, Mastodon’s a big one, with Facebook, and Dribbble being popular places for people to see my content. You may have more photo-related content, in which Instagram or Pinterest might be good. Add some links to your social accounts on your site so that people can find them.&lt;/p&gt;

&lt;h3 id=&quot;rss&quot;&gt;RSS&lt;/h3&gt;

&lt;p&gt;I add &lt;a href=&quot;https://cssanimation.rocks/feed.xml&quot;&gt;an XML feed&lt;/a&gt; to each site I create. I suspect this is less popular than it used to be but it’s usually not to complex to set up.&lt;/p&gt;

&lt;h2 id=&quot;2-create-value&quot;&gt;2. Create value&lt;/h2&gt;

&lt;p&gt;This part is where you engage your enthusiasm and ideas, and create something. Maybe you love to sketch or illustrate, or arrange flowers, or review truck stops. Either way, put pen to paper, flower to vase, or pedal to the metal and get creating. Personally I enjoy making things with CSS and writing about how it works. There’s usually something I learn along the way, which makes it more fun to share while the learning is fresh.&lt;/p&gt;

&lt;p&gt;If you’re stuck for ideas, start with something you half-know but would like to know more. You don’t have to be an expert, and in fact you might have a more accessible style of writing if you’re learning it while you create. People can follow along.&lt;/p&gt;

&lt;p&gt;Main thing is, create something. Put it out there and know you’ll improve.&lt;/p&gt;

&lt;h2 id=&quot;3-get-traffic&quot;&gt;3. Get traffic&lt;/h2&gt;

&lt;p&gt;It’s nice creating things but for most of us want the validation of having people see it. Maybe we have a business plan, or want to set ourselves apart and be more competitive in the job market.&lt;/p&gt;

&lt;p&gt;There are plenty of expensive ways to get your content seen. You could buy a newspaper or sports team if you have a few billion going spare. Or you could place a Google or Twitter ad if you like. Personally I’ve not had much luck from Google ads or buying newspapers, and what I’ve found works is free but takes a little longer and involves more communication. With people.&lt;/p&gt;

&lt;h3 id=&quot;newsletters&quot;&gt;Newsletters&lt;/h3&gt;

&lt;p&gt;It’s likely that whatever niche you’re in, whether it’s &lt;a href=&quot;http://www.sassnews.com/&quot;&gt;writing Sass&lt;/a&gt; or &lt;a href=&quot;http://www.knittingdaily.com/&quot;&gt;creating Knitting patterns&lt;/a&gt;, there’s a newletter or two. When you create something, reach out to a newsletter and let them know. The kind people running the newsletters love original, quality content, so don’t be afraid to ask what they’d like to see if you’re looking for ideas.&lt;/p&gt;

&lt;p&gt;I often contact a few newsletters that I subscribe to, such as &lt;a href=&quot;http://css-weekly.com/&quot;&gt;CSS Weekly&lt;/a&gt; to let them know when a new article goes live. There’s no guarantee it’ll make it but when it does, it’s a great way to reach a relevant audience.&lt;/p&gt;

&lt;h3 id=&quot;link-sites&quot;&gt;Link sites&lt;/h3&gt;

&lt;p&gt;Second to newsletters are the sites that link to blogs and news. The biggest of these is &lt;a href=&quot;http://reddit.com&quot;&gt;Reddit&lt;/a&gt;. Reddit is a community of smaller “sub-Reddits”, so it could be worth finding one that fits your niche and submitting your link there. Other sites that I would look to include &lt;a href=&quot;https://news.layervault.com/&quot;&gt;Designer News&lt;/a&gt;, &lt;a href=&quot;http://sidebar.io&quot;&gt;Sidebar&lt;/a&gt; and &lt;a href=&quot;http://www.webdesignernews.com/&quot;&gt;Web Designer News&lt;/a&gt;. If your thing is knitting, you’d probably want to find different sites though.&lt;/p&gt;

&lt;h3 id=&quot;publications&quot;&gt;Publications&lt;/h3&gt;

&lt;p&gt;Online publications are also a decent way to get yourself in front of an audience. Most will be prepared to pay a modest fee for your time also. I’ve written for some publications and while I’d certainly recommend looking into it, I wouldn’t do it at the expense of creating content for your own site. A blog post on your site is yours forever, and you can update it, republish it or whatever when you like. When your content goes to another publication it’s gone.&lt;/p&gt;

&lt;p&gt;There are definitely advantages and disadvantages.&lt;/p&gt;

&lt;h3 id=&quot;republishing&quot;&gt;Republishing&lt;/h3&gt;

&lt;p&gt;Similar to having content published in an magazine or online resource is republishing your own content. A popular option is to republish on a site such as &lt;a href=&quot;http://medium.com&quot;&gt;Medium&lt;/a&gt;. Again this increases the chances of your work finding a wider audience. I’ve had limited success with this but it could be the technical nature of my content. This is definitely worth trying.&lt;/p&gt;

&lt;h2 id=&quot;stickers&quot;&gt;Stickers&lt;/h2&gt;

&lt;p&gt;One weird trick™ I’ve found is giving away stickers. I recently had some printed up for &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS Animation Rocks&lt;/a&gt; and they’ve been really popular. So far I’ve been giving them away on Twitter, and it seems to be a very popular way to spread the word. People loves the stickers. If you’re getting some printed, I’d use &lt;a href=&quot;https://www.stickermule.com/unlock?ref_id=9628460701&quot;&gt;Stickermule&lt;/a&gt;. They are great quality and a solid service. (Note: the &lt;a href=&quot;https://www.stickermule.com/unlock?ref_id=9628460701&quot;&gt;affiliate link&lt;/a&gt; gets you $10 off, and me also, so share the love!)&lt;/p&gt;

&lt;h2 id=&quot;try-and-try-again&quot;&gt;Try and try again&lt;/h2&gt;

&lt;p&gt;I hesitate to use the phrase “fail fast”, because I don’t think any of this counts as failing. But do try. Looking back at some of my earlier posts, I cringe a little and want to change things, but that just means I’ve improved.&lt;/p&gt;

&lt;p&gt;So have a go. And do &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;let me know&lt;/a&gt; when you have something in place. Maybe you’ll start seeing those “follow” notifications and do a dance of your own.&lt;/p&gt;

&lt;h2 id=&quot;get-in-touch&quot;&gt;Get in touch&lt;/h2&gt;

&lt;p&gt;Have you built or are thinking about building a product? Want to talk about how it could be done? I’m happy to bounce ideas around and help where I can. Send me &lt;a href=&quot;mailto:d@hop.ie&quot;&gt;an email&lt;/a&gt; or hit me up on [Mastodon(https://mastodon.ie/@donovanh) and let’s chat.&lt;/p&gt;

&lt;p&gt;If you find this or &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS Animation&lt;/a&gt; useful, please consider giving it a mention on Twitter, &lt;a href=&quot;https://twitter.com/cssanimation&quot;&gt;or following along&lt;/a&gt;. It’s going to be interesting.&lt;/p&gt;
</description>
        <pubDate>Sun, 24 May 2015 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/audience/</link>
        <guid isPermaLink="true">http://hop.ie/blog/audience/</guid>
      </item>
    
      <item>
        <title>From blog to product</title>
        <description>&lt;p&gt;This year I decided to get serious about creating something of value. I &lt;a href=&quot;/blog/launching-new-website/&quot;&gt;created a new website&lt;/a&gt; all about &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS animation&lt;/a&gt;. I then created and launched a &lt;a href=&quot;https://cssanimation.rocks/courses/animation-101/&quot;&gt;paid email course&lt;/a&gt;. Here are some things I learned along the way.&lt;/p&gt;

&lt;h2 id=&quot;what-sort-of-product&quot;&gt;What sort of product&lt;/h2&gt;

&lt;p&gt;One of many decisions I’ve made in the last few month has been choosing a product I would feel confident creating and shipping. There are many options, from ebooks, live seminars, video guides and more. I’ve tried making screencasts a little in the past, but wasn’t comfortable going the video route (yet) so instead I chose to focus on writing.&lt;/p&gt;

&lt;p&gt;For the last couple of years I’ve been writing tutorials on CSS, front-end development and animation. It seemed an obvious idea then to focus on CSS animation as a topic, as it’s something I understand quite well and enjoy writing about.&lt;/p&gt;

&lt;p&gt;The next step was to write a book. To test the market for such a book I put up a quick landing page, with a signup form to see if there would be interest. There was a little, and it was encouraging, so I started working out a plan for what the book would be.&lt;/p&gt;

&lt;p&gt;However after a couple of weeks I realised I’d bitten off too big a project, especially for my first go. A book is a noble goal but not something I could put together in a month and see results. I’m too impatient to work on something for a year without validation. So I put that idea on the back burner (for now).&lt;/p&gt;

&lt;h2 id=&quot;narrow-the-focus&quot;&gt;Narrow the focus&lt;/h2&gt;

&lt;p&gt;Starting with too big an idea was not going to work for me. I need to start with small steps. I’m used to doing that when designing and building websites, but writing educational content was new to me so I needed to make more of an effort to start small.&lt;/p&gt;

&lt;p&gt;Having decided against writing a book, I brainstormed some other approaches and remembered an interesting course called Write Up! by the wonderful &lt;a href=&quot;https://twitter.com/rellyab&quot;&gt;Relly Annett-Baker&lt;/a&gt;. It was a simple idea, you pay £25, and get an email each day for a month that guides and encourages you to write. This seemed like an interesting approach for learning about CSS animation. People could sign up, get an email each day that would gradually introduce the subjects, and end the month with a better understanding. So &lt;a href=&quot;https://cssanimation.rocks/courses/animation-101/&quot;&gt;I tried that&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;choosing-a-topic&quot;&gt;Choosing a topic&lt;/h2&gt;

&lt;p&gt;The tutorials on &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS Animation Rocks&lt;/a&gt; expect a certain knowledge of CSS and CSS animation. Most tend to assume that the reader knows &lt;a href=&quot;https://cssanimation.rocks/transition-vs-animation/&quot;&gt;the difference between a transition and an animation&lt;/a&gt;. For some this then becomes a barrier; the posts are hard to understand for some.&lt;/p&gt;

&lt;p&gt;This was something a course could help. I decided to focus on the gap between knowing some CSS, but no animation, and being able to understand the more advanced tutorials. The goal would be to create a course that would empower people to be able to read any tutorial on the site and understand it.&lt;/p&gt;

&lt;h2 id=&quot;planning-and-creating-a-course&quot;&gt;Planning and creating a course&lt;/h2&gt;

&lt;p&gt;There are many ways to set about creating an online course, but this is roughly the sequence of steps I followed. For a first go it worked out, but I did learn a few things &lt;em&gt;not&lt;/em&gt; to do along the way.&lt;/p&gt;

&lt;h3 id=&quot;step-1-plan-the-content&quot;&gt;Step 1: Plan the content&lt;/h3&gt;

&lt;p&gt;I set up a new Trello board and started listing out all the content I wanted to cover. This being a 101-level course, I focused people with no knowledge of CSS animation. This meant setting a goal (understanding transitions and animations), and structuring 4 weeks of messages that would lead to that goal.&lt;/p&gt;

&lt;p&gt;This is how I structured it:&lt;/p&gt;

&lt;h3 id=&quot;week-1&quot;&gt;Week 1&lt;/h3&gt;

&lt;p&gt;Introducing the topic. In this week I set out a plan for the course, discuss why animation is interesting and useful, and define the terms &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transition&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt;. I felt this would be a good way to have readers familiarise themselves with the ideas without getting bogged down by implementation detail.&lt;/p&gt;

&lt;h3 id=&quot;week-2&quot;&gt;Week 2&lt;/h3&gt;

&lt;p&gt;After setting the tone, I go deep on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transition&lt;/code&gt; property this week. To do this I give some examples of it in action, then talk through all the properties involved, and then cover topics such as using multiple transitions for interesting effects. The goal for this week was to make sure everyone understood what the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transition&lt;/code&gt; property is and how it’s not the same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;week-3&quot;&gt;Week 3&lt;/h3&gt;

&lt;p&gt;With transitions covered, the third week is all about the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt; property. I follow a similar pattern to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transition&lt;/code&gt; week, but talk about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keyframes&lt;/code&gt; and the othe properties that make animation work.&lt;/p&gt;

&lt;h3 id=&quot;week-4&quot;&gt;Week 4&lt;/h3&gt;

&lt;p&gt;With the topic introduced, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transitions&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animations&lt;/code&gt; covered in detail, the fourth week is where I then put it together and talk through some demo code that incorporates both. I think it would be remiss to not cover accessibility, so I include a day on that topic and finish with links to further reading and other resources.&lt;/p&gt;

&lt;h2 id=&quot;pricing&quot;&gt;Pricing&lt;/h2&gt;

&lt;p&gt;With the course planned out (note: I hadn’t actually written the content at this point, just the summaries of each day), I considered pricing. Pricing is a tough one. If I priced the content too low, I’d potentially get more signups but would have to gain exponentially more purchases to make the same amount as if I charge more. If I charge too much, it won’t sell.&lt;/p&gt;

&lt;p&gt;After looking at lots of similar products I decided to aim for &lt;em&gt;slightly&lt;/em&gt; expensive. I wanted the product to feel like it was worth paying for, and the first step was to actually charge for it. At the same time, I also wanted flexibility to be able to change the price as I learn more. To do this, I chose a &lt;em&gt;expectation&lt;/em&gt; price and a &lt;em&gt;real&lt;/em&gt; price.&lt;/p&gt;

&lt;p&gt;I set the &lt;em&gt;expectation&lt;/em&gt; price at $99. I knew this was quite high but didn’t feel confident charging that much. Instead, I left it on the page, crossed out, and made the real price at $49. This meant that the course felt like a bargain, being half price!&lt;/p&gt;

&lt;p&gt;The only way to find out if this would work was to put the page live, so I put together a landing page with a simple Paypal Buy Now button, and put word out.&lt;/p&gt;

&lt;h2 id=&quot;initial-reaction&quot;&gt;Initial reaction&lt;/h2&gt;

&lt;p&gt;I had a hope that maybe 10 people would sign up. That would be enough to validate the idea. In the first week, 60 people signed up. What!?&lt;/p&gt;

&lt;p&gt;This blew my mind. People were actually keen to pay me for a product. Over the couple of weeks that followed, I continued to promote the course and experiment with different channels (and I continued blogging, but that’s another topic), and in month one I had attracted over 120 paying customers.&lt;/p&gt;

&lt;h2 id=&quot;delivering-the-course&quot;&gt;Delivering the course&lt;/h2&gt;

&lt;p&gt;I used Mailchimp to deliver the course, using their automation workflow. Setting up the structure with the 20 emails was very simple. What was harder was creating the content.&lt;/p&gt;

&lt;p&gt;Even though I knew what I wanted to say, the work involved in creating demos, and writing each of the emails was pretty intense. Every morning and every evening I was working full tilt on creating the content. In the end I managed to get it in place just in time, but if I was to do it again I would put a little more time into creating content in advance.&lt;/p&gt;

&lt;p&gt;It’s a balance, between having the stress-driven incentive to get it done and live, and the potential to plan too much and lose momentum before anything goes public. It would be worth finding your own balance on this, especially if you’re balancing having a family and job alongside.&lt;/p&gt;

&lt;h2 id=&quot;next-steps&quot;&gt;Next steps&lt;/h2&gt;

&lt;p&gt;With the first run of the course done, my next steps are to get back my momentum, improve the course (including creating an ebook version) and think about more products. In my “Just Ship” approach I’ve also let the design of CSSAnimation.rocks take second place, and it needs attention. So much to do.&lt;/p&gt;

&lt;p&gt;I also stopped writing blog posts while I wrote the course emails. I’d like to write more on the subject of audience building, but the short version is that a regular schedule of interesting posts, along with an email update to the mailing list, is pretty important.&lt;/p&gt;

&lt;h2 id=&quot;update-month-two&quot;&gt;Update: Month two&lt;/h2&gt;

&lt;p&gt;With little promotion, I managed to get 45 paid purchases of the course’s May run. It’s definitely quieter now though, which I think is down to my not posting new blog posts in a few weeks. Got to fix that.&lt;/p&gt;

&lt;h2 id=&quot;get-in-touch&quot;&gt;Get in touch&lt;/h2&gt;

&lt;p&gt;Have you built or are thinking about building a product? Want to talk about how it could be done? I’m happy to bounce ideas around and help where I can. Send me &lt;a href=&quot;mailto:d@hop.ie&quot;&gt;an email&lt;/a&gt; or hit me up on [Mastodon(https://mastodon.ie/@donovanh) and let’s chat.&lt;/p&gt;
</description>
        <pubDate>Fri, 15 May 2015 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/css-animation-101/</link>
        <guid isPermaLink="true">http://hop.ie/blog/css-animation-101/</guid>
      </item>
    
      <item>
        <title>Launching a new site</title>
        <description>&lt;p&gt;There’s a lot that goes into creating and launching a new website. In this post I’ll discuss the steps involved in planning, building and launching &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSSAnimation.rocks&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;initial-idea&quot;&gt;Initial idea&lt;/h2&gt;

&lt;p&gt;At the beginning of 2015, I began to write a book. I had been thinking of topics and brainstorming ideas for a few weeks, and when January arrived I’d decided to focus on CSS animation as my topic.&lt;/p&gt;

&lt;p&gt;My initial plan was to focus on writing the book while building a basic landing page with signup form. Once I began, it became clear I had a lot of work to do. A simple landing page wasn’t going to be enough, I needed to work on expanding my knowledge of the topic and building an audience along the way.&lt;/p&gt;

&lt;p&gt;After taking some advice from the wonderful Paul Boag, I’d decided to create a website specifically about CSS animation. So I got to work building it.&lt;/p&gt;

&lt;h2 id=&quot;choosing-a-name&quot;&gt;Choosing a name&lt;/h2&gt;

&lt;p&gt;Some people can start working on a project without a name. I’m not so great at that. I also tend to get unhealthily excited about choosing domain names. The effort required to resist registering all the names is unbearable.&lt;/p&gt;

&lt;p&gt;I settled on the domain &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;cssanimation.rocks&lt;/a&gt;. It’s a simple, straightforward name that is obvious, and the “rocks” domain name is fun. CSS animation absolutely does rock.&lt;/p&gt;

&lt;h2 id=&quot;creating-the-story&quot;&gt;Creating the story&lt;/h2&gt;

&lt;p&gt;It’s one thing having a domain name, but a website needs a bit of narrative, or something more to act as a hook. Considering this in the shower, the crazy idea occurred to me that I should go for the literal meaning of rocks. As in stone.&lt;/p&gt;

&lt;p&gt;Rock’s aren’t exactly known for being &lt;strong&gt;animated&lt;/strong&gt;. All the more reason to play on the theme, as far as I’m concerned. It would certainly be more memorable than a fist in the air.&lt;/p&gt;

&lt;p&gt;With a name and a bit of a theme, I opened Illustrator and drew some ideas for how some rocks might look. I put together the initial draft of the logo, and drew a smiling rock that would act as a mascot.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/cssanimation/rocky.svg&quot; style=&quot;max-width:300px&quot; alt=&quot;Smiling rock image used as a mascot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The design could do with some polish but it would do for a start. The next thing was to decide some goals.&lt;/p&gt;

&lt;h2 id=&quot;setting-goals-for-the-site&quot;&gt;Setting goals for the site&lt;/h2&gt;

&lt;p&gt;I wanted the site to be fun and educational, and include lots of examples and tutorials around the subject of CSS animation. Having written quite a few tutorials here over the past couple of years, I knew that was something I enjoyed doing, so seemed like a good approach.&lt;/p&gt;

&lt;p&gt;At the same time, I’d already been fleshing out the topics for what will become a book on the topic. Part of that was going through each of the CSS properties involved and describing what they were for. While still a rough draft, it could act as a reference. This reference could include examples, link to relevant tutorials, and act as a launching point for exploring the topic.&lt;/p&gt;

&lt;p&gt;Through this, the goal was to use Twitter, along with emails, to build up an audience and create some momentum around the project. Having a useful, visible side project has been helpful to me in many ways over the years. It’s not only a great place to try and learn new skills, but can also result in creating more opportunities to collaborate or work with others.&lt;/p&gt;

&lt;p&gt;There’s no better way to work on what you like that to work on what you like first.&lt;/p&gt;

&lt;h2 id=&quot;technology-and-hosting-choices&quot;&gt;Technology and hosting choices&lt;/h2&gt;

&lt;p&gt;With a url, a basic theme chosen, some images drawn up, and a goal in mind I set about building the site. This site runs on &lt;a href=&quot;http://hop.ie/blog/jekyll-github-pages/&quot;&gt;Jekyll&lt;/a&gt; and is hosted on Github, two services I was quite familiar with using and knew were reliable and secure.&lt;/p&gt;

&lt;p&gt;Jekyll is a free open-source blogging platform that generates static HTML. This means that when it serves pages, it doesn’t generate the content each time it’s requested. Instead it runs once to generate a set of static HTML pages, and every time a request comes through, these are served. It means that there’s no server-side code to maintain, nothing to update and page requests are very fast.&lt;/p&gt;

&lt;p&gt;Github offers a service that runs Jekyll natively and also hosts static HTML pages at custom URLs. While these static HTML pages could also be deployed to a server or to a service such as Amazon Web Services.&lt;/p&gt;

&lt;h3 id=&quot;adding-in-sass-autoprefixer&quot;&gt;Adding in Sass, Autoprefixer&lt;/h3&gt;

&lt;p&gt;Over the past year I’ve set up a few Jekyll blogs, and I have wrangled a few Grunt and Gulp files to help take care of things like Sass. I can tell you it’s not worth the hassle.&lt;/p&gt;

&lt;p&gt;While Jekyll &lt;strong&gt;does&lt;/strong&gt; support Sass now out of the box, it doesn’t have any mechanism for Autoprefixer, Linters and other tools useful when building front-end code. After a bit of research I settled on setting up the site using &lt;a href=&quot;http://yeoman.io/&quot;&gt;Yeoman&lt;/a&gt;, and specifically, the &lt;a href=&quot;https://github.com/sondr3/generator-jekyllized&quot;&gt;Jekyllized&lt;/a&gt; generator.&lt;/p&gt;

&lt;p&gt;Yeoman’s a handy tool for generating sites and cutting back on a lot of boring set up time. With the Jekyllized generator, I was able to set up the new site and run it locally using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gulp&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;This opens a browser window automatically, initialises Live Reload, and handles all the CSS preprocessing.&lt;/p&gt;

&lt;h3 id=&quot;skeleton-grids-and-typography&quot;&gt;Skeleton grids and typography&lt;/h3&gt;

&lt;p&gt;There are a lot of frameworks designed to help designers and developers scaffold a web page quickly. &lt;a href=&quot;http://getbootstrap.com/&quot;&gt;Bootstrap&lt;/a&gt; is one, &lt;a href=&quot;http://foundation.zurb.com/&quot;&gt;Zurb Foundation&lt;/a&gt; is another.&lt;/p&gt;

&lt;p&gt;I didn’t need many of the advanced tools these provided, so decided to go with a simpler option, &lt;a href=&quot;http://getskeleton.com/&quot;&gt;Skeleton&lt;/a&gt;. Skeleton provides a light grid framework, nicely proportioned fonts, and some helpful utilities, including styles for buttons and form elements.&lt;/p&gt;

&lt;p&gt;Since I was using Sass, the Sass option was a handy way to bring the framework into my CSS and use it as a starting point.&lt;/p&gt;

&lt;h3 id=&quot;github-pages-custom-url&quot;&gt;Github Pages custom URL&lt;/h3&gt;

&lt;p&gt;I use Github to manage the code. This means it’s in a safe place should I lose my work locally. It also means I can host the pages as raw HTML files using Github Pages. You can use a custom URL with Github Pages by telling it your URL in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CNAME&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;The content of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CNAME&lt;/code&gt; was simply: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cssanimation.rocks&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The next step was to add an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; record to my DNS to point to Github’s IP address. This then points the browser to Github, and they connect up the request to the right files. More instructions on setting up custom URLs is &lt;a href=&quot;https://help.github.com/articles/setting-up-a-custom-domain-with-github-pages/&quot;&gt;available here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;deploying---not-ideal-but-works&quot;&gt;Deploying - not ideal but works&lt;/h3&gt;

&lt;p&gt;While it’s possible to deploy Jekyll sites directly to master (if you name your repo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username.github.io&lt;/code&gt;), I wasn’t able to get it to work with the Jekyllized set up. Because I had a non-standard Jekyll setup, it didn’t run as expected. Instead I found I could push the contents of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serve&lt;/code&gt; directory directly to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gh-pages&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;It’s an extra step but works for now. I don’t doubt there’s a better way but my goal was to get up and running quickly, and it’s enough.&lt;/p&gt;

&lt;h2 id=&quot;making-content&quot;&gt;Making content&lt;/h2&gt;

&lt;p&gt;With the site in place the next step is to create content. It’s easy to spend a lot of time bike-shedding, looking at design details, font choices, colours etc, but at some point you need to take the step of writing something people might want to read.&lt;/p&gt;

&lt;p&gt;Before starting to write, I created a spreadsheet for potential topics, along with other content I might want to create at some stage. This list gives me a decent set of ideas should I be stuck, and acts as a record of topics covered.&lt;/p&gt;

&lt;p&gt;From this I picked the first topic, &lt;a href=&quot;https://cssanimation.rocks/twitter-fave/&quot;&gt;Twitter’s “fave” animation&lt;/a&gt;. A couple of hours creating the demo and code, then another few hours writing up the description of the process involved, and I had my first post.&lt;/p&gt;

&lt;p&gt;Before publishing I was starting the second post, &lt;a href=&quot;https://cssanimation.rocks/pseudo-elements/&quot;&gt;Animating pseudo-elements&lt;/a&gt;. As I write this, I have two more posts in the pipeline almost ready to publish. This gives me time to polish them and update them for a while before they go live.&lt;/p&gt;

&lt;h2 id=&quot;publishing-and-spreading-the-word&quot;&gt;Publishing and spreading the word&lt;/h2&gt;

&lt;p&gt;Often I publish articles a little earlier than I’m comfortable. There may be the occasional typo, or even small issues with the demo code, but I find it helps to get feedback as soon as possible, and budget time to fix up the article over the first few days.&lt;/p&gt;

&lt;p&gt;Your milage may vary but I find that if I sit on an article for too long, it feels stale and I struggle to give it as much attention. If it’s live, and I hear of a typo, I act immediately.&lt;/p&gt;

&lt;p&gt;With the article published, I put out word. Twitter’s a good place to start. There are some people that have followed me over the last couple of years that would often retweet or mention articles if I let them know.&lt;/p&gt;

&lt;p&gt;A second option is a mailing list. Email is far from dead, and is still a good way to reach people when you want to let them know about new content. It’s a bit more work setting up a message but worth it.&lt;/p&gt;

&lt;p&gt;The next step is to contact relevant newsletters. I follow some great newsletters and they’re often happy to receive submissions for inclusion. As long as your content matches the audience for the news letter, it’s a good way to quickly reach a larger number of people.&lt;/p&gt;

&lt;h2 id=&quot;maintaining-momentum&quot;&gt;Maintaining momentum&lt;/h2&gt;

&lt;p&gt;Over the next week after the initial article went live, I continued to mention it to people that might like it, and it was featured in three newsletters. It also made the first page of &lt;a href=&quot;http://news.layervault.com&quot;&gt;Designer News&lt;/a&gt;. Between these, it received over 10 thousand visits.&lt;/p&gt;

&lt;p&gt;This was a great start. Better than I’d hoped, and it brought with it quite a few email sign ups and Twitter followers. The next week, I did the same but also sent a newsletter update to the new sign ups, and again received over 10 thousand visits that week.&lt;/p&gt;

&lt;p&gt;While it’s very exciting to get a bunch of visitors, it’s better to maintain momentum. Something I found when blogging here over the years was that the visitors can drop off very quickly. Spending months making a single blog post is great and produces interesting results, but a more consistent result comes from frequent, smaller posts.&lt;/p&gt;

&lt;p&gt;I’m aiming to post on the &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS Animation Rocks&lt;/a&gt; site at least twice per month. I feel a weekly update would be good but until I can set aside more time, a little less often will do.&lt;/p&gt;

&lt;h2 id=&quot;future-plans&quot;&gt;Future plans&lt;/h2&gt;

&lt;p&gt;I’d like to also build out the site as a reference for &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS animation properties&lt;/a&gt; and how to use them. This will be quite a lot of work so I may put this back a while until the book is further along.&lt;/p&gt;

&lt;p&gt;Beyond this, I’d like to keep on enjoying making content more than anything else. I have no doubt there will be more ideas, and I’ll keep open to them as they come along.&lt;/p&gt;

&lt;h2 id=&quot;get-in-touch&quot;&gt;Get in touch&lt;/h2&gt;

&lt;p&gt;If you find this or &lt;a href=&quot;https://cssanimation.rocks&quot;&gt;CSS Animation&lt;/a&gt; useful, please consider giving it a mention on Twitter, &lt;a href=&quot;https://twitter.com/cssanimation&quot;&gt;or following along&lt;/a&gt;. For any thoughts, tips, questions (or typos), you can also &lt;a href=&quot;mailto:d@hop.ie&quot;&gt;email me&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Tue, 10 Feb 2015 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/launching-new-website/</link>
        <guid isPermaLink="true">http://hop.ie/blog/launching-new-website/</guid>
      </item>
    
      <item>
        <title>On Running</title>
        <description>&lt;p&gt;In March, 2014 I began running outside. Until that point I’d been occasionally running two kilometres or so at a gym, but never thought to go for a proper run.&lt;/p&gt;

&lt;p&gt;I hadn’t run properly outside since school. I did try about 5 years ago. I joined a running club, went to the first meet, and managed to tag along for about 200 yards before veering off to a side road and walking home.&lt;/p&gt;

&lt;p&gt;Last week I ran 21.6km in just over two hours. I’m happy with that, but it’s not really the main reason I run. I run for lots of reasons, fitness, the challenge, but also to clear my head and take time away from computers.&lt;/p&gt;

&lt;p&gt;In 2015 I have set the goal of running the Dublin marathon. This takes place in late October.&lt;/p&gt;

&lt;h2 id=&quot;realising-its-possible&quot;&gt;Realising it’s possible&lt;/h2&gt;

&lt;p&gt;In the past year, the biggest thing that’s helped me get from barely 10 minutes of slow jogging to over 2 hours, is the realisation that it’s possible. The first time I realised this was after signing up to take part in the 5k run at &lt;a href=&quot;http://2014.ull.ie&quot;&gt;Úll&lt;/a&gt;. The evening after signing up, I mapped out a 5k run online and went to see how it felt to run it.&lt;/p&gt;

&lt;p&gt;About 30 minutes or so later I got home, barely able to catch a breath, with sweat flying everywhere. I’d run 5k. Nearly fainted in the process, but it was possible.&lt;/p&gt;

&lt;p&gt;Each time I started getting comfortable with a distance, I’d wonder if it was possible to do another couple of kilometres more. It wasn’t always easy but it was possible.&lt;/p&gt;

&lt;h2 id=&quot;injury&quot;&gt;Injury&lt;/h2&gt;

&lt;p&gt;I can’t stress enough the importance of form. I’ve never taken any professional lessons, which meant I made some mistakes that caused shin and knee pains. It wasn’t too much of a problem around the 5k level, but once I started running 10k several times per week, I found that it was getting harder and harder rather than easier.&lt;/p&gt;

&lt;p&gt;My problem was that I was taking long, loping strides. When I’d start the run I’d be quite fast and running reasonably well (with my feet landing under my hips, around the mid-foot point). This fast start would lead to my getting tired after a while, then I’d kind of limply run along, landing on the back of the foot and out of alignment.&lt;/p&gt;

&lt;p&gt;It took me a while, and a lot of knee pain, to realise that I was better off taking small steps. Reading up on the subject, I found that it was generally recommended to aim for 180 steps per minute.&lt;/p&gt;

&lt;p&gt;When I tried aiming for this rate, or cadence, I found that while my overall running time was slower, the pain vanished and I could run for longer without stopping to walk.&lt;/p&gt;

&lt;h2 id=&quot;gym-training&quot;&gt;Gym training&lt;/h2&gt;

&lt;p&gt;An aspect I hadn’t considered was the need to train at a gym. Especially when getting the legs used to running, it’s helpful to work on muscle strength. While I’ve not been to the gym for a few months, I did find focusing on leg exercises at the start helped get rid of some of the pains.&lt;/p&gt;

&lt;h2 id=&quot;enjoying-it&quot;&gt;Enjoying it&lt;/h2&gt;

&lt;p&gt;There’s lots of talk of pain but it would be impossible for me to run regularly if I didn’t enjoy it. Aside from the feeling of accomplishment that comes from beating a record (or simply finishing a long run without any pain), I find running clears my head. It’s a bit of time where it’s just me, some music, and the road.&lt;/p&gt;

&lt;p&gt;It’s important to get away from interruptions. I’m terrible at maintaining focus, always checking what’s happening on Twitter or Slack. When I run I put the phone into silent mode, put on a podcast or some music and keep it in my pocket.&lt;/p&gt;

&lt;h3 id=&quot;temptations-of-technology&quot;&gt;Temptations of technology&lt;/h3&gt;

&lt;p&gt;Having said that, it could be useful to know my current pace and step cadence. Having that information available at a glance might help me keep up with my target running speed, but there are other ways of getting this information than having a screen. I’m already listening to music, so that’s a vector.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://welcome.moov.cc/&quot;&gt;Moov&lt;/a&gt; portable fitness coach is a device that I’m currently trying out. It’s a disk you strap onto your ankle that tracks movement including steps per minute, step weight, and other factors. It then reports this information back in the form of recommendations such as “Speed up, your pace is currently lower than the target pace” or “Try stepping more lightly”.&lt;/p&gt;

&lt;p&gt;So far I’m not entirely convinced by this. As far as interruptions go it’s quite intense having a voice prompting you every few seconds and it interrupts my flow. I might use this tool to monitor my progress on occasion but as the moment I suspect my need for quiet time will be more important.&lt;/p&gt;

&lt;h2 id=&quot;essential-equipment&quot;&gt;Essential equipment&lt;/h2&gt;

&lt;p&gt;Your milage may vary but here’s what I have found essential:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Decent, light running shoes&lt;/li&gt;
  &lt;li&gt;Shorts&lt;/li&gt;
  &lt;li&gt;Summer: short sleeve polyester running top&lt;/li&gt;
  &lt;li&gt;Winter: I have an Underarmour long-sleeve top works well as a base layer&lt;/li&gt;
  &lt;li&gt;Winter: I am also big fan of this &lt;a href=&quot;http://www.inov-8.com/New/Global/Product-View-Clothing-OffRoad-Outer-Race-Elite-70.asp?L=26&quot;&gt;inov-8 Windshell&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;iPhone with Runkeeper, Overcast, Spotify, and earphones&lt;/li&gt;
  &lt;li&gt;Facecloth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had a hard time adjusting from the summer to winter at first until discovering the Windshell. It’s very light, but stops the wind from getting through and stays comfortable even for a long run.&lt;/p&gt;

&lt;p&gt;Lastly, don’t forget lubricant. Not a lot of places seem to mention this, but when you’re running, things rub against other things. &lt;a href=&quot;http://www.amazon.com/Mueller-Lube-Stick-Runners-2-25-Ounce/dp/B000TS8MHG&quot;&gt;This one&lt;/a&gt; works well. Apply liberally.&lt;/p&gt;

&lt;p&gt;Also, as &lt;a href=&quot;https://medium.com/having-it-some/the-awful-truth-about-jogging-66b45bb2f2ca&quot;&gt;noted here&lt;/a&gt;, it’s always good to plan for a decent visit to the rest room before going for a run. So much more relaxing.&lt;/p&gt;

&lt;h2 id=&quot;what-about-you&quot;&gt;What about you?&lt;/h2&gt;

&lt;p&gt;Do you run? Feel free to look me up on Runkeeper. I’m Donovan Hutchinson. If you’ve had running experiences you’d like to share or questions, do &lt;a href=&quot;mailto:d@hop.ie&quot;&gt;drop me an email&lt;/a&gt; or say hi &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Wed, 07 Jan 2015 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/running/</link>
        <guid isPermaLink="true">http://hop.ie/blog/running/</guid>
      </item>
    
      <item>
        <title>Hop interviews: Dan O'Neill</title>
        <description>&lt;p&gt;Working remotely is becoming increasingly popular, and it’s great to hear people’s experiences. One such person is Dan O’Neill, web developer and author of &lt;a href=&quot;http://wordsandmagic.com/&quot;&gt;Words &amp;amp; Magic&lt;/a&gt;. Here’s how he manages the challenges of working for a full time client from home with his own web development work.&lt;/p&gt;

&lt;h2 id=&quot;what-sort-of-work-do-you-do&quot;&gt;What sort of work do you do?&lt;/h2&gt;

&lt;p&gt;So I’m in the lucky (or not) position to have 2 jobs. I work full time for a large tech company as a windows systems engineer. I would give you my full title but you’d be bored half way through. Its a mouthful. I work almost full time from home for this job. I also have a tiny (i.e. just me) freelance web development company called 26Squared that I work on the side from home too.&lt;/p&gt;

&lt;h2 id=&quot;for-how-long-have-you-been-working-remotely&quot;&gt;For how long have you been working remotely?&lt;/h2&gt;

&lt;p&gt;I’ve been working from home with my full time job for 3 years now. I wasn’t always full time from home but gradually moved that way whenever I could.&lt;/p&gt;

&lt;h2 id=&quot;were-you-working-full-time-in-an-office-before&quot;&gt;Were you working full-time in an office before?&lt;/h2&gt;

&lt;p&gt;Yeah, every job I’ve had before required quite a bit of on site work so I was always tethered to a desk. I like the atmosphere that an office can build, but larger companies can be quite different that way. I look after servers in data centres in Japan, California and the east coast of America so I’m no closer in the office than I am at home!&lt;/p&gt;

&lt;h2 id=&quot;where-do-you-work-from&quot;&gt;Where do you work from?&lt;/h2&gt;

&lt;p&gt;I have a home office/library room that I work from. I’ve just moved the home office from one room to another in my house to give me more room. Having a separate space for just work is essential if you have the room - it makes it easier to hunker down and work if you have a deadline, or makes it easier to disconnect from work by just leaving the room!&lt;/p&gt;

&lt;h2 id=&quot;do-you-work-remotely-all-the-time-or-part-time&quot;&gt;Do you work remotely all the time, or part time?&lt;/h2&gt;

&lt;p&gt;Almost full time. With the day job I get into the office a day a week if I can, everyone on my wider team does so we can chat about football, the weekend or whatever. I think this is important - you learn more about a persons tone and mannerisms in person that you can later assign to messages or emails that you might get. With the web development stuff, I work full time from home. Though I like to meet up with certain clients a couple of times over projects. I find I learn more from these meetings than I might just over an exclusive email exchange. I’m not saying its impossible - I work with one client from Kilkenny exclusively over email. That relationship works a little differently though.&lt;/p&gt;

&lt;h2 id=&quot;do-you-have-multiple-clients--projects&quot;&gt;Do you have multiple clients / projects?&lt;/h2&gt;

&lt;p&gt;Apart from the day job, yes, I have multiple projects from a set of repeat clients.&lt;/p&gt;

&lt;h2 id=&quot;how-do-you-approach-managing-the-time&quot;&gt;How do you approach managing the time?&lt;/h2&gt;

&lt;p&gt;Your calendar app is going to become your best friend. Be it sunrise or google calendar or the mac calendar app. One important thing I’ve had to learn to do is make sure I don’t randomly reply to emails while just browsing the internet late at night. You have to set boundaries around what times you’re willing to work. Working from home you can quickly slide outside the boundaries of a 9 to 5, because you have no commute or you’re in the zone working on something or whatever.&lt;/p&gt;

&lt;h2 id=&quot;what-would-you-say-are-the-best-and-worst-things-about-working-remotely&quot;&gt;What would you say are the best and worst things about working remotely?&lt;/h2&gt;

&lt;p&gt;The ability to get work done when I need to. The ability to not do any work at all if I need that too! Extra freedom, better work-life balance too if you can get the time management right. The loneliness of not being in a shared office is something that took me by surprise, that’s why all the Irish folks on my team try to meet regularly. One of the worst things is the expectation of all day support. Well you have the ability to do that remotely so why wait until you get into an office.&lt;/p&gt;

&lt;h2 id=&quot;do-you-attend-meet-ups-or-conferences-to-network&quot;&gt;Do you attend meet ups or conferences to network?&lt;/h2&gt;

&lt;p&gt;I try to but with a non web dev day job I’d have to take vacation days! I would love to go to some of them.&lt;/p&gt;

&lt;h2 id=&quot;how-do-you-tend-to-find-clients&quot;&gt;How do you tend to find clients?&lt;/h2&gt;

&lt;p&gt;All word of mouth, fortunately, I did some work for some charities, that lead to some other work with some organisations, and then it spread from there. My client list is small but they’re all repeat jobs.&lt;/p&gt;

&lt;h2 id=&quot;when-approaching-clients-do-you-find-selling-an-hourly-service-preferable-to-value-based-pricing&quot;&gt;When approaching clients, do you find selling an hourly service preferable to value-based pricing?&lt;/h2&gt;

&lt;p&gt;Clients definitely I think find an hourly rate easier. I try to estimate how much time I will spend on a project and let the client know how we’re doing as we go along. Communication is key.&lt;/p&gt;

&lt;h2 id=&quot;is-late-paying--non-paying-ever-an-issue-how-might-you-handle-it&quot;&gt;Is late paying / non-paying ever an issue? How might you handle it?&lt;/h2&gt;

&lt;p&gt;Yes, with some work I did for an Irish TV channel, I did 4 hours of freelance work and have still not been paid a year later. I’ve written that off. Luckily that’s been it.&lt;/p&gt;

&lt;h2 id=&quot;ever-had-to-fire-a-client&quot;&gt;Ever had to fire a client?&lt;/h2&gt;

&lt;p&gt;Nope, but I wont be doing any work for that TV channel!&lt;/p&gt;

&lt;h2 id=&quot;overall-would-you-recommend-remote-working&quot;&gt;Overall, would you recommend remote working?&lt;/h2&gt;

&lt;p&gt;I know I’ve probably been a bit harsh on working from home but I really enjoy it. It gives me freedom that you’ll never get in an office job!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;thanks-dan&quot;&gt;Thanks, Dan!&lt;/h2&gt;

&lt;p&gt;If you enjoyed this interview (or if you didn’t), please let me know &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt; or &lt;a href=&quot;mailto:d@hop.ie&quot;&gt;email&lt;/a&gt;. If you have experience working for yourself and want to add something, do get in touch.&lt;/p&gt;
</description>
        <pubDate>Tue, 09 Dec 2014 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/dan-oneill/</link>
        <guid isPermaLink="true">http://hop.ie/blog/dan-oneill/</guid>
      </item>
    
      <item>
        <title>Dark patterns in retail</title>
        <description>&lt;p&gt;We’ve all seen the tricks companies use online to get us to do things against our better judgement. “Subscribe to mailing list” checkboxes pre-checked, insurance opt-out hidden in a list of countries, modal windows that appear when you try to leave the page. But what sort of dark nastiness do retailers try to pull on us in the physical world?&lt;/p&gt;

&lt;p&gt;This week saw uproar online when several large US pharmacy and retailers deactivated their NFC terminals, stopping all contactless payments. This meant anyone wanting to use Apple Pay or Google Wallet were out of luck.&lt;/p&gt;

&lt;h2 id=&quot;mcx-and-currentc&quot;&gt;MCX and CurrentC&lt;/h2&gt;

&lt;p&gt;In 2012 &lt;a href=&quot;http://www.mcx.com/&quot;&gt;a group of companies&lt;/a&gt;, led by Walmart, got together to put together a system that would enable them to charge people directly and avoid paying credit card processing fees on each transaction.&lt;/p&gt;

&lt;p&gt;The result of this groups efforts is a product called &lt;a href=&quot;http://currentc.com/&quot;&gt;CurrentC&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;dead-on-arrival---but-that-doesnt-matter&quot;&gt;Dead on arrival - but that doesn’t matter&lt;/h2&gt;

&lt;p&gt;The CurrentC product is a terribly designed product from a user experience point of view. Adding extra complexity to the checkout and relying on the use of a dedicated app is not helpful to customers. Storing bank account information, social security numbers and more in centralised locations known for leaking data is dangerous.&lt;/p&gt;

&lt;p&gt;It doesn’t look like this product is aiming to be as easy to use or secure as Apple Pay. Instead it is a tool aimed at &lt;a href=&quot;http://www.imore.com/depth-look-currentc-and-personal-data-they-want-collect&quot;&gt;collecting as much data about customers&lt;/a&gt; are possible.&lt;/p&gt;

&lt;p&gt;Having said that, the goal here may not even be to get customer information, but instead leverage this product as a negotiation tool to push for cheaper credit card processing fees.&lt;/p&gt;

&lt;p&gt;In either case, the tragic side effect of this is that a large number of retailers are engaging in the act of cutting off their nose to spite their face. By disabling NFC payments, they are offering customers less choice and holding back more secure and pleasant ways to pay.&lt;/p&gt;

&lt;h2 id=&quot;dark-patterns&quot;&gt;Dark patterns&lt;/h2&gt;

&lt;p&gt;This act of shutting off a service so that another, competing, service can be introduced sounds like one of these dark patterns we see online. By creating a pain for customers, it looks like the retailers are planning to wheel out their own solutions as a way to end this artificially-induced limitation.&lt;/p&gt;

&lt;h2 id=&quot;your-experiences&quot;&gt;Your experiences&lt;/h2&gt;

&lt;p&gt;I’d be interested to hear of your experiences in retail and how people are being manipulated through other dark patterns. Drop me a line on [Mastodon(http://mastodon.ie/@donovanh) or by &lt;a href=&quot;mailto:d@hop.ie&quot;&gt;email&lt;/a&gt; and I’ll include your thoughts here.&lt;/p&gt;

&lt;h2 id=&quot;update&quot;&gt;Update&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://techcrunch.com/2014/10/29/retailer-backed-apple-pay-rival-currentc-has-been-hacked-testers-email-addresses-stolen/&quot;&gt;According to Techcrunch&lt;/a&gt;, the initial testing version of CurrentC has been hacked. I would strongly advise anyone to not put their bank account details in this system.&lt;/p&gt;
</description>
        <pubDate>Wed, 29 Oct 2014 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/currentc/</link>
        <guid isPermaLink="true">http://hop.ie/blog/currentc/</guid>
      </item>
    
      <item>
        <title>A Better 404</title>
        <description>
</description>
        <pubDate>Tue, 12 Aug 2014 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/better-404/</link>
        <guid isPermaLink="true">http://hop.ie/blog/better-404/</guid>
      </item>
    
      <item>
        <title>Hop interviews: Jamie Lawrence</title>
        <description>&lt;p&gt;Working for yourself, or even just by yourself, is a tempting idea for many people but not always an option. I thought it would be interesting to hear from some others in Ireland who have decided to take control of their own time and work remotely.&lt;/p&gt;

&lt;p&gt;Introducing &lt;a href=&quot;http://ideasasylum.com/&quot;&gt;Jamie Lawrence&lt;/a&gt;. Jamie is a web developer living in Cork. With a skill set that covers not only the full technology stack but also how to apply it to real business situations, he’s well qualified to be considered “your interim CTO”. He was kind enough to answer some questions I had about what it means to work remotely, set your own terms, and &lt;em&gt;fear less&lt;/em&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;what-sort-of-work-do-you-do&quot;&gt;What sort of work do you do?&lt;/h2&gt;

&lt;p&gt;I’m currently a Rails and Javascript developer but I market myself in the wider context of an “interim CTO” because I’m more interested in business / startups / copywriting / marketing / sales etc than just coding.&lt;/p&gt;

&lt;h2 id=&quot;for-how-long-have-you-been-working-remotely&quot;&gt;For how long have you been working remotely?&lt;/h2&gt;

&lt;p&gt;I worked in IBM for 5 years, 3 of which were spent working remotely for projects in the US. For over 18 months prior to March I worked for myself, mostly from home. Since March, I joined my client’s company as CTO working onsite in the office in Cork.&lt;/p&gt;

&lt;h2 id=&quot;were-you-working-full-time-in-an-office-before-if-so-what-prompted-the-change&quot;&gt;Were you working full-time in an office before? If so, what prompted the change?&lt;/h2&gt;

&lt;p&gt;So, I’ve had a checkered history. I’ve worked in offices 9 - 5:30 wearing a suit and tie and I’ve worked in relaxed open spaces with no fixed hours. In IBM, I was working in the Cork office, which was originally quite “small company” but quickly became very “big company”. As it grew, the work became more diversified and most of us ended up working on projects that were run entirely from the US offices. For about 18mths I was the only non-US employee working this project with 10-15 other developers and testers. Some of the US employees worked remotely from home but I was still based in the Cork office even though all my colleagues and management structures were based in the US.&lt;/p&gt;

&lt;p&gt;It was an odd time. More Irish employees joined the project but we were still remote despite sitting in the same building. We’d have daily “stand ups” over the phone and that was how I knew what the testers a few desks over or the doc writer upstairs was working on. Occasionally, I’d go over to their desk to walk them through an issue but maybe only once or twice a week. We mostly communicated with email, issue tracker and Sametime (IM)&lt;/p&gt;

&lt;p&gt;Ultimately, I left because I was really depressed and fucking hated the work and company. I wanted more control, more influence over the work I did, more choice in the technologies that I used, etc.&lt;/p&gt;

&lt;h2 id=&quot;where-did-you-work-from-converted-attic-shared-working-space&quot;&gt;Where did you work from? (Converted attic, shared working space?)&lt;/h2&gt;

&lt;p&gt;I worked from a spare room upstairs at home. It was always set up as an “office” for the iMac, photo processing etc. but this was the first time I’d used it for a job. Later, I took a part-time desk in a shared space for 1 day a week to give myself a solid interruption-free day of work. For the last six months, I took that desk full-time and used it 3 or 4 days a week.&lt;/p&gt;

&lt;h2 id=&quot;do-you-work-remotely-all-the-time-or-part-time&quot;&gt;Do you work remotely all the time, or part time?&lt;/h2&gt;

&lt;p&gt;All the time when I was working for myself. Even though my client was also in Cork and wanted me specifically because I was local, we only met in-person once every 3-4 months.&lt;/p&gt;

&lt;h2 id=&quot;did-you-have-multiple-clients--projects&quot;&gt;Did you have multiple clients / projects?&lt;/h2&gt;

&lt;p&gt;No. I always felt like I was a failure as a freelancer for only having one client… but then I realised that the projects I wanted were not of the 6-8wk variety. I wanted long-term engagements where I could work closely with and influence the client’s business over an extended period of time. Also, he was happy to keep giving me as much work as I wanted (I aimed for 20-25 billable hours a week) so there really wasn’t room for taking on another client. In the future, I’d like to have a one or two 2-5hr/wk retainers plus one bigger client, but I think it takes time to build up to retainer work.&lt;/p&gt;

&lt;h2 id=&quot;what-would-you-say-are-the-best-and-worst-things-about-working-remotely&quot;&gt;What would you say are the best and worst things about working remotely?&lt;/h2&gt;

&lt;p&gt;This is a loaded question and probably a good time to unpack the 3 issues at stake: working remotely, working at home, and working for yourself. Each have their own distinct advantages/disadvantages although they overlap.&lt;/p&gt;

&lt;h3 id=&quot;working-remotely&quot;&gt;Working remotely:&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;The good:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I enjoy the location freedom and being able to choose my best work environment.&lt;/li&gt;
  &lt;li&gt;I can limit the distractions (e.g. setting a DND status is easier than physically blocking someone from talking to you)&lt;/li&gt;
  &lt;li&gt;I can listen to music, etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Not so good:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Some problems are best resolved face-to-face and it probably helps if you’ve met the people you’re working with at least a few times in real life&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;working-from-home&quot;&gt;Working from home:&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;The good:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;No commute (though mine was only a 20mins commute any way)&lt;/li&gt;
  &lt;li&gt;More flexibility to see the kids (I could take them to school, or pick them up, or time my lunch break to coincide with when they came home)&lt;/li&gt;
  &lt;li&gt;I kept up with small jobs around the house like tidying the office, or putting on the dishwasher, or fixing a toy. I’m not sure if that’s a good thing but, if I needed a break, at least there was something productive to do whilst I mulled over a problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Not so good:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I got distracted easily (e.g. if my mum came over in the afternoon or if it was a beautiful day)&lt;/li&gt;
  &lt;li&gt;I could hear the kids if they were screaming (happily or not). It’s hard to get dragged into little family dramas (someone falls over, the meat for dinner had gone off, etc, etc). It took a few months to educate the kids that I was working upstairs but then they mostly got it. I might get some noise-cancelling headphones soon though.&lt;/li&gt;
  &lt;li&gt;Less reason to get out of the house, leading to manic episodes at the weekend where I’d need to escape.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;working-for-yourself&quot;&gt;Working for yourself:&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;The good:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Set my own hours. I looked back and realised that my brain doesn’t slip into “coding mode” until at least 11am, but more often 2-3pm. Why the hell had I been wasting 3 or 4 hours each day of my time, sitting at a desk being unproductive, for the past 10 years? That’s like 7500 hours of my life being wasted. I felt that I certainly couldn’t charge for this unproductive time so I’d use it for something else that would make me happier and more productive for the hours I charged for.&lt;/li&gt;
  &lt;li&gt;So I can swim in the mornings. Or maybe take a walk. And sometimes grab brunch in Kinsale. This had the single biggest positive effect on my life and I’ll defend it at all costs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Not so good:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Worrying about where to get more clients (employees suffer from depression because of the lack of control over their destiny; freelancers suffer from anxiety worrying about the next client etc — I couldn’t have it both ways!)&lt;/li&gt;
  &lt;li&gt;Accounts, bookkeeping, taxes, VAT returns, etc. even though I have a great accountant&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;socially-do-you-attend-meet-ups-or-conferences-to-network&quot;&gt;Socially, do you attend meet ups or conferences to network?&lt;/h2&gt;

&lt;p&gt;I used to run the Ruby user group in Cork just so I could meet some like-minded people although it was really small. I stopped running it earlier this year when I stopped freelancing but I make sure to attend some of the local events (CorkDev, BuiltInCork). I wish Cork had a more vibrant tech scene like Dublin though, and having a wider group of people to mingle with. I do 1 or 2 conferences a year in Ireland / UK / Europe but I’d like to be able to justify 3 or 4 in the future&lt;/p&gt;

&lt;h2 id=&quot;what-would-you-recommend-people-do-if-they-are-thinking-of-moving-from-full-time-employment-to-contracting-or-remote-working&quot;&gt;What would you recommend people do if they are thinking of moving from full-time employment to contracting or remote working?&lt;/h2&gt;

&lt;p&gt;Try to do a little moonlighting in your spare time, particularly if the work will be different to your current employment.&lt;/p&gt;

&lt;p&gt;Decide who your perfect client would be and what makes you different (unlike full-time employers, clients don’t care about your CV. They just care about what you can do for them so you need to be able to sell yourself).&lt;/p&gt;

&lt;p&gt;Try to book your first client before your leave your job (they might say they need you now, but tell them you’re busy and have a slot opening up in a month — then resign).&lt;/p&gt;

&lt;p&gt;Have a little financial cushion to cover initial costs, payment delays etc. but…&lt;/p&gt;

&lt;p&gt;Fear less. How long did it take you to get your last job? How in-demand are your skills? As a developer, I’m pretty sure I could get a full-time job in a month if I needed to.&lt;/p&gt;

&lt;p&gt;Lastly, read everything that &lt;a href=&quot;http://brennandunn.com/&quot;&gt;Brennan Dunn&lt;/a&gt; has produced. &lt;a href=&quot;http://www.freelancersshow.com/&quot;&gt;The Freelancer Show&lt;/a&gt; is also an incredible resource (go back to the start) and they’ve recently run a “preparing for freelancing” series of episodes.&lt;/p&gt;

&lt;h2 id=&quot;how-do-you-tend-to-find-clients&quot;&gt;How do you tend to find clients?&lt;/h2&gt;

&lt;p&gt;Mostly they find me though &lt;a href=&quot;http://ideasasylum.com&quot;&gt;my website&lt;/a&gt;, word-of-mouth and by Googling “ruby cork” and finding my meetup page. I’ll have to make a more concerted marketing push in the future though.&lt;/p&gt;

&lt;h2 id=&quot;when-approaching-clients-do-you-find-selling-an-hourly-service-preferable-to-value-based-pricing&quot;&gt;When approaching clients, do you find selling an hourly service preferable to value-based pricing?&lt;/h2&gt;

&lt;p&gt;I charged hourly but I’d like to change to a day/weekly rate and some more productised consulting approaches. I’m specifically thinking of augmenting my developer/interim CTO roles with a product for analysing, predicting and mitigating churn in SaaS apps. Initially this would be “flintstoned” (part software, largely my effort) before moving to a fully-automated approach.&lt;/p&gt;

&lt;h2 id=&quot;is-late-paying--non-paying-ever-an-issue-how-might-you-handle-it&quot;&gt;Is late paying / non-paying ever an issue? How might you handle it?&lt;/h2&gt;

&lt;p&gt;Rarely an issue for me but, in general, yes. I worked in two-week sprints, billed at the end of every sprint and had non-negotiable 15-day terms. That meant that product development was always in-step with the income. My contract also states that I cease work (and retain the intellectual property) if an invoice remains unpaid… so, at most, they’d get 1 month (i.e., 1 sprint + 15 days) of work out of me for free. If possible, I’d also take an initial deposit of 2 to 4 weeks to cover this.&lt;/p&gt;

&lt;h2 id=&quot;ever-had-to-fire-a-client&quot;&gt;Ever had to fire a client?&lt;/h2&gt;

&lt;p&gt;No. Though I probably should have transitioned my last client on to a full-time team sooner.&lt;/p&gt;

&lt;h2 id=&quot;overall-would-you-recommend-remote-working&quot;&gt;Overall, would you recommend remote working?&lt;/h2&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; lang=&quot;en&quot;&gt;&lt;p&gt;Dear Recruiters, if you want me to move to Dublin you should know that you&amp;#39;re competing with this &lt;a href=&quot;http://t.co/oibCZveguu&quot;&gt;pic.twitter.com/oibCZveguu&lt;/a&gt;&lt;/p&gt;&amp;mdash; Jamie Lawrence (@ideasasylum) &lt;a href=&quot;https://twitter.com/ideasasylum/statuses/488422851272519680&quot;&gt;July 13, 2014&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async=&quot;&quot; src=&quot;//platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;

&lt;p&gt;Hell, yes!&lt;/p&gt;

&lt;p&gt;Location freedom is important, but it’s more important to me to have time-freedom. All those wasted hours at my previous jobs still haunt me. Why should 40 hours each week be the standard?&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;thanks-jamie&quot;&gt;Thanks, Jamie!&lt;/h2&gt;

&lt;p&gt;If you enjoyed this interview (or if you didn’t), please let me know &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;on Mastodon&lt;/a&gt; or &lt;a href=&quot;mailto:d@hop.ie&quot;&gt;email&lt;/a&gt;. If you have experience working for yourself and want to add something, do get in touch.&lt;/p&gt;
</description>
        <pubDate>Sun, 27 Jul 2014 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/jamie-lawrence/</link>
        <guid isPermaLink="true">http://hop.ie/blog/jamie-lawrence/</guid>
      </item>
    
      <item>
        <title>Wraith</title>
        <description>&lt;p&gt;We have many tools manage and test how our front-end code works, but not so many to test how it looks. I’ve been looking for tools that make it easier to catch visual changes when working on CSS. One such tool is &lt;a href=&quot;http://bbc-news.github.io/wraith/&quot;&gt;Wraith&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;testing-responsive-designs&quot;&gt;Testing responsive designs&lt;/h2&gt;

&lt;p&gt;While there are all sorts of ways to test back-end code, testing the way the front-end looks and works usually involves manually checking how pages look and feel at various sizes. Changes to CSS in one page might break other parts of the site, and it can be easy to miss issues.&lt;/p&gt;

&lt;p&gt;There are many tools for testing front-end functionality, such as &lt;a href=&quot;http://hop.ie/blog/casperjs/&quot;&gt;CasperJS&lt;/a&gt;. CasperJS drives a virtual web browser and stepping through interactions. While it can handle the functional testing of a site, it doesn’t test how a site looks.&lt;/p&gt;

&lt;h2 id=&quot;wraith&quot;&gt;Wraith&lt;/h2&gt;

&lt;p&gt;Wraith is a tool for testing responsive designs. It generates sets of screenshots, at various page widths, and compares a two versions to highlight differences. It then creates a gallery of thumbnails of both versions of each page.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/wraith/hop.png&quot; alt=&quot;Some screenshots at various breakpoints of Hop.ie pages&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The tool is built and maintained by BBC web developers &lt;a href=&quot;http://twitter.com/dblooman&quot;&gt;Dave Blooman&lt;/a&gt;, &lt;a href=&quot;http://twitter.com/jcleveley&quot;&gt;John Cleveley&lt;/a&gt;, and &lt;a href=&quot;http://twitter.com/sthulbourn&quot;&gt;Simon Thulbourn&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;installing&quot;&gt;Installing&lt;/h3&gt;

&lt;p&gt;Wraith needs some other software in order to run. On Mac, I’d recommend using &lt;a href=&quot;http://brew.sh/&quot;&gt;Homebrew&lt;/a&gt; to install these. If you’re using Windows, &lt;a href=&quot;http://bbc-news.github.io/wraith/os-install.html#Windows&quot;&gt;find instructions here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First you’ll need to choose either &lt;a href=&quot;http://phantomjs.org/&quot;&gt;PhantomJS (webkit based)&lt;/a&gt; or &lt;a href=&quot;http://slimerjs.org/&quot;&gt;SlimerJS (gecko/Firefox)&lt;/a&gt;. I have been using PhantomJS so far and it seems alright, but it’s up to you. You’ll also need to install &lt;a href=&quot;http://www.imagemagick.org/&quot;&gt;ImageMagick&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;brew install imagemagick
brew install phantomjs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You will also need &lt;a href=&quot;https://www.ruby-lang.org/&quot;&gt;Ruby&lt;/a&gt; installed.&lt;/p&gt;

&lt;p&gt;Once you’ve done that, install the Wraith gem:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem install wraith
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The Wraith gem will now be available to use globally.&lt;/p&gt;

&lt;h3 id=&quot;setup&quot;&gt;Setup&lt;/h3&gt;

&lt;p&gt;To set up an example and see Wraith in action, create the initial skeleton directories. Navigate to a test directory, and run the setup command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wraith setup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This generates two folders, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configs&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;javascript&lt;/code&gt;. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configs&lt;/code&gt; folder contains configuration for your tests, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;javascript&lt;/code&gt; folder contains a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snap.js&lt;/code&gt; file that instructs Wraith how to operate PhantomJS and take the screenshots.&lt;/p&gt;

&lt;p&gt;By default, this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snap.js&lt;/code&gt; file will work well for standard web-facing sites. If you need cookie-based authentication or other options, have a look at this file. For now we’ll stick with the default. Instead, open the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.yaml&lt;/code&gt; file inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configs&lt;/code&gt; folder.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#Headless browser option
browser:
  webkit: &quot;phantomjs&quot;
  # gecko: &quot;slimerjs&quot;

#If you want to have multiple snapping files, set the file name here
snap_file: &quot;javascript/snap.js&quot;

# Type the name of the directory that shots will be stored in
directory:
  - 'shots'

# Add only 2 domains, key will act as a label
domains:
  english: &quot;http://www.live.bbc.co.uk/news&quot;
  russian: &quot;http://www.live.bbc.co.uk/russian&quot;

#Type screen widths below, here are a couple of examples
screen_widths:
  - 320
  - 600
  - 768
  - 1024
  - 1280

#Type page URL paths below, here are a couple of examples
paths:
  home: /
  uk_index: /uk

# If you don't want to name the paths explicitly you can use a yaml
# collection as follows, and names will be derived by replacing / with _
#
# paths:
#  - /imghp
#  - /maps

#Amount of fuzz ImageMagick will use
fuzz: '20%'

#Set the number of days to keep the site spider file
spider_days:
  - 10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This file uses &lt;a href=&quot;http://en.wikipedia.org/wiki/YAML&quot;&gt;YAML&lt;/a&gt; to store the config options. Two domains are required, usually a local domain and a remote version. These URLs form the basis of a set of testing paths. The labels (home, uk_index, etc) are used to identify your page in the gallery generated later.&lt;/p&gt;

&lt;p&gt;You can also specify any screen widths in pixels. A screenshot at each pixel width is generated, taking in the entire height of the page. Fuzziness is used to allow for small differences between the two pages.&lt;/p&gt;

&lt;p&gt;I found the BBC URLs caused issues, so we will need to change a couple of things before this will work.&lt;/p&gt;

&lt;h3 id=&quot;adjusting-the-settings&quot;&gt;Adjusting the settings&lt;/h3&gt;

&lt;p&gt;First, change the domains to this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Add only 2 domains, key will act as a label
domains:
  english: &quot;http://www.google.co.uk&quot;
  french: &quot;http://www.google.fr&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, remove the “uk_index” entry:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#Type page URL paths below, here are a couple of examples
paths:
  home: /
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will instruct Wraith to look up the Google UK and France landing pages, and compare them. Let’s try that!&lt;/p&gt;

&lt;h3 id=&quot;running&quot;&gt;Running&lt;/h3&gt;

&lt;p&gt;With the config in place, Wraith can be run with this command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wraith capture config
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config&lt;/code&gt; part of the command refers to the config file we’ve just edited. If you have multiple config files, you can refer to them individually.&lt;/p&gt;

&lt;p&gt;If all goes to plan, you should see a series of updates like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Snapping http://www.google.co.uk/ at width 320
Snapping http://www.google.fr/ at width 320
Snapping http://www.google.co.uk/ at width 600
Snapping http://www.google.fr/ at width 600
Snapping http://www.google.co.uk/ at width 768
...
cropping images
cropping images
...
Saved diff
Saved diff
...
Generating thumbnails
Gallery generated
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Open the generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shots&lt;/code&gt; folder, and you should see a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gallery.html&lt;/code&gt; page. Open this in browser and you’ll be presented with a set of screenshots at various sizes, along with a third column showing any differences.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/wraith/example.png&quot; alt=&quot;Google.co.uk and Google.fr compared&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Flicking through these is a handy way to see any changes across the selected pages. The right column also includes a percentage difference between the two pages.&lt;/p&gt;

&lt;p&gt;Running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wraith capture config&lt;/code&gt; again will re-generate the images and overwrite any existing files.&lt;/p&gt;

&lt;h3 id=&quot;possible-workflow&quot;&gt;Possible workflow&lt;/h3&gt;

&lt;p&gt;Once the initial shots folder has been created, and a set of pages added to the config, you will have a set of images documenting any visual changes to your pages. One approach might be to check these into your version control system. This would give you a visual history of all layout changes, tied to specific commits.&lt;/p&gt;

&lt;p&gt;This may not be very practical though, as you may find if you’re creating many screenshots that it adds a lot of weight to your repository. Testing a few pages on this blog quickly resulted in 9MB of screenshots being pushed with each test.&lt;/p&gt;

&lt;p&gt;At it’s most basic, the tool presents a useful way to quickly scan through a set of pages when working on the front-end and check that nothing has changed unexpectedly.&lt;/p&gt;

&lt;h3 id=&quot;no-passing-or-failing&quot;&gt;No passing or failing&lt;/h3&gt;

&lt;p&gt;This testing does not result in a green or red pass/fail result. Instead it offers a way to more easily manually check layouts. Other unit or functional test tools are better suited to continuous integration testing.&lt;/p&gt;

&lt;p&gt;It’s also worth noting that the web drivers supported only include Webkit and Gecko. This means testing in other browsers, such as Internet Explorer, is not covered by this tool.&lt;/p&gt;

&lt;h3 id=&quot;live-gallery-example&quot;&gt;Live gallery example&lt;/h3&gt;

&lt;p&gt;I have started using this tool to check on this blog, you can &lt;a href=&quot;/wraith/shots/gallery.html&quot;&gt;see the results here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;your-process&quot;&gt;Your process&lt;/h2&gt;

&lt;p&gt;Do you currently use tools to help with responsive design and flexible layout testing? &lt;a href=&quot;https://mastodon.ie/@donovanh&quot;&gt;Do let me know&lt;/a&gt;, I’d love to hear what works for you.&lt;/p&gt;
</description>
        <pubDate>Fri, 25 Apr 2014 00:00:00 +0000</pubDate>
        <link>http://hop.ie/blog/wraith/</link>
        <guid isPermaLink="true">http://hop.ie/blog/wraith/</guid>
      </item>
    
  </channel>
</rss>
