Skip to content

Commit aba1bb3

Browse files
authored
refactor!: re-implement details and accordion structure (#5093)
1 parent fe5d26d commit aba1bb3

37 files changed

Lines changed: 1024 additions & 512 deletions

‎dev/accordion.html‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99

1010
<script type="module">
1111
import '@vaadin/accordion';
12+
// import '@vaadin/accordion/theme/material/vaadin-accordion.js';
1213
</script>
1314
</head>
1415

1516
<body>
1617
<vaadin-accordion>
1718
<vaadin-accordion-panel>
18-
<div slot="summary">Panel 1</div>
19+
<vaadin-accordion-heading slot="summary">Panel 1</vaadin-accordion-heading>
1920
Panel 1 content
2021
</vaadin-accordion-panel>
2122
<vaadin-accordion-panel>
22-
<div slot="summary">Panel 2</div>
23+
<vaadin-accordion-heading slot="summary">Panel 2</vaadin-accordion-heading>
2324
Panel 2 content
2425
</vaadin-accordion-panel>
2526
<vaadin-accordion-panel>
26-
<div slot="summary">Panel 3</div>
27+
<vaadin-accordion-heading slot="summary">Panel 3</vaadin-accordion-heading>
2728
Panel 3 content
2829
</vaadin-accordion-panel>
2930
</vaadin-accordion>

‎dev/details.html‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
<script type="module">
1111
import '@vaadin/details';
1212
import '@vaadin/tooltip';
13+
// import '@vaadin/details/theme/material/vaadin-details.js';
14+
// import '@vaadin/tooltip/theme/material/vaadin-tooltip.js';
1315
</script>
1416
</head>
1517

1618
<body>
1719
<vaadin-details>
18-
<div slot="summary">Expandable Details</div>
19-
Toggle using mouse, Enter and Space keys.
20+
<vaadin-details-summary slot="summary">Expandable Details</vaadin-details-summary>
21+
<div>Toggle using mouse, Enter and Space keys.</div>
2022
<vaadin-tooltip slot="tooltip" text="Click to toggle"></vaadin-tooltip>
2123
</vaadin-details>
2224
</body>

‎integration/tests/component-tooltip.test.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ import { mouseenter, mouseleave } from '@vaadin/tooltip/test/helpers.js';
4949
position: 'top',
5050
applyShouldNotShowCondition: (element) => element.querySelector('input').click(),
5151
},
52-
{ tagName: Details.is, targetSelector: '[part="summary"]', position: 'bottom-start' },
52+
{
53+
tagName: Details.is,
54+
children: '<vaadin-details-summary slot="summary"></vaadin-details-summary>',
55+
targetSelector: '[slot="summary"]',
56+
position: 'bottom-start',
57+
},
5358
{ tagName: EmailField.is, position: 'top' },
5459
{ tagName: Icon.is },
5560
{ tagName: IntegerField.is, position: 'top' },
@@ -88,7 +93,7 @@ import { mouseenter, mouseleave } from '@vaadin/tooltip/test/helpers.js';
8893
});
8994

9095
it('should set tooltip target', () => {
91-
const target = targetSelector ? element.shadowRoot.querySelector(targetSelector) : element;
96+
const target = targetSelector ? element.querySelector(targetSelector) : element;
9297
expect(tooltip.target).to.equal(target);
9398
});
9499

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
2+
import { fixtureSync, nextRender, oneEvent } from '@vaadin/testing-helpers';
33
import { sendKeys } from '@web/test-runner-commands';
44
import './not-animated-styles.js';
55
import '@vaadin/accordion';
@@ -14,56 +14,58 @@ describe('accordion in dialog', () => {
1414
root.innerHTML = `
1515
<vaadin-accordion>
1616
<vaadin-accordion-panel>
17-
<div slot="summary">Panel 1</div>
18-
<button>Button 1</button>
17+
<vaadin-accordion-heading slot="summary">Panel 1</vaadin-accordion-heading>
18+
<div>
19+
<input type="text" placeholder="Input 1" />
20+
</div>
1921
</vaadin-accordion-panel>
2022
<vaadin-accordion-panel>
21-
<div slot="summary">Panel 2</div>
22-
<button>Button 2</button>
23+
<vaadin-accordion-heading slot="summary">Panel 2</vaadin-accordion-heading>
24+
<div>Content 2</div>
2325
</vaadin-accordion-panel>
2426
</vaadin-accordion>
2527
`;
2628
};
2729
dialog.opened = true;
2830
overlay = dialog.$.overlay;
2931
await oneEvent(overlay, 'vaadin-overlay-open');
30-
await nextFrame();
32+
await nextRender();
3133
accordion = overlay.querySelector('vaadin-accordion');
3234
panels = accordion.items;
3335
});
3436

3537
it('should move focus from panel heading to content on Tab', async () => {
3638
// Focus first panel (opened)
3739
await sendKeys({ press: 'Tab' });
38-
expect(document.activeElement).to.equal(panels[0]);
40+
expect(document.activeElement).to.equal(panels[0].focusElement);
3941

4042
// Move focus to the content
4143
await sendKeys({ press: 'Tab' });
42-
expect(document.activeElement).to.equal(panels[0].querySelector('button'));
44+
expect(document.activeElement).to.equal(panels[0].querySelector('input'));
4345
});
4446

4547
it('should move focus from panel content to heading on Shift + Tab', async () => {
4648
// Focus the first panel content
47-
panels[0].querySelector('button').focus();
49+
panels[0].querySelector('input').focus();
4850

4951
// Move focus back to heading
5052
await sendKeys({ down: 'Shift' });
5153
await sendKeys({ press: 'Tab' });
5254
await sendKeys({ up: 'Shift' });
5355

54-
expect(document.activeElement).to.equal(panels[0]);
56+
expect(document.activeElement).to.equal(panels[0].focusElement);
5557
});
5658

5759
it('should skip focusable element in closed panel on Tab', async () => {
5860
accordion.opened = 1;
5961

6062
// Focus first panel (closed)
6163
await sendKeys({ press: 'Tab' });
62-
expect(document.activeElement).to.equal(panels[0]);
64+
expect(document.activeElement).to.equal(panels[0].focusElement);
6365

6466
// Focus second panel (opened)
6567
await sendKeys({ press: 'Tab' });
66-
expect(document.activeElement).to.equal(panels[1]);
68+
expect(document.activeElement).to.equal(panels[1].focusElement);
6769
});
6870

6971
it('should skip focusable element in closed panel on Shift + Tab', async () => {
@@ -77,6 +79,6 @@ describe('accordion in dialog', () => {
7779
await sendKeys({ press: 'Tab' });
7880
await sendKeys({ up: 'Shift' });
7981

80-
expect(document.activeElement).to.equal(panels[0]);
82+
expect(document.activeElement).to.equal(panels[0].focusElement);
8183
});
8284
});

‎packages/accordion/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ A web component for displaying a vertically stacked set of expandable panels.
1010
```html
1111
<vaadin-accordion>
1212
<vaadin-accordion-panel theme="filled">
13-
<div slot="summary">Accordion Panel 1</div>
13+
<vaadin-accordion-heading slot="summary">Accordion Panel 1</vaadin-accordion-heading>
1414
<div>Accordion is a set of expandable sections.</div>
1515
</vaadin-accordion-panel>
1616
<vaadin-accordion-panel theme="filled">
17-
<div slot="summary">Accordion Panel 2</div>
17+
<vaadin-accordion-heading slot="summary">Accordion Panel 2</vaadin-accordion-heading>
1818
<div>Only one accordion panel can be opened.</div>
1919
</vaadin-accordion-panel>
2020
</vaadin-accordion>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 { ActiveMixin } from '@vaadin/component-base/src/active-mixin.js';
7+
import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js';
8+
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
9+
10+
/**
11+
* The accordion heading element.
12+
*
13+
* `vaadin-accordion-heading` is the element for the headings in the accordion.
14+
* As recommended by the WAI ARIA Best Practices, each heading needs to wrap a
15+
* `<button>`. This element puts that button in the Shadow DOM, as it is more
16+
* convenient to use and doesn’t make styling of the heading more problematic.
17+
*
18+
* The WAI ARIA Best Practices also recommend setting `aria-level` depending
19+
* on what level the headings are. It is hard to determine the level of a heading
20+
* algorithmically, and setting it is not strictly required to have an accessible
21+
* accordion. To keep things easier to use, this element does not set `aria-level`
22+
* attribute but leaves that to the developer creating an accordion.
23+
*
24+
* ### Styling
25+
*
26+
* The following shadow DOM parts are exposed for styling:
27+
*
28+
* Part name | Description
29+
* -----------|-------------------
30+
* `toggle` | The icon element
31+
* `content` | The content wrapper
32+
*
33+
* The following state attributes are available for styling:
34+
*
35+
* Attribute | Description
36+
* -------------| -----------
37+
* `active` | Set when the element is pressed down, either with mouse, touch or the keyboard.
38+
* `opened` | Set when the collapsible content is expanded and visible.
39+
* `disabled` | Set when the element is disabled.
40+
*
41+
* See [Styling Components](https://vaadin.com/docs/latest/styling/custom-theme/styling-components) documentation.
42+
*
43+
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
44+
*/
45+
declare class AccordionHeading extends ActiveMixin(DirMixin(ThemableMixin(HTMLElement))) {
46+
/**
47+
* When true, the element is opened.
48+
*/
49+
opened: boolean;
50+
}
51+
52+
declare global {
53+
interface HTMLElementTagNameMap {
54+
'vaadin-accordion-heading': AccordionHeading;
55+
}
56+
}
57+
58+
export { AccordionHeading };
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
7+
import { ActiveMixin } from '@vaadin/component-base/src/active-mixin.js';
8+
import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js';
9+
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
10+
11+
/**
12+
* The accordion heading element.
13+
*
14+
* `vaadin-accordion-heading` is the element for the headings in the accordion.
15+
* As recommended by the WAI ARIA Best Practices, each heading needs to wrap a
16+
* `<button>`. This element puts that button in the Shadow DOM, as it is more
17+
* convenient to use and doesn’t make styling of the heading more problematic.
18+
*
19+
* The WAI ARIA Best Practices also recommend setting `aria-level` depending
20+
* on what level the headings are. It is hard to determine the level of a heading
21+
* algorithmically, and setting it is not strictly required to have an accessible
22+
* accordion. To keep things easier to use, this element does not set `aria-level`
23+
* attribute but leaves that to the developer creating an accordion.
24+
*
25+
* ### Styling
26+
*
27+
* The following shadow DOM parts are exposed for styling:
28+
*
29+
* Part name | Description
30+
* -----------|-------------------
31+
* `toggle` | The icon element
32+
* `content` | The content wrapper
33+
*
34+
* The following state attributes are available for styling:
35+
*
36+
* Attribute | Description
37+
* -------------| -----------
38+
* `active` | Set when the element is pressed down, either with mouse, touch or the keyboard.
39+
* `opened` | Set when the collapsible content is expanded and visible.
40+
* `disabled` | Set when the element is disabled.
41+
*
42+
* See [Styling Components](https://vaadin.com/docs/latest/styling/custom-theme/styling-components) documentation.
43+
*
44+
* @extends HTMLElement
45+
* @mixes ActiveMixin
46+
* @mixes DirMixin
47+
* @mixes ThemableMixin
48+
*/
49+
class AccordionHeading extends ActiveMixin(DirMixin(ThemableMixin(PolymerElement))) {
50+
static get is() {
51+
return 'vaadin-accordion-heading';
52+
}
53+
54+
static get template() {
55+
return html`
56+
<style>
57+
:host {
58+
display: block;
59+
outline: none;
60+
-webkit-user-select: none;
61+
-moz-user-select: none;
62+
user-select: none;
63+
}
64+
65+
:host([hidden]) {
66+
display: none !important;
67+
}
68+
69+
button {
70+
display: flex;
71+
align-items: center;
72+
width: 100%;
73+
margin: 0;
74+
padding: 0;
75+
background-color: initial;
76+
color: inherit;
77+
border: initial;
78+
outline: none;
79+
font: inherit;
80+
text-align: inherit;
81+
}
82+
</style>
83+
<button id="button" part="content" disabled$="[[disabled]]">
84+
<span part="toggle" aria-hidden="true"></span>
85+
<slot></slot>
86+
</button>
87+
`;
88+
}
89+
90+
static get properties() {
91+
return {
92+
/**
93+
* When true, the element is opened.
94+
*/
95+
opened: {
96+
type: Boolean,
97+
reflectToAttribute: true,
98+
},
99+
};
100+
}
101+
102+
/**
103+
* @param {DocumentFragment} dom
104+
* @return {null}
105+
* @protected
106+
* @override
107+
*/
108+
_attachDom(dom) {
109+
const root = this.attachShadow({ mode: 'open', delegatesFocus: true });
110+
root.appendChild(dom);
111+
return root;
112+
}
113+
114+
/** @protected */
115+
ready() {
116+
super.ready();
117+
118+
// By default, if the user hasn't provided a custom role,
119+
// the role attribute is set to "heading".
120+
if (!this.hasAttribute('role')) {
121+
this.setAttribute('role', 'heading');
122+
}
123+
}
124+
}
125+
126+
customElements.define(AccordionHeading.is, AccordionHeading);
127+
128+
export { AccordionHeading };

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
* Copyright (c) 2019 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { Details } from '@vaadin/details/src/vaadin-details.js';
6+
import { DetailsMixin } from '@vaadin/details/src/vaadin-details-mixin.js';
7+
import { DelegateFocusMixin } from '@vaadin/field-base/src/delegate-focus-mixin.js';
8+
import { DelegateStateMixin } from '@vaadin/field-base/src/delegate-state-mixin.js';
9+
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
710

811
/**
912
* Fired when the `opened` property changes.
@@ -25,9 +28,6 @@ export type AccordionPanelEventMap = AccordionPanelCustomEventMap & HTMLElementE
2528
*
2629
* Part name | Description
2730
* -----------------|----------------
28-
* `summary` | The element used to open and close collapsible content.
29-
* `toggle` | The element used as indicator, can represent an icon.
30-
* `summary-content`| The wrapper for the slotted summary content.
3131
* `content` | The wrapper for the collapsible panel content.
3232
*
3333
* The following attributes are exposed for styling:
@@ -43,7 +43,7 @@ export type AccordionPanelEventMap = AccordionPanelCustomEventMap & HTMLElementE
4343
*
4444
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
4545
*/
46-
declare class AccordionPanel extends Details {
46+
declare class AccordionPanel extends DetailsMixin(DelegateFocusMixin(DelegateStateMixin(ThemableMixin(HTMLElement)))) {
4747
addEventListener<K extends keyof AccordionPanelEventMap>(
4848
type: K,
4949
listener: (this: AccordionPanel, ev: AccordionPanelEventMap[K]) => void,

0 commit comments

Comments
 (0)