Skip to content

fix(Anchor): prevent sticky link hover styles after tapping on touch devices - #11044

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

langz wants to merge 7 commits into
mainfrom
fix/anchor-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 tapped link kept its hover style until something else was tapped. It's the same issue as reported for Switch in #11007. It shows mostly on links that keep the user on the page:

  • An Anchor kept its light green hover background.
  • A TableOfContent link kept its darker hover text color, and the marker of the current section kept its hover border.
  • A TermDefinition trigger kept the green text and lost its dotted underline after its popover was closed with a second tap.

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 link in a paragraph on a touch device after one tap. Before: it keeps the hover background. After: it shows the normal link look.

A TermDefinition term on a touch device after one tap. Before: it keeps the green hover text color. After: it shows the normal look.

A TableOfContent on a touch device after one tap on Choose a goal. Before: the link keeps the darker hover color. After: it shows the normal color.

Touch browsers keep :hover on the last tapped element, so a tapped link
kept its hover background until something else was tapped. That shows
on links that keep the user on the page, such as TableOfContent links
and the TermDefinition trigger. Limit the Anchor and TableOfContent
hover styles to devices that can hover.

The TermDefinition trigger shows its dotted underline and the text
color only while it isn't hovered. On touch devices, style a tapped
trigger like one without the mouse over it.
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.

Rules that make a tapped element look as at rest now apply exactly
when hover styles are off: on devices that cannot hover, and while the
tracked input is touch or keyboard. Before, they also applied when no
input had been tracked yet, for example without JavaScript, and then
cancelled part of the hover style.
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 d67e30c: Changes needed

The PR hides Anchor, TableOfContent and TermDefinition hover styles after a tap or a key press. The touch fix works. But the last commit (d67e30c, "Restore the html hover check") brings back the specificity problem that da0bb4a had fixed, so pressed and disabled styles now lose to hover for mouse users. Tested merged with main at ecaa2f5.

Must fix

  1. Hover now wins over pressed, disabled and the sidebar look for mouse users. anchor-mixins.scss:159-161 and dnb-table-of-content.scss:113, :166 and :202 prefix the hover rules with html:not([data-whatintent='touch'], [data-whatintent='keyboard']). That raises the hover rule from (0,2,0) on main to (0,3,1). It now beats every same-element rule of (0,2,0) to (0,4,0) that used to win by coming later:

    • A pressed Anchor keeps its hover look. :active no longer changes the text color or background. This also hits the TermDefinition trigger.
    • A hovered disabled Anchor (.dnb-anchor--disabled, aria-disabled, disabled) turns teal instead of staying grey.
    • A sidebar TableOfContent link gets an underline on hover and press. On main, --sidebar sets text-decoration: none for it.
    • A pressed sidebar TableOfContent link keeps the darker hover color instead of the pressed neutral-alternative color.

    I measured computed styles with real mouse hover and press in Chromium, Firefox and WebKit. I used CSS compiled from each commit with the UI theme. Every case above matches main on origin/main and on da0bb4a, and is wrong on d67e30c. The commit message of da0bb4a lists the same symptoms. CI agrees: the e2e job fails tableOfContent.spec.ts:724 ("matches the compact desktop rows and plain hover styling") in Firefox, where it expects text-decoration-line: none on a hovered sidebar link and gets underline.

    The :where() wrapper in da0bb4a cannot come back as it was. I ran the repo's postcss-isolated-style-scope plugin on it, and it puts the scope class first (.eufemia-scope--portal :where(html…) .dnb-anchor:hover). So that rule never matches, as d67e30c says. On the da0bb4a preview, mouse hover on an Anchor shows no background at all.

    Suggested fix: put the guard on the element, with zero specificity. SidebarMenu already does this on main (dnb-sidebar-menu.scss:215): &:hover:not(:where(html[data-whatintent='touch'] *, html[data-whatintent='keyboard'] *)). The scope plugin leaves it working (.eufemia-scope--portal .dnb-anchor:hover:not(:where(html[…] *))). I built that variant for the Anchor. Pressed and disabled then match main in all three engines, and touch and keyboard intent still hide hover. The three TableOfContent rules need the same change.

How I verified this

I compiled dnb-ui-core, ui-theme-components, ui-theme-extensions and ui-theme-basis from the package source at each commit, and served them on a test page with the DNB fonts (I checked that the fonts loaded). A Playwright script sets data-whatintent, then reads the computed styles at rest, on page.hover() and after mouse.down().

Firefox, data-whatintent="mouse":

css-main   a-text     ACTIVE color=rgb(20, 85, 90)  shadow=rgb(188, 232, 220)   (pressed)
css-d67e30 a-text     ACTIVE color=rgb(0, 114, 114) shadow=rgb(216, 243, 236)   (still hover)
css-main   a-disabled HOVER  color=rgb(204, 204, 204)
css-d67e30 a-disabled HOVER  color=rgb(0, 114, 114)
css-da0bb4 a-text     ACTIVE color=rgb(20, 85, 90)  (control: the :where() version was right)

Sidebar TableOfContent link, the same in all three engines:

css-main   HOVER deco=none       ACTIVE color=rgb(112, 112, 112) deco=none
css-d67e30 HOVER deco=underline  ACTIVE color=rgb(51, 51, 51)    deco=underline

With the zero-specificity variant, pressed is rgb(20, 85, 90), and disabled on hover stays rgb(204, 204, 204). With touch and keyboard intent, hover shows shadow=none.

Scope plugin output for the three selector shapes:

html:not([data-whatintent=touch], [data-whatintent=keyboard]) .eufemia-scope--portal .dnb-anchor:hover
.eufemia-scope--portal :where(html:not([data-whatintent=touch], [data-whatintent=keyboard])) .dnb-anchor:hover
.eufemia-scope--portal .dnb-anchor:hover:not(:where(html[data-whatintent=touch] *, html[data-whatintent=keyboard] *))

Suggestions (non-blocking)

  1. The TermDefinition reset (dnb-term-definition.scss:27 and :31) uses html:not([data-whatintent='mouse']), but the Anchor guard uses not touch, not keyboard. While the intent is initial, or before input tracking starts, a hovered trigger gets the Anchor hover background together with the at-rest color and the dotted underline. da0bb4a had aligned the two. The same zero-specificity pattern on the element would align them again. This is rare in practice, because the first mouse move sets mouse.
  2. The @supports not (selector(*:where(*))) fallback inside notNoHover is now nested under a selector that needs :not() with a selector list. Browsers without :where() also lack that, so the fallback rule cannot match any more. It is harmless, but it is now dead code inside this rule.

What I checked

  • Touch: with hasTouch in Chromium and WebKit, a tap sets data-whatintent="touch" on the da0bb4a preview. The tapped Anchor shows no hover background there, while main keeps it. The touch path is the same in d67e30c. Locally, touch and keyboard intent remove the hover background on d67e30c for text, disabled, dark-surface, button.dnb-anchor and --no-hover Anchors in Chromium.
  • Dark surface: the hover and pressed backgrounds on main and on the PR use the right ondark tokens. Pressed has the same specificity problem as above.
  • Trial merge with ecaa2f5 (a real git merge --no-commit): no conflicts. On the merge with 53fa3bd, Anchor.test.tsx, TermDefinition.test.tsx, TableOfContent.test.tsx and TableOfContent.style.test.ts pass: 133 tests. One style test timed out at a load average of 83, then passed alone.
  • stylelint and prettier --check on the 3 SCSS files: clean.

Mouse states in Firefox. Before (main): the pressed link is darker, the disabled link stays grey, and the sidebar link has no underline. After (this PR, d67e30c): the pressed link keeps the hover look, the disabled link turns teal, and the sidebar link is underlined.


CI on d67e30ce2c when I posted this: 21 checks passed; Run e2e tests failure; 1 still running (Run visual-regression tests). The e2e job fails the TableOfContent test "matches the compact desktop rows and plain hover styling" in Firefox: a hovered sidebar link is underlined, because the new hover prefix outranks the sidebar's text-decoration: none (must-fix 1). Rechecked just before posting: the head is still d67e30c, 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 — d67e30ce Deployed Nov 24, 2026 by github-actions[bot]
Portal preview — d67e30ce 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