title: New HTML Control Lands in Safari
url: https://alvaromontoro.com/blog/68049/new-toggle-switch-lands-in-safari
hash_url: b351cda8d2ccd73362ca1729845b7e79
archive_date: 2024-01-28
og_image: https://alvaromontoro.com/images/blog/safari-toggle-0.webp
description: Toggle switches are a common request by Designers and Developers. Safari proposed a native way to create switches in HTML without the hassle of dealing with CSS or unnecessary control states.
favicon: https://alvaromontoro.com/fav.ico
language: en_US

<p>Native toggle switches landed in one browser. Safari introduced this new HTML control as part of the <a href="https://webkit.org/blog/14885/release-notes-for-safari-technology-preview-185/">Safari Technology Preview 185 release</a> and then <a href="https://developer.apple.com/documentation/safari-technology-preview-release-notes/stp-release-186#New-Features">expanded it in the 186 release</a>.</p>

<p>Creating a semi-native HTML toggle switch component is quite <em>hacky</em>. It involves using a checkbox, adding the "switch" ARIA role, removing its appearance with CSS, and adding styles to make it look like the classic switch or however we want —<a href="https://codepen.io/collection/aMPYMo">check this toggles collection</a>.</p>

<p>And even doing all that, some things cannot be avoided. For example, developers can still set the indeterminate attribute into the base checkbox using JavaScript, which would make the toggle switch invalid.</p>

<p>A native solution would prevent these problems and provide a better experience. Even when it's only an experiment on one browser and has some issues (as we'll describe soon), this attempt is welcomed, and I hope it gets traction.</p>

<h2 id="how-does-it-work">
  How does it work?
</h2>

<p>Safari's implementation is as straightforward as it can be. Just add the <code>switch</code> attribute to a regular checkbox, and you'll be done!</p>

<div class="highlight js-code-highlight">
<pre class="highlight html"><code><span class="c">&lt;!-- this will render a checkbox --&gt;</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="nt">/&gt;</span>

<span class="c">&lt;!-- this will render a toggle switch --&gt;</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">switch</span> <span class="nt">/&gt;</span>
</code></pre>
</div>

<p>It's really that simple. There is no need for additional CSS, JS, or anything complicated, and the browser will take care of the rest, including out-of-the-box interactions and styles.</p>

<figure>
  <p>
    <img src="https://alvaromontoro.com/images/blog/safari-toggle-1.webp" alt="Just add the switch attribute to a checkbox. There is some HTML code in a box: With arrows pointing to checkboxes (from the first line of code) and toggle switches (from the second line of code)" loading="lazy">
  </p>
  <figcaption>Just add the switch attribute to a checkbox!</figcaption>
</figure>

<p>The default visualization resembles the toggle switch of iOS, but as part of the feature, Safari includes two pseudo-elements <code>::track</code> and <code>::thumb</code> that make it easy to update the look and feel of the control. <a href="https://twitter.com/yisibl/status/1737739579418141116">@yisibl added a few examples of how they work</a> (you'll need Safari Technology Preview 185 or above to see them).</p>

<h3 id="thoughts-on-this-syntax">
  Thoughts on this syntax
</h3>

<p>Having a <code>switch</code> attribute in the checkbox makes it easy, but it also may be misleading as it looks like it equates toggle switches to checkboxes. While this may seem logical to many, it is unfair to the switch control, which becomes a "sub-control" <em>tightly coupled</em> to the checkbox.</p>

<p>They are not the same component: they have different values (checked/unchecked vs. on/off), one of them has an indeterminate state (checkbox, although it's not technically a state), and one is supposed to have an action associated on change (switch).</p>

<p>So why couple them? Just because they look the same or behave in a "similar way"? That's wrong. Following that logic, we shouldn't have inputs of the type <code>number</code>, <code>date</code>, <code>password</code>, <code>email</code>, <code>search</code>... because we already have the text input to handle all of them.<br>
</p>

<div class="highlight js-code-highlight">
<pre class="highlight html"><code><span class="c">&lt;!-- We don't do this --&gt;</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">number</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">date</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">password</span> <span class="nt">/&gt;</span>

<span class="c">&lt;!-- So why do this? --&gt;</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">switch</span> <span class="nt">/&gt;</span>

<span class="c">&lt;!-- Wouldn't this make more sense? --&gt;</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"switch"</span> <span class="nt">/&gt;</span>
</code></pre>
</div>

<p>In my opinion, a toggle switch should be its own type of input. And not a "sub-type" or "sub-control" dependent on the checkbox.</p>

<h2 id="pros-and-cons">
  Pros and cons
</h2>

<p>As with every technology —especially a new one—, Safari's toggle switch has good and bad things. Here are some pros and cons of this component.</p>

<h3 id="pros">
  Pros
</h3>

<ul>
<li>
<strong>Provides native support for the control</strong>: no need for hacky or complex solutions that require too much code and may be error-prone.</li>
<li>
<strong>Straightforward interface</strong>: easy to implement (add an attribute!) and easy-to-follow new pseudo-elements (<code>::track</code> and <code>::thumb</code>).</li>
<li>
<strong>Easy styling in CSS</strong>: with the introduction of two pseudo-elements (<code>::track</code> and <code>::thumb</code>), changing the styles of the components would become simple.</li>
<li>
<strong>Accessibility out of the box</strong>: the control is announced correctly by the Voice Over screen reader without needing extra ARIA labels or roles. <a href="https://adrianroselli.com/2021/10/switch-role-support.html#Update02">Adrian Roselli ran tests and shared the results</a> over time.</li>
</ul>

<p>
  <img src="https://alvaromontoro.com/images/blog/safari-toggle-2.webp" alt="Summary of pros in image format" loading="lazy">
</p>

<h3 id="cons">
  Cons
</h3>

<ul>
<li>
<strong>Non-standard feature</strong>: this is a Safari proposal and hasn't been adopted by other browsers or taken into the standard.</li>
<li>
<strong>Safari-only feature</strong>: as it is a new suggestion by Safari, it will work only on Safari and only in the Technology Preview version of the browser (from version 185).</li>
<li>
<strong>Default styles are <em>sketchy</em></strong>: the toggle looks like the regular iOS toggle, but <a href="https://front-end.social/@alvaromontoro/111763578202895753">it presents some issues when scaling and applying zoom</a>.</li>
</ul>

<p>Considering those cons, it is safe to say that <strong>this is not a production-ready feature</strong>. At least not yet. It could be an excellent case for progressive enhancement approaches (maybe the reason why they made it an attribute instead of a full type: falling back into a checkbox if not supported), but waiting to see how the component moves forward is better. It is still too early.</p>

<p>
  <img src="https://alvaromontoro.com/images/blog/safari-toggle-3.webp" alt="Summary of cons in image format" loading="lazy">
</p>

<h2 id="final-thoughts">
  Final thoughts
</h2>

<p>Toggle switches are a great addition to a browser, and the Safari team did nicely adding it to the Technology Preview version. Still, the implementation feels a bit rushed —maybe Safari is trying to earn brownie points with developers while shaking the "The New IE Browser" label?</p>

<p>One of the things that is more counterintuitive is the used syntax. Having a <code>switch</code> attribute in the checkbox makes it easy, but it also unnecessarily associates both controls and doesn't follow the same notation that other form controls follow. I wouldn't be surprised if this is one thing that changes when —not if— this proposal moves forward. So beware of support and likely changes in the future.</p>

<p>As I said, this is a great addition to the form controls. I hope it triggers a deeper conversation about its addition to the standard. I look forward to what it becomes and the possibility of using it on any browser.</p>