Skip to content

fix(Card): prevent sticky Card.Action hover outline after tapping on touch devices - #11043

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

langz wants to merge 8 commits into
mainfrom
fix/card-action-sticky-hover-touch

Conversation

@langz

@langz langz commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

On iOS Safari, and in other touch browsers, a Card.Action kept its green hover outline after being tapped, until something else was tapped. It's the same issue as reported for Switch in #11007 and for List in #11042.

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.

The keyboard focus outline excludes a hovered card. A tapped card keeps :hover on touch devices, so when the user came back to it with the keyboard (for example on a tablet with a keyboard), it showed the green hover outline instead of the blue focus outline. When the current intent is keyboard, the focus outline now shows on a tapped card too.

Use together with #11053 for the shared hover behavior.

Before / after

A clickable Card on a touch device after one tap. Before: it keeps the green hover outline. After: it shows the normal border.

A clickable Card on a touch device after a tap, then Tab away and Shift+Tab back. Before: the green hover outline shows instead of the focus outline. After: the blue focus outline shows.

Touch browsers keep :hover on the last tapped element, so a tapped
Card.Action kept its green hover outline until something else was
tapped. Limit the hover outline to devices that can hover.

The keyboard focus outline excluded a hovered card. On touch devices a
tapped card keeps :hover, so it showed the hover outline instead of the
focus outline when the user returned to it with the keyboard. Show the
focus outline there too.
langz added a commit that referenced this pull request Nov 24, 2026
… devices

A tapped element keeps :hover on touch devices, so a focus style with :not(:hover) does not show when the user comes back to it with the keyboard. Add a bullet that tells how to avoid that, as done in #11025, #11042, #11043, #11048 and #11049.
Firefox does not show focus-visible on a focus() call after a tap, so the test failed there although the focus outline works with the keyboard. Move focus with Tab and Shift+Tab, as a keyboard user does.
langz

This comment was marked as outdated.

@joakbjerk joakbjerk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The (hover: none) focus rule keeps keyboard focus visible on a tapped card, and matches the pattern #11053 uses for Tabs.

@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.
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 6e2fc0c: Changes needed

The Card.Action hover outline now applies only inside @media (any-hover: hover) and while the tracked intent is not touch or keyboard. A tapped card now shows the focus outline when focus comes back to it. The touch fix works. The restored html:not(…) prefix removes the pressed look, and Run visual-regression tests fails on it. Tested merged with main at ecaa2f5 (clean merge, including #10972's Card changes).

Must fix

  1. A pressed Card.Action keeps the hover outline, and the visual test fails. The prefix adds (0,1,1) to the hover rule (dnb-card.scss:288-296). It is now (0,4,1). The &:active > .dnb-card rule below it (:298-302) is still (0,3,0). On main the two had the same specificity, so the later pressed rule won while the mouse button was down. Now hover wins: a pressed card shows the 2px hover outline instead of the 1px pressed outline. Commit e696269 fixed exactly this, and 6e2fc0c brought it back when it restored the html check.
    CI fails "Interactive Card for ui > have to match active state" in Firefox after 5 retries (1,628 px differ, 1.262%). I verified it on the previews of main (d85af17f) and this head (the pr-11043 alias, which serves the html:not(…) form of the rule). I pressed the mouse on the 4 Card.Action demos. On main, 0 of 4 kept the hover look (--outline-width: .0625rem, #14555a). On this head, 4 of 4 did, in Chromium and in Firefox (.125rem, #007272). Control: when I remove only the prefix from the loaded CSS, 0 of 4 do.
    One way to fix it, not verified by me: exclude the pressed state from the hover rule (&:hover:not(:active) > .dnb-card). Another is to keep :where() once the scope plugin puts the scope after :where(html…).

Before (main) and after (this PR): a Card.Action while the mouse button is down

Earlier findings

  • Still holds: the earlier review on 7ceec54 said that after a tap, Tab and Shift+Tab the card shows the blue 3px focus outline. On this head, it shows --outline-width: .1875rem and #276ace, both on a phone and on a touchscreen laptop. On main it shows the 2px green hover outline.

What I checked

  • Phone (Chromium, isMobile + hasTouch): after a tap, the card shows the rest outline (.0625rem, #ebebeb). On main it keeps the hover outline (.125rem, #007272).
  • Touchscreen laptop (desktop Chromium, where (any-hover: hover) matches, plus CDP touch input): the same results as on the phone.
  • Mouse hover: the same as main in Chromium and Firefox.
  • Unit tests on the merge with main (ecaa2f5): all Card test files, 113 of 113 passed.
  • The visual job had one more failure on the first attempt: SidebarMenu for sbanken ("keyboard-focused section option under the mouse pointer"). It passed on retry, and the same failure shows in CI logs of four other PRs today, so it is not this PR. The tree of 6e2fc0c is identical to 42e35ab.
  • Not covered: WebKit and real iOS Safari.

CI on 6e2fc0c5e9 when I posted this: 23 checks passed; Run visual-regression tests failure. Run visual-regression tests fails 'Interactive Card for ui > have to match active state': the restored hover prefix makes the hover outline win over the pressed outline. The SidebarMenu sbanken screenshot in the same job failed once and passed on retry; it is unrelated. Rechecked just before posting: the head is still 6e2fc0c, and a real trial merge with main at 541b379 (which now includes #11052) has no conflicts.

This branch was successfully deployed

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

3 participants