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-within support, 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

  1. 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:focus is 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 is tr:focus-within.

  2. 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-within approach.
            // 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; }
    
  3. 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 :focus style.)
  4. Highlighting a group of related controls, e.g. date/time, shipping address. Use :focus to 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:focus to tr:focus-within in Seven's table style.
  • Follow-up issue: change tr:focus to tr:focus-within in Claro's table style.

Remaining tasks

Decide which focus-within polyfill to use. What criteria to evaluate this?
Change record.

CommentFileSizeAuthor
#11 2021-08-21 16.31.41.gif64.25 KBkentr
#11 2021-08-21 15.53.21.gif76.9 KBkentr

Comments

andrewmacpherson created an issue. See original summary.

andrewmacpherson’s picture

andrewmacpherson’s picture

Issue summary: View changes

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

asmita26’s picture

Assigned: Unassigned » asmita26
andrewmacpherson’s picture

Issue summary: View changes

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

mgifford’s picture

Issue tags: +IE11

Looks like this is going to be a problem until we give up support for IE11 - https://caniuse.com/?search=%3Afocus-within

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kentr’s picture

StatusFileSize
new76.9 KB
new64.25 KB

Another 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:focus to .visually-hidden.focusable:focus-within in hidden.module.css.

Examples

Bartik

Bartik hidden comment link example

Olivero

Olivero hidden comment link example

gauravvvv’s picture

Assigned: asmita26 » Unassigned
longwave’s picture

The 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?

kentr’s picture

@longwave

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?

You can use focus-within in 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.

longwave’s picture

I'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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mherchel’s picture

Status: Active » Closed (outdated)

Closing this as outdated, since 10.x supported browsers natively support focus-within.