Skip to content

Commit

Permalink
fix(MatButtonToggle): use radio pattern for single select Mat toggle …
Browse files Browse the repository at this point in the history
…button group
  • Loading branch information
clamli committed Jan 30, 2024
1 parent 2455a42 commit aeb2144
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/dev-app/button-toggle/button-toggle-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<h1>Exclusive Selection</h1>

<section>
<mat-button-toggle-group name="alignment" [vertical]="isVertical">
<mat-button-toggle-group name="standard alignment" [vertical]="isVertical">
<mat-button-toggle value="left" [disabled]="isDisabled">
<mat-icon>format_align_left</mat-icon>
</mat-button-toggle>
Expand All @@ -26,7 +26,7 @@ <h1>Exclusive Selection</h1>
</section>

<section>
<mat-button-toggle-group appearance="legacy" name="alignment" [vertical]="isVertical">
<mat-button-toggle-group appearance="legacy" name="legacy alignment" [vertical]="isVertical">
<mat-button-toggle value="left" [disabled]="isDisabled">
<mat-icon>format_align_left</mat-icon>
</mat-button-toggle>
Expand Down
27 changes: 13 additions & 14 deletions src/material/button-toggle/button-toggle.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<button #button class="mat-button-toggle-button mat-focus-indicator"
type="button"
[id]="buttonId"
[attr.tabindex]="disabled ? -1 : tabIndex"
[attr.aria-pressed]="checked"
[disabled]="disabled || null"
[attr.name]="_getButtonName()"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
(click)="_onButtonClick()">
<span class="mat-button-toggle-label-content">
<ng-content></ng-content>
</span>
</button>
<input #button class="mat-button-toggle-button mat-focus-indicator"
[type]="type"
[id]="buttonId"
[attr.tabindex]="disabled ? -1 : tabIndex"
[attr.aria-pressed]="_getAriaPressed()"
[disabled]="disabled || null"
[attr.name]="_getButtonName()"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
(click)="_onButtonClick()">
<label class="mat-button-toggle-label-content" [for]="buttonId">
<ng-content></ng-content>
</label>

<span class="mat-button-toggle-focus-overlay"></span>
<span class="mat-button-toggle-ripple" matRipple
Expand Down
8 changes: 8 additions & 0 deletions src/material/button-toggle/button-toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,12 @@ $_standard-tokens: (
&::-moz-focus-inner {
border: 0;
}

opacity: 0.01;
z-index: 100;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
16 changes: 15 additions & 1 deletion src/material/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class MatButtonToggleChange {
{provide: MAT_BUTTON_TOGGLE_GROUP, useExisting: MatButtonToggleGroup},
],
host: {
'role': 'group',
'class': 'mat-button-toggle-group',
'[role]': "multiple ? 'group' : 'radiogroup'",
'[attr.aria-disabled]': 'disabled',
'[class.mat-button-toggle-vertical]': 'vertical',
'[class.mat-button-toggle-group-appearance-standard]': 'appearance === "standard"',
Expand Down Expand Up @@ -417,6 +417,11 @@ export class MatButtonToggle implements OnInit, AfterViewInit, OnDestroy {
*/
@Input('aria-labelledby') ariaLabelledby: string | null = null;

/** Type of the button toggle. Either 'radio' or 'button'. */
get type(): string {
return this._isSingleSelector() ? 'radio' : 'button';
}

/** Underlying native `button` element. */
@ViewChild('button') _buttonElement: ElementRef<HTMLButtonElement>;

Expand Down Expand Up @@ -573,6 +578,15 @@ export class MatButtonToggle implements OnInit, AfterViewInit, OnDestroy {
return this.name || null;
}

/** Get the aria-pressed attribute value. */
_getAriaPressed(): boolean | null {
// When the toggle stands alone, or in multiple selection mode, use aria-pressed attribute.
if (!this._isSingleSelector()) {
return this.checked;
}
return null;
}

/** Whether the toggle is in single selection mode. */
private _isSingleSelector(): boolean {
return this.buttonToggleGroup && !this.buttonToggleGroup.multiple;
Expand Down

0 comments on commit aeb2144

Please sign in to comment.