Skip to content

fix(Switch): prevent sticky hover styles after tapping on touch devices - #11007

Open
langz wants to merge 6 commits into
mainfrom
fix/switch-sticky-hover-touch
Open

langz wants to merge 6 commits into
mainfrom
fix/switch-sticky-hover-touch

Conversation

@langz

@langz langz commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Motivation: https://dnb-it.slack.com/archives/CMXABCHEY/p1795427065802309

On iOS Safari, and in other touch browsers, a Switch kept its hover styles after being tapped, until something else was tapped. Because the hover styles use the same green colors as the checked state, an unchecked Switch looked checked, which was reported as confusing.

Hover now follows the current input, using the same data-whatintent tracking as Button. A tap hides hover styling, it stays hidden when switching to the keyboard, and using the mouse brings it back. This also works on devices with both a touchscreen and a mouse.

@media (hover: hover) only describes the primary pointer's capabilities. It can stay true while the touchscreen is being used, so it does not provide the same switching.

Use together with #11053 for the shared hover behavior.

Before / after

A Switch on a touch device after one tap that checks it. Before: it keeps the hover look (light background, green ring). After: it shows the normal checked look.

A Switch on a touch device after one tap that unchecks it. Before: it keeps the hover look, so it looks checked (green ring and green knob). After: it shows the normal unchecked look.

Touch browsers keep :hover on the last tapped element, so an unchecked
Switch kept the green hover colors and looked checked until something
else was tapped. Limit the hover styles to devices that can hover.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
langz

This comment was marked as outdated.

langz

This comment was marked as outdated.

langz

This comment was marked as outdated.

langz

This comment was marked as outdated.

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

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.
The inline hover guard raised the specificity of rules that were a
plain :hover on main. Hover then won over pressed, disabled and error
styles: a disabled Anchor turned teal on hover, a pressed Card action
kept its hover outline, and a Textarea with an error lost its red
border on hover. Wrap the guard in :where() so each rule keeps the
specificity it has on main.

@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 f7f8eba: Approve (posted as a comment, because GitHub does not let the PR author approve their own PR)

Since 95382fe, the hover rules moved from @media (hover: hover) to @media (any-hover: hover) plus a check that the tracked intent is not touch or keyboard, wrapped in :where() so the specificity matches main. I found no defects. Tested merged with main at 541b379: the merge is clean.

Suggestions (non-blocking)

  1. A mouse click on a form field, then a tap, can still leave the hover look (touchscreen laptops). setInput in whatInput.ts does not update the intent while an input, select or textarea has focus. A mouse click on a Switch leaves focus in its input. A tap on another Switch then sets data-whatinput="touch", but data-whatintent stays mouse, so the hover rule still matches. I reproduced this in Chromium with a pointer that can hover and real touch taps: the tapped Switch kept the hover colors, also after a second tap. With a 2 px finger movement during the tap, the intent becomes touch and the look is correct. So this needs a perfectly still tap, and main is sticky in every case anyway. It is not a regression. #11052, which just landed in main, does not change this: a jsdom probe on 541b379 still keeps the intent at mouse when an input has focus, and sets it to touch when the body has focus. The cause is shared with Button's hover mixin, so a fix belongs in whatInput.ts, for example by applying the form-field exception only to keyboard events.
  2. No test covers the touch behavior any more. 48b6619 removed Switch.e2e.spec.ts. The compiled-CSS snapshot is now the only guard: it fails when I restore the old SCSS, but it cannot show that a tap leaves the rest look.

What I checked

  • Delta 95382fe..f7f8eba in full. Specificity: html:where(...) adds nothing, so each rule keeps the (0,6,1) or (0,7,1) of main. The browserslist floor (Safari 14.1, Chrome 109, Firefox 115) supports :where(), :not() with a list, and any-hover.
  • Merge with main (ecaa2f5, then 541b379, which adds the whatInput.ts change from #11052): main + #11007 + #11008 + #11028 together pass the Switch, Checkbox, Radio, ToggleButton and whatInput tests, 198 of 198. On ecaa2f5 alone: Switch.test.tsx 33 of 33 and Toggle.test.tsx all pass (one forced-colors test timed out at load 27 and passed alone). Stylelint and Prettier pass. With only the SCSS reverted to the base, the style snapshot fails as expected.
  • Shipped CSS on the previews (main versus this head), all 6 theme bundles: the 4 Switch hover selectors sit inside @media (any-hover:hover) with the guard. After I strip the wrapper and the guard, all 58 Switch entries match main in content and order. Control: a wrong guard string reports a difference at the first hover rule.
  • Touch emulation on the previews, all 7 enabled Switches tapped twice each, in Chromium, Firefox and WebKit, at 390 px (mobile) and 1280 px (touch enabled): main keeps the hover look on 14 of 14 taps, this head on 0 of 14. The same holds in Chromium with a pointer that can hover (touchscreen laptop model) plus real touch taps.
  • After a tap: Tab keeps the hover look off. Moving the mouse over a Switch brings the hover look back.
  • Pressed look with a real mouse, all 7 enabled Switches, pressing on the control and on the label, in Chromium and Firefox: identical to main (42 of 42 states). The hover rules keep :not(:active), and their specificity equals main.
  • Mouse hover, in the same three browsers: the hover colors of all 7 Switches, and hover after a click, are identical to main. With JavaScript off (no data-whatintent), mouse hover still shows in Chromium and Firefox.
How I verified the touchscreen-laptop case

A script opens the Switch demos in a Chromium context without touch emulation, so (any-hover: hover) matches, and sends taps with CDP Input.dispatchTouchEvent. Scenario C clicks the second Switch with the mouse, moves the mouse away, taps the first Switch, and compares its look with the look after a tap on an empty spot. Result on this head: stickyAfterTap: true, intent: "mouse", input: "touch", active: "dnb-switch__input". If the mouse clicks the page background first (scenario C3), the tap sets the intent to touch and the look is correct. A second run repeats scenario C with a 2 px touchMove: the intent becomes touch and the background is the rest color.


CI on f7f8eba428: all 23 checks passed. Rechecked just before posting: the head is still f7f8eba.

This branch was successfully deployed

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