Skip to content

fix(Accordion): show the focus outline on a tapped header on touch devices - #11048

Open
langz wants to merge 5 commits into
mainfrom
fix/accordion-focus-after-tap
Open

langz wants to merge 5 commits into
mainfrom
fix/accordion-focus-after-tap

Conversation

@langz

@langz langz commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

A tapped Accordion header can keep :hover when the user switches to the keyboard. That used to hide its focus outline.

Use the tracked input to show keyboard focus even while native hover remains. This also covers laptops with both a touchscreen and mouse, where a hover media query can still match during touch use. Use together with #11053 to keep hover styles off after touch followed by a key press.

Before / after

An Accordion on a touch device after a tap on the header, then Tab away and Shift+Tab back. Before: the green hover outline shows instead of the focus outline. After: the blue focus outline shows.

Touch browsers keep :hover on the last tapped element. The hover mixin
hides the hover style after a touch, but not after the user presses a
key. So when the user tapped a header and came back to it with the
keyboard, the header showed the hover border instead of the focus
outline, because the focus outline excludes hovered headers.

Limit the hover styles to devices that can hover, and show the focus
outline on a hovered header on devices that can't hover.
langz added a commit that referenced this pull request Nov 24, 2026
… devices

A tapped element keeps :hover on touch devices, so a focus style with :not(:hover) does not show when the user comes back to it with the keyboard. Add a bullet that tells how to avoid that, as done in #11025, #11042, #11043, #11048 and #11049.
Prettier wrapped a custom property value differently after the snapshot was written, and Sass keeps those line breaks in the CSS. Firefox does not show focus-visible on a focus() call after a tap, so move focus with Tab and Shift+Tab, as a keyboard user does.
langz

This comment was marked as outdated.

#11053 limits the hover mixin to devices that can hover. Wrapping the mixin call here as well gave a doubled media query and a stale style snapshot when both changes were merged. Keep only the focus outline fix here, and check only the focus outline in the touch test.
@langz

langz commented Nov 24, 2026 •

Copy link
Copy Markdown
Member Author

Why @media (hover: hover): touch browsers keep :hover on the last tapped element, so hover styles stay after a tap until the user taps somewhere else. The query tests whether the primary input can hover (MDN, in all browsers since 2018). So mouse and trackpad hover stay as they are, and phones and tablets never get the sticky state.

This is the industry standard: Tailwind CSS v4 applies its hover variant only inside @media (hover: hover) (upgrade guide), and Radix Themes and Mantine wrap their component hover styles the same way.

The :not([data-whatintent='touch']) check alone is not enough: a key press outside a text field, for example Tab or an arrow key, sets the intent to keyboard, so on a tablet with a keyboard the tapped control shows its hover style again (measured on Button, Tag, ToggleButton and Tabs). #11053 now shows hover only while the current input is a mouse (html[data-whatintent='mouse']). That also covers Chromebooks and touchscreen laptops, where (hover: hover) matches even though the user can tap. The trade-off: without Eufemia's JavaScript nothing sets data-whatintent, so these styles show no hover at all.

@joakbjerk joakbjerk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. A tapped header now shows the focus outline when the user comes back with the keyboard, and the forced-colors outline is unchanged. Matches the mouse-intent hover approach in #11053.

@langz langz left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 03cf051: Approve (posted as a comment, because GitHub does not let the PR author approve their own PR)

A header that was tapped and then reached with the keyboard now shows the focus outline. Desktop keyboard focus is unchanged, and so is a focused header that the mouse hovers with mouse intent. Tested merged with main at ecaa2f5.

Earlier findings

  • Resolved: the doubled @media wrapper and the stale snapshot when merged with #11053. f4327d2 leaves hover to the mixin. The snapshot now differs from main only by the new focus rule.

Suggestions (non-blocking)

  1. Without #11053, the tapped-then-Tab header shows the focus outline and the 2px hover border together. The hover mixin still applies with keyboard intent. The description says to use this with #11053, and that removes the border, so merge #11053 first or together with this.
  2. The e2e tests for the tapped header were removed in 025bd4b, so only the style snapshot covers the new rule now. A unit test cannot do the tap-then-Tab flow, so this is only a note.

What I checked

  • I used the outlined and filled headers copied from the main preview, with CSS compiled from origin/main and from 03cf051. Focus came from a real Tab, so :focus-visible matched. I then set data-whatinput and data-whatintent and moved the real mouse. Chromium, Firefox and WebKit all gave the same result:
    • Keyboard only: the outline shows on main and on the PR.
    • Tap then Tab, with the header still hovered: no outline on main. The PR shows the outline.
    • Keyboard focus, then the mouse moved onto the header (mouse intent): no outline on main or on the PR. Desktop behavior is unchanged.
    • data-whatinput="touch": no outline on main or on the PR.
    • No input tracked yet, hovered: no outline on main. The PR shows the outline.
  • Forced colors (Chromium emulation): the outline stays visible in every focused case. In the tap-then-Tab case it now uses the same color as plain keyboard focus, instead of CanvasText.
  • The tertiary variant uses a Button, not .dnb-accordion__header, so this rule does not reach it.
  • Scoped builds: the rule starts with html, so the scope plugin inserts the scope class after it.
  • Trial merge with ecaa2f5 (a real git merge --no-commit): no conflicts. Accordion.test.tsx passes: 66 tests. stylelint on dnb-accordion.scss: clean.

CI on 03cf0516b8 when I posted this: 22 checks passed; Run visual-regression tests cancelled. Run visual-regression tests never ran on this head: twice, GitHub did not assign a runner, and the job was cancelled with no steps. The Accordion screenshots are still unchecked, so it needs a re-run before merge. Rechecked just before posting: the head is still 03cf051, and a real trial merge with main at 541b379 (which now includes #11052) has no conflicts.

This branch was successfully deployed

2 active deployments
Package preview — 03cf0516 Deployed Nov 24, 2026 by github-actions[bot]
Portal preview — 03cf0516 Deployed Nov 24, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants