Skip to content

Commit df76492

Browse files
committed
fix(material/slide-toggle): increase clickable area
Fixes that the clickable area of the slide toggle was only on the `button` itself and the `label` which meant that there was some space above and below the label which isn't clickable. Fixes #29483.
1 parent fd47a0e commit df76492

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

src/material/slide-toggle/slide-toggle.scss

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@use '../core/tokens/m2/mat/switch' as tokens-mat-switch;
44
@use '../core/tokens/m2/mdc/switch' as tokens-mdc-switch;
55
@use '../core/tokens/token-utils';
6+
@use '../core/style/vendor-prefixes';
67

78
$_mdc-slots: (tokens-mdc-switch.$prefix, tokens-mdc-switch.get-token-slots());
89
$_mat-slots: (tokens-mat-switch.$prefix, tokens-mat-switch.get-token-slots());
@@ -12,23 +13,14 @@ $_interactive-disabled-selector: '.mat-mdc-slide-toggle-disabled-interactive.mdc
1213
align-items: center;
1314
background: none;
1415
border: none;
15-
cursor: pointer;
1616
display: inline-flex;
1717
flex-shrink: 0;
1818
margin: 0;
1919
outline: none;
2020
overflow: visible;
2121
padding: 0;
2222
position: relative;
23-
24-
&.mdc-switch--disabled {
25-
cursor: default;
26-
pointer-events: none;
27-
}
28-
29-
&.mat-mdc-slide-toggle-disabled-interactive {
30-
pointer-events: auto;
31-
}
23+
z-index: 1; // Put the button above the label.
3224

3325
@include token-utils.use-tokens($_mdc-slots...) {
3426
@include token-utils.create-token-slot(width, track-width);
@@ -503,8 +495,35 @@ $_interactive-disabled-selector: '.mat-mdc-slide-toggle-disabled-interactive.mdc
503495
}
504496

505497
.mat-mdc-slide-toggle {
498+
@include vendor-prefixes.user-select(none);
506499
display: inline-block;
507500
-webkit-tap-highlight-color: transparent;
501+
cursor: pointer;
502+
position: relative;
503+
504+
&:has(.mdc-switch--disabled) {
505+
pointer-events: none;
506+
cursor: auto;
507+
}
508+
509+
&:has(.mat-mdc-slide-toggle-disabled-interactive) {
510+
pointer-events: auto;
511+
}
512+
513+
button, label {
514+
cursor: inherit;
515+
}
516+
517+
// Increase the clickable area to cover the entire container.
518+
label::before {
519+
content: '';
520+
display: block;
521+
position: absolute;
522+
width: 100%;
523+
height: 100%;
524+
top: 0;
525+
left: 0;
526+
}
508527

509528
// Remove the native outline since we use the ripple for focus indication.
510529
outline: 0;
@@ -564,11 +583,6 @@ $_interactive-disabled-selector: '.mat-mdc-slide-toggle-disabled-interactive.mdc
564583
}
565584
}
566585

567-
// If our slide-toggle is enabled the cursor on label should appear as a pointer.
568-
.mdc-switch:enabled + .mdc-label {
569-
cursor: pointer;
570-
}
571-
572586
// TODO(wagnermaciel): Use our custom token system to emit this css rule.
573587
.mdc-switch--disabled + label {
574588
color: var(--mdc-switch-disabled-label-text-color);

0 commit comments

Comments
 (0)