Skip to content

fix: make empty space next to checkbox label clickable - #13381

Merged
web-padawan merged 19 commits into
mainfrom
fix/checkbox-empty-space-click
Nov 13, 2026
Merged

web-padawan merged 19 commits into
mainfrom
fix/checkbox-empty-space-click

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes vaadin/flow-components#6699

The hover styles of vaadin-checkbox and vaadin-switch cover the whole component, but only clicks on the input, the label text or the required indicator toggled the checked state. In a checkbox wider than its label (e.g. width: 100%), clicking the empty space next to the label showed the hover style but did nothing. In Vaadin 23 the slotted label stretched over that space, so the click toggled the checkbox.

What changed

  • The visually hidden <input> now covers the whole first row (the row with the control and the label) of checkbox, switch and radio button, in the base styles and in the Lumo checkable-field mixin. Clicks on the empty space next to the label reach the input natively, so they toggle the checked state and fire a single click event.
  • The label part is sized to its content (justify-self: start) and stays above the input, so the label text, links in it and the required indicator work as before. No pointer-events rules are added, so an ancestor with pointer-events: none (e.g. the body while a modal overlay is open) still blocks clicks on the label.
  • The helper text and error message rows are not covered and do not toggle the checked state.
  • The minimum 24px click target is kept, and the visible layout is unchanged in base styles, Lumo and Aura.
  • Side effects: the input box, and so a screen reader's focus highlight, now spans the label row. The host (base styles) or the container (Lumo) gets position: relative. The label part box no longer stretches over the empty space, so ::part(label):hover does not match there.
  • Radio button behaves the same as before: its slotted label already spans the row.

Type of change

  • Bugfix

Test summary

  • A real mouse press and release on the empty space next to the label sets active, toggles checked and fires one click event.
  • Hit tests check that the empty space next to the label (also in reverse theme) is covered by the input, and that the label, a link in it and the helper text row are not. Covered for base styles, for checkbox, switch and radio button.
  • The input keeps the 24px minimum size, also in reverse theme.
  • Checked across checkbox, switch and radio button with base styles, Lumo and Aura, including required, helper text, reverse theme, RTL, wrapped labels and no label, in Chromium, Firefox and WebKit (Lumo and Aura checked by hand). Part positions are unchanged compared to main.

Working on behalf of @Legioth

The hover styles of checkbox and switch cover the whole component, but
only clicks on the input, the label text or the required indicator
toggled the checked state. Clicking the empty space next to the label,
e.g. in a full-width checkbox, showed the hover style but did nothing.

Clicks on any element in the shadow DOM, except the helper text and
error message, now toggle the checked state, like the required
indicator already did. Clicks targeting the host itself are ignored,
so that synthetic clicks dispatched to the host (e.g. by the grid on
Space key) do not toggle it.
Limit the clickable empty space to the label part and the container
element, which is the empty space next to the label in Lumo, instead
of excluding helper text and error message from the whole shadow DOM.
A real mouse click on the empty space moves the starting point of
sequential focus navigation in WebKit, so the following Space key
tests no longer reached the input with Tab.

@web-padawan web-padawan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 AI-generated

Reviewed 9498a15 and left 2 comments: 1 issue, 1 nitpick.

Comment thread packages/checkbox/test/checkbox.test.js Resolved
In Lumo the container element spans all rows, so clicks next to the
helper text or error message also hit it. Only treat container clicks
within the vertical bounds of the label part as clicks on the empty
space next to the label.
ActiveMixin calls _shouldSetActive with a gesture event that has the
coordinates in detail instead of clientY, so container mousedown in the
label row did not set the active attribute.
@web-padawan

Copy link
Copy Markdown
Member

🤖 AI-generated

suggestion (design, non-blocking): The fix can be CSS only: let the visually hidden <input> cover the label row, so that clicks on the empty space reach it natively.

checkable-base-styles.js already extends the input to keep a 24px target and to cover the gap next to the label. If the input covers the whole first row, __onClick, __isEmptySpaceTarget and the [class$="-container"] match are not needed. This also fixes the Lumo helper row case from the other thread.

Sketch for checkable-base-styles.js. Lumo checkable-field.css needs the same rules, with position: relative on the container instead of the host, because Lumo does not use the base styles.

:host {
  position: relative;
}

::slotted(input) {
  position: absolute;
  /* An absolute grid item needs an explicit end line, or it also covers the helper rows */
  grid-row: 1 / 2;
  grid-column: 1 / -1;
  /* Ensure minimum click target (WCAG) */
  inset-block: min(0px, (100% - 24px) / 2);
  inset-inline: min(0px, (100% - 24px) / 2);
  margin: 0 !important;
  width: auto !important;
  height: auto !important;
}

/* Let clicks next to the label text and on the required indicator reach the input */
[part='label'] {
  pointer-events: none;
}

[part='label'] ::slotted(*) {
  pointer-events: auto;
}

The old input margins and the reverse rules for the input can be removed. In JS, _onRequiredIndicatorClick, its @click bindings and the required-indicator check in _shouldSetActive can be removed too.

I checked a prototype with real mouse clicks in Chromium, Firefox and WebKit with base styles and Lumo, and on the "Bells & Whistles" checkboxes of dev/playground/checkbox-group.html with base styles, Lumo and Aura:

Case This PR CSS only
Empty space next to the label toggles, 2 click events toggles, 1 click event
Required indicator toggles toggles
Helper row, under the checkbox (Lumo) toggles does not toggle
Link in the label, preventDefault() in a parent no toggle no toggle
reverse theme, RTL toggles toggles
Label with an image that wraps to 2 lines, any point of the label row toggles toggles
Layout and 24px target of a checkbox without label unchanged unchanged
Screenshot of the checkbox reference pixel-identical

Trade-offs: the input box, and so the screen reader highlight, now covers the label row. The host gets position: relative, and ::part(label):hover no longer matches over the empty space. I did not check switch or radio button, which share these styles. Tests can check document.elementFromPoint(x, y) === checkbox.inputElement instead of calling .click() on shadow parts, and one test file can import the Lumo CSS like test/visual/lumo does.

…icks

Let the visually hidden input cover the whole first row of checkbox,
radio button and switch, and let clicks pass through the label part.
Clicks on the empty space next to the label and on the required
indicator now reach the input natively, so the JS click forwarding and
the required indicator click handler are no longer needed. The helper
text and error message rows are not covered.
@totally-not-ai totally-not-ai Bot changed the title fix: toggle checkbox on click on empty space next to the label fix: make empty space next to checkbox label clickable Nov 10, 2026
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@web-padawan Thanks, switched to the CSS-only approach. The input is absolutely positioned over the first row in checkable() and in Lumo's checkable-field.css (where the container gets position: relative). The label part has pointer-events: none, and the slotted <label> gets position: relative so it stays above the input. The JS click forwarding, the required indicator @click bindings and the indicator check in _shouldSetActive are removed. The old input margins and the reverse margin rule are removed too.

I checked checkbox, switch and radio button with base styles, Lumo and Aura (default, required, helper text, reverse, RTL, no label) in Chromium, Firefox and WebKit. Part and label positions are identical to main. One difference from the sketch: in Lumo the required indicator overlaps the label's padding, so a click on it reaches the label instead of the input, which still toggles natively. Tests now use elementFromPoint, with one Lumo test file in the checkbox package. Two focus tests now click the checkbox part, because the center of the input is now on the label. The PR description is updated.

Comment thread packages/checkbox/test/checkbox.test.js Resolved
The checkable-field mixin sets pointer-events: auto on the slotted label,
which overrode pointer-events: none of the disabled host in Lumo.
@web-padawan
web-padawan marked this pull request as ready for review November 10, 2026 14:23
@web-padawan
web-padawan removed the request for review from Legioth November 10, 2026 14:24
@web-padawan

Copy link
Copy Markdown
Member

note: with the CSS only fix, hover styles for Lumo are still shown when mouse moves over the helper text / error message, but clicking doesn't toggle the checked state, as expected. Not sure if it can be easily fixed.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@web-padawan Good catch. I don't see a simple CSS fix inside Lumo's shadow styles:

  • The natural selector would be :host(:has(> input:hover)) [part='checkbox']. In a quick test with a real mouse in Chromium, :host(:has()) never matched slotted light DOM children, so it can't follow the input's hover state.
  • ::slotted(input:hover) can't style a sibling shadow part.
  • Setting pointer-events: none on the host and auto on the input and label would limit :hover to the label row. But it would also affect clicks on the helper and error content, the tooltip, and the existing disabled rules, so it's too broad for this PR.

The remaining options are a JS-reflected hover attribute, or limiting the Lumo hover style to the label row in a follow-up. I've left this PR as is, so it only fixes the click target. Happy to open a follow-up issue if you'd like that changed.

@vaadin-bot

Copy link
Copy Markdown
Collaborator

Slide deck that explains this pull request from its code.

Generated 2026-11-10 14:35 UTC

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the changes — left 2 comments.

Risk: 🔴 high — It rewrites the hit area and pointer-event handling of checkbox, radio button and switch in both the shared base styles and the Lumo mixin, so every consumer's most-used form control changes interaction behavior, and one verified case lets labels stay clickable behind a modal overlay.

Finding
⚠️ pointer-events: auto on the slotted label makes labels clickable through an ancestor that disables pointer events, including the page behind a modal overlay
🧪 No test asserts that clicking the empty space actually toggles checked

Comment thread packages/checkbox/test/checkbox.test.js Resolved
…ter events

Setting pointer-events: auto on the slotted label made it clickable when
an ancestor sets pointer-events: none, such as the body while a modal
overlay is open. Instead, keep the label part hit-testable and size it
to its content, so that only the empty space next to it reaches the
input. The required indicator keeps its click handler.
Comment thread packages/checkbox/test/checkbox.test.js Resolved
@web-padawan
web-padawan requested a review from jouni November 11, 2026 08:09
Comment thread packages/switch/test/switch.test.ts Resolved
Comment thread packages/checkbox/test/checkbox.test.js Resolved
Comment thread packages/checkbox/test/checkbox.test.js Resolved
@sonarqubecloud

Copy link
Copy Markdown

@web-padawan
web-padawan merged commit 09d43f0 into main Nov 13, 2026
23 of 24 checks passed
@web-padawan
web-padawan deleted the fix/checkbox-empty-space-click branch November 13, 2026 07:56
web-padawan added a commit that referenced this pull request Nov 13, 2026
Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Co-authored-by: web-padawan <iamkulykov@gmail.com>
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 25.4.0-alpha7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Checkbox clickable area not as big as the size of the checkbox

4 participants