Skip to content

Commit c72650f

Browse files
Merge pull request #5787 from material-components:disabled-link-support
PiperOrigin-RevId: 742326684
2 parents ed14c53 + c3c4848 commit c72650f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

Diff for: button/demo/stories.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,49 @@ const buttons: MaterialStoryInit<StoryKnobs> = {
120120
const links: MaterialStoryInit<StoryKnobs> = {
121121
name: 'Links',
122122
styles,
123-
render({label}) {
123+
render({label, disabled, softDisabled}) {
124124
return html`
125125
<div class="column">
126126
<div class="row">
127127
<md-filled-button
128+
?disabled=${disabled}
129+
?soft-disabled=${softDisabled}
128130
href="https://google.com"
129131
target="_blank"
130132
trailing-icon>
131133
${label || 'Filled'}
132134
</md-filled-button>
133135
134136
<md-outlined-button
137+
?disabled=${disabled}
138+
?soft-disabled=${softDisabled}
135139
href="https://google.com"
136140
target="_blank"
137141
trailing-icon>
138142
${label || 'Outlined'}
139143
</md-outlined-button>
140144
141145
<md-elevated-button
146+
?disabled=${disabled}
147+
?soft-disabled=${softDisabled}
142148
href="https://google.com"
143149
target="_blank"
144150
trailing-icon>
145151
${label || 'Elevated'}
146152
</md-elevated-button>
147153
148154
<md-filled-tonal-button
155+
?disabled=${disabled}
156+
?soft-disabled=${softDisabled}
149157
href="https://google.com"
150158
target="_blank"
151159
trailing-icon>
152160
${label || 'Tonal'}
153161
</md-filled-tonal-button>
154162
155163
<md-text-button
164+
?disabled=${disabled}
165+
?soft-disabled=${softDisabled}
156166
href="https://google.com"
157167
target="_blank"
158168
trailing-icon>
@@ -162,6 +172,8 @@ const links: MaterialStoryInit<StoryKnobs> = {
162172
<div class="row">
163173
<md-filled-button
164174
aria-label="${label || 'Filled'} link with trailing icon"
175+
?disabled=${disabled}
176+
?soft-disabled=${softDisabled}
165177
href="https://google.com"
166178
target="_blank"
167179
trailing-icon>
@@ -171,6 +183,8 @@ const links: MaterialStoryInit<StoryKnobs> = {
171183
172184
<md-outlined-button
173185
aria-label="${label || 'Outlined'} link with trailing icon"
186+
?disabled=${disabled}
187+
?soft-disabled=${softDisabled}
174188
href="https://google.com"
175189
target="_blank"
176190
trailing-icon>
@@ -180,6 +194,8 @@ const links: MaterialStoryInit<StoryKnobs> = {
180194
181195
<md-elevated-button
182196
aria-label="${label || 'Elevated'} link with trailing icon"
197+
?disabled=${disabled}
198+
?soft-disabled=${softDisabled}
183199
href="https://google.com"
184200
target="_blank"
185201
trailing-icon>
@@ -189,6 +205,8 @@ const links: MaterialStoryInit<StoryKnobs> = {
189205
190206
<md-filled-tonal-button
191207
aria-label="${label || 'Tonal'} link with trailing icon"
208+
?disabled=${disabled}
209+
?soft-disabled=${softDisabled}
192210
href="https://google.com"
193211
target="_blank"
194212
trailing-icon>
@@ -198,6 +216,8 @@ const links: MaterialStoryInit<StoryKnobs> = {
198216
199217
<md-text-button
200218
aria-label="${label || 'Text'} link with trailing icon"
219+
?disabled=${disabled}
220+
?soft-disabled=${softDisabled}
201221
href="https://google.com"
202222
target="_blank"
203223
trailing-icon>

Diff for: button/internal/button.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ export abstract class Button extends buttonBaseClass implements FormSubmitter {
142142
}
143143

144144
protected override render() {
145-
// Link buttons may not be disabled
146-
const isRippleDisabled = !this.href && (this.disabled || this.softDisabled);
145+
const isRippleDisabled = this.disabled || this.softDisabled;
147146
const buttonOrLink = this.href ? this.renderLink() : this.renderButton();
148147
// TODO(b/310046938): due to a limitation in focus ring/ripple, we can't use
149148
// the same ID for different elements, so we change the ID instead.
@@ -190,6 +189,8 @@ export abstract class Button extends buttonBaseClass implements FormSubmitter {
190189
aria-label="${ariaLabel || nothing}"
191190
aria-haspopup="${ariaHasPopup || nothing}"
192191
aria-expanded="${ariaExpanded || nothing}"
192+
aria-disabled=${this.disabled || this.softDisabled || nothing}
193+
tabindex="${this.disabled && !this.softDisabled ? -1 : nothing}"
193194
href=${this.href}
194195
download=${this.download || nothing}
195196
target=${this.target || nothing}
@@ -211,10 +212,10 @@ export abstract class Button extends buttonBaseClass implements FormSubmitter {
211212
}
212213

213214
private handleClick(event: MouseEvent) {
214-
// If the button is soft-disabled, we need to explicitly prevent the click
215-
// from propagating to other event listeners as well as prevent the default
216-
// action.
217-
if (!this.href && this.softDisabled) {
215+
// If the button is soft-disabled or a disabled link, we need to explicitly
216+
// prevent the click from propagating to other event listeners as well as
217+
// prevent the default action.
218+
if (this.softDisabled || (this.disabled && this.href)) {
218219
event.stopImmediatePropagation();
219220
event.preventDefault();
220221
return;

0 commit comments

Comments
 (0)