Skip to content

Commit c42022b

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 c42022b

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

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

Lines changed: 16 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,7 +13,6 @@ $_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;
@@ -21,15 +21,6 @@ $_interactive-disabled-selector: '.mat-mdc-slide-toggle-disabled-interactive.mdc
2121
padding: 0;
2222
position: relative;
2323

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-
}
32-
3324
@include token-utils.use-tokens($_mdc-slots...) {
3425
@include token-utils.create-token-slot(width, track-width);
3526
}
@@ -503,8 +494,23 @@ $_interactive-disabled-selector: '.mat-mdc-slide-toggle-disabled-interactive.mdc
503494
}
504495

505496
.mat-mdc-slide-toggle {
497+
@include vendor-prefixes.user-select(none);
506498
display: inline-block;
507499
-webkit-tap-highlight-color: transparent;
500+
cursor: pointer;
501+
502+
&:has(.mdc-switch--disabled) {
503+
pointer-events: none;
504+
cursor: auto;
505+
}
506+
507+
&:has(.mat-mdc-slide-toggle-disabled-interactive) {
508+
pointer-events: auto;
509+
}
510+
511+
button, label {
512+
cursor: inherit;
513+
}
508514

509515
// Remove the native outline since we use the ripple for focus indication.
510516
outline: 0;
@@ -564,11 +570,6 @@ $_interactive-disabled-selector: '.mat-mdc-slide-toggle-disabled-interactive.mdc
564570
}
565571
}
566572

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-
572573
// TODO(wagnermaciel): Use our custom token system to emit this css rule.
573574
.mdc-switch--disabled + label {
574575
color: var(--mdc-switch-disabled-label-text-color);

src/material/slide-toggle/slide-toggle.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ let nextUniqueId = 0;
8282
'[class.mat-mdc-slide-toggle-checked]': 'checked',
8383
'[class._mat-animation-noopable]': '_noopAnimations',
8484
'[class]': 'color ? "mat-" + color : ""',
85+
'(click)': '_handleHostClick($event)',
8586
},
8687
exportAs: 'matSlideToggle',
8788
encapsulation: ViewEncapsulation.None,
@@ -319,4 +320,12 @@ export class MatSlideToggle
319320
// `aria-labelledby`, because the button gets flagged as not having a label by tools like axe.
320321
return this.ariaLabel ? null : this._labelId;
321322
}
323+
324+
protected _handleHostClick(event: MouseEvent) {
325+
// Treat any clicks on the container as clicks on the button element.
326+
const switchEl = this._switchElement?.nativeElement;
327+
if (switchEl && !switchEl.contains(event.target as HTMLElement)) {
328+
this._handleClick();
329+
}
330+
}
322331
}

tools/public_api_guard/material/slide-toggle.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export class MatSlideToggle implements OnDestroy, AfterContentInit, OnChanges, C
6363
// (undocumented)
6464
_getAriaLabelledBy(): string | null;
6565
_handleClick(): void;
66+
// (undocumented)
67+
protected _handleHostClick(event: MouseEvent): void;
6668
hideIcon: boolean;
6769
id: string;
6870
get inputId(): string;

0 commit comments

Comments
 (0)