Skip to content

fix(Radio): prevent sticky hover styles after tapping on touch devices - #11009

Open
langz wants to merge 7 commits into
mainfrom
fix/radio-sticky-hover-touch
Open

langz wants to merge 7 commits into
mainfrom
fix/radio-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 Radio kept its hover styles after being tapped, until something else was tapped. The tapped Radio then looked different from other checked radios (light green background and a different dot color). It's the same issue as reported for Switch in #11007.

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 Radio group on a touch device after one tap on Second. Before: Second keeps the hover look (light green background, green ring and dot). After: it shows the normal checked look.

Touch browsers keep :hover on the last tapped element, so a tapped
Radio kept its hover colors and looked different from other checked
radios 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.
langz

This comment was marked as outdated.

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.
The :where() wrapper put the html element after the style scope class
in scoped builds (.eufemia-scope--portal :where(html...)), so these
hover rules never matched there. Restore the previous check until the
specificity fix also works with style scoping.

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

578330b reverts 7d2d575, so the tree is identical to bffa29e, the head of the last review. I checked the specificity question this leaves, and touch, mouse and pressed behavior in real browsers. I found no defects. Tested merged with main at 541b379: the merge is clean.

Earlier findings

  • Still deferred, as agreed: with a mouse, the pressed state of a Radio never shows. On this head and on main the pressed look equals the hover look in 40 of 40 presses (Chromium). #11028 fixes it.

Suggestions (non-blocking)

  1. A mouse click on a form field, then a tap, can still leave the hover look (touchscreen laptops). The same whatInput.ts limitation as in #11007: while a form input has focus, a tap does not update data-whatintent. In Chromium with a pointer that can hover and real touch taps, a Radio tapped after a mouse click on another Radio kept the hover look (intent: "mouse"). main is sticky in all cases, so this is not a regression.

What I checked

  • git diff bffa29e 578330b is empty, so the delta since the last review is zero.
  • Specificity: the hover rules go from (0,4,0) on main to (0,5,1). In all 6 shipped theme bundles, 4 of the 54 other rules on .dnb-radio__button and __dot change winner. All 4 are disabled rules, which cannot match together with :not([disabled]).
  • Touch emulation, all 20 enabled Radios tapped twice each, in Chromium, Firefox and WebKit, at 390 px (mobile) and 1280 px (touch enabled): main keeps the hover look on 40 of 40 taps, this PR on 0 of 40. A Tab press after a tap keeps the hover look off; on main it stays. The touchscreen-laptop model in Chromium also gives 0 of 40.
  • Mouse hover in the same three browsers: the hover look of all 20 Radios, and hover after a click, are identical to main. With JavaScript off, mouse hover still shows (Chromium, Firefox). Pressed looks equal main in Chromium and Firefox (120 of 120 states).
  • Merge with main (ecaa2f5, then 541b379, which adds the whatInput.ts change from #11052): main + #11007 + #11008 + #11028 (which contains the #11009 changes) together pass the Switch, Checkbox, Radio, ToggleButton and whatInput tests, 198 of 198. On ecaa2f5 alone: the Radio and ToggleButton tests pass 104 of 104. Stylelint and Prettier pass. CI's visual job ran 13 impacted screenshot files on this head, and they passed.

CI on 578330beab: all 23 checks passed. Rechecked just before posting: the head is still 578330b.

This branch was successfully deployed

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