Repository navigation
Conversation
Touch browsers keep :hover on the last tapped element, so an unchecked Switch kept the green hover colors and looked checked until something else was tapped. Limit the hover styles to devices that can hover. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Why This is the industry standard: Tailwind CSS v4 applies its The |
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.
langz
left a comment
There was a problem hiding this comment.
Review of f7f8eba: Approve (posted as a comment, because GitHub does not let the PR author approve their own PR)
Since 95382fe, the hover rules moved from @media (hover: hover) to @media (any-hover: hover) plus a check that the tracked intent is not touch or keyboard, wrapped in :where() so the specificity matches main. I found no defects. Tested merged with main at 541b379: the merge is clean.
Suggestions (non-blocking)
- A mouse click on a form field, then a tap, can still leave the hover look (touchscreen laptops).
setInputinwhatInput.tsdoes not update the intent while aninput,selectortextareahas focus. A mouse click on a Switch leaves focus in its input. A tap on another Switch then setsdata-whatinput="touch", butdata-whatintentstaysmouse, so the hover rule still matches. I reproduced this in Chromium with a pointer that can hover and real touch taps: the tapped Switch kept the hover colors, also after a second tap. With a 2 px finger movement during the tap, the intent becomestouchand the look is correct. So this needs a perfectly still tap, andmainis sticky in every case anyway. It is not a regression. #11052, which just landed inmain, does not change this: a jsdom probe on541b379still keeps the intent atmousewhen an input has focus, and sets it totouchwhen the body has focus. The cause is shared with Button's hover mixin, so a fix belongs inwhatInput.ts, for example by applying the form-field exception only to keyboard events. - No test covers the touch behavior any more.
48b6619removedSwitch.e2e.spec.ts. The compiled-CSS snapshot is now the only guard: it fails when I restore the old SCSS, but it cannot show that a tap leaves the rest look.
What I checked
- Delta
95382fe..f7f8ebain full. Specificity:html:where(...)adds nothing, so each rule keeps the (0,6,1) or (0,7,1) ofmain. The browserslist floor (Safari 14.1, Chrome 109, Firefox 115) supports:where(),:not()with a list, andany-hover. - Merge with
main(ecaa2f5, then541b379, which adds thewhatInput.tschange from #11052): main + #11007 + #11008 + #11028 together pass the Switch, Checkbox, Radio, ToggleButton andwhatInputtests, 198 of 198. Onecaa2f5alone:Switch.test.tsx33 of 33 andToggle.test.tsxall pass (one forced-colors test timed out at load 27 and passed alone). Stylelint and Prettier pass. With only the SCSS reverted to the base, the style snapshot fails as expected. - Shipped CSS on the previews (
mainversus this head), all 6 theme bundles: the 4 Switch hover selectors sit inside@media (any-hover:hover)with the guard. After I strip the wrapper and the guard, all 58 Switch entries matchmainin content and order. Control: a wrong guard string reports a difference at the first hover rule. - Touch emulation on the previews, all 7 enabled Switches tapped twice each, in Chromium, Firefox and WebKit, at 390 px (mobile) and 1280 px (touch enabled):
mainkeeps the hover look on 14 of 14 taps, this head on 0 of 14. The same holds in Chromium with a pointer that can hover (touchscreen laptop model) plus real touch taps. - After a tap: Tab keeps the hover look off. Moving the mouse over a Switch brings the hover look back.
- Pressed look with a real mouse, all 7 enabled Switches, pressing on the control and on the label, in Chromium and Firefox: identical to
main(42 of 42 states). The hover rules keep:not(:active), and their specificity equalsmain. - Mouse hover, in the same three browsers: the hover colors of all 7 Switches, and hover after a click, are identical to
main. With JavaScript off (nodata-whatintent), mouse hover still shows in Chromium and Firefox.
How I verified the touchscreen-laptop case
A script opens the Switch demos in a Chromium context without touch emulation, so (any-hover: hover) matches, and sends taps with CDP Input.dispatchTouchEvent. Scenario C clicks the second Switch with the mouse, moves the mouse away, taps the first Switch, and compares its look with the look after a tap on an empty spot. Result on this head: stickyAfterTap: true, intent: "mouse", input: "touch", active: "dnb-switch__input". If the mouse clicks the page background first (scenario C3), the tap sets the intent to touch and the look is correct. A second run repeats scenario C with a 2 px touchMove: the intent becomes touch and the background is the rest color.
CI on f7f8eba428: all 23 checks passed. Rechecked just before posting: the head is still f7f8eba.
Motivation: https://dnb-it.slack.com/archives/CMXABCHEY/p1795427065802309
On iOS Safari, and in other touch browsers, a Switch kept its hover styles after being tapped, until something else was tapped. Because the hover styles use the same green colors as the checked state, an unchecked Switch looked checked, which was reported as confusing.
Hover now follows the current input, using the same
data-whatintenttracking 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