Skip to content

Commit 4183fbc

Browse files
crisbetojelbourn
authored andcommitted
fix(button): improved tap responsiveness on mobile (#1792)
* fix(button): improved tap responsiveness on mobile Improves the tap responsiveness of buttons on mobile by replacing the `::after` overlay with a DOM node that prevents the default touch action. Previously, the `::after` overlay would capture the first tap, causing the button to have to tapped twice in order to fire it's click handler. Fixes #1316.
1 parent 3d4abac commit 4183fbc

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

src/lib/button/_button-base.scss

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33
@import '../core/style/button-common';
44

55

6-
// Applies a focus style to an md-button element.
7-
@mixin md-button-focus {
8-
&::after {
9-
// The button spec calls for focus on raised buttons (and FABs) to be indicated with a
10-
// black, 12% opacity shade over the normal color (for both light and dark themes).
11-
// We do this by placing an :after pseudo-element with the appropriate shade over the button.
12-
position: absolute;
13-
top: 0;
14-
left: 0;
15-
bottom: 0;
16-
right: 0;
17-
content: '';
18-
background-color: rgba(black, 0.12);
19-
border-radius: inherit;
20-
pointer-events: none;
21-
}
22-
}
23-
246
// Flat and raised button standards
257
$md-button-padding: 0 16px !default;
268
$md-button-min-width: 88px !default;
@@ -74,7 +56,9 @@ $md-mini-fab-padding: 8px !default;
7456
}
7557

7658
&.md-button-focus {
77-
@include md-button-focus();
59+
.md-button-focus-overlay {
60+
opacity: 1;
61+
}
7862
}
7963
}
8064

src/lib/button/_button-theme.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
$accent: map-get($theme, accent);
88
$warn: map-get($theme, warn);
99

10-
&.md-primary::after {
10+
&.md-primary .md-button-focus-overlay {
1111
background-color: md-color($primary, 0.12);
1212
}
1313

14-
&.md-accent::after {
14+
&.md-accent .md-button-focus-overlay {
1515
background-color: md-color($accent, 0.12);
1616
}
1717

18-
&.md-warn::after {
18+
&.md-warn .md-button-focus-overlay {
1919
background-color: md-color($warn, 0.12);
2020
}
2121
}

src/lib/button/button.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
[md-ripple-trigger]="getHostElement()"
55
[md-ripple-color]="isRoundButton() ? 'rgba(255, 255, 255, 0.2)' : ''"
66
md-ripple-background-color="rgba(0, 0, 0, 0)"></div>
7+
<!-- the touchstart handler prevents the overlay from capturing the initial tap on touch devices -->
8+
<div class="md-button-focus-overlay" (touchstart)="$event.preventDefault()"></div>

src/lib/button/button.scss

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
// Only flat buttons and icon buttons (not raised or fabs) have a hover style.
1111
&:hover {
1212
// Use the same visual treatment for hover as for focus.
13-
@include md-button-focus();
13+
.md-button-focus-overlay {
14+
opacity: 1;
15+
}
1416
}
1517

1618
&[disabled]:hover {
17-
&.md-primary, &.md-accent, &.md-warn, &::after {
19+
&.md-primary, &.md-accent, &.md-warn, .md-button-focus-overlay {
1820
background-color: transparent;
1921
}
2022
}
@@ -56,14 +58,26 @@
5658
}
5759

5860
// The ripple container should match the bounds of the entire button.
59-
.md-button-ripple {
61+
.md-button-ripple, .md-button-focus-overlay {
6062
position: absolute;
6163
top: 0;
6264
left: 0;
6365
bottom: 0;
6466
right: 0;
6567
}
6668

69+
// Overlay to be used as a tint. Note that the same effect can be achieved by using a pseudo
70+
// element, however we use a proper DOM element in order to be able to disable the default
71+
// touch action. This makes the buttons more responsive on touch devices.
72+
.md-button-focus-overlay {
73+
// The button spec calls for focus on raised buttons (and FABs) to be indicated with a
74+
// black, 12% opacity shade over the normal color (for both light and dark themes).
75+
background-color: rgba(black, 0.12);
76+
border-radius: inherit;
77+
pointer-events: none;
78+
opacity: 0;
79+
}
80+
6781
// For round buttons, the ripple container should clip child ripples to a circle.
6882
.md-button-ripple-round {
6983
border-radius: 50%;

0 commit comments

Comments
 (0)