Complex and accessible JavaScript widgets

Logo Nothing AG

(with simple HTML)

Wednesday, 28th of July

Nothing AG, Berne

Logo Nothing AG

Live, accessible slides broadcast:
links see email!ย ๐Ÿ”—

  • Please use only as additional aid
  • Shared screen is main reference
    • โ€‹Switch between applications
    • Use DOM Inspector

Additional helpers:

  • Visible key strokes
  • Visible mouse clicks
  • Zoom
  • Highlight/paint on screen

KeyCastrย ๐Ÿ”—, ScreenBrushย ๐Ÿ”—

Portrait of Josua Muheim

Hello everybody! ๐Ÿ‘‹๐Ÿผ

Logo Nothing AG
AD G* Created with Sketch.

A11y Dev Guide (ADG)

Logo Nothing AG
Logo Accessibility Developer Guide AD G* Created with Sketch.
Screenshot of Accessibility Developer Guide

Nothing AG

Logo Nothing AG
  • Peer to peer (P2P)
  • Web agency / venture lab
  • Berne, Switzerland
  • www.nothing.ch
Logo Nothing AG
Logo Nothing AG

Goal:
Become a well-known "accessible-first" web
company in (and outside) Switzerland

Nothing AG

Logo Nothing AG
Peerdom screenshot

Let us share our skills with you!

  • Design reviews
  • Semantic HTML prototyping
  • Visual and functional prototyping
  • Review & optimise existing project
  • Presentations and workshops
    ย 
  • Hands-on work on your product
  • Written & video documentation

On your accessibility journey

Reach out: josua@nothing.ch

Let us share our skills with you!

  • Design reviews
  • Semantic HTML prototyping
  • Visual and functional prototyping
  • Review & optimise existing project
  • Presentations and workshops
    ย 
  • Hands-on work on your product
  • Written & video documentation

On your accessibility journey

Reach out: josua@nothing.ch

We want to empower you! Let's transfer our skills to your team!

Who are you? ๐Ÿ‘‹๐Ÿผ

  • What's your name?
  • What's your profession / role?
  • Do you have any form of disability?
    • We'll form little groups (breakout rooms)
      ย 
  • What brought you here?
  • Experiences with accessibility?
  • Expectations for today's workshop?
    ย 
  • Please pass on to another participant!

A question

of trust

Carabiner

What do you trust more?

<a href="โ€ฆ">
  Click me!
</a>
<span onclick="location.href='โ€ฆ'">
  Click me!
</span>
Two climbers: one secures the other who is hanging over the abyss with a rope

Why? ๐Ÿค”

Benefits of traditional HTML

<a href="โ€ฆ">
  Click me!
</a>
<span onclick="location.href='โ€ฆ'">
  Click me!
</span>
  • Behaviour for free
    • Go to address on click
    • Keyboard focusability
  • Styles for free
    • Underline
    • Focus, hover, active states
  • Compatibility
    • โ€‹Forward and backwards
    • Without JavaScript
    • Degrades gracefully

Sure, we can "enhance" the fake

<a href="โ€ฆ">
  Click me!
</a>
<span onclick="location.href='โ€ฆ'"
      class="link"
      tabindex="0"
      role="link">
  Click me!
</span>

<style>
  .link        {โ€ฆ}
  .link:hover  {โ€ฆ}
  .link:focus  {โ€ฆ}
  .link:active {โ€ฆ}
</style>
  • Apply custom...
    • aesthetics
    • behaviour
    • semantics

But how will it perform?

<a href="โ€ฆ">
  Click me!
</a>
  • Still: it's not really a linkโ€‹! ๐Ÿคฅ
    • Can't open context menu
    • Can't bookmark
    • Possibly many more things
<span onclick="location.href='โ€ฆ'"
      class="link"
      tabindex="0"
      role="link">
  Click me!
</span>

<style>
  .link        {โ€ฆ}
  .link:hover  {โ€ฆ}
  .link:focus  {โ€ฆ}
  .link:active {โ€ฆ}
</style>

Traditional HTML beats quirky custom solutions

  • Much functionality "for free"
  • Robust
  • Performant
<a href="โ€ฆ">
  Click me!
</a>
<span onclick="location.href='โ€ฆ'"
      class="link"
      tabindex="0"
      role="link">
  Click me!
</span>

<style>
  .link        {โ€ฆ}
  .link:hover  {โ€ฆ}
  .link:focus  {โ€ฆ}
  .link:active {โ€ฆ}
</style>

๐Ÿ˜ƒ

But why

would anyone do this?!

Reason I: Ignorance โ˜ ๏ธ

<span onclick="location.href='โ€ฆ'">
  Click me!
</span>
  • Encountered innumerable times
  • Typical bad use cases:
    • List of search results
    • Menu in single page apps
  • See Apple's product listย ๐Ÿ”—
  • Please stop doing this!
  • Use real links (or buttons)!

Reason II: HTML can't be styled

<select>
  <option id="cat">Cat</option>
  <option id="dog">Dog</option>
</select>

<style><!-- Won't have an effect -->
  select { font-weight: bold; }

  #cat { background-color: blue; }
  #dog { background-color: red; }
</style>
  • In earlier days, many HTML elements offered limited CSS
  • Some still can't be styled
  • Instead, use role-attributes
    • role="listbox" for <select>
    • role="option" for <option>
<div role="listbox">
  <div role="option" id="cat">Cat</div>
  <div role="option" id="dog">Dog</div>
</div>

<style>
  [role=listbox] { font-weight: bold; }

  #cat { background-color: blue; }
  #dog { background-color: red; }
</style>

Reason III: HTML missing

<!-- No such thing -->
<tablist>
  <tab id="tab1">Tab 1</tab>
  <!-- And more tabs -->

  <content for="tab1">
    Content 1
  </content>
  <!-- More content -->
</tablist>

Quite a few well established usage patterns without HTML equivalent.

Requires to tinker own custom solution.

<ul role="tablist">
  <li role="tab">
    <a href="#section1" role="presentation"
        id="tab1" aria-selected="true">
      Tab 1
    </a>
  </li>
</ul>

<section id="section1" role="tabpanel"
    tabindex="-1" aria-labelledby="tab1">
  Content 1
</section>

role = ARIA

  • Accessible Rich Internet Applications standard
  • Version 1.0ย ๐Ÿ”— released in 2014 by W3Cย ๐Ÿ”—
  • Offers attributes to enrich traditional HTML with semantics
  • But: they don't add functionality!
  • Still a lot of JavaScript to add:
    • Manage visibility and styles
    • Provide keyboard focusability
    • Provide interactivity
    • Update changes to semantics
      • aria-selected="true"
  • Quickly becomes complex
<div role="listbox">
  <div role="option" id="cat">Cat</div>
  <div role="option" id="dog">Dog</div>
</div>

<style>
  [role=listbox] { font-weight: bold; }

  #cat { background-color: blue; }
  #dog { background-color: red; }
</style>

ARIA example

  • Collapsible Dropdown Listbox Example: w3c.github.io/aria-practicesย ๐Ÿ”—
    • ~150 lines of CSS
    • โ€‹~900 lines of JavaScript!
  • โ€‹Support varies drastically among browser / screen reader combos
  • ARIA offer limited:
    • "listbox" or "tablist" available
    • "datepicker" and others missing
    • You will quickly reach the point where there is no official recipe
  • โ€‹So: what to do then?

First rule of ARIA use

If you can use a native HTML element (or attribute) with the semantics and behavior you require, then do so. Do not instead re-purpose an element and add an ARIA role, state or property to make it accessible.

Source: w3.org/TR/using-aria/#rule1ย ๐Ÿ”—

First rule of ARIA use

Source: w3.org/TR/using-aria/#rule1ย ๐Ÿ”—

There (nearly) always exists a native element, even when not obvious at first sight!

If you can use a native HTML element (or attribute) with the semantics and behavior you require, then do so. Do not instead re-purpose an element and add an ARIA role, state or property to make it accessible.

Taking a closer look

at custom dropdown listboxes

Animation of Google autocomplete in action

Google autocomplete

  • URL: google.comย ๐Ÿ”—
  • role attributes
  • Mediocre screen reader accessibility
    • Announces itself as searchable combo box ๐Ÿ‘
    • Does not announce availability of options ๐Ÿ‘Ž
    • Announces selected option ๐Ÿ‘
<ul role="listbox">
  <li role="presentation">
    <div role="option">1st option</div>
  </li>
  <li role="presentation">
    <div role="option">2nd option</div>
  </li>
</ul>
Screenshot of Google's autocomplete

Amazon autocomplete

  • URL: amazon.com ๐Ÿ”—
  • <div> and <span>
  • Bad screen reader accessibility
    • Announces itself as bare text search ๐Ÿ‘Ž
    • Does not announce availability of options ๐Ÿ‘Ž
    • Announces selected option ๐Ÿ‘
<div>
  <div>
    <span>1st option</span>
  </div>
  <div>
    <span>2nd option</span>
  </div>
</div>
Screenshot of Amazon autocomplete

Interim conclusion

  • Both need a dropdown
  • Dropdown needs fancy styling
    • <select> not an option
  • Google uses ARIA
  • Amazon uses <div> and <span>
  • Lots of JavaScript, mediocre to bad accessibility
  • Couldn't this be done better?
Thinking face

No traditional HTML element available...?

Hmmm...

  • A <select> offers predefined list of options
  • One of which can be chosen
  • What other HTML element allows for this?
Thinking face

Come on!

Radio buttons!

Thinking face
Logo Accessibility Developer Guide AD G* Created with Sketch.
  • 100% customisable
    • <label> elements can be styled
    • <input> can be visually hidden (moved off-screen)
  • No JavaScript needed
  • 100% accessible
Radio buttons

A listbox

of radio buttons!

Logo Accessibility Developer Guide AD G* Created with Sketch.

ADG autosuggest

  • URL: ADG: examples/widgets/autosuggest/ย ๐Ÿ”—
  • Radio buttons
  • Very good screen reader accessibility
    • Announces itself appropriately ๐Ÿ‘
    • Announces availability of options ๐Ÿ‘
    • Announces selected option + total of options ๐Ÿ‘
<input type="radio" name="hobbies" id="dancing">
<label for="dancing">Dancing</label>

<input type="radio" name="hobbies" id="gardening">
<label for="gardening">Gardening</label>

Only tiny bit of JavaScript! ๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช

Logo Accessibility Developer Guide AD G* Created with Sketch.

4 simple milestones to implement

  1. Plain HTML
  2. Interactivity
Logo Accessibility Developer Guide AD G* Created with Sketch.
  1. Accessibility optimisations
  2. Visual styles

Verdict

  • Very accessible
  • ~60 lines of CSS
  • ~230 lines of JavaScript
    • Including filter functionality
Logo Accessibility Developer Guide AD G* Created with Sketch.

But wait...

...there's more!

Logo Accessibility Developer Guide AD G* Created with Sketch.

Unexpected usages of form controls

Logo Accessibility Developer Guide AD G* Created with Sketch.
Screenshot of Accessibility Developer Guide

Let's recap

Form controls are powerful

  • They handle much more than basic user input
  • They can handle many simple usage patterns
    • Even complex ones when combined
  • They are:
    • Simple and compatible
    • Robust and performant
    • Innately accessible
  • They can be styled as wished
Spock from Starship Enterprise

What do you trust?

<a href="โ€ฆ">
  Click me!
</a>
<span onclick="location.href='โ€ฆ'"
      class="link"
      tabindex="0"
      role="link">
  Click me!
</span>
Carabiner

Remember next time

When you're tempted to glue together some random <div> / <span> elements with JavaScript handlers

We love to share our skills with you!

  • Design reviews
  • Semantic HTML prototyping
  • Visual and functional prototyping
  • Review & optimise existing project
  • Presentations and workshops
    ย 
  • Hands-on work on your product
  • Written & video documentation

On your accessibility journey

Reach out: josua@nothing.ch

Portrait of Josua Muheim

This workshop's conscious pricing

  • Accessibility is all about inclusion
  • We encourage everyone to participate
  • Happy to let you determine your contribution
  • Payment after the workshop
  • You'll receive email with further details

Nobody to be left behind

Thank you for your support!
๐Ÿ™โค๏ธ๐Ÿ’ธ

That's all folks!

Logo Nothing AG

Reach out: josua@nothing.ch

Coffee break
Portrait of Josua Muheim

Our competences: nothing.ch/accessibility

Questions & discussion

Complex and accessible JavaScript widgets with simple HTML

By Nothing

Complex and accessible JavaScript widgets with simple HTML

  • 276