<?xml-stylesheet href="/pretty-feed-v2.xsl" type="text/xsl"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Trys Mudford's Blog</title>
    <link>https://www.trysmudford.com/year/2024/</link>
    <description>Posts, thoughts, links and photos from Trys</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 31 Dec 2024 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://www.trysmudford.com/blog/index.xml" rel="self" type="application/rss+xml"/>
    
    <item>
      <title>2024 Wrapped</title>
      <link>https://www.trysmudford.com/blog/2024-wrapped/</link>
      <pubDate>Tue, 31 Dec 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/2024-wrapped/</guid>
      <description><![CDATA[
<p>I was going to write a bit of a 2024 recap, but quite frankly, don&rsquo;t have the energy to do so. It&rsquo;s been a tricky <del>Christmas break</del> year full of illness and stress and finding the headspace and time to pen anything feels impossible right now.</p>
<p>There have been some tremendous highs in amongst the lows, both personally, familially, and in my career; two wonderful holidays with Lauren &amp; Jacob, delivering a <a href="/blog/css-cafe">meetup talk</a> at long last, launching some exciting <a href="https://utopia.fyi">Utopia tools</a>, watching Jacob take his first steps and say his first words (then pretty much not stopping talking!), being nominated and winning a Motorway award, organising a carol concert, and working with some incredibly smart folk at Motorway.</p>
<p>I&rsquo;m so grateful for my friends, colleagues, and most importantly, family who&rsquo;ve helped support us through the hospital trips, the sleepless nights, and the exhaustion. 2025 will be a year for saying &lsquo;no&rsquo; more, and prioritising our family&rsquo;s health and wellbeing over everything else. That&rsquo;s the plan, anyway.</p>
<p>So long 2024 - it&rsquo;s been a ride.</p>
<p><img src="/images/blog/2024.jpg" alt="A selfie of my, my wife, and my son - we&rsquo;re standing on the beach smiling at the camera"></p>
]]>
      </description>
    </item>
    
    <item>
      <title>12 days of illness</title>
      <link>https://www.trysmudford.com/blog/12-days-of-illness/</link>
      <pubDate>Thu, 12 Dec 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/12-days-of-illness/</guid>
      <description><![CDATA[
<p>December feels like an advent calendar of illnesses right now, so we took it a bit further and documented the whole year of questionable health in the family. You might know the tune&hellip;</p>
<ol reversed>
<li>Dr's phonecalls</li>
<li>New teeth</li>
<li>A&E visits (strike that, it's 11 now)</li>
<li>Bottles of Calpol</li>
<li>Times sent home from nursery</li>
<li>Extreme fevers</li>
<li>Hand foot and mouth spots</li>
<li>111 calls</li>
<li>Months of rashes</li>
<li>Sickness bugs</li>
<li>Positive COVID tests<br/>
And...</li>
<li>Conjunctivitis infection</li>
</ol>]]>
      </description>
    </item>
    
    <item>
      <title>Type scale graphs</title>
      <link>https://www.trysmudford.com/blog/type-scale-graphs/</link>
      <pubDate>Tue, 26 Nov 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/type-scale-graphs/</guid>
      <description><![CDATA[
<a href="https://utopia.fyi/blog/graph/" class="button">Read the article</a>

]]>
      </description>
    </item>
    
    <item>
      <title>FFConf 2024</title>
      <link>https://www.trysmudford.com/blog/ffconf-2024/</link>
      <pubDate>Wed, 13 Nov 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/ffconf-2024/</guid>
      <description><![CDATA[
<p>I don&rsquo;t remember a single line of code from <a href="https://2024.ffconf.org/">FFConf 2024</a>.</p>
<p>Perhaps <a href="https://www.skeptic.org.uk/author/mike-hall/">Mike Hall</a> included a short nod when talking about micro-optimisations in <code>for</code> loops, but that was about it. This conference was and is about so much more than code. The talks focused on themes like:</p>
<ul>
<li>Finding purpose in our work</li>
<li>Building and scaling inclusive communities</li>
<li>Critically thinking about tech and AI</li>
<li>Handling death online more respectfully</li>
<li>Self-reflecting and communicating more effectively</li>
</ul>
<p>It was the annual community gathering for like-minded, web-loving people. It was a space to reflect on our industry and ourselves. It was an opportunity to catch up with online friends and make new ones. It was a <a href="https://adactio.com/journal/21549">balm for the soul</a>.</p>
<p>I don&rsquo;t remember a single line of code from FFConf—and that&rsquo;s a feature, not a bug.</p>
<p>Here are a few snaps:</p>
<p><img src="/images/blog/ffconf-2024-2.jpg" alt="Looking out from the stage back towards the packed out crowd, with their hands raised and all smiling">
<img src="/images/blog/ffconf-2024-4.jpg" alt="A black and white photograph of Mike Hall speaking to the crowd, dressed in a sharp suit with a pocketwatch visible">
<img src="/images/blog/ffconf-2024-10.jpg" alt="A wide angled photograph from the back of the cinema in the raised seating area, looking towards the stage, where Olu Niyi-Awosusi is opening the conference">
<img src="/images/blog/ffconf-2024-1.jpg" alt="Crowds and a rogue pigeon gather outside the Duke of Yorks cinema ahead of FFConf 2024 beginning">
<img src="/images/blog/ffconf-2024-8.jpg" alt="Andy Bell presenting from the stage with a slide behind him with the Piccalilli logo">
<img src="/images/blog/ffconf-2024-3.jpg" alt="A starburst shot of Amy Hupe presenting in the afternoon session">
<img src="/images/blog/ffconf-2024-6.jpg" alt="Michael Kibedi standing in front of a screen filled with names, sharing those who influenced him and this talk.=">
<img src="/images/blog/ffconf-2024-7.jpg" alt="Imran Afzal presenting the final talk of the day on reflection, standing in front of a large FFConf banner">
<img src="/images/blog/ffconf-2024-5.jpg" alt="The conference organisers, and most of the 2024 speakers standing and waving on stage."></p>
<a href="https://flickr.com/photos/remysharp/albums/72177720321895884/" class="button">The full album on Flickr</a>

]]>
      </description>
    </item>
    
    <item>
      <title>In praise of hype docs</title>
      <link>https://www.trysmudford.com/blog/in-praise-of-hype-docs/</link>
      <pubDate>Thu, 24 Oct 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/in-praise-of-hype-docs/</guid>
      <description><![CDATA[
<p>Hype docs are something I&rsquo;ve recommended others keep but in all honesty, have struggled to complete in my career thus far. I&rsquo;ve always worked in small teams where there&rsquo;s high visibility of achievements and challenges. But when I joined Motorway last year, I quickly realised that if I didn&rsquo;t toot my own trumpet (or at least record the notes I played), they&rsquo;d quickly get lost in the noise of a 500+ person organisation.</p>
<p>I kept a few scribbles last year but struggled to remember the impact I&rsquo;d made when it came to writing my end of year self reflection. This year, however, I&rsquo;ve been really intentional about keeping an accurate hype doc, full of facts, figures, notes and quotes.</p>
<p><img src="/images/blog/hype.jpg" alt="A blurry screenshot of my hype doc/table in Notion, with &lsquo;what, date, tags, value&rsquo; columns"></p>
<p>The medium isn&rsquo;t important. I chose Notion because we use it at work, but any doc/sheet would do. I had columns for &ldquo;what&rdquo;, &ldquo;date&rdquo;, &ldquo;tags&rdquo; and &ldquo;values&rdquo;. Our end of year review always asks how our achievements uphold the &ldquo;values&rdquo; of the company, so I wanted to make sure I had a clear record of that for each line item. The tricky bit is then filling it in.</p>
<p>The easiest route, I found, was the make it part of my &lsquo;launch&rsquo; checklist. Merge the PR, release the application, test the change, inform the team, update the hype doc. Then, a few weeks after the feature has launched, take a few mins to capture some performance stats, and any feedback colleagues/customers have shared in relation to it. It&rsquo;ll paint a clearer picture come the end of the year, and make it clear that you&rsquo;re data-driven (companies ruddy love that).</p>
<p>&ldquo;Hype&rdquo; doesn&rsquo;t always have to be positive. When things went wrong, I also noted it down, showing how I&rsquo;d learned from my mistakes.</p>
<p>Come the end of the year, this document has been a <em>godsend</em>, and has made the end of year review a breeze to compile. It&rsquo;s full of quotes and stats, which have been particularly useful in a year where I&rsquo;ve not had an active manager aware of my impact. It&rsquo;s also been really uplifting to see just how much my team achieved this year.</p>
<p>Sometimes you&rsquo;ve gotta be your own cheerleader. 📣</p>
]]>
      </description>
    </item>
    
    <item>
      <title>Use Google Docs in dark mode</title>
      <link>https://www.trysmudford.com/blog/google-docs-dark-mode/</link>
      <pubDate>Thu, 17 Oct 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/google-docs-dark-mode/</guid>
      <description><![CDATA[
<p>It&rsquo;s end of year review time, so instead of actually writing, I spent a few minutes configuring Google Docs to work in a pseudo-dark mode.</p>
<ol>
<li>Open a new <a href="https://docs.new">Google Doc</a></li>
<li>Click on &ldquo;File → Page setup&rdquo;</li>
<li>Choose the &ldquo;Pageless&rdquo; option, and set the background colour to black/dark grey, the &ldquo;OK&rdquo;
<img src="/images/blog/google-docs-dark-mode-2.png" alt="A screenshot of the Google Docs page setup modal, with pages/pageless mode"></li>
<li>Change the text colour to white (you might want to set this as a theme if you&rsquo;re going all-in on this)</li>
<li>Make your window &ldquo;fullscreen&rdquo; (the green button on the top left of a window on Mac)</li>
<li>Click the little &ldquo;^&rdquo; icon on the right of the task bar (by the &ldquo;Editing&rdquo; select)
<img src="/images/blog/google-docs-dark-mode-4.png" alt="A screenshot showing the &lsquo;Hide the menus&rsquo; button"></li>
<li>Pinch zoom in a hair, and&hellip;
<img src="/images/blog/google-docs-dark-mode-5.png" alt="Google Docs running in dark mode"></li>
</ol>
<p>voila. Now to write this review&hellip;</p>
]]>
      </description>
    </item>
    
    <item>
      <title>Hyper-responsive web components</title>
      <link>https://www.trysmudford.com/blog/hyper-responsive-web-components/</link>
      <pubDate>Wed, 09 Oct 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/hyper-responsive-web-components/</guid>
      <description><![CDATA[
<p>Some time ago, I was assigned the task to build a new component: an embeddable call to action to sign up for email alerts. Unbranded, it looked roughly like this:</p>
<p><img src="/images/blog/hyper-alt-1.jpg" alt="A rough wireframe of an email signup form rendered on a wide monitor - on the left is a heading, paragraph, email address field, and a sign up button, and on the right is a presentational illustration."></p>
<p>This component needed to be incredibly portable, looking great on <em>any third-party website</em>, in <em>any position</em>, at <em>any viewport</em>, with <em>any amount of content</em>. It had to be a “hyper-responsive” component.</p>
<p>Three immediate approaches came to mind:</p>
<ol>
<li>A script that inserted HTML into the page</li>
<li>An iframe pointing to a website rendering this component</li>
<li>A web component</li>
</ol>
<p>The script option had the primary drawback of CSS leakage. Not only would page styles easily leak into the component, but styles inserted from the component could also pollute the page.</p>
<p>Iframes solved the style encapsulation challenge; an iframe acts as if it’s a window within a window, but they can’t dynamically resize based on internal content changes. Furthermore, if a <code>&lt;form&gt;</code> <code>POST</code>s from within an iframe, the iframe itself navigates, not the wrapping page. this was a no-go.</p>
<p>Web components have been gradually gathering momentum and browser support over the years. Although my preference would usually be the flavour of a <a href="https://adactio.com/journal/20618">HTML web component</a>, this feature called for the use of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM">Shadow DOM</a>. The Shadow DOM handles the style encapsulation we’re after, preventing our styles from leaking out, and (most) page styles from leaking in. Web components can be placed anywhere on the page, like any other HTML element, which makes them incredibly portable and practical. Problem solved.</p>
<h2 id="writing-an-encapsulated-web-component">Writing an encapsulated web component</h2>
<p>You can begin building an encapsulated Web Component in just a few lines of Javascript; with no build system required:</p>
<div class="highlight"><pre class="chroma"><code class="language-jsx" data-lang="jsx"><span class="kr">class</span> <span class="nx">MyWebComponent</span> <span class="kr">extends</span> <span class="nx">HTMLElement</span> <span class="p">{</span>
    <span class="nx">renderCSS</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="sb">`&lt;style&gt;
</span><span class="sb">        /* Component CSS */
</span><span class="sb">    &lt;/style&gt;`</span><span class="p">;</span>

    <span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
        <span class="c1">// Opt into the Shadow DOM
</span><span class="c1"></span>        <span class="k">this</span><span class="p">.</span><span class="nx">attachShadow</span><span class="p">({</span> <span class="nx">mode</span><span class="o">:</span> <span class="s1">&#39;open&#39;</span> <span class="p">});</span>

        <span class="c1">// Create a wrapper for the component &amp; add to the Shadow DOM
</span><span class="c1"></span>        <span class="kr">const</span> <span class="nx">wrapper</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s1">&#39;div&#39;</span><span class="p">);</span>
        <span class="nx">wrapper</span><span class="p">.</span><span class="nx">setAttribute</span><span class="p">(</span><span class="s1">&#39;class&#39;</span><span class="p">,</span> <span class="s1">&#39;wrapper&#39;</span><span class="p">);</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">shadowRoot</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">wrapper</span><span class="p">);</span>

        <span class="c1">// Render the component&#39;s HTML
</span><span class="c1"></span>        <span class="nx">wrapper</span><span class="p">.</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="sb">`
</span><span class="sb">            </span><span class="si">${</span><span class="k">this</span><span class="p">.</span><span class="nx">renderCSS</span><span class="p">()</span><span class="si">}</span><span class="sb">
</span><span class="sb">            &lt;!-- Component HTML --&gt;
</span><span class="sb">        `</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="nx">customElements</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">&#39;my-web-component&#39;</span><span class="p">,</span> <span class="nx">MyWebComponent</span><span class="p">);</span>
</code></pre></div><p>This can then be inserted into a page by including the script, then adding the HTML <code>&lt;my-web-component&gt;&lt;/my-web-component&gt;</code> wherever you’d like it to render. Because HTML is intrinsically fault-tolerant, any HTML placed between the component tag is automatically rendered if the web component script fails to load.</p>
<p>All the CSS in this web component is encapsulated, meaning nothing from the page will seep <em>into</em> the component, and nothing will leak <em>out</em> from it. Any CSS added to that style tag will only be applied to the elements within the component. By no means is this the perfect way to write CSS, and this could definitely be improved with a build step, but for this use-case, it works pretty well.</p>
<h2 id="responsive-typography--space">Responsive typography &amp; space</h2>
<p>I’m a <a href="https://www.trysmudford.com/categories/web/">huge fan</a> of fluid typography and space, and have co-founded an open source project in this <em>ahem</em> space, called <a href="https://utopia.fyi/">Utopia</a>. We provide a set of tools to help designers and engineers get started with fluid responsive design.</p>
<p>Taking the small and large screen designs for this component, I created a set of <code>clamp()</code> functions to interpolate the typography and space fluidly:</p>
<div class="highlight"><pre class="chroma"><code class="language-css" data-lang="css"><span class="p">.</span><span class="nc">wrapper</span> <span class="p">{</span>
    <span class="nv">--body-size</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mi">1</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">0.9565</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.2174</span><span class="n">vi</span><span class="p">,</span> <span class="mf">1.125</span><span class="kt">rem</span><span class="p">);</span>
    <span class="nv">--heading-size</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mf">1.5</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">1.3261</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.8696</span><span class="n">vi</span><span class="p">,</span> <span class="mi">2</span><span class="kt">rem</span><span class="p">);</span>
    <span class="nv">--item-spacing</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mf">0.5</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">0.3261</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.8696</span><span class="n">vi</span><span class="p">,</span> <span class="mi">1</span><span class="kt">rem</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><p>Applying these to our component is pretty straightforward thanks to the wonder of custom properties:</p>
<div class="highlight"><pre class="chroma"><code class="language-css" data-lang="css"><span class="p">.</span><span class="nc">content</span> <span class="p">{</span>
    <span class="k">display</span><span class="p">:</span> <span class="kc">flex</span><span class="p">;</span>
    <span class="k">flex-direction</span><span class="p">:</span> <span class="kc">column</span><span class="p">;</span>
    <span class="n">gap</span><span class="p">:</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="n">item</span><span class="o">-</span><span class="n">spacing</span><span class="p">);</span>
    <span class="k">font-size</span><span class="p">:</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="n">body</span><span class="o">-</span><span class="n">size</span><span class="p">);</span>
<span class="p">}</span>

<span class="nt">h2</span> <span class="p">{</span>
    <span class="k">font-size</span><span class="p">:</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="n">heading</span><span class="o">-</span><span class="n">size</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><p>The component looks pretty good on a large and small screen (although the illustration is a bit large and in charge):</p>
<p><img src="/images/blog/hyper-alt-2.jpg" alt="A side-by-side of the component on a large screen (as above), and small screen where the left and right columns are stacked."></p>
<p>But remember, this component needed to be portable, and able to be placed <em>anywhere</em> on a page. So what happens if we render the component in a sidebar and view it on a large screen…</p>
<p><img src="/images/blog/hyper-alt-3.jpg" alt="A broken layout where large text is rendering in a small container."></p>
<p>Yeah… Not ideal. This situation comes about because the fluid typography uses the <code>vi</code> unit. The <code>vi</code>/<code>vw</code> unit is calculated as a proportion of the viewport, not the container. So when we render the component on a large screen, but in a small area, the typography assumes there’s space to play with and explodes out of the component.</p>
<p>Traditionally, we’d create variants at this point, or use Javascript to handle component sizing, but we have another option available to us: <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_containment/Container_queries">CSS container queries</a>!</p>
<p>They’re finally here and stable! Container queries brought along a new length unit: <code>CQI</code>. This unit is calculated as a proportion of the nearest named container; the element opted in with <code>container-type: inline-size</code>. In the same way that <code>1vi</code> === 1% of the viewport, <code>1cqi</code> === 1% of the container.</p>
<p>Using the <code>CQI</code> unit keeps our typography in tune with the component, rather than the whole page. Rendering the component in a column or sidebar therefore tells the typography to shrink down to a size that is appropriate for that space, even if the viewport is much wider.</p>
<p>This is incredibly powerful, but isn’t appropriate for <em>every</em> component. Typography is strongest when it’s in harmony across a page. This provides clear hierarchy for users to help them distinguish what’s important and linked on a page. If everything becomes in tune with itself, nothing is in tune. For this encapsulated component, however, it’s the perfect solution.</p>
<h3 id="applying-container-driven-typography">Applying container-driven typography</h3>
<p>It might be tempting to hot-swap out our custom properties, but it’s always a good idea to build in graceful degradation, particularly when using shiny new CSS features. CSS has the <code>@supports</code> query for this very reason. It’s a way to feature detect and layer on progressive enhancement for browsers that support the feature.</p>
<p>Our wrapper stays as it was, with the inclusion of <code>container-type: inline-size</code>, then we add a <code>@support</code> query to see if the current browser can handle the <code>CQI</code> unit:</p>
<div class="highlight"><pre class="chroma"><code class="language-css" data-lang="css"><span class="p">.</span><span class="nc">wrapper</span> <span class="p">{</span>
    <span class="n">container-type</span><span class="p">:</span> <span class="kc">inline</span><span class="o">-</span><span class="k">size</span><span class="p">;</span>

    <span class="nv">--body-size</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mi">1</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">0.9565</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.2174</span><span class="n">vi</span><span class="p">,</span> <span class="mf">1.125</span><span class="kt">rem</span><span class="p">);</span>
    <span class="nv">--heading-size</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mf">1.5</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">1.3261</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.8696</span><span class="n">vi</span><span class="p">,</span> <span class="mi">2</span><span class="kt">rem</span><span class="p">);</span>
    <span class="nv">--item-spacing</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mf">0.5</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">0.3261</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.8696</span><span class="n">vi</span><span class="p">,</span> <span class="mi">1</span><span class="kt">rem</span><span class="p">);</span>
<span class="p">}</span>

<span class="p">@</span><span class="k">support</span> <span class="o">(</span><span class="nt">font-size</span><span class="o">:</span> <span class="nt">1cqi</span><span class="o">)</span> <span class="p">{</span>
    <span class="p">.</span><span class="nc">wrapper</span> <span class="p">{</span>
        <span class="nv">--body-size</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mi">1</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">0.9565</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.2174</span><span class="n">cqi</span><span class="p">,</span> <span class="mf">1.125</span><span class="kt">rem</span><span class="p">);</span>
        <span class="nv">--heading-size</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mf">1.5</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">1.3261</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.8696</span><span class="n">cqi</span><span class="p">,</span> <span class="mi">2</span><span class="kt">rem</span><span class="p">);</span>
        <span class="nv">--item-spacing</span><span class="p">:</span> <span class="nf">clamp</span><span class="p">(</span><span class="mf">0.5</span><span class="kt">rem</span><span class="p">,</span> <span class="mf">0.3261</span><span class="kt">rem</span> <span class="o">+</span> <span class="mf">0.8696</span><span class="n">cqi</span><span class="p">,</span> <span class="mi">1</span><span class="kt">rem</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><p>Better!</p>
<p><img src="/images/blog/hyper-alt-4.jpg" alt="A &lsquo;fixed&rsquo; component where the typography has scaled to fit the space available."></p>
<p><em><strong>Note:</strong> when I said earlier that the web component is totally encapsulated from the page, that wasn’t entirely true. Because a web component does not have a <code>:root</code> element, it calculates any internal <code>REM</code> values from the page <code>:root</code> size. This isn’t normally a problem, but if the page you place the component on has altered the <code>html/:rootfont-size</code>, the component will also be scaled accordingly, for better or worse.</em></p>
<h2 id="intrinsic-layouts">Intrinsic layouts</h2>
<p><a href="https://every-layout.dev/">Every Layout</a> is a fantastic resource that should be in a design engineer’s arsenal. Much like <a href="https://utopia.fyi/">Utopia</a>, Every Layout advocates avoiding imperative media queries, and letting the content and computer decide how best to render a component. <a href="https://every-layout.dev/layouts/sidebar/">‘The Sidebar’</a> is their take on the classic ‘two-column’ problem, where a secondary element gets placed below the primary content when space gets tight. But rather than anchoring to a set viewport width, we can use some clever <code>flex</code> magic to perform the change when the content area itself drops below a certain width.</p>
<p>This intrinsic approach is perfect for not only the overall component layout, but also for the input/button combination:</p>
<p><img src="/images/blog/hyper-alt-6.jpg" alt="Two images of the form, one with the email field and button aside one another, and the other with them stacked."></p>
<p>In this case, the ‘sidebar’ is the button, and the input should expand to take up as much space as is available. When the input gets thinner than our defined minimum width, the elements stack and expand to fill the space.</p>
<div class="highlight"><pre class="chroma"><code class="language-css" data-lang="css"><span class="p">.</span><span class="nc">form</span> <span class="p">{</span>
  <span class="k">display</span><span class="p">:</span> <span class="kc">flex</span><span class="p">;</span>
  <span class="k">flex-wrap</span><span class="p">:</span> <span class="kc">wrap</span><span class="p">;</span>
  <span class="n">gap</span><span class="p">:</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="kc">space</span><span class="mi">-2</span><span class="n">xs</span><span class="p">)</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="kc">space</span><span class="o">-</span><span class="n">xs</span><span class="p">);</span>
<span class="p">}</span>

<span class="p">.</span><span class="nc">form</span> <span class="o">&gt;</span> <span class="nt">button</span> <span class="p">{</span>
  <span class="k">flex-basis</span><span class="p">:</span> <span class="mi">11</span><span class="kt">rem</span><span class="p">;</span> 
  <span class="k">flex-grow</span><span class="p">:</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>

<span class="p">.</span><span class="nc">form</span> <span class="o">&gt;</span> <span class="nt">input</span> <span class="p">{</span>
  <span class="k">flex-basis</span><span class="p">:</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">flex-grow</span><span class="p">:</span> <span class="mi">999</span><span class="p">;</span>
  <span class="n">min-inline-size</span><span class="p">:</span> <span class="mi">60</span><span class="kt">%</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h3 id="limitations-of-intrinsic-design">Limitations of intrinsic design</h3>
<p>One limitation of ‘The Sidebar’ approach is the ability to hide content when the element reaches a certain size. We may wish to hide illustrative or presentational elements on smaller screens and only show them when there is the real-estate available.</p>
<p>To handle this, we need to use container queries themselves. Once again, we begin with a usable baseline and layer on fidelity for browsers that support it. ‘The Sidebar’ uses <code>display: flex</code>, a feature that’s very well supported. And although it’s not ideal to show the illustration on smaller devices, it doesn’t look broken, and the feature still works, so this is an acceptable trade-off.</p>
<p>Using a <code>@supports (container-type: inline-size)</code> query, we can convert the wrapper from <code>flex</code> to <code>grid</code>, effectively nullifying ‘The Sidebar’ for browsers that support container queries. Then we can write a targeted container query to hide the illustration column when the component is smaller than our desired size:</p>
<div class="highlight"><pre class="chroma"><code class="language-css" data-lang="css"><span class="p">.</span><span class="nc">layout</span> <span class="p">{</span>
  <span class="k">display</span><span class="p">:</span> <span class="kc">flex</span><span class="p">;</span>
  <span class="k">flex-wrap</span><span class="p">:</span> <span class="kc">wrap</span><span class="p">;</span>
  <span class="n">gap</span><span class="p">:</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="kc">space</span><span class="o">-</span><span class="n">s</span><span class="p">)</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="kc">space</span><span class="o">-</span><span class="n">xl</span><span class="mi">-2</span><span class="n">xl</span><span class="p">);</span>
<span class="p">}</span>

<span class="p">.</span><span class="nc">illustration</span> <span class="p">{</span>
  <span class="k">flex-basis</span><span class="p">:</span> <span class="mi">18</span><span class="kt">rem</span><span class="p">;</span> 
  <span class="k">flex-grow</span><span class="p">:</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>

<span class="p">.</span><span class="nc">primary</span> <span class="p">{</span>
  <span class="k">flex-basis</span><span class="p">:</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">flex-grow</span><span class="p">:</span> <span class="mi">999</span><span class="p">;</span>
  <span class="n">min-inline-size</span><span class="p">:</span> <span class="mi">50</span><span class="kt">%</span><span class="p">;</span>
<span class="p">}</span>

<span class="p">@</span><span class="k">supports</span> <span class="o">(</span><span class="nt">container-type</span><span class="o">:</span> <span class="nt">inline-size</span><span class="o">)</span> <span class="p">{</span>
    <span class="p">.</span><span class="nc">layout</span> <span class="p">{</span>
        <span class="k">display</span><span class="p">:</span> <span class="k">grid</span><span class="p">;</span>
        <span class="k">grid-template-columns</span><span class="p">:</span> <span class="mi">100</span><span class="kt">%</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="p">@</span><span class="k">container</span> <span class="o">(</span><span class="nt">max-width</span><span class="o">:</span> <span class="nt">600px</span><span class="o">)</span> <span class="p">{</span>
        <span class="p">.</span><span class="nc">illustration</span> <span class="p">{</span>
            <span class="k">display</span><span class="p">:</span> <span class="kc">none</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><p><img src="/images/blog/hyper-alt-5.jpg" alt="A fully &lsquo;fixed&rsquo; component where the typography has scaled to fit the space available and illustration is hidden."></p>
<h2 id="the-finer-details">The finer details</h2>
<p>For a couple of finishing touches, we can use the reasonably new <code>text-wrap: balance</code> on headings within the content. When the width of the element forces the heading text onto multiple lines, CSS will aim to ‘balance’ the text across the available space. This leads to fewer <a href="https://fonts.google.com/knowledge/glossary/widows_orphans">orphans</a> hanging on the final line, improving the aesthetic quality and readability of the heading.</p>
<p>Finally, we can also use the <code>ch</code> unit to limit text width to ensure lines remain readable. This unit maps to the width of the “0” glyph in the rendered typeface, and provides a good way for us to keep line lengths under control based on the current font-size.</p>
]]>
      </description>
    </item>
    
    <item>
      <title>BBC Sound Effects</title>
      <link>https://www.trysmudford.com/blog/bbc-sound-effects/</link>
      <pubDate>Wed, 02 Oct 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/bbc-sound-effects/</guid>
      <description><![CDATA[
<p>The BBC have released a veritable trove of free (for research/personal use) <a href="https://sound-effects.bbcrewind.co.uk/">sound effects</a> from their vast archives.</p>
<p>You can search for all manner of noises, from the clunk of <a href="https://sound-effects.bbcrewind.co.uk/search?q=07071160">trains being coupled</a> in a goods yard, to the <a href="https://sound-effects.bbcrewind.co.uk/search?q=07066001">distant sound of crockery</a> being moved in a restaurant back in 1974.</p>
<p>What&rsquo;s particularly exciting, is how they&rsquo;ve chosen to build this on top of the web platform, rather than go native. It effortlessly showcases what we can do with the web today, but it doesn&rsquo;t feel remotely space age or bleeding edge; it&rsquo;s just a really nice website. It runs on all browsers and devices I&rsquo;ve tested on, and it&rsquo;s fast and smooth.</p>
<p>Every sound has a deep link, the option to favourite and download. Waveforms update live as you play each track. And the best part of all? There&rsquo;s a sound mixer where you can combine multiple sounds into a shareable link.</p>
<p>Tell you what, if you&rsquo;re partial to the sounds of a peregrine falcon, combined with a church clock, combined with a baby rattle, <em>have I got a <a href="https://sound-effects.bbcrewind.co.uk/miximport?mixstring=eyJzaW1wbGlmaWVkTWl4ZXJBc3NldHMiOnsiMSI6eyJpZCI6Ik5IVTA1MTA0MjMzIiwibWl4ZXJTZXR0aW5ncyI6eyJpc0xvb3BpbmciOmZhbHNlLCJ2b2x1bWUiOjF9fSwiMiI6eyJpZCI6IjA3MDAyMjY1IiwibWl4ZXJTZXR0aW5ncyI6eyJpc0xvb3BpbmciOmZhbHNlLCJ2b2x1bWUiOjF9fSwiMyI6eyJpZCI6IjA3MDM2MTI1IiwibWl4ZXJTZXR0aW5ncyI6eyJpc0xvb3BpbmciOmZhbHNlLCJ2b2x1bWUiOjF9fX19">mix for you</a></em>.</p>
<p>You can mix each sound and even re-record &amp; download that mix as a WAV file. All in the browser. It would be awesome if you could chop up the tracks or pop in configurable audio effects into the mix too, but that&rsquo;s just me being greedy.</p>
<p>The Web Audio API is often seen as a <a href="https://pedalboard.netlify.app/">side-project API</a>, so it&rsquo;s great to see it being used on a &lsquo;genuine&rsquo; project.</p>
]]>
      </description>
    </item>
    
    <item>
      <title>I wasted a day on CSS selector performance to make a website load 2ms faster</title>
      <link>https://www.trysmudford.com/blog/i-spent-a-day-making-the-website-go-2ms-faster/</link>
      <pubDate>Tue, 01 Oct 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/i-spent-a-day-making-the-website-go-2ms-faster/</guid>
      <description><![CDATA[
<p>I&rsquo;ve been doing some performance tinkering at work. It&rsquo;s written in <a href="https://nextjs.org/">Next.js</a> and employs judicious use of <a href="https://nextjs.org/docs/app/building-your-application/rendering/server-components">Server Components</a> to minimise client-side JS. I&rsquo;m really proud of it. It scores ~90 on Lighthouse mobile, which for Next.js isn&rsquo;t too bad. Sure, it pains me to see the tiny amount of client-side interaction resulting in such a large bundle, but it&rsquo;s the price we pay for developer convenience, apparently. But I digress.</p>
<p>I was showing some traces to a colleague, and we were looking intently at the considerable gap between the CSS being downloaded, and the website becoming visible.</p>
<p><img src="/images/blog/waterfall.png" alt="A trace from WebPageTest showing the browser main thread locking up before rendering"></p>
<p>My hunch was the bundle of <code>&lt;script&gt;</code>s than Next generates were being parsed at the point of CSS rendering, causing the main thread to choke. Incredibly, you <em>still</em> can&rsquo;t swap <code>async</code> to <code>defer</code> when using the the App Router (despite it being possible with their previous router), which would almost certainly close this gap. But I digress.</p>
<p>He found an option in the &ldquo;Performance&rdquo; tab in dev tools called &ldquo;Enable CSS selector stats (slow)&rdquo;, which analyses every CSS selector and pops them in a table.</p>
<p><img src="/images/blog/selectors.png" alt="The Selector Stats table in Chrome Dev Tools, showing the most &lsquo;expensive&rsquo; selectors"></p>
<p>In addition, the trace showed an alarming blob of purple where the main thread locked up under the label of &ldquo;Recalculate style&rdquo;. 270ms of delay just to calculate styles.</p>
<p>Goodness, I thought, that&rsquo;s some tasty low hanging fruit.</p>
<p>Now, I know in my heart that CSS selector performance isn&rsquo;t something to worry about. I&rsquo;ve heard it again and again that browsers are incredibly efficient at handling CSS selectors these days, and yet the graph didn&rsquo;t (appear to) lie; 270ms to recalculate styles on every DOM change and page load. Against my better judgement, on a day where I didn&rsquo;t have a tonne of other work to do, I felt I had to go deeper.</p>
<p>I was particularly drawn to the column &ldquo;Match attempts&rdquo; compared to &ldquo;Match count&rdquo;. Some selectors were attempting to match against <em>hundreds</em> of DOM nodes, but still end up not matching many/any elements. This fruit was looking tastier by the second.</p>
<p>The common culprits were any selector ending in <code>*, :last-child, :first-child, :nth-child</code>, which included the infamous <a href="https://alistapart.com/article/axiomatic-css-and-lobotomized-owls/">owl selector</a>, being used for <a href="https://piccalil.li/blog/my-favourite-3-lines-of-css/">flow spacing</a> amongst other things.</p>
<p>The penny suddenly dropped onto a piece of knowledge I&rsquo;d squirrelled away years ago. Selectors are read from right to left in CSS. This form of <a href="https://en.wikipedia.org/wiki/Bottom-up_parsing">bottom-up parsing</a> is more efficient for the browser when matching to DOM nodes. However, you can begin to see why a selector like <code>.parent &gt; * + *</code> is flagged as one of the more inefficient ones. Read backwards, the browser sees the selector as follows:</p>
<ol>
<li><code>*</code> - this matches <em>all</em> elements</li>
<li><code>* + *</code> - this narrows it to all elements that follow another (so almost all elements)</li>
<li><code>.parent &gt; * + *</code> - finally, this narrows it down to any direct children of <code>.parent</code> that follow another child</li>
</ol>
<p>So this selector <em>attempts</em> to match on <em>every</em> DOM node, but might only actually match five elements on the page.</p>
<p>Now, at this point, I should&rsquo;ve seen the column marked &lsquo;Elapsed (ms)&rsquo; with values such as <code>0.013</code> and thought, hmm, that doesn&rsquo;t seem very long, nor could all the selectors <em>really</em> add up to 270ms. But by now, the low hanging fruit looked far too tasty, and I had a hankering for some refactoring.</p>
<p>I took the table of selectors and calculated which selectors had the greatest difference between match attempts and elements found, and began working through those. A few were fed through our design system, but many came from my codebase.</p>
<p>Through much inversion of control and additional classes/data attributes, I updated almost all instances of <code>:last-child</code> and <code>*</code>. I made a release to our design system fixing the oh-so-inefficient selectors and marvelled at the improvement.</p>
<blockquote>
<p>Recalculate style /
Before: 270ms /
After: 40ms</p>
</blockquote>
<p>For a few minutes, I felt like Indiana Jones staring at the holy grail. 230ms of savings from some CSS selector changes. This felt <em>significant</em>. And yet, the niggling phrase of &ldquo;CSS selector efficiency is not something to worry about in 2024&rdquo; kept ringing through my head.</p>
<p>I ran the before/after through <a href="https://webpagetest.org/">WebPageTest</a> and to my surprise (and lack of surprise), nothing significant had changed.</p>
<p>It was at this point that the second penny dropped and I realised I&rsquo;d been duped by a graphing misunderstanding and my own enthusiasm for improving frontend performance. When clicking &ldquo;Enable CSS selector stats (slow)&rdquo;, I assumed it took longer to record the rendering process, but would provide the results as if they were not being profiled. I then realised that the graph was showing a big purple blob <em>because</em> I had the checkbox enabled. Suffice to say, I felt like a right doughnut.</p>
<p>I ran it all again without that option enabled and here are the fruits of my labour:</p>
<blockquote>
<p>Recalculate style /
Before: 11.95ms /
After: 10.23ms</p>
</blockquote>
<p>Whoop-de-doo</p>
<p>But if, somehow, you&rsquo;re browsing this site <em>with</em> CSS selector stats enabled, and find it to be a snappy experience, you&rsquo;re welcome.</p>
]]>
      </description>
    </item>
    
    <item>
      <title>All code is fleeting</title>
      <link>https://www.trysmudford.com/blog/all-code-is-fleeting/</link>
      <pubDate>Fri, 27 Sep 2024 00:00:00 +0000</pubDate>
      
      <guid>https://www.trysmudford.com/blog/all-code-is-fleeting/</guid>
      <description><![CDATA[
<p><a href="https://www.w3.org/Provider/Style/URI">Cool URI&rsquo;s don&rsquo;t change</a> but codebases and teams do. It can be painful to say goodbye to the code you&rsquo;ve written when it&rsquo;s rewritten or removed without warning. Conversely, being asked to refactor or rebuild a dated function/page/system can feel positively heroic. It all comes down to how personally attached you are to the thing being replaced, and how much it frustrates you.</p>
<p>Having been in a startup situation where literally <em>everything</em> was deleted, you begin to understand that it ain&rsquo;t all about the code we write. The &ldquo;outputs&rdquo;; all the code, the designs, the processes, the tests—they&rsquo;re ultimately fleeting. It might be our choice, it might not, but it&rsquo;ll all be replaced or removed in time.</p>
<p>But the relationships you make, the impact you have in colleagues' &amp; customers' lives, and the growth that occurs in your professional journey outlasts any fork in the road.</p>
<p>As Mandy Brown beautifully writes, <a href="https://everythingchanges.us/blog/after-the-rupture/">big changes only make sense in hindsight</a>. The trick is try to appreciate the lasting &amp; important stuff in the moment. Throw yourself deep into a problem for your growth, to support your colleagues, or to end the day feeling proud you did the best job you could for your customers. Be wary when you begin to chase code perfection over these things; you&rsquo;re only making the inevitable break-up harder.</p>
<blockquote>
<p><a href="https://www.youtube.com/watch?v=d2WhP3BTP6U">Healthy things grow and growing things change</a></p>
</blockquote>
]]>
      </description>
    </item>
    
  </channel>
</rss>