Skip to content

Commit

Permalink
fix: contain activation to header content
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed May 12, 2022
1 parent 70e57ce commit 10183ce
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
17 changes: 1 addition & 16 deletions packages/accordion/src/AccordionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@ export class AccordionItem extends Focusable {
return this.shadowRoot.querySelector('#header') as HTMLElement;
}

constructor() {
super();
this.addEventListener('keydown', this.onKeyDown);
}

private onKeyDown(event: KeyboardEvent): void {
/* c8 ignore next 3 */
if (this.disabled) {
return;
}
if (event.code === 'Enter' || event.code === 'Space') {
event.preventDefault();
this.toggle();
}
}

private onClick(): void {
/* c8 ignore next 3 */
if (this.disabled) {
Expand Down Expand Up @@ -92,6 +76,7 @@ export class AccordionItem extends Focusable {
@click=${this.onClick}
aria-expanded=${this.open}
aria-controls="content"
?disabled=${this.disabled}
>
${this.label}
</button>
Expand Down
16 changes: 15 additions & 1 deletion packages/accordion/stories/accordion.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { html, TemplateResult } from '@spectrum-web-components/base';

import '../sp-accordion.js';
import '../sp-accordion-item.js';
import '@spectrum-web-components/link/sp-link.js';

export default {
title: 'Accordion',
Expand Down Expand Up @@ -61,7 +62,20 @@ const Template = (
<sp-accordion-item label="Heading 2" ?open=${open}>
Item 2
</sp-accordion-item>
<sp-accordion-item label="Heading 3">Item 3</sp-accordion-item>
<sp-accordion-item label="Heading 3">
<p>
This is content that has a
<sp-link
href="http://opensource.adobe.com/spectrum-web-components"
target="_blank"
>
link back to Spectrum Web Components
</sp-link>
so that it is easy to test that "Space" and "Enter"
interactions on focusable content does NOT toggle the
Accordion Item.
</p>
</sp-accordion-item>
</sp-accordion>
`;
};
Expand Down
67 changes: 62 additions & 5 deletions packages/accordion/test/accordion-item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { spy } from 'sinon';

import '../sp-accordion-item.js';
import { AccordionItem } from '../src/AccordionItem';
import { enterEvent, spaceEvent } from '../../../test/testing-helpers.js';
import { sendKeys } from '@web/test-runner-commands';

describe('Accordion Item', () => {
it('can exist with no parent accessibly', async () => {
Expand Down Expand Up @@ -88,7 +88,10 @@ describe('Accordion Item', () => {

expect(open).to.be.false;

el.dispatchEvent(enterEvent());
el.focus();
await sendKeys({
press: 'Enter',
});

await elementUpdated(el);

Expand All @@ -97,7 +100,10 @@ describe('Accordion Item', () => {
el.disabled = false;
await elementUpdated(el);

el.dispatchEvent(enterEvent());
el.focus();
await sendKeys({
press: 'Enter',
});

await elementUpdated(el);

Expand All @@ -124,7 +130,10 @@ describe('Accordion Item', () => {

expect(open).to.be.false;

el.dispatchEvent(spaceEvent());
el.focus();
await sendKeys({
press: 'Space',
});

await elementUpdated(el);

Expand All @@ -133,10 +142,58 @@ describe('Accordion Item', () => {
el.disabled = false;
await elementUpdated(el);

el.dispatchEvent(spaceEvent());
el.focus();
await sendKeys({
press: 'Space',
});

await elementUpdated(el);

expect(open).to.be.true;
});

it('does not dispatch toggle events on key events in Item content', async () => {
let closed = false;
const onAccordionToggle = (): void => {
closed = true;
};
const el = await fixture<AccordionItem>(
html`
<sp-accordion-item
open
@sp-accordion-item-toggle=${onAccordionToggle}
>
<div>
<button>Test Button</button>
</div>
</sp-accordion-item>
`
);

const button = el.querySelector('button') as HTMLButtonElement;
await elementUpdated(el);

expect(el.open).to.be.true;
expect(closed).to.be.false;

button.focus();
await sendKeys({
press: 'Space',
});

await elementUpdated(el);

expect(closed).to.be.false;

await elementUpdated(el);

await sendKeys({
press: 'Enter',
});

await elementUpdated(el);

expect(closed).to.be.false;
expect(el.open).to.be.true;
});
});

0 comments on commit 10183ce

Please sign in to comment.