Skip to content

fix(Chip): prevent sticky hover styles after tapping on touch devices - #11010

Open
langz wants to merge 8 commits into
mainfrom
fix/chip-sticky-hover-touch
Open

langz wants to merge 8 commits into
mainfrom
fix/chip-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 Chip kept its hover styles (green ring and indicator colors) after being tapped, until something else was tapped. 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.

Builds on #11008: the checkbox indicator inside a Chip also gets the Checkbox hover styles, so this branch includes that commit. Merge #11008 first. This diff then only contains the Chip changes.

Use together with #11053 for the shared hover behavior.

Before / after

A default Chip on a touch device after one tap that selects it. Before: it keeps the hover look (green border). After: it shows the normal selected look.

A Chip with a checkbox indicator on a touch device after one tap that selects it. Before: it keeps the hover look (green border and green indicator). After: it shows the normal selected look.

langz and others added 2 commits November 23, 2026 10:59
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>
Touch browsers keep :hover on the last tapped element, so a tapped Chip
kept its hover ring and indicator colors 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 e361b1c: Changes needed

Since the last review (d43b146), the branch added 1d709d1 (wrap the check in :where()) and then e361b1c (restore the html:not(...) check). The tree of e361b1c is identical to d43b146, so nothing changed in the code. But that also restores the specificity problem that 1d709d1 set out to fix, and for Chip it breaks the pressed state. Tested merged with main at 53fa3bd.

Must fix

  1. Hover now wins over the pressed state, so a Chip never shows its pressed look with a mouse. The html:not([data-whatintent='touch'], [data-whatintent='keyboard']) prefix adds 0,1,1 to every Chip hover rule. On main, the hover and the pressed rules have the same specificity, and the later pressed rule wins. Now the hover rule wins while the mouse button is down. This affects chipState(':not(:disabled):hover', ...) against chipState(':not(:disabled):active', ...) (dnb-chip.scss:109 and :153), the selected pair (:137 and :176), the error button pair (:293 and :311) and the error checkbox and radio pair (:429 and :439). The Checkbox rules are not affected, because they already use :hover:not(:active).
    I pressed every enabled Chip on the demo page with a real mouse (hover, then mouse button down, then release outside the Chip), on the main preview and on the branch preview 382744cb, which is a build of the same tree as e361b1c. Hover looks the same on 81 of 81 Chips in every engine. The pressed look differs on 79 of 81 Chips in Chromium and on 76 of 81 in WebKit. In Firefox it differs on the 5 addable (button) Chips. Firefox does not give the input :active when you press its label, so checkbox and radio Chips have no pressed look in Firefox on main either. Control: when I remove only the html:not(...) prefix from the loaded CSS on the branch preview, the pressed look equals main on 81 of 81 Chips in Chromium.
    Suggested fix: keep the check at zero specificity, but put it inside the compound selector, so the style scope plugin leaves it alone. For example &:hover:where(html:not([data-whatintent='touch'], [data-whatintent='keyboard']) *). I ran the scope plugin on this form. It produces .eufemia-scope--portal .dnb-chip__button:hover:where(html:not(...) *). In a test page inside .eufemia-scope--portal in Chromium, Firefox and WebKit, that rule matches with the intent mouse or with no intent, and it does not match with touch or keyboard. A later :active rule still wins. The :where(html...) & form from 1d709d1 never matched there, as the commit message says.

Earlier findings

  • Still true: the branch carries the Checkbox change from #11008. Its Checkbox and ToggleButton files are identical to the #11008 head c0f4aca, so merge #11008 first.

Suggestions (non-blocking)

  1. A tap that is held for more than 200 ms sets the intent to mouse. The intent helper ignores a mousedown only if it comes within 200 ms of the touch (validateTouch in whatInput.ts). Browsers send the compatibility mousedown after the finger lifts. In Chromium with touch emulation, a tap held for 80 ms left data-whatintent="touch", and a tap held for 350 ms left data-whatintent="mouse" with the input still in :hover. On a phone this does not matter, because (any-hover: hover) does not match. On a device that also has a mouse or trackpad (a touchscreen laptop, an iPad with a trackpad), a slow tap brings the sticky hover back. I inferred that last step; my emulation cannot make any-hover match together with touch. This lives in the shared helper, so it may fit better in #11053. True but rare.

What I checked

  • Range-diff d43b146..e361b1c: two new commits. git diff d43b146 e361b1c is empty.
  • Merge with main 53fa3bd: no conflicts, yarn.lock unchanged. The Chip folder, Checkbox.test.tsx and ToggleButton.test.tsx pass 149 of 149. Two tests timed out under a load average of 74 and passed on a rerun with a longer timeout. Stylelint and Prettier pass on both style files.
  • Mouse hover, pressed and rest styles of all 81 enabled demo Chips, compared with main, in Chromium, Firefox and WebKit (see above). The rest look matches main, apart from one Chip in Chromium. That one also differs on the control run without the prefix, so the prefix does not cause it.
  • CI on e361b1c: the visual job fails on exactly the 12 Chip active screenshots (matches active states, matches error active states and matches dark error 'active' states, in ui, sbanken, coconut and carnegie). The other 761 screenshots in the run pass, including every Chip hover screenshot. This is the same defect as must-fix 1.

Chip pressed state with a mouse, Before (main) and After (this PR), in desktop Chromium. After, the hover ring stays while the mouse button is down


CI on e361b1c364 when I posted this: 23 checks passed; Run visual-regression tests failure. Run visual-regression tests fails on the 12 Chip active-state screenshots (4 themes x 3 tests). They show the hover look while pressed, which is the must-fix in this review.

This branch was successfully deployed

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