Skip to content

fix: keep hover styles from coming back after a key press on touch devices - #11053

Open
langz wants to merge 5 commits into
mainfrom
fix/hover-mixin-touch-keyboard
Open

langz wants to merge 5 commits into
mainfrom
fix/hover-mixin-touch-keyboard

Conversation

@langz

@langz langz commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Touch browsers can keep :hover on a tapped control. A key press then changes the tracked input from touch to keyboard, which used to bring hover styles back. On a laptop with both a mouse and touchscreen, @media (hover: hover) can still match during touch use because it describes the primary pointer's capability.

Show hover styles only on devices with a pointer that can hover (@media (any-hover: hover)), and not while the tracked input is touch or keyboard. This follows switches between touch, keyboard and mouse on the same device, and keeps the Tabs keyboard outline visible after a tap. Without Eufemia's input tracking, the media query alone decides.

The browser may update hover media queries when its primary pointer changes, but that does not guarantee tracking each input switch. See the CSS media query definition.

Before / after

Buttons on a touch device after a tap on Primary, then a press on Tab. Before: Primary shows its hover look again. After: it keeps its normal look.

Tabs on a touch device after a tap on Second, then Tab away and Shift+Tab back. Before: Second shows its hover look again (light green background). After: it shows the normal selected look.

…press

The hover mixin hides hover styles while data-whatintent is "touch".
Touch browsers keep :hover on the last tapped element, and whatInput
sets data-whatintent to "keyboard" as soon as the user presses a key.
So a tapped Button, Tag, ToggleButton, Tab or DrawerList option showed
its hover style again when the user typed or pressed Tab on a touch
device with a keyboard.

Also limit the hover mixin and the Tabs tab-hover mixin to devices that
can hover. Devices that can hover keep the JS check, so touchscreen
laptops work as before.

The Tabs focus outline excludes hovered tabs. On devices that can't
hover, show it on a tapped tab too.
langz added a commit that referenced this pull request Nov 24, 2026
#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 added a commit that referenced this pull request Nov 24, 2026
#11053 limits the hover mixin to devices that can hover. Wrapping the mixin calls here as well gave a doubled media query, and the Table style snapshot went stale without a merge conflict when both changes were merged. Keep the wrappers only on the plain :hover rules.
@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). This PR 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.

Wrap the hover guard in @media (any-hover: hover) and check that the
current input is not touch or keyboard, instead of requiring mouse intent.

Hover styles now work before Eufemia's input tracking has started and
without it, while a tap or a key press still turns them off. any-hover
keeps hover for a trackpad on iPad and a mouse on Android tablets, where
hover: hover is false. The selector specificity is unchanged.

@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 29f8a22: no critical findings.

  • New in this round: utilities.hover() now puts the hover rule inside @media (any-hover: hover) and checks html:not([data-whatintent='touch'], [data-whatintent='keyboard']) instead of html[data-whatintent='mouse']. The specificity is the same (0,1,1).
  • Touch emulation on the branch preview (https://31b28adf.eufemia-e25.pages.dev): after a tap and a key press, Button and Tabs look the same as on the previous head (d02b778): focus outline, no hover look.
  • Mouse hover in desktop Chromium equals main, also with JavaScript turned off. The previous head needed JavaScript for any hover (measured on Button and Input).
  • The 24 changed style snapshots only add the wrapper (81 times) and the new check. Merges cleanly with main. CI: 13 of 21 checks are done and none failed; the rest is still running.

@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.

Follow-up on 29f8a22: one required check fails

This adds one finding to the review from 15:19 on the same head. At that time the visual job was still running. It has now finished, and it fails.

Must fix

  1. Run visual-regression tests fails 2 screenshots: "Table in Card in light mode > shows the card menu hover and keyboard focus", and the same test in dark mode. The other 119 screenshot files pass. The test (Table.screenshot.test.ts:18) simulates hover and then focus on the card menu button ([data-visual-test="table-in-card"] .dnb-popover__trigger). The focus step uses a key press, which sets data-whatintent="keyboard". With this PR, a key press turns hover styles off, so the button loses its hover background while it keeps the focus ring.
    • I repeated the test's steps in Firefox on the main preview and on this PR's preview. main shows the hover background (rgb(216, 243, 236)). This PR shows rgb(255, 255, 255).
    • #11055 (stacked on this PR) fails the same 2 screenshots. Its render is pixel-identical to this PR's (0 px differ), so the cause is here, not in #11055.
    • This is the behavior the PR intends. So the fix is in the test, not in the CSS: update the 2 baselines, or split the test into a hover screenshot and a keyboard focus screenshot. Splitting keeps a baseline for the hover background.

Card menu with simulated hover and then keyboard focus, Firefox, light mode. Before (main): hover background. After (this PR): no hover background, focus ring only

What I checked

  • The CI log of Run visual-regression tests on 29f8a22 (job 138622558333): 1 of 120 files fails, with these 2 tests after 5 retries each.
  • The same steps in Firefox on the main preview, this PR's preview and #11055's head (light mode). I did not render the dark variant locally.
  • The trigger has no aria-disabled, so #11055's guard cannot cause it.

CI on 29f8a22494 when I posted this: 23 checks passed; Run visual-regression tests failure. Run visual-regression tests fails the 2 Table in Card screenshots in must-fix 1. Rechecked just before posting: the head is still 29f8a22.

This branch was successfully deployed

2 active deployments
Package preview — 29f8a224 Deployed Nov 24, 2026 by github-actions[bot]
Portal preview — 29f8a224 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.

2 participants