Skip to content

fix(Checkbox): prevent sticky hover styles after tapping on touch devices - #11008

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

langz wants to merge 7 commits into
mainfrom
fix/checkbox-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 Checkbox kept its hover styles (green border and background) after being tapped, until something else was tapped. It's the same issue as reported for Switch in #11007, so the tapped Checkbox looked different from the others in the same state.

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 Checkbox on a touch device after one tap that checks it. Before: it keeps the hover look (lighter green fill and thicker border). After: it shows the normal checked look.

Touch browsers keep :hover on the last tapped element, so a Checkbox
kept its hover colors after being tapped 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 c0f4aca: Approve (posted as a comment, because GitHub does not let the PR author approve their own PR)

c0f4aca reverts ffd2ef7, so the tree is identical to 785e50d, the head of the last review. I checked the one open question this leaves: the guard raises the hover rules from (0,5,0) to (0,6,1), and I found no state where that changes the look. Tested merged with main at 541b379: the merge is clean.

Earlier findings

  • Still deferred, as agreed: the Chip ring after a tap in Field.Toggle comes from the Chip styles and is handled in #11010.

Suggestions (non-blocking)

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

What I checked

  • git diff 785e50d c0f4aca is empty, so the delta since the last review is zero.
  • Specificity. I listed every rule in all 6 shipped theme bundles that targets .dnb-checkbox__button, __gfx or __indeterminate and sets a property that a hover rule also sets: 92 rules against 7 hover rules. 17 pairs change winner because of the higher specificity. In 15 of them, the two rules cannot match at the same time (:active against :not(:active), disabled against :not([disabled]), or ToggleButton, whose inner input has pointer-events: none). The other 2 need an indeterminate Chip that has keyboard focus while the mouse hovers it and the tracked intent is not keyboard. I did not test that combination in a browser, and I consider it negligible.
  • Pressed look with a real mouse, on all 18 enabled Checkbox demos, pressing on the box and on the label, in Chromium and Firefox: identical to main (108 of 108 states). Control: when I remove only :not(:active) from the loaded hover rules, the pressed look becomes the hover look in 36 of 36 presses, and the check reports all of them.
  • Touch emulation, every enabled Checkbox tapped twice, in Chromium, Firefox and WebKit, at 390 px (mobile) and 1280 px (touch enabled): main keeps the hover look on 36 of 36 taps, this PR on 0 of 36. A Tab press after a tap keeps the hover look off; on main it stays. The touchscreen-laptop model in Chromium gives 0 of 36 as well.
  • Mouse hover in the same three browsers: the hover look of all 18 Checkboxes, and hover after a click, are identical to main. With JavaScript off, mouse hover still shows (Chromium, Firefox).
  • CI gap: the visual-regression job has not run on this tree. It was cancelled in the queue, with no runner and no steps, on 785e50d, ffd2ef7 and c0f4aca. So CI has not compared the Checkbox hover screenshots for the new guard. My browser checks above cover the computed hover look, but a re-run of that job should pass before merge.
  • 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: Checkbox.test.tsx, ToggleButton.test.tsx and the Checkbox style tests pass 94 of 94. Stylelint and Prettier pass. The shipped CSS keeps html in front of the scope class (html:not(...) .eufemia-scope--portal .dnb-checkbox__input...), so the hover rules match in scoped builds.

CI on c0f4aca2a6 when I posted this: 22 checks passed; Run visual-regression tests cancelled. The visual-regression job was cancelled while queued (no runner, no steps) on this head and on the two earlier heads with the same Checkbox styles, so it never compared the Checkbox screenshots. It needs a re-run; it is not a failure of this change. Rechecked just before posting: the head is still c0f4aca.

This branch was successfully deployed

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