From ddc307e28449045c484510ff26798fc1a6efa7c1 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 7 Aug 2024 22:09:29 +0200 Subject: [PATCH] feat(material/button-toggle): allow disabled buttons to be interactive (#29550) Adds the `disabledInteractive` input to the button toggle that allows users to opt into supporting focus on disabled button toggles. --- .../button-toggle/button-toggle-demo.html | 46 ++++++++++++++++--- .../button-toggle/button-toggle-demo.ts | 1 + src/material/button-toggle/button-toggle.html | 5 +- src/material/button-toggle/button-toggle.scss | 15 ++++-- .../button-toggle/button-toggle.spec.ts | 28 +++++++++-- src/material/button-toggle/button-toggle.ts | 33 +++++++++++++ .../testing/button-toggle-harness.spec.ts | 10 ++++ .../testing/button-toggle-harness.ts | 4 +- .../material/button-toggle.md | 13 +++++- 9 files changed, 134 insertions(+), 21 deletions(-) diff --git a/src/dev-app/button-toggle/button-toggle-demo.html b/src/dev-app/button-toggle/button-toggle-demo.html index f0619ad7895a..3b183d3df0c6 100644 --- a/src/dev-app/button-toggle/button-toggle-demo.html +++ b/src/dev-app/button-toggle/button-toggle-demo.html @@ -6,6 +6,10 @@ Disable Button Toggle Items

+

+ Allow Interaction with Disabled Button Toggles +

+

Hide Single Selection Indicator

@@ -17,7 +21,11 @@

Exclusive Selection

- + format_align_left @@ -34,7 +42,12 @@

Exclusive Selection

- + format_align_left @@ -53,7 +66,12 @@

Exclusive Selection

Disabled Group

- + format_bold @@ -61,14 +79,18 @@

Disabled Group

format_italic - format_underline + format_underlined

Multiple Selection

- + Flour Eggs Sugar @@ -76,7 +98,12 @@

Multiple Selection

- + Flour Eggs Sugar @@ -90,7 +117,12 @@

Single Toggle

Dynamic Exclusive Selection

- + @for (pie of pieOptions; track pie) { {{pie}} } diff --git a/src/dev-app/button-toggle/button-toggle-demo.ts b/src/dev-app/button-toggle/button-toggle-demo.ts index 4e4c9568c56d..a2e4f1fbd05c 100644 --- a/src/dev-app/button-toggle/button-toggle-demo.ts +++ b/src/dev-app/button-toggle/button-toggle-demo.ts @@ -24,6 +24,7 @@ import {MatIconModule} from '@angular/material/icon'; export class ButtonToggleDemo { isVertical = false; isDisabled = false; + disabledInteractive = false; hideSingleSelectionIndicator = false; hideMultipleSelectionIndicator = false; favoritePie = 'Apple'; diff --git a/src/material/button-toggle/button-toggle.html b/src/material/button-toggle/button-toggle.html index dc70074e886c..9452213fa907 100644 --- a/src/material/button-toggle/button-toggle.html +++ b/src/material/button-toggle/button-toggle.html @@ -2,13 +2,14 @@ type="button" [id]="buttonId" [attr.role]="isSingleSelector() ? 'radio' : 'button'" - [attr.tabindex]="disabled ? -1 : tabIndex" + [attr.tabindex]="disabled && !disabledInteractive ? -1 : tabIndex" [attr.aria-pressed]="!isSingleSelector() ? checked : null" [attr.aria-checked]="isSingleSelector() ? checked : null" - [disabled]="disabled || null" + [disabled]="(disabled && !disabledInteractive) || null" [attr.name]="_getButtonName()" [attr.aria-label]="ariaLabel" [attr.aria-labelledby]="ariaLabelledby" + [attr.aria-disabled]="disabled && disabledInteractive ? 'true' : null" (click)="_onButtonClick()"> diff --git a/src/material/button-toggle/button-toggle.scss b/src/material/button-toggle/button-toggle.scss index 3b19d6c77dd9..1c98b53f7c4b 100644 --- a/src/material/button-toggle/button-toggle.scss +++ b/src/material/button-toggle/button-toggle.scss @@ -121,6 +121,8 @@ $_standard-tokens: ( } .mat-button-toggle-disabled { + pointer-events: none; + @include token-utils.use-tokens($_legacy-tokens...) { @include token-utils.create-token-slot(color, disabled-state-text-color); @include token-utils.create-token-slot(background-color, disabled-state-background-color); @@ -134,6 +136,10 @@ $_standard-tokens: ( } } +.mat-button-toggle-disabled-interactive { + pointer-events: auto; +} + .mat-button-toggle-appearance-standard { @include token-utils.use-tokens($_standard-tokens...) { $divider-color: token-utils.get-token-variable(divider-color); @@ -185,16 +191,15 @@ $_standard-tokens: ( @include token-utils.create-token-slot(background-color, state-layer-color); } - &:not(.mat-button-toggle-disabled):hover .mat-button-toggle-focus-overlay { + &:hover .mat-button-toggle-focus-overlay { @include token-utils.create-token-slot(opacity, hover-state-layer-opacity); } // Similar to components like the checkbox, slide-toggle and radio, we cannot show the focus // overlay for `.cdk-program-focused` because mouse clicks on the