Skip to content

Commit 0544deb

Browse files
tinayuangaommalerba
authored andcommitted
feat(button-toggle): Add a focus overlay for button-toggle (#3119)
* Add a focus overlay for button-toggle * Use FocusOriginMonitor * . * Fix test * Add 0.12 opacity white and black. * Add dark-overlay
1 parent 85ba82a commit 0544deb

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

src/lib/button-toggle/_button-toggle-theme.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
@import '../core/theming/palette';
22
@import '../core/theming/theming';
33

4+
// Applies a focus style to an md-button-toggle element for each of the supported palettes.
5+
@mixin _mat-button-toggle-focus-color($theme) {
6+
$background: map-get($theme, background);
7+
8+
.mat-button-toggle-focus-overlay {
9+
background-color: mat-color($background, dark-overlay);
10+
}
11+
}
412

513
@mixin mat-button-toggle-theme($theme) {
614
$foreground: map-get($theme, foreground);
715
$background: map-get($theme, background);
816

917
.mat-button-toggle {
1018
color: mat-color($foreground, hint-text);
19+
20+
&.cdk-focused {
21+
@include _mat-button-toggle-focus-color($theme);
22+
}
1123
}
1224

1325
.mat-button-toggle-checked {

src/lib/button-toggle/button-toggle.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
<ng-content></ng-content>
1313
</div>
1414
</label>
15+
<!-- the touchstart handler prevents the overlay from capturing the initial tap on touch devices -->
16+
<div class="mat-button-toggle-focus-overlay" (touchstart)="$event.preventDefault()"></div>

src/lib/button-toggle/button-toggle.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
@import '../core/a11y/a11y';
12
@import '../core/style/elevation';
3+
@import '../core/style/layout-common';
24

35
$mat-button-toggle-padding: 0 16px !default;
46
$mat-button-toggle-line-height: 36px !default;
@@ -34,6 +36,11 @@ $mat-button-toggle-border-radius: 2px !default;
3436
.mat-button-toggle {
3537
white-space: nowrap;
3638
font-family: $mat-font-family;
39+
position: relative;
40+
}
41+
42+
.mat-button-toggle.cdk-focused .mat-button-toggle-focus-overlay {
43+
opacity: 1;
3744
}
3845

3946
.mat-button-toggle-label-content {
@@ -47,3 +54,13 @@ $mat-button-toggle-border-radius: 2px !default;
4754
.mat-button-toggle-label-content > * {
4855
vertical-align: middle;
4956
}
57+
58+
// Overlay to be used as a tint. Note that the same effect can be achieved by using a pseudo
59+
// element, however we use a proper DOM element in order to be able to disable the default
60+
// touch action. This makes the buttons more responsive on touch devices.
61+
.mat-button-toggle-focus-overlay {
62+
border-radius: inherit;
63+
pointer-events: none;
64+
opacity: 0;
65+
@include mat-fill;
66+
}

src/lib/button-toggle/button-toggle.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import {NG_VALUE_ACCESSOR, ControlValueAccessor, FormsModule} from '@angular/forms';
2222
import {Observable} from 'rxjs/Observable';
2323
import {
24+
FocusOriginMonitor,
2425
UniqueSelectionDispatcher,
2526
coerceBooleanProperty,
2627
UNIQUE_SELECTION_DISPATCHER_PROVIDER,
@@ -276,7 +277,6 @@ export class MdButtonToggleGroupMultiple {
276277
set vertical(value) {
277278
this._vertical = coerceBooleanProperty(value);
278279
}
279-
280280
}
281281

282282
/** Single button inside of a toggle group. */
@@ -332,7 +332,9 @@ export class MdButtonToggle implements OnInit {
332332
constructor(@Optional() toggleGroup: MdButtonToggleGroup,
333333
@Optional() toggleGroupMultiple: MdButtonToggleGroupMultiple,
334334
public buttonToggleDispatcher: UniqueSelectionDispatcher,
335-
private _renderer: Renderer) {
335+
private _renderer: Renderer,
336+
private _elementRef: ElementRef,
337+
private _focusOriginMonitor: FocusOriginMonitor) {
336338
this.buttonToggleGroup = toggleGroup;
337339

338340
this.buttonToggleGroupMultiple = toggleGroupMultiple;
@@ -363,6 +365,7 @@ export class MdButtonToggle implements OnInit {
363365
if (this.buttonToggleGroup && this._value == this.buttonToggleGroup.value) {
364366
this._checked = true;
365367
}
368+
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
366369
}
367370

368371
/** Unique ID for the underlying `input` element. */
@@ -477,7 +480,7 @@ export class MdButtonToggle implements OnInit {
477480
CompatibilityModule,
478481
],
479482
declarations: [MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle],
480-
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER]
483+
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER, FocusOriginMonitor]
481484
})
482485
export class MdButtonToggleModule {
483486
/** @deprecated */

src/lib/core/theming/_palette.scss

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
$black-87-opacity: rgba(black, 0.87);
1010
$white-87-opacity: rgba(white, 0.87);
11+
$black-12-opacity: rgba(black, 0.12);
12+
$white-12-opacity: rgba(white, 0.12);
1113

1214
$mat-red: (
1315
50: #ffebee,
@@ -649,8 +651,9 @@ $mat-light-theme-background: (
649651
hover: rgba(black, 0.04), // TODO(kara): check style with Material Design UX
650652
card: white,
651653
dialog: white,
652-
disabled-button: rgba(black, 0.12),
654+
disabled-button: $black-12-opacity,
653655
raised-button: white,
656+
dark-overlay: $black-12-opacity,
654657
);
655658

656659
// Background palette for dark themes.
@@ -661,15 +664,16 @@ $mat-dark-theme-background: (
661664
hover: rgba(white, 0.04), // TODO(kara): check style with Material Design UX
662665
card: map_get($mat-grey, 800),
663666
dialog: map_get($mat-grey, 800),
664-
disabled-button: rgba(white, 0.12),
667+
disabled-button: $white-12-opacity,
665668
raised-button: map-get($mat-grey, 800),
669+
dark-overlay: $white-12-opacity,
666670
);
667671

668672
// Foreground palette for light themes.
669673
$mat-light-theme-foreground: (
670674
base: black,
671-
divider: rgba(black, 0.12),
672-
dividers: rgba(black, 0.12),
675+
divider: $black-12-opacity,
676+
dividers: $black-12-opacity,
673677
disabled: rgba(black, 0.38),
674678
disabled-button: rgba(black, 0.38),
675679
disabled-text: rgba(black, 0.38),
@@ -683,8 +687,8 @@ $mat-light-theme-foreground: (
683687
// Foreground palette for dark themes.
684688
$mat-dark-theme-foreground: (
685689
base: white,
686-
divider: rgba(white, 0.12),
687-
dividers: rgba(white, 0.12),
690+
divider: $white-12-opacity,
691+
dividers: $white-12-opacity,
688692
disabled: rgba(white, 0.3),
689693
disabled-button: rgba(white, 0.3),
690694
disabled-text: rgba(white, 0.3),

0 commit comments

Comments
 (0)