<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jerome Smadja</title>
    <description>The latest articles on Forem by Jerome Smadja (@jeromesm).</description>
    <link>https://forem.com/jeromesm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F155213%2F44ad1bd9-0c48-4c6f-b704-2b86445e17bf.jpeg</url>
      <title>Forem: Jerome Smadja</title>
      <link>https://forem.com/jeromesm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jeromesm"/>
    <language>en</language>
    <item>
      <title>Focus Style for Keyboard Navigation Only</title>
      <dc:creator>Jerome Smadja</dc:creator>
      <pubDate>Thu, 25 Apr 2019 22:14:29 +0000</pubDate>
      <link>https://forem.com/jeromesm/focus-style-for-keyboard-navigation-only-40ln</link>
      <guid>https://forem.com/jeromesm/focus-style-for-keyboard-navigation-only-40ln</guid>
      <description>&lt;p&gt;&lt;em&gt;I had the idea of this post after reading this one &lt;a href="https://dev.to/elizabethschafer/designing-button-focus-states-for-better-usability-gm2"&gt;Designing button focus states for better usability&lt;/a&gt;, if you haven't read it yet, go for it.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessibility vs UI
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Well, I didn't really want to oppose a11y to UI as a fight but couldn't come up with a better name.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Sometimes, you might want to remove the default outline the browser will provide for free. When you do that, you certainly please your designers, but you're making it hard for people who rely on assistive technologies, or keyboard navigation.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;:focus-visible&lt;/code&gt; (&lt;a href="https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo" rel="noopener noreferrer"&gt;Specs in Working Draft&lt;/a&gt;) you can make both sides happy. I still wouldn't recommend to remove the whole outline on &lt;code&gt;:focus&lt;/code&gt; on every element of your app because it can still benefit and help some users.&lt;/p&gt;

&lt;p&gt;Though, there are some cases where the focus ring might not be great in your UI, the ones I can think of would be clickable icons, e.g. arrows ← → for pagination, hamburger icon to open a menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqkg98rzv4dhldz208w8o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqkg98rzv4dhldz208w8o.gif" alt="Example of a click on a hamburger icon to open a menu which also shows the native outline focus ring" width="100" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, in the case of a custom style &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; having a high &lt;code&gt;border-radius&lt;/code&gt;, the &lt;code&gt;outline&lt;/code&gt; won't follow the rounded corners, and will result in this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmh3fed7yu03o085kbs9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmh3fed7yu03o085kbs9w.png" alt="button with native outline when focused" width="440" height="106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When clicking with a mouse or tap with your finger&lt;/strong&gt; the added value of this focus ring is low (&lt;code&gt;:hover&lt;/code&gt; and &lt;code&gt;:active&lt;/code&gt; are probably better candidates), and it even adds useless noise to the UI. On the other hand, when tabbing through a website, users need to know that the focus is on the hamburger icon so that they can open the menu.&lt;/p&gt;

&lt;h2&gt;
  
  
  :focus-visible To The Rescue
&lt;/h2&gt;

&lt;p&gt;The main difference between the two &lt;code&gt;:focus&lt;/code&gt; and &lt;code&gt;:focus-visible&lt;/code&gt;, is that you can use &lt;br&gt;
&lt;code&gt;:focus-visible&lt;/code&gt; for people not navigating with a pointing device (mouse, touch, etc...).&lt;br&gt;
That means that we can target people relying on keyboard navigation.&lt;/p&gt;

&lt;p&gt;As of April 2019, it's implemented in Chrome only &lt;strong&gt;and&lt;/strong&gt; behind a flag. However the good news is that if we still want to use this, we still can by using a polyfill &lt;a href="https://github.com/WICG/focus-visible" rel="noopener noreferrer"&gt;github.com/WICG/focus-visible&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If in your codebase, you can remove the &lt;code&gt;outline : none&lt;/code&gt; that you had, install this polyfill, and update your CSS with this (as a first step)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*
  This will hide the focus indicator if the element receives focus via the mouse,
  but it will still show up on keyboard focus.
*/
.js-focus-visible :focus:not(.focus-visible) {
  outline: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This selector will filter out all the elements focused by a keyboard event, and won't apply in that case, so you can keep the default &lt;code&gt;outline&lt;/code&gt; by default.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A little detail that I really like in this spec is that &lt;code&gt;:focus-visible&lt;/code&gt; also applies in case the user focuses an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; field either with a mouse or a keyboard. Why? The rule is simple, if an interactive element opens the keyboard (on a mobile) it should keep the focus ring. I completely agree with this, even on desktop having an indication on where your caret is, can only be a good thing.&lt;/p&gt;

&lt;p&gt;A workmate showed me this great example on how to get crazy with focus for keyboard navigation, there even are animations based on the direction when you navigate: &lt;a href="https://www.sbb.ch/en/home.html" rel="noopener noreferrer"&gt;sbb.ch/en/home.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's end this era of &lt;code&gt;:focus { outline: none; }&lt;/code&gt; now that we have a reliable solution that works for everyone and can benefit to lots of people.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;em&gt;Ressources&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;A good read on the intent behind &lt;code&gt;focus-visible&lt;/code&gt;:&lt;br&gt;
&lt;a href="https://github.com/WICG/focus-visible/issues/128" rel="noopener noreferrer"&gt;github.com/WICG/focus-visible/issues/128&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Specifications:&lt;br&gt;
&lt;a href="https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo" rel="noopener noreferrer"&gt;drafts.csswg.org/selectors-4/#the-focus-visible-pseudo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>a11y</category>
      <category>javascript</category>
      <category>css</category>
    </item>
  </channel>
</rss>
