Skip to content

Commit 3284496

Browse files
authored
fix(material-experimental/mdc-button): set proper touch target (#22931)
This is a reimplementation of #22846 which includes a fix for RTL layouts. Adds some styles to ensure that the MDC-based button always has a touch target of at least 48px. Note that I decided against using MDC's built-in touch target class, because it would've added a margin on the button host node. Fixes #22799.
1 parent 30cfd7d commit 3284496

File tree

8 files changed

+42
-3
lines changed

8 files changed

+42
-3
lines changed

src/material-experimental/mdc-button/_button-base.scss

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@use '@material/touch-target' as mdc-touch-target;
2+
@use '../mdc-helpers/mdc-helpers';
13
@use '../../material/core/style/layout-common';
24

35
// Adds styles necessary to provide stateful interactions with the button. This includes providing
@@ -59,3 +61,25 @@
5961
pointer-events: none;
6062
}
6163
}
64+
65+
@mixin mat-private-button-touch-target($is-square) {
66+
// Element used to ensure that the button has a touch target that meets the required minimum.
67+
// Note that we use this, instead of MDC's built-in `mdc-button--touch` class, because the MDC
68+
// class is implemented as `margin-top: 6px; margin-bottom: 6px` on the host element which
69+
// goes against our rule of not having margins on the host node. Furthermore, having the margin on
70+
// the button itself would require us to wrap it in another div. See:
71+
// https://github.com/material-components/material-components-web/tree/master/packages/mdc-button#making-buttons-accessible
72+
.mat-mdc-button-touch-target {
73+
@include mdc-touch-target.touch-target(
74+
$set-width: $is-square,
75+
$query: mdc-helpers.$mat-base-styles-query);
76+
77+
@if ($is-square) {
78+
[dir='rtl'] & {
79+
left: auto;
80+
right: 50%;
81+
transform: translate(50%, -50%);
82+
}
83+
}
84+
}
85+
}

src/material-experimental/mdc-button/_button-theme-private.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ $fab-state-target: '.mdc-fab__ripple';
4545
@include mdc-theme.prop(background-color, rgba(mdc-theme-color.prop-value(on-surface), 0.12));
4646
}
4747

48+
// Hides the touch target on lower densities.
49+
@mixin touch-target-density($scale) {
50+
@if ($scale == -2 or $scale == 'minimum') {
51+
.mat-mdc-button-touch-target {
52+
display: none;
53+
}
54+
}
55+
}

src/material-experimental/mdc-button/_button-theme.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
.mat-mdc-unelevated-button,
160160
.mat-mdc-outlined-button {
161161
@include mdc-button-theme.density($density-scale, $query: mdc-helpers.$mat-base-styles-query);
162+
@include button-theme-private.touch-target-density($density-scale);
162163
}
163164
}
164165

src/material-experimental/mdc-button/_icon-button-theme.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
$density-scale: theming.get-density-config($config-or-theme);
5959
.mat-mdc-icon-button {
6060
@include mdc-icon-button.density($density-scale, $query: mdc-helpers.$mat-base-styles-query);
61+
@include button-theme-private.touch-target-density($density-scale);
6162
}
6263
}
6364

src/material-experimental/mdc-button/button.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
[matRippleDisabled]="_isRippleDisabled()"
2222
[matRippleCentered]="_isRippleCentered"
2323
[matRippleTrigger]="_elementRef.nativeElement"></span>
24+
25+
<span class="mat-mdc-button-touch-target"></span>

src/material-experimental/mdc-button/button.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
@use '@material/button/variables' as mdc-button-variables;
33
@use '../mdc-helpers/mdc-helpers';
44
@use '../../cdk/a11y';
5-
@use '_button-base';
5+
@use 'button-base';
66

77

88
@include mdc-button.without-ripple($query: mdc-helpers.$mat-base-styles-query);
99

1010
.mat-mdc-button, .mat-mdc-unelevated-button, .mat-mdc-raised-button, .mat-mdc-outlined-button {
1111
@include button-base.mat-private-button-interactive();
1212
@include button-base.mat-private-button-disabled();
13+
@include button-base.mat-private-button-touch-target(false);
1314
}
1415

1516
// MDC expects button icons to contain this HTML content:

src/material-experimental/mdc-button/fab.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
@use '@material/fab' as mdc-fab;
22
@use '../mdc-helpers/mdc-helpers';
3-
@use '_button-base';
3+
@use 'button-base';
44

55
@include mdc-fab.without-ripple($query: mdc-helpers.$mat-base-styles-query);
66

77
.mat-mdc-fab, .mat-mdc-mini-fab {
88
@include button-base.mat-private-button-interactive();
99
@include button-base.mat-private-button-disabled();
10+
@include button-base.mat-private-button-touch-target(true);
1011

1112
// MDC adds some styles to fab and mini-fab that conflict with some of our focus indicator
1213
// styles and don't actually do anything. This undoes those conflicting styles.

src/material-experimental/mdc-button/icon-button.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@use '@material/icon-button' as mdc-icon-button;
22
@use '../mdc-helpers/mdc-helpers';
3-
@use '_button-base';
3+
@use 'button-base';
44

55
@include mdc-icon-button.without-ripple($query: mdc-helpers.$mat-base-styles-query);
66

@@ -13,6 +13,7 @@
1313
border-radius: 50%;
1414

1515
@include button-base.mat-private-button-disabled();
16+
@include button-base.mat-private-button-touch-target(true);
1617

1718
// MDC adds some styles to icon buttons that conflict with some of our focus indicator styles
1819
// and don't actually do anything. This undoes those conflicting styles.

0 commit comments

Comments
 (0)