Just use button -- A11ycasts #05

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

Key Takeaways

Discusses the importance of using the button element for accessibility

Full Transcript

[Music] hey there folks Rob here welcome back to the show so in the last episode we were talking a little bit about tab index and how you can add focusability to some of the custom controls that you're working on I promised in this episode that we would be you know talking more about tabex and I'd show you how to build some custom components but before I get into that I realized I was getting a little ahead of myself and I should actually spend a bit more time singing the Praises of native HTML in particular The Humble button element it's one of those things that I see a lot of developers actually not using in their projects using Alternatives like like divs or or maybe even abusing the anchor tag and so today what I wanted to do was just show off why instead of building your own custom button you should always just use a button now follow me over here to my code editor and I'm going to walk you through kind of the process of building like a fake button and then demonstrate why we should stick with the native button so here I've got a just a div tag and I'm going to turn this into a signup button one of the things I think a lot of developers feel is that maybe the button element is not super styleable right so they'll go they'll drop in their own styles for uh for this div button we'll give it a class button or something like that actually I'm going to call this fancy because this a really fancy looking button so we'll give this class fancy okay so now I've got this very fancy looking sign up button that I've created here um if you remember from the previous episodes though one of the most important things for our us users to make sure that our controls our interactive controls at least are focusable and as you know this div uh if we go and we try it with our keyboard and we press the Tab Key we're not going to see any sort of focus indicator actually on this element and again that's because to the browser a div semantically is just sort of like a a grouping element there's nothing implicitly interactive about it and so there's no reason why it should be in the tab order so if you're creating a fancy div button you might realize that and say ah okay well I I saw that last episode that Rob so I know that I should maybe add a tab index to this thing ah okay I'll give it a tab index of zero now I can go and I can tab to this button and sure enough it is in the focus order but remember keyboard focus is not the total story we've also got our screen reader users out there so let's see what happens when we try and interact with this control using something like voice over sign up group so you saw there that instead of saying sign up button it said sign up group again because semantically to the browser a div it's just sort of like a generic containing grouping element type thing now we haven't really talked about Arya much but to fix this we need to add our own Ro and now we're starting to kind of get to a point where creating our own custom button has diminishing returns let's instead look at what we get when we just use a native button tag so I'll drop another button in here just a regular old button element I'll also write sign up next to this one I'll say sign up for real okay and uh right away you can see that we don't have that really cool beautiful styling that we had on our custom button but you know a native button element is is totally stylable so I'm just going to give it that same class of fancy and then boom now we have two buttons that look pretty much identical uh we can see that they are both in the tab order which is cool I didn't have to add anything to my button element it just sort of like gets that for free and now let's uh try this out with the screen reader and see what we get voice over on Chrome JS bin sign up group sign up for real button so you can see there with that second button it actually announced the proper role to our user uh so had they previously just been working with that first thing they wouldn't really know that it was an interactive control they might think it's maybe just some text on the page that tells them to sign up whereas now we've we've kind of explicitly told them hey this is an action that you can take to sign up for our application but there are some more reasons why the humble button is pretty awesome another one is that we can add our own click handlers to this element and I want to show you kind of an important distinction that happens here so I'm going to just uh drop in a little bit of JavaScript in my page and what this JavaScript is doing I'll I'll hide the CSS there what this JavaScript is doing is it's selecting everything that has the class of fancy right so we have this array of buttons now that have that fancy class for each of those we're adding a click event listener and when we click on the element it should alert this little message that says I was clicked okay so I'll go click on the the first button you can see it says I was clicked right let's go click on the second one okay we can see it also says I was clicked all right well that makes sense uh now let's try this with our keyboard though so typically on a keyboard pressing the space bar or maybe the inner key in some situations we'll interact with a control so let's see what we get here we will focus the first button and hit space bar nothing's happening hit enter nothing's happening let's go over to that actual the native button element we hit space bar ah now we get our alert message we hit enter we get our alert message as well so this little trick that I'm showing off here in the HTML spec I believe it's referred to as synthetic click activation and basically it's a little bit of black magic that the browser does that says hey if you have a button element and you add a click Handler I'm just going to assume that you would also like that to run when someone presses the space bar or the inner key so again you're not going to get that with a div button to add that kind of keyboard support now you're going to have to add key down handlers you're going to have to look at the key codes differentiate was that a space bar or an inner key if so run my click Handler right okay so now we're really really getting diminishing returns with that div button uh the last thing that I want to show off is the disabled attribute so it can be really really handy to just turn a control off sometimes based on the state of your application so I'll write a little bit of CSS here just so we can see what's going on so I'll say hey anytime anything that is fancy uh is disabled then we're just going to set this background to gray okay and and I'll add that disabled attribute to both of these controls right so we'll say one two all right now they both look disabled uh but let's see here now when I go and I click on the sign up button it's not actually disabled right I'm still seeing that click message running if I go and I click on sign up for real ah nothing's happening right and again as we saw here we can see the focus ring is on this control because we added tab index to it but we really don't want it to be focused right now because it should be disabled and so you can see I can not tab to that sign up for real button the native button has been kind of removed from the focus order because of that attribute so again I just wanted to highlight there's a lot of reasons why you want to use the native controls whenever possible you're getting a ton of built-in behavior and you might not realize it until you actually go to use one of these attributes one of these features and then notice oh shoot this is not implemented in my button I'm going to have to add a bunch of JavaScript to make that work so you see this with button you see this with other controls like like select and input there's a lot of reasons why whenever possible you want to stick to the native elements so that's it for today uh if you have any questions please leave them for me down below in the comments or you can always 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

Talking about tabindex and custom controls is super fun, and we'll definitely be doing more of it in the future. But I wanted to pause for a moment and shine a light on some of the awesomness of the humble button element. Too often I see developers use divs or spans to make buttons, but when you do this you're missing out on SO MANY FEATURES. You may not realize just how much accessibility and general purpose functionality is built into the simple button, but it's actually a wonderful tool that should be your first and last option whenever you're adding an action to your page. So today I want to sing the praises of button and clue you in to a few handy features you might not have seen before. Synthetic Click Activation in the HTML spec https://goo.gl/Ey4RBf 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

📰
Will a Degree Still Matter in the AI Era?
In the AI era, a degree may still open doors, but its value is being reevaluated, making it essential to acquire relevant skills to remain competitive
Medium · AI
📰
Will a Degree Still Matter in the AI Era?
In the AI era, a degree may not be the sole determinant of career success, but it can still open doors and provide foundational knowledge
Medium · Startup
📰
AI Is Not the Enemy of Journalists, but a Test of Journalism’s Integrity
AI is transforming journalism, but its impact depends on the integrity of journalists, who must learn to work with AI tools while maintaining ethical standards
Medium · AI
📰
Apple Sends Letters to Dozens of Former Employees Now at OpenAI
Apple is taking aggressive legal action against former employees now at OpenAI, demanding document preservation and meetings with lawyers, in relation to a recent lawsuit
Daring Fireball
Up next
I Lost 94% of My Traffic. Then I Found the Unfair Advantage
Conor Martin
Watch →