Skip to content

fix(Radio): show the pressed state while the mouse button is held down - #11028

Open
langz wants to merge 8 commits into
mainfrom
fix/radio-pressed-state
Open

langz wants to merge 8 commits into
mainfrom
fix/radio-pressed-state

Conversation

@langz

@langz langz commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

A Radio never showed its pressed state. While the mouse button was held down it kept its hover colors, unlike Checkbox and Switch, even though the pressed colors are defined. The hover rule (:not([disabled]):hover) was more specific than the pressed rule (:active), so hover always won.

The hover rules now exclude :active, like Checkbox and Switch do. New visual tests cover the pressed state of unchecked and checked radios.

Builds on #11009, which changes the same rules, so this branch includes that commit. Merge #11009 first. This diff then only contains the pressed state changes.

Use together with #11053 for the shared hover behavior.

Before / after

A radio held down with the mouse button, unchecked and checked, in the DNB ui theme. Before: it keeps the hover colors. After: it shows the pressed colors.

Hover follows the current input, like Button. A tap hides hover styling, and using the mouse brings it back, including on devices with both a mouse and touchscreen.

langz and others added 2 commits November 23, 2026 11:01
Touch browsers keep :hover on the last tapped element, so a tapped
Radio kept its hover colors and looked different from other checked
radios until something else was tapped. Limit the hover styles to
devices that can hover.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The hover rule was more specific than the pressed rule, so a pressed
Radio kept its hover colors. Exclude :active from the hover rules, as
Checkbox and Switch do, so the pressed styles apply.

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

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 8845e48: Approve (posted as a comment, because GitHub does not let the PR author approve their own PR)

Since fcc5dac, the branch took over the #11009 changes (any-hover and the intent check). Its own change on top of #11009 is still only :not(:active) on the two hover rules plus the six pressed screenshots. I found no defects. Tested merged with main at 541b379: the merge is clean.

Suggestions (non-blocking)

  1. The same touchscreen-laptop limitation as #11009. After a mouse click on a form field, a tap does not update data-whatintent (whatInput.ts), so the tapped Radio can keep its hover look. It is not a regression, and the fix belongs in whatInput.ts.

What I checked

  • Delta: 8845e48 reverts 32cc364, so the tree equals fcc20da. git diff 578330b 8845e48 (the current #11009 head) changes only the two hover selectors, the two style snapshots and the six new screenshots. The screenshots and the screenshot test are unchanged since fcc5dac.
  • Stack: I squash-merged #11009 into main ecaa2f5. A plain merge of this branch on top then conflicts in 3 files (the SCSS and two snapshots), because both branches rewrote the same lines. The PR's own patch applies cleanly there, and the Radio and ToggleButton tests pass 104 of 104. So the rebase after #11009, as the description says, is needed and is simple.
  • Pressed look with a real held mouse button, all 20 enabled Radios on the demos page, pressing on the control and on the label: Chromium and WebKit show the pressed look in 40 of 40 presses, Firefox in 21 of 21 presses where it sets :active. On main, all of them keep the hover look. Rest and hover looks equal main in all three browsers (80 of 80 each). Control: when I remove only :not(:active) from the loaded hover rules, Chromium matches main again in 120 of 120 states.
  • Checked pressed: the dot and ring use --token-color-background-action-pressed and --token-color-stroke-action-pressed (ui: #14555a on #bce8dc). These tokens resolve in all 6 theme bundles, with eiendom using the ui values.
  • Error radios: pressed uses the existing error --active values from main (dnb-radio.scss:412). These equal the rest look, so the 2 px hover ring drops to the 1 px error border while the button is down. That is what the existing tokens define, not a change in this PR.
  • Touch emulation in Chromium, every enabled Radio tapped twice: 0 of 40 taps keep the hover look on this head, 40 of 40 on main, the same as #11009. ToggleButton is unaffected, because its inner radio has pointer-events: none.
  • Merge with main 541b379, which adds the whatInput.ts change from #11052: main + #11007 + #11008 + this PR together pass the Switch, Checkbox, Radio, ToggleButton and whatInput tests, 198 of 198.
  • CI on this head: the visual job ran 13 impacted screenshot files, including the 6 pressed tests, and they passed.

CI on 8845e48135: all 23 checks passed. Rechecked just before posting: the head is still 8845e48.

This branch was successfully deployed

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