Skip to content

Commit 097ef6c

Browse files
crisbetommalerba
authored andcommitted
fix(radio): host element unable to receive focus events (#13956)
In #13323 we started clearing the `tabindex` from the `host` element in order to avoid having two elements with the same `tabindex` when the consumer sets a custom `tabindex`. As a result, the `mat-radio-button` can no longer receive focus and forward it to the `input`. These changes always reset the `tabindex` to -1 so the element is focusable, but not tabbable. Fixes #13953.
1 parent c954c2a commit 097ef6c

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

src/lib/radio/radio.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ $mat-radio-ripple-radius: 20px;
1010
.mat-radio-button {
1111
display: inline-block;
1212
-webkit-tap-highlight-color: transparent;
13+
outline: 0;
1314
}
1415

1516
// Inner label container, wrapping entire element.
@@ -141,7 +142,7 @@ $mat-radio-ripple-radius: 20px;
141142
opacity: 0.04;
142143
}
143144

144-
.mat-radio-button.cdk-focused & {
145+
.mat-radio-button:not(.mat-radio-disabled).cdk-focused & {
145146
opacity: 0.12;
146147
}
147148

src/lib/radio/radio.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ describe('MatRadio', () => {
723723
const radioButtonEl =
724724
predefinedFixture.debugElement.query(By.css('.mat-radio-button')).nativeElement;
725725

726-
expect(radioButtonEl.getAttribute('tabindex')).toBeFalsy();
726+
expect(radioButtonEl.getAttribute('tabindex')).toBe('-1');
727727
});
728728

729729
});

src/lib/radio/radio.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ export const _MatRadioButtonMixinBase:
336336
'[class.mat-radio-checked]': 'checked',
337337
'[class.mat-radio-disabled]': 'disabled',
338338
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
339-
'[attr.tabindex]': 'null',
339+
// Needs to be -1 so the `focus` event still fires.
340+
'[attr.tabindex]': '-1',
340341
'[attr.id]': 'id',
341342
// Note: under normal conditions focus shouldn't land on this element, however it may be
342343
// programmatically set, for example inside of a focus trap, in this case we want to forward

0 commit comments

Comments
 (0)