Skip to content

docs: explain how to keep hover styles from sticking on touch devices - #11027

Open
langz wants to merge 9 commits into
mainfrom
docs/hover-styles-touch-devices
Open

langz wants to merge 9 commits into
mainfrom
docs/hover-styles-touch-devices

Conversation

@langz

@langz langz commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Motivation: https://dnb-it.slack.com/archives/CMXABCHEY/p1795427065802309

Touch browsers can keep hover styles after a tap, making controls look checked or selected. Explain how to use the shared hover mixin and current input tracking, while keeping pressed and keyboard focus feedback.

A hover media query describes pointer capability and can stay true during touchscreen use on a laptop. The guide explains why it does not replace input tracking when the user switches between touch, keyboard and mouse.

Use together with #11053 for the shared hover behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
langz

This comment was marked as outdated.

… hover query

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
langz

This comment was marked as outdated.

langz

This comment was marked as outdated.

… 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.
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.

tujoworker and others added 3 commits November 24, 2026 14:19
@media (hover: hover) describes the primary pointer, so it doesn't prevent sticky hover on devices with both a touchscreen and a mouse or touchpad. Recommend the hover mixin and the data-whatintent check that the components use, and show how to keep focus styles visible after a tap.
The previous commit replaced a more complete version of this section.
The mixin now applies hover styles inside @media (any-hover: hover) unless the current input is touch or keyboard. Explain why any-hover is used instead of hover, and what happens before the input tracking starts.
langz

This comment was marked as outdated.

Recommend :where() around the hover check for custom selectors, so pressed, disabled and error styles still win over hover. Add a bullet for resting styles that exclude :hover: they must apply exactly where hover styles are off.
The :where(html...) form breaks in scoped builds, where the scope class ends up before the html element. Restore the previous text until a form that keeps the specificity 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 fabd9cd: Changes needed

Since the last review (3e65085), 866f395 changed the custom-selector advice to a :where() form, and fabd9cd restored it. The tree of fabd9cd equals 3e65085, so the text is the one reviewed then. Looking at it again, the custom-selector advice leads to the pressed-state regression that #11010 and #11025 now have. Tested merged with main at ecaa2f5.

Must fix

  1. The custom-selector advice hides the pressed style. for-styling.mdx:114 says to use html:not([data-whatintent='touch'], [data-whatintent='keyboard']) &:hover for custom selectors. That prefix adds 0,1,1 to the selector. A typical &:active rule after it then loses, so while the mouse button is down the element keeps its hover look. The :not(:active) bullet only speaks about the mixin and says "when needed", so a reader who follows this paragraph does not see that it is needed here.
    I tested it in a page inside .eufemia-scope--portal, with a hover rule and a later :active rule. In Chromium, Firefox and WebKit, the pressed color wins with a plain :hover. With the documented guard, the hover color wins while the button is down. With the documented guard plus :not(:active), the pressed color wins again. The same pattern breaks the pressed look on 79 of 81 Chips (#11010) and 27 of 27 MultiSelection rows (#11025) in Chromium.
    Suggested fix: say that the guard raises specificity, and add :not(:active) to the documented selector: html:not([data-whatintent='touch'], [data-whatintent='keyboard']) &:hover:not(:active). A zero-specificity form that works with style scoping is &:hover:where(html:not([data-whatintent='touch'], [data-whatintent='keyboard']) *). The :where(html...) & form in 866f395 never matched in scoped builds, as 1d709d1 on #11010 notes.

Suggestions (non-blocking)

  1. Merge after #11053. On main, utilities.hover() is html:not([data-whatintent='touch']) &:hover, with no any-hover query and no keyboard check. So "shows hover styles only on devices with a pointer that can hover, and not while the current input is touch or keyboard" is true only with #11053.
  2. The design tokens guide still shows unguarded &:hover in its examples (design-tokens/guide.mdx:97 and :162, and design-tokens/info.mdx:53). Readers who copy them get the sticky hover. A link to #hover-styles would help. This can be a follow-up.
  3. "A tap hides hover styles" has one exception. The input helper treats a mousedown as touch only within 200 ms of the touch. In Chromium with touch emulation, a tap held for 350 ms set data-whatintent="mouse". On a device with a touchscreen and a mouse or trackpad, a slow tap can then bring the hover back. True but rare; the fix belongs in the helper, not in this text.

What I checked

  • git diff 3e65085 fabd9cd is empty. Real trial merge with main ecaa2f5: no conflicts. Prettier passes on the file.
  • Each statement against the source on #11053 (29f8a22, the live head): the mixin wraps the rule in @media (any-hover: hover) and checks html:not([data-whatintent='touch'], [data-whatintent='keyboard']). Before the tracking starts there is no attribute, so the media query alone decides. whatInput.ts keeps the intent when a key is pressed in an input, select or textarea. Button uses utilities.hover() on main.
  • The hover and any-hover claims: an iPad with a trackpad reports hover: none and any-hover: hover (see the Tailwind CSS issue #11109). I could not test real hardware.
  • The SCSS example compiles with the #11053 mixin. The hover rule ends in :not([disabled]):not(:active), so the pressed style from utilities.active() wins.
  • The focus-style bullet matches the #11025 rule html:not([data-whatintent='mouse'])[data-whatinput='keyboard'] &:not(:active):has(...). With touch emulation, a tapped row shows its focus frame again after Tab and Shift+Tab, in all three engines.
  • The branch preview (pr-11027) renders the section with the restored paragraph. The PR changes no library code, so the docs type fits.

CI on fabd9cd2cf: all 23 checks passed. Rechecked just before posting: the head is still fabd9cd, and it merges into main at ecaa2f5 without conflicts.

This branch was successfully deployed

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