Skip to content

Commit 5905e14

Browse files
authored
feat: add summary property to details and accordion panel (#5164)
1 parent 2743423 commit 5905e14

12 files changed

Lines changed: 440 additions & 153 deletions

‎dev/accordion.html‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
<vaadin-accordion-heading slot="summary">Panel 2</vaadin-accordion-heading>
2424
<div>Panel 2 content</div>
2525
</vaadin-accordion-panel>
26-
<vaadin-accordion-panel>
27-
<vaadin-accordion-heading slot="summary">Panel 3</vaadin-accordion-heading>
26+
<vaadin-accordion-panel summary="Panel 3">
2827
<div>Panel 3 content</div>
2928
</vaadin-accordion-panel>
3029
</vaadin-accordion>

‎dev/details.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
</head>
1717

1818
<body>
19+
<vaadin-details summary="Expandable Details">
20+
<div>Toggle using mouse, Enter and Space keys.</div>
21+
</vaadin-details>
22+
23+
<!--
1924
<vaadin-details>
2025
<vaadin-details-summary slot="summary">Expandable Details</vaadin-details-summary>
2126
<div>Toggle using mouse, Enter and Space keys.</div>
2227
<vaadin-tooltip slot="tooltip" text="Click to toggle"></vaadin-tooltip>
2328
</vaadin-details>
29+
-->
2430
</body>
2531
</html>

‎packages/accordion/src/vaadin-accordion-panel.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export type AccordionPanelEventMap = AccordionPanelCustomEventMap & HTMLElementE
4444
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
4545
*/
4646
declare class AccordionPanel extends DetailsMixin(DelegateFocusMixin(DelegateStateMixin(ThemableMixin(HTMLElement)))) {
47+
/**
48+
* A text that is displayed in the heading, if no
49+
* element is assigned to the `summary` slot.
50+
*/
51+
summary: string | null | undefined;
52+
4753
addEventListener<K extends keyof AccordionPanelEventMap>(
4854
type: K,
4955
listener: (this: AccordionPanel, ev: AccordionPanelEventMap[K]) => void,

‎packages/accordion/src/vaadin-accordion-panel.js‎

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,11 @@ import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
88
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
99
import { DelegateFocusMixin } from '@vaadin/component-base/src/delegate-focus-mixin.js';
1010
import { DelegateStateMixin } from '@vaadin/component-base/src/delegate-state-mixin.js';
11-
import { SlotChildObserveController } from '@vaadin/component-base/src/slot-child-observe-controller.js';
1211
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
12+
import { SummaryController } from '@vaadin/details/src/summary-controller.js';
1313
import { DetailsMixin } from '@vaadin/details/src/vaadin-details-mixin.js';
1414
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
1515

16-
class SummaryController extends SlotChildObserveController {
17-
constructor(host) {
18-
super(host, 'summary', 'vaadin-accordion-heading', {
19-
initializer: (node, host) => {
20-
host._setFocusElement(node);
21-
host.stateTarget = node;
22-
},
23-
});
24-
}
25-
}
26-
2716
/**
2817
* The accordion panel element.
2918
*
@@ -94,6 +83,23 @@ class AccordionPanel extends DetailsMixin(
9483
`;
9584
}
9685

86+
static get properties() {
87+
return {
88+
/**
89+
* A text that is displayed in the heading, if no
90+
* element is assigned to the `summary` slot.
91+
*/
92+
summary: {
93+
type: String,
94+
observer: '_summaryChanged',
95+
},
96+
};
97+
}
98+
99+
static get observers() {
100+
return ['__updateAriaAttributes(focusElement, _contentElements)'];
101+
}
102+
97103
static get delegateAttrs() {
98104
return ['theme'];
99105
}
@@ -105,21 +111,26 @@ class AccordionPanel extends DetailsMixin(
105111
constructor() {
106112
super();
107113

108-
this._summaryController = new SummaryController(this);
114+
this._summaryController = new SummaryController(this, 'vaadin-accordion-heading');
115+
this._summaryController.addEventListener('slot-content-changed', (event) => {
116+
const { node } = event.target;
117+
118+
this._setFocusElement(node);
119+
this.stateTarget = node;
120+
121+
this._tooltipController.setTarget(node);
122+
});
123+
109124
this._tooltipController = new TooltipController(this);
125+
this._tooltipController.setPosition('bottom-start');
110126
}
111127

112128
/** @protected */
113129
ready() {
114130
super.ready();
115131

116132
this.addController(this._summaryController);
117-
118133
this.addController(this._tooltipController);
119-
this._tooltipController.setTarget(this.focusElement);
120-
this._tooltipController.setPosition('bottom-start');
121-
122-
this._initContent();
123134
}
124135

125136
/**
@@ -134,26 +145,26 @@ class AccordionPanel extends DetailsMixin(
134145
}
135146

136147
/** @private */
137-
_initContent() {
138-
this._contentController.addEventListener('slot-content-changed', (event) => {
139-
// Store nodes to toggle `aria-hidden` attribute
140-
const content = event.target.nodes || [];
141-
this._contentElements = content;
142-
143-
// See https://www.w3.org/WAI/ARIA/apg/patterns/accordion/
144-
const node = content[0];
148+
_summaryChanged(summary) {
149+
this._summaryController.setSummary(summary);
150+
}
151+
152+
/** @private */
153+
__updateAriaAttributes(focusElement, contentElements) {
154+
if (focusElement && contentElements) {
155+
const node = contentElements[0];
156+
145157
if (node) {
146158
node.setAttribute('role', 'region');
147-
node.setAttribute('aria-labelledby', this.focusElement.id);
159+
node.setAttribute('aria-labelledby', focusElement.id);
148160
}
149161

150-
if (node && node.parentNode === this && node.id) {
151-
this.focusElement.setAttribute('aria-controls', node.id);
162+
if (node && node.id) {
163+
focusElement.setAttribute('aria-controls', node.id);
152164
} else {
153-
this.focusElement.removeAttribute('aria-controls');
165+
focusElement.removeAttribute('aria-controls');
154166
}
155-
});
156-
this.addController(this._contentController);
167+
}
157168
}
158169
}
159170

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
3+
import { sendKeys } from '@web/test-runner-commands';
4+
import sinon from 'sinon';
5+
import '../vaadin-accordion-panel.js';
6+
7+
describe('vaadin-accordion-panel', () => {
8+
let panel;
9+
10+
describe('opened', () => {
11+
let contentPart, contentNode;
12+
13+
beforeEach(async () => {
14+
panel = fixtureSync(`
15+
<vaadin-accordion-panel>
16+
<div>Content</div>
17+
</vaadin-accordion-panel>
18+
`);
19+
await nextRender();
20+
contentPart = panel.shadowRoot.querySelector('[part="content"]');
21+
contentNode = panel.querySelector('div');
22+
});
23+
24+
it('should set opened to false by default', () => {
25+
expect(panel.opened).to.be.false;
26+
});
27+
28+
it('should reflect opened property to attribute', () => {
29+
panel.opened = true;
30+
expect(panel.hasAttribute('opened')).to.be.true;
31+
});
32+
33+
it('should hide the content when opened is false', () => {
34+
expect(getComputedStyle(contentPart).display).to.equal('none');
35+
});
36+
37+
it('should show the content when opened is true', () => {
38+
panel.opened = true;
39+
expect(getComputedStyle(contentPart).display).to.equal('block');
40+
});
41+
42+
it('should set aria-hidden on the slotted element to true by default', () => {
43+
expect(contentNode.getAttribute('aria-hidden')).to.equal('true');
44+
});
45+
46+
it('should set aria-hidden on the slotted element to false when opened', () => {
47+
panel.opened = true;
48+
expect(contentNode.getAttribute('aria-hidden')).to.equal('false');
49+
});
50+
});
51+
52+
['default', 'custom'].forEach((type) => {
53+
const fixtures = {
54+
default: `
55+
<vaadin-accordion-panel summary="Summary">
56+
<div>Content</div>
57+
</vaadin-accordion-panel>
58+
`,
59+
custom: `
60+
<vaadin-accordion-panel>
61+
<vaadin-accordion-heading slot="summary">Summary</vaadin-accordion-heading>
62+
<div>Content</div>
63+
</vaadin-accordion-panel>
64+
`,
65+
};
66+
67+
let heading;
68+
69+
describe(`${type} summary`, () => {
70+
let toggle;
71+
72+
beforeEach(async () => {
73+
panel = fixtureSync(fixtures[type]);
74+
await nextRender();
75+
heading = panel.querySelector('[slot="summary"]');
76+
toggle = heading.shadowRoot.querySelector('button');
77+
});
78+
79+
it(`should toggle opened on ${type} heading button click`, () => {
80+
toggle.click();
81+
expect(panel.opened).to.be.true;
82+
83+
toggle.click();
84+
expect(panel.opened).to.be.false;
85+
});
86+
87+
it(`should toggle opened on ${type} heading button Enter`, async () => {
88+
toggle.focus();
89+
90+
await sendKeys({ press: 'Enter' });
91+
expect(panel.opened).to.be.true;
92+
93+
await sendKeys({ press: 'Enter' });
94+
expect(panel.opened).to.be.false;
95+
});
96+
97+
it(`should toggle opened on ${type} heading button Space`, async () => {
98+
toggle.focus();
99+
100+
await sendKeys({ press: 'Space' });
101+
expect(panel.opened).to.be.true;
102+
103+
await sendKeys({ press: 'Space' });
104+
expect(panel.opened).to.be.false;
105+
});
106+
107+
it(`should not update opened on ${type} heading button Arrow Down`, async () => {
108+
toggle.focus();
109+
await sendKeys({ press: 'ArrowDown' });
110+
expect(panel.opened).to.be.false;
111+
});
112+
113+
it(`should fire opened-changed event on ${type} heading button click`, () => {
114+
const spy = sinon.spy();
115+
panel.addEventListener('opened-changed', spy);
116+
toggle.click();
117+
expect(spy.calledOnce).to.be.true;
118+
});
119+
120+
it(`should update aria-expanded on ${type} heading button click`, () => {
121+
toggle.click();
122+
expect(toggle.getAttribute('aria-expanded')).to.equal('true');
123+
124+
toggle.click();
125+
expect(toggle.getAttribute('aria-expanded')).to.equal('false');
126+
});
127+
128+
it(`should propagate disabled attribute to ${type} heading`, () => {
129+
panel.disabled = true;
130+
expect(heading.hasAttribute('disabled')).to.be.true;
131+
132+
panel.disabled = false;
133+
expect(heading.hasAttribute('disabled')).to.be.false;
134+
});
135+
});
136+
});
137+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 - 2022 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { SlotChildObserveController } from '@vaadin/component-base/src/slot-child-observe-controller.js';
7+
8+
/**
9+
* A controller to manage the summary element.
10+
*/
11+
export class SummarController extends SlotChildObserveController {
12+
/**
13+
* String used for the summary.
14+
*/
15+
protected summary: string | null | undefined;
16+
17+
/**
18+
* Set summary based on corresponding host property.
19+
*/
20+
setSummary(label: string | null | undefined): void;
21+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2019 - 2022 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { SlotChildObserveController } from '@vaadin/component-base/src/slot-child-observe-controller.js';
7+
8+
/**
9+
* A controller to manage the summary element.
10+
*/
11+
export class SummaryController extends SlotChildObserveController {
12+
constructor(host, tagName) {
13+
super(host, 'summary', tagName);
14+
}
15+
16+
/**
17+
* Set summary based on corresponding host property.
18+
*
19+
* @param {string} summary
20+
*/
21+
setSummary(summary) {
22+
this.summary = summary;
23+
24+
// Restore the default summary, if needed.
25+
const summaryNode = this.getSlotChild();
26+
if (!summaryNode) {
27+
this.restoreDefaultNode();
28+
}
29+
30+
// When default summary is used, update it.
31+
if (this.node === this.defaultNode) {
32+
this.updateDefaultNode(this.node);
33+
}
34+
}
35+
36+
/**
37+
* Override method inherited from `SlotChildObserveController`
38+
* to restore and observe the default summary element.
39+
*
40+
* @protected
41+
* @override
42+
*/
43+
restoreDefaultNode() {
44+
const { summary } = this;
45+
46+
// Restore the default summary.
47+
if (summary && summary.trim() !== '') {
48+
this.attachDefaultNode();
49+
}
50+
}
51+
52+
/**
53+
* Override method inherited from `SlotChildObserveController`
54+
* to update the default summary element text content.
55+
*
56+
* @param {Node | undefined} node
57+
* @protected
58+
* @override
59+
*/
60+
updateDefaultNode(node) {
61+
if (node) {
62+
node.textContent = this.summary;
63+
}
64+
65+
// Notify the host after update.
66+
super.updateDefaultNode(node);
67+
}
68+
}

0 commit comments

Comments
 (0)