Skip to content

Commit

Permalink
fix(shared): allow "disabled" first to return to "tabindex=0" in "foc…
Browse files Browse the repository at this point in the history
…usable"
  • Loading branch information
Westbrook committed May 12, 2023
1 parent 04ccb39 commit 160bc59
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/button/src/ButtonBase.ts
Expand Up @@ -208,7 +208,7 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [
protected override firstUpdated(changed: PropertyValues): void {
super.firstUpdated(changed);
if (!this.hasAttribute('tabindex')) {
this.tabIndex = 0;
this.setAttribute('tabindex', '0');
}
this.manageAnchor();
this.addEventListener('keydown', this.handleKeydown);
Expand Down
23 changes: 23 additions & 0 deletions packages/button/test/button.test.ts
Expand Up @@ -226,6 +226,29 @@ describe('Button', () => {
await elementUpdated(el);
expect(clickSpy.calledOnce).to.be.true;
});
it('`disabled` manages `tabindex`', async () => {
const el = await fixture<Button>(
html`
<sp-button disabled>Button</sp-button>
`
);

await elementUpdated(el);
expect(el.tabIndex).to.equal(-1);
expect(el.getAttribute('tabindex')).to.equal('-1');

el.disabled = false;
await elementUpdated(el);

expect(el.tabIndex).to.equal(0);
expect(el.getAttribute('tabindex')).to.equal('0');

el.disabled = true;
await elementUpdated(el);

expect(el.tabIndex).to.equal(-1);
expect(el.getAttribute('tabindex')).to.equal('-1');
});
it('manages `aria-disabled`', async () => {
const el = await fixture<Button>(
html`
Expand Down
2 changes: 1 addition & 1 deletion packages/swatch/src/Swatch.ts
Expand Up @@ -237,7 +237,7 @@ export class Swatch extends SizedMixin(Focusable, {
this.addEventListener('keydown', this.handleKeydown);
this.addEventListener('keypress', this.handleKeypress);
if (!this.hasAttribute('tabindex')) {
this.tabIndex = 0;
this.setAttribute('tabindex', '0');
}
}
}
7 changes: 1 addition & 6 deletions packages/tags/src/Tag.ts
Expand Up @@ -137,12 +137,7 @@ export class Tag extends SizedMixin(SpectrumElement, {
this.setAttribute('role', 'listitem');
}
if (this.deletable) {
this.setAttribute(
'tabindex',
!this.disabled && this.matches(':first-of-type:not([disabled])')
? '0'
: '-1'
);
this.setAttribute('tabindex', '0');
}
}

Expand Down
2 changes: 1 addition & 1 deletion tools/shared/src/focusable.ts
Expand Up @@ -123,7 +123,7 @@ export class Focusable extends FocusVisiblePolyfillMixin(SpectrumElement) {
}
this.manageFocusElementTabindex(tabIndex);
}
private _tabIndex = -1;
private _tabIndex = 0;

private onPointerdownManagementOfTabIndex(): void {
if (this.tabIndex === -1) {
Expand Down

0 comments on commit 160bc59

Please sign in to comment.