From 793e1f1310c0d5e38d7fb0766d07292c1ed83522 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 7 Jun 2021 22:47:30 +0200 Subject: [PATCH] fix(material-experimental/mdc-button): set proper touch target (#22846) 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. --- .../mdc-button/_button-base.scss | 16 ++++++++++++++++ src/material-experimental/mdc-button/button.html | 2 ++ src/material-experimental/mdc-button/button.scss | 3 ++- src/material-experimental/mdc-button/fab.scss | 3 ++- .../mdc-button/icon-button.scss | 1 + 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/material-experimental/mdc-button/_button-base.scss b/src/material-experimental/mdc-button/_button-base.scss index 9b6a0f9220dc..7c5cbfa28515 100644 --- a/src/material-experimental/mdc-button/_button-base.scss +++ b/src/material-experimental/mdc-button/_button-base.scss @@ -1,3 +1,5 @@ +@use '@material/touch-target' as mdc-touch-target; +@use '../mdc-helpers/mdc-helpers'; @use '../../material/core/style/layout-common'; // Adds styles necessary to provide stateful interactions with the button. This includes providing @@ -59,3 +61,17 @@ pointer-events: none; } } + +@mixin mat-private-button-touch-target($is-square) { + // Element used to ensure that the button has a touch target that meets the required minimum. + // Note that we use this, instead of MDC's built-in `mdc-button--touch` class, because the MDC + // class is implemented as `margin-top: 6px; margin-bottom: 6px` on the host element which + // goes against our rule of not having margins on the host node. Furthermore, having the margin on + // the button itself would require us to wrap it in another div. See: + // https://github.com/material-components/material-components-web/tree/master/packages/mdc-button#making-buttons-accessible + .mat-mdc-button-touch-target { + @include mdc-touch-target.touch-target( + $set-width: $is-square, + $query: mdc-helpers.$mat-base-styles-query); + } +} diff --git a/src/material-experimental/mdc-button/button.html b/src/material-experimental/mdc-button/button.html index 32b22df3a4b6..e5abbe3801ee 100644 --- a/src/material-experimental/mdc-button/button.html +++ b/src/material-experimental/mdc-button/button.html @@ -21,3 +21,5 @@ [matRippleDisabled]="_isRippleDisabled()" [matRippleCentered]="_isRippleCentered" [matRippleTrigger]="_elementRef.nativeElement"> + + diff --git a/src/material-experimental/mdc-button/button.scss b/src/material-experimental/mdc-button/button.scss index 1ca09d997b9c..62b10a18d10c 100644 --- a/src/material-experimental/mdc-button/button.scss +++ b/src/material-experimental/mdc-button/button.scss @@ -2,7 +2,7 @@ @use '@material/button/variables' as mdc-button-variables; @use '../mdc-helpers/mdc-helpers'; @use '../../cdk/a11y'; -@use '_button-base'; +@use 'button-base'; @include mdc-button.without-ripple($query: mdc-helpers.$mat-base-styles-query); @@ -10,6 +10,7 @@ .mat-mdc-button, .mat-mdc-unelevated-button, .mat-mdc-raised-button, .mat-mdc-outlined-button { @include button-base.mat-private-button-interactive(); @include button-base.mat-private-button-disabled(); + @include button-base.mat-private-button-touch-target(false); } // MDC expects button icons to contain this HTML content: diff --git a/src/material-experimental/mdc-button/fab.scss b/src/material-experimental/mdc-button/fab.scss index 4aa590867d66..074015505382 100644 --- a/src/material-experimental/mdc-button/fab.scss +++ b/src/material-experimental/mdc-button/fab.scss @@ -1,12 +1,13 @@ @use '@material/fab' as mdc-fab; @use '../mdc-helpers/mdc-helpers'; -@use '_button-base'; +@use 'button-base'; @include mdc-fab.without-ripple($query: mdc-helpers.$mat-base-styles-query); .mat-mdc-fab, .mat-mdc-mini-fab { @include button-base.mat-private-button-interactive(); @include button-base.mat-private-button-disabled(); + @include button-base.mat-private-button-touch-target(true); // MDC adds some styles to fab and mini-fab that conflict with some of our focus indicator // styles and don't actually do anything. This undoes those conflicting styles. diff --git a/src/material-experimental/mdc-button/icon-button.scss b/src/material-experimental/mdc-button/icon-button.scss index f7daec321298..7088c836934b 100644 --- a/src/material-experimental/mdc-button/icon-button.scss +++ b/src/material-experimental/mdc-button/icon-button.scss @@ -13,6 +13,7 @@ border-radius: 50%; @include button-base.mat-private-button-disabled(); + @include button-base.mat-private-button-touch-target(true); // MDC adds some styles to icon buttons that conflict with some of our focus indicator styles // and don't actually do anything. This undoes those conflicting styles.