Problem/Motivation
The CSS :focus-within pseudo-class looks very handy, but we don't use it in core yet. It's particularly useful as a way to achieve better parity between mouse and keyboard interaction, by pairing :hover with :focus-within styles for container elements.
It has good support, but will need a polyfill for some of the browsers we support, in particular Internet Explorer (the forthcoming Chromium-based Edge supports it).
- Several polyfills are available. Most of these work by programmatically adding an atttribute to all ancestors of the element which matches
:focus. - Stylesheets need extra selectors to target the polyfill's attribute, but there are utilities for SASS/Post-CSS to manage this.
- Modernizr has a test for
:focus-withinsupport, since version 3.6.0. Currently D8 has version 3.3.1. D9 has modernizer 3.8.0 - #3100937: Update modernizr to 3.8.0.
Examples where :focus-within could help
- Highlighting table rows. The Seven theme already has this style rule in
tables.css, and Claro has something similar:
tbody tr:hover, tbody tr:focus { background: #f7fcff; }Mouse users get a highlight on the row currently being hovered over, which is a useful aid to reading, comprehension, and hitting the correct checkboxes and drop-buttons. The
tr:focusis intended to provide an equivalent effect for keyboard users too; but it fails because table rows aren't normally focusable, so the selector never matches. What's really needed here istr:focus-within. - Media library truncates long names, but expands them when a keyboard user tabs to a media item, so they can read the whole name. Currently JS events are used to manage a class on a container element, but this could be simplified by a
:focus-withinapproach.
// Adds is-focus class to the click-to-select element. .on('focus blur', ({ currentTarget, type }) => { $(currentTarget) .closest('.js-click-to-select') .toggleClass('is-focus', type === 'focus'); });.media-library-item--grid.is-focus .media-library-item__name { white-space: normal; } - Highlighting the label and description associated with a form input. For example a coloured background on
div.form-item:focus-within. (The input itself would still use a:focusstyle.) - Highlighting a group of related controls, e.g. date/time, shipping address. Use
:focusto style the current control, and style the containing fieldset with:focus-within
Proposed resolution
- Add a focus-within polyfill to core, make it available as a core library which CSS libraries can depend on.
- Optionally load it via modernizr?
- Follow-up issue: change
tr:focustotr:focus-withinin Seven's table style. - Follow-up issue: change
tr:focustotr:focus-withinin Claro's table style.
Remaining tasks
Decide which focus-within polyfill to use. What criteria to evaluate this?
Change record.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 2021-08-21 16.31.41.gif | 64.25 KB | kentr |
| #11 | 2021-08-21 15.53.21.gif | 76.9 KB | kentr |
Comments
Comment #2
andrewmacpherson commentedComment #3
andrewmacpherson commentedComment #6
asmita26 commentedComment #7
andrewmacpherson commentedComment #9
mgiffordLooks like this is going to be a problem until we give up support for IE11 - https://caniuse.com/?search=%3Afocus-within
Comment #11
kentr commentedAnother use-case example:
Hide arbitrary focusable skip-links that are nested with
<ul><li>, such as the "Add new comment" link.This would add another follow-up issue: change
.visually-hidden.focusable:focusto.visually-hidden.focusable:focus-withininhidden.module.css.Examples
Bartik
Olivero
Comment #12
gauravvvv commentedComment #13
longwaveThe polyfill would only be required for IE11: https://caniuse.com/?search=focus-within
It seems a waste of time and effort to introduce this now, late in 9.x and only for a browser that is effectively EOL, only to remove it again when 10.0.x opens. Can we just start using this pseudo-class now, with the caveat that it won't work on IE11?
Comment #14
kentr commented@longwave
You can use
focus-withinin your site's CSS without any problems if you the browser support isn't a problem for your site.I'm working on an issue / patch for
hidden.module.css.Comment #15
longwaveI'm talking about using it in core in Seven/Claro as suggested in the OP.
I just checked our browser policy at https://www.drupal.org/docs/system-requirements/browser-requirements which states that we can "start using any given browser feature once all the supported browsers have either native support or a polyfill", and for now we need to support IE11, UC Browser and Opera Mini which do not support focus-within, so we either need to change the policy to allow progressive enhancement in modern browsers for features like this, or add the polyfill, I guess.
Comment #19
mherchelClosing this as outdated, since 10.x supported browsers natively support focus-within.