Skip to content

Commit

Permalink
fix(material/slide-toggle): increase clickable area
Browse files Browse the repository at this point in the history
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 angular#29483.
  • Loading branch information
crisbeto committed Jul 30, 2024
1 parent fd47a0e commit df76492
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/material/slide-toggle/slide-toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@use '../core/tokens/m2/mat/switch' as tokens-mat-switch;
@use '../core/tokens/m2/mdc/switch' as tokens-mdc-switch;
@use '../core/tokens/token-utils';
@use '../core/style/vendor-prefixes';

$_mdc-slots: (tokens-mdc-switch.$prefix, tokens-mdc-switch.get-token-slots());
$_mat-slots: (tokens-mat-switch.$prefix, tokens-mat-switch.get-token-slots());
Expand All @@ -12,23 +13,14 @@ $_interactive-disabled-selector: '.mat-mdc-slide-toggle-disabled-interactive.mdc
align-items: center;
background: none;
border: none;
cursor: pointer;
display: inline-flex;
flex-shrink: 0;
margin: 0;
outline: none;
overflow: visible;
padding: 0;
position: relative;

&.mdc-switch--disabled {
cursor: default;
pointer-events: none;
}

&.mat-mdc-slide-toggle-disabled-interactive {
pointer-events: auto;
}
z-index: 1; // Put the button above the label.

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

.mat-mdc-slide-toggle {
@include vendor-prefixes.user-select(none);
display: inline-block;
-webkit-tap-highlight-color: transparent;
cursor: pointer;
position: relative;

&:has(.mdc-switch--disabled) {
pointer-events: none;
cursor: auto;
}

&:has(.mat-mdc-slide-toggle-disabled-interactive) {
pointer-events: auto;
}

button, label {
cursor: inherit;
}

// Increase the clickable area to cover the entire container.
label::before {
content: '';
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}

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

// If our slide-toggle is enabled the cursor on label should appear as a pointer.
.mdc-switch:enabled + .mdc-label {
cursor: pointer;
}

// TODO(wagnermaciel): Use our custom token system to emit this css rule.
.mdc-switch--disabled + label {
color: var(--mdc-switch-disabled-label-text-color);
Expand Down

0 comments on commit df76492

Please sign in to comment.