Focus Ring! -- A11ycasts #16

Chrome for Developers · Beginner ·📰 AI News & Updates ·9y ago
Skills: UX Research70%

Key Takeaways

Explains the importance of focus indicators for keyboard users

Full Transcript

[Music] hey everybody what's up it's Rob Dodson welcome back to the alast show today we're going to be talking about Focus rings so the focus ring is the indicator the browser uses on an element to indicate if it has focus and to indicate if that element is about to receive keyboard events and the focus ring is really really really important for keyboard users because you can think of it kind of like their Mouse pointer it's their way of knowing which element they're about to click on or interact with using something like the space bar now the problem with the focus ring and where I see some developers kind of running a foul here is the focus ring is kind of displayed inconsistently in the browser and follow me over here on my laptop and I'll show you what I mean by that so I've got a few elements on screen here I've got an input text field I've got a button and I've got another button that I've actually styled to look kind of fancy and I'll zoom in and you'll see that when I click on this input text field I see a focus outline I see a focus ring right there but when I click on this native button I see the active state but I I don't see a focus ring drawn on top of it and so already you're seeing that the browser is kind of trying to be smart here and it's trying to sort of differentiate based on sort of a heris between different elements so when a user clicks on a button or they tap on a button the browser is going to say well this user they kind of know what they just went over and clicked on and they know what they just interacted with so I don't need to draw a focus ring in that case but for something like a like a text field it can be really really helpful to have that Focus indicator especially if you're working on like a long form form and maybe you look away for a bit and you come back and you want to kind of see where your place is in the form right so there's an underlying heris that the browser used to differentiate between mouse and keyboard focus on different elements the problem though starts to happen again follow me over here to my laptop when we have custom controls and custom elements so this button that I've styled this is still a native button element when I click on it now I see a focus indicator and that's based entirely on the fact that I've sort of changed the uh the default browser styling and at this point now the browser says uh I guess I'm just going to draw a focus ring you'll also see this if you create a a custom control you know you create your own drop down or something like that and you give it a tab index so opting something into the tab order now when you click on it it's going to display a focus ring and this throws a lot of Developers for a loop they'll be building a UI widget suddenly this ring starts appearing they're not quite sure what to do because for the mouse users out there it's aesthetically unpleasing right it's it's not something they're used to seeing they might not like the way that it looks and so you might have your art director or somebody coming down and saying hey that thing looks weird just remove it okay and if you go over to stack Overflow or something like that you just Google stack Overflow remove blue ring and there's just an endless amount of answers here where people say oh yeah when you see that thing just use the CSS style outline none and that'll get rid of the focus ring the problem is it gets rid of the focus ring for everybody including your keyboard users and and this is the real issue really what we need is the ability to differentiate between mouse and keyboard Focus for different elements and so we can have kind of two different experiences enter Focus ring so Focus ring is a brand new CSS selector is part of the CSS level four selectors spec I can show that to you here on my laptop and what Focus ring does is it says the focus ring Pudo class applies while an element matches the focus pseudo class and the UA or the user agent basically the browser determines via heuristics that the focus should be specifically indicated on that element typically by a focus ring so basically what this means is focus ring gives you the ability to opt into these same heris STS that the browser uses when they differentiate between mouse and keyboard users for things like inputs and buttons now as of today there are no browsers that ship the focus ring pseudo class it just landed in the spec very recently but there is a polyfill which you can take advantage of in your application so github.com slwic cg/ Focus ring here's a PO F created by my teammate Alice boxhall and our friend Brian Cardell Focus ring polyfill is going to apply a focus ring class two elements that sort of match theistic and so that way you can say ah this element should definitely always draw a focus ring it has this class so you know I can I can put my own style on it or I can I can opt out of that I'm going to show you an example here i' I've already downloaded the poly fill I've got some HTML this is that same example page that I showed you so I've got an input I've got a button just says hello world and then I've got another button with a class on it so it looks like kind of a nice big material design button and uh what I'm going to do is I'm going to start by including the script for Focus ring JS and what that's going to do is it's going to here I'll actually walk you through Focus ring JS so you can kind of see how it works so inside the JavaScript for this class the first thing it does is it tries to set a flag to indicate if a keyboard action has just taken place then it sort of defines a white list of elements and these are Elements which should always display the focus ring so for instance something like input type equals text regardless of if that was clicked on with a mouse or a keyboard we're saying it should always draw a focus ring just like what the native browser does today but you'll notice that button is not in this white list so if uh someone clicks on a button it's not going to display the focus ring class and therefore we can turn off the outline for our Mouse users but still display it for our keyboard users if we go down to the bottom we can see that this element is actually or this uh class is just going through and adding some event listeners so it adds a key down listener and when someone presses a key it sets that keyboard flag and at this point it starts kind of a little uh keyboard throttle timer of about 100 milliseconds so at that point if anything else receives Focus within that window we're going to assume the user is a keyboard user and we're going to apply the focus ring class to that element so over here in my index file I've included Focus ring JS and what we should expect to see is this input should have the focus ring class this button should not because it's not in the white list and this other button should not because because it is also not in the white list over in my CSS file because I can't just outright use you know the colon Focus ring pseudo class just yet what the polyfill is going to make available to me is just a regular Focus ring CSS class and I can use a selector that looks like this so what I'm saying here is for any element that is focused that does not have a focus ring class on it go ahead and remove the outline so what that's saying is anything that a mouse user clicks on essentially we're going to not display the outline or anything that is not part of our white list we're going to not display the outline anything that is in the white list or anything that has been interacted with the keyboard and has a focus ring class we will display the outline okay so let's see how that works we will go back over to that application that we were working with before I will open up this div so you can kind of see our elements here and as I click on the input you'll note that it gets a focus ring class added to it and so it also gets the native Focus ring indicator when I click on these buttons though no Focus ring class and so now this this fancier button that I defined before is no longer displaying a focus ring however when I use my keyboard to navigate the page I hit tab I see a focus ring class on the input I hit tab I see it on the first button I hit Tab and I see it on that last Button as well so now we've been able to differentiate between our mouse and keyboard users we can have the right experience for each modality I also want to point out that there is another Library out there called what input this does a very similar thing to the uh the focus ring polyfill what input has been around for a little bit longer you can find that at 1017 SL what- input on GitHub again does a very similar thing to the uh to the focus ring polyfill it's been out there for a bit longer so maybe you know try both see which one you prefer they have slightly different apis uh but definitely try and give you know one or both of these a shot in your application figure out which one you prefer and and make sure you're not just you know removing Focus outlines throughout your app cuz that's going to create a really gnarly experience for your keyboard users that about covers it for today if you have any questions for me as always you can leave them in the comments below or you can hit me up on a social network of your choosing as always thank you so much for watching and I'll see you next time [Music]

Original Description

Focus-ring on GitHub: https://goo.gl/GZCfmQ Have you ever noticed the little blue or dashed ring that appears around elements when you activate them with your keyboard or mouse? This is known as a focus indicator, and it's extremely important for users who rely primarily on their keyboard to navigate the screen. For a keyboard user, the focus indicator (or ""focus ring"") is kind of like their mouse pointer. It shows them which element is currently active and receiving keyboard events. Because the focus ring can show up on custom controls or whenever we add styles to native elements like the button tag, it's a common anti-pattern to remove the focus indicator using the CSS outline: none style. But doing this degrades the user experience for our keyboard users as they are no longer able to tell which elements are currently focused. Enter :focus-ring! The :focus-ring pseudo class is a new CSS selector designed to help differentiate between mouse and keyboard focus. Today on a11ycasts we'll look at how :focus-ring works, and show off a few :focus-ring polyfills that you can start using today to improve the accessible styles on your site! :focus-ring in CSS Selectors Level 4 spec: https://goo.gl/ljrqNg what-input on GitHub: https://goo.gl/BT49JG Watch all A11ycasts episodes: https://goo.gl/06qEUW Subscribe to the Chrome Developers YouTube channel for updates on new episodes of A11ycasts: http://goo.gl/LLLNvf
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Chrome for Developers · Chrome for Developers · 0 of 60

← Previous Next →
1 Polymer Performance Patterns (The Polymer Summit 2015)
Polymer Performance Patterns (The Polymer Summit 2015)
Chrome for Developers
2 Polymer Power Tools (The Polymer Summit 2015)
Polymer Power Tools (The Polymer Summit 2015)
Chrome for Developers
3 Chrome Dev Summit 2014 – Chrome Case Studies
Chrome Dev Summit 2014 – Chrome Case Studies
Chrome for Developers
4 Web Directions Code 2015 round up
Web Directions Code 2015 round up
Chrome for Developers
5 Maintainable Code - HTTP203
Maintainable Code - HTTP203
Chrome for Developers
6 iron-ajax… wat?! -- Polycasts #26
iron-ajax… wat?! -- Polycasts #26
Chrome for Developers
7 The Guardian - Supercharged
The Guardian - Supercharged
Chrome for Developers
8 ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
Chrome for Developers
9 #AskPolymer: Rob answers all the questions ever -- Polycasts #27
#AskPolymer: Rob answers all the questions ever -- Polycasts #27
Chrome for Developers
10 The Future of JavaScript - HTTP203
The Future of JavaScript - HTTP203
Chrome for Developers
11 Data Binding 101 -- Polycasts #28
Data Binding 101 -- Polycasts #28
Chrome for Developers
12 The Guardian part 2 - Supercharged
The Guardian part 2 - Supercharged
Chrome for Developers
13 The Future of Web Audio: with Chris Wilson and Chris Lowis
The Future of Web Audio: with Chris Wilson and Chris Lowis
Chrome for Developers
14 Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome for Developers
15 Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Chrome for Developers
16 #AskPolymer: How do you make the show? -- Polycasts #29
#AskPolymer: How do you make the show? -- Polycasts #29
Chrome for Developers
17 Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Chrome for Developers
18 Binding to Objects -- Polycasts #30
Binding to Objects -- Polycasts #30
Chrome for Developers
19 Player FM - Supercharged
Player FM - Supercharged
Chrome for Developers
20 Where’s the Designer? #AskPolymer -- Polycasts #31
Where’s the Designer? #AskPolymer -- Polycasts #31
Chrome for Developers
21 Jake Beats Wikipedia - HTTP203
Jake Beats Wikipedia - HTTP203
Chrome for Developers
22 Supercharged Observers! -- Polycasts #32
Supercharged Observers! -- Polycasts #32
Chrome for Developers
23 Jai's Web blog - Supercharged
Jai's Web blog - Supercharged
Chrome for Developers
24 Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Chrome for Developers
25 What about internationalization? #AskPolymer -- Polycasts #33
What about internationalization? #AskPolymer -- Polycasts #33
Chrome for Developers
26 Developing for Billions (Chrome Dev Summit 2015)
Developing for Billions (Chrome Dev Summit 2015)
Chrome for Developers
27 Google+ Performance Improvement Comparison
Google+ Performance Improvement Comparison
Chrome for Developers
28 Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Chrome for Developers
29 Progressive Web Apps (Chrome Dev Summit 2015)
Progressive Web Apps (Chrome Dev Summit 2015)
Chrome for Developers
30 Instant Loading with Service Workers (Chrome Dev Summit 2015)
Instant Loading with Service Workers (Chrome Dev Summit 2015)
Chrome for Developers
31 Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Chrome for Developers
32 Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Chrome for Developers
33 Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Chrome for Developers
34 Polymer - State of the Union (Chrome Dev Summit 2015)
Polymer - State of the Union (Chrome Dev Summit 2015)
Chrome for Developers
35 Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Chrome for Developers
36 Introduction to RAIL (Chrome Dev Summit 2015)
Introduction to RAIL (Chrome Dev Summit 2015)
Chrome for Developers
37 DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
Chrome for Developers
38 RAIL in the real world (Chrome Dev Summit 2015)
RAIL in the real world (Chrome Dev Summit 2015)
Chrome for Developers
39 #ChromeDevSummit talks are up - W00T! -- Polycast #34
#ChromeDevSummit talks are up - W00T! -- Polycast #34
Chrome for Developers
40 V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
Chrome for Developers
41 Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Chrome for Developers
42 Owning your performance: RAIL (Chrome Dev Summit 2015)
Owning your performance: RAIL (Chrome Dev Summit 2015)
Chrome for Developers
43 HTTP/2 101 (Chrome Dev Summit 2015)
HTTP/2 101 (Chrome Dev Summit 2015)
Chrome for Developers
44 Leadership Panel (Chrome Dev Summit 2015)
Leadership Panel (Chrome Dev Summit 2015)
Chrome for Developers
45 Build Processes, Totally Tooling Tips (S2, Ep 5)
Build Processes, Totally Tooling Tips (S2, Ep 5)
Chrome for Developers
46 Accessibility (Chrome Dev Summit 2015)
Accessibility (Chrome Dev Summit 2015)
Chrome for Developers
47 Binding to Arrays -- Polycasts #35
Binding to Arrays -- Polycasts #35
Chrome for Developers
48 HTTP2 - HTTP203
HTTP2 - HTTP203
Chrome for Developers
49 Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome for Developers
50 Call For Submissions - Supercharged
Call For Submissions - Supercharged
Chrome for Developers
51 Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Chrome for Developers
52 Testing AJAX with Web Component Tester -- Polycasts #37
Testing AJAX with Web Component Tester -- Polycasts #37
Chrome for Developers
53 Slack: Extended Xmas Special - Supercharged
Slack: Extended Xmas Special - Supercharged
Chrome for Developers
54 Browser testing with Travis & Sauce Labs -- Polycasts #38
Browser testing with Travis & Sauce Labs -- Polycasts #38
Chrome for Developers
55 Optimize for production with Vulcanize -- Polycasts #39
Optimize for production with Vulcanize -- Polycasts #39
Chrome for Developers
56 Highlights from Chrome Dev Summit 2015
Highlights from Chrome Dev Summit 2015
Chrome for Developers
57 Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome for Developers
58 Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Chrome for Developers
59 How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
Chrome for Developers
60 Colors – DevTools Tonight #0 (Pilot)
Colors – DevTools Tonight #0 (Pilot)
Chrome for Developers

Related Reads

📰
LPKWJ Tech Report: Bitcoin Miners Power the AI Computing Surge
Bitcoin miners are powering the AI computing surge due to a critical power shortage, enabling decentralized computing facilities to become strategic assets for tech companies
Dev.to AI
📰
Should ChatGPT Pay for What It Learns?
Explore the legal implications of AI training on copyrighted content and its potential consequences on fair use and copyright laws
Medium · AI
📰
Should ChatGPT Pay for What It Learns?
Explore the legal implications of AI training on copyrighted content and its potential consequences
Medium · ChatGPT
📰
How Artificial Intelligence Is Changing Everyday Life
Learn how AI is transforming everyday life, from voice assistants to personalized shopping recommendations, and why it matters for professionals
Medium · AI
Up next
Instagram Story Hack: (EASILY) Save Your Brand Colors WITHOUT Needing the Instagram Edits App
Mariah Magazine
Watch →