The path of bringing :focus-visible to WebKit

A presentation at CSS Conf Armenia 2021 in November 2021 in Yerevan, Armenia by Manuel Rego Casasnovas

Slide 1

Slide 1

The path of bringing :focus-visible to WebKit Manuel Rego 13th November 2021 | CSS Conf Armenia

Slide 2

Slide 2

About Me Igalia Web Platform Team Web engines hacker working on Chromium/Blink and Safari/WebKit CSS Working Group member since 2017

Slide 3

Slide 3

TOC 1. Introduction 2. Open Prioritization 3. Work Done 4. Next Steps

Slide 4

Slide 4

Introduction

Slide 5

Slide 5

The Problem Click me! :focus { outline: none; }

Slide 6

Slide 6

❌ The Problem :focus { outline: none; } Click me!

Slide 7

Slide 7

The Solution :focus-visible

Slide 8

Slide 8

Spec The :focus-visible pseudo-class applies while an element matches the :focus pseudo-class and the user agent determines via heuristics that the focus should be made evident on the element.

Slide 9

Slide 9

While the :focus pseudo-class always matches the currently-focused element, UAs only sometimes visibly indicate focus (such as by drawing a “focus ring”), instead using a variety of heuristics to visibly indicate the focus only when it would be most helpful to the user. The :focus-visible pseudo-class matches a focused element in these situations only, allowing authors to change the appearance of the focus indicator without changing when a focus indicator appears.

Slide 10

Slide 10

Heuristics

Slide 11

Slide 11

If the element which supports keyboard input, indicate focus.

Slide 12

Slide 12

Basically any <input> , <textarea> or contenteditable element. This is a text area.

Slide 13

Slide 13

What happens with <select> or <input type=”date”> on mobile? 13/11/2021

Slide 14

Slide 14

If the user interacts with the page via keyboard, indicate focus.

Slide 15

Slide 15

Everyone agrees here, keyboard navigation should always show a focus ring. foo bar baz

Slide 16

Slide 16

If the user interacts with the page via a pointing device, do not indicate focus.

Slide 17

Slide 17

This is the key part. When you click on a <button> or <div tabindex=0> you do not want to see a focus ring, only when you tab navigate. foo bar baz

Slide 18

Slide 18

Default UA Stylesheet User agents should also use :focus-visible to specify the default focus style, so that authors using :focus-visible will not also need to disable the default :focus style.

Slide 19

Slide 19

https://twitter.com/LeaVerou/status/104576827975366656

Slide 20

Slide 20

:-internal-direct-focus WebKit uses :-internal-direct-focus in the default UA stylesheet (not web exposed). It uses this to avoid showing a double focus ring, when you focus something in a Shadow DOM tree.

Slide 21

Slide 21

Open Prioritization

Slide 22

Slide 22

An experiment in crowd-funding prioritization of new feature implementations for web browsers. https://www.igalia.com/open-prioritization/

Slide 23

Slide 23

https://opencollective.com/focus-visible-in-webkit

Slide 24

Slide 24

75 Contributors 0:00 / 0:26

Slide 25

Slide 25

Work Done

Slide 26

Slide 26

Implementation From January to May 2021. Monthly reports on my blog: https://blogs.igalia.com/mrego/ . WebKit implementation ready behind a runtime flag (not shipped yet). Passing all tests, some slight differences depending on the platform. Develop > Experimental Features > :focus-visible pseudo-class

Slide 27

Slide 27

WebKit 27 patches

Slide 28

Slide 28

WebKit 27 patches WPT 26 patches

Slide 29

Slide 29

WebKit 27 patches WPT 26 patches Chromium 9 patches

Slide 30

Slide 30

WebKit WPT 27 patches 26 patches Chromium 9 patches Specs 3 patches

Slide 31

Slide 31

UA Stylesheet Status January 2021: Nobody was using :focus-visible in the default UA stylesheet. May 2021: Chromium and Firefox started using :focus-visible in the default UA stylesheet.

Slide 32

Slide 32

Web Platform Tests (WPT) The tests were not in a good shape. Fixed tests, added new tests, ended up with ~40 tests. Tests use WebDriver (added missing bits in WebKit).

Slide 33

Slide 33

https://wpt.fyi/results/css/selectors/

Slide 34

Slide 34

Next Steps

Slide 35

Slide 35

Goal Ship :focus-visible in WebKit (Safari)

Slide 36

Slide 36

Platform Conventions Buttons are not click focusable on Mac platforms. Thus buttons are not click focusable in Safari. Apple wants to be sure that :focus-visible changes are aligned with the platform conventions. https://webkit.org/b/22261 (2008)

Slide 37

Slide 37

Examples Apple engineers were reluctant to hide the focus ring on click on <div tabindex=0> . Perhaps one possibility would be to hide the focus ring on click on <div tabindex=0 role=button> . How this affects user interaction (e.g. scrolling with spacebar) if focus ring is not visible? https://github.com/WICG/focus-visible/issues/257

Slide 38

Slide 38

Status Apple showed interested on :focus-visible at the WebKit Contributors Meeting (Sep 2021). Conversations are still ongoing, latest updates have been positive. Interoperability might weight in.

Slide 39

Slide 39

Proposal? A new HTML attribute to identify an element that is keyboard focusable but not mouse focusable. This will allow to mimic Mac platform conventions regarding focus.

Slide 40

Slide 40

tabindex=”-1” already allows to make an element not keyboard focusable. foo bar tabindex=”-1” baz

Slide 41

Slide 41

Today is not possible to make an element keyboard focusable but not mouse focusable. button

Slide 42

Slide 42

The Boy, the Mole, the Fox and the Horse by Charlie Mackesy

Slide 43

Slide 43

Thanks! Email: rego@igalia.com Twitter: @regocas

Slide 44

Slide 44