Skip to content

Commit f98b233

Browse files
committed
Update defaults of allowMultipleExpanded and allowZeroExpanded to true
1 parent 041929b commit f98b233

File tree

12 files changed

+109
-132
lines changed

12 files changed

+109
-132
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
> All notable changes to this project are documented in this file. This project
44
> adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
55
6+
## [[v6.0.0]](https://github.com/springload/react-accessible-accordion/releases/tag/v6.0.0)
7+
8+
**A sort-of breaking change** which updates the default behavior regarding
9+
`allowMultipleExpanded` and `allowZeroExpanded`. They both now default to
10+
`true`.
11+
12+
This is based on
13+
[advice from NNG](https://www.nngroup.com/articles/accordions-on-desktop/), as
14+
well as Springload's views on accordion usability.
15+
16+
From this version onwards:
17+
18+
- to disable allowing more than one accordion panel to be open at once, you
19+
must explicitly set `allowMultipleExpanded={false}`
20+
- to disable collapsing all accordion panels, you must explicitly set
21+
`allowZeroExpanded={false}`
22+
623
## [[v5.0.0]](https://github.com/springload/react-accessible-accordion/releases/tag/v5.0.0)
724

825
React Accessible Accordion now supports React 18 with its out-of-order streaming

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ needs, particularly if you're using your own `className`s.
8585
8686
### Accordion
8787
88-
#### allowMultipleExpanded : `boolean` [*optional*, default: `false`]
88+
#### allowMultipleExpanded : `boolean` [*optional*, default: `true`]
8989
90-
Don't autocollapse items when expanding other items.
90+
Don't auto-collapse items when expanding other items.
9191
92-
#### allowZeroExpanded : `boolean` [*optional*, default: `false`]
92+
#### allowZeroExpanded : `boolean` [*optional*, default: `true`]
9393
9494
Allow the only remaining expanded item to be collapsed.
9595

demo/src/code-examples.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,6 @@ export const ExampleDefault = `import {
2121
))}
2222
</Accordion>`;
2323

24-
export const ExampleAllowMultipleExpanded = `<Accordion allowMultipleExpanded>
25-
{items.map((item) => (
26-
<AccordionItem key={item.uuid}>
27-
<AccordionItemHeading>
28-
<AccordionItemButton>
29-
{item.heading}
30-
</AccordionItemButton>
31-
</AccordionItemHeading>
32-
<AccordionItemPanel>
33-
{item.content}
34-
</AccordionItemPanel>
35-
</AccordionItem>
36-
))}
37-
</Accordion>`;
38-
3924
export const ExampleAllowMultipleExpandedFalse = `<Accordion allowMultipleExpanded={false}>
4025
{items.map((item) => (
4126
<AccordionItem key={item.uuid}>
@@ -51,7 +36,7 @@ export const ExampleAllowMultipleExpandedFalse = `<Accordion allowMultipleExpand
5136
))}
5237
</Accordion>`;
5338

54-
export const ExampleAllowZeroExpanded = `<Accordion allowZeroExpanded>
39+
export const ExampleAllowZeroExpandedFalse = `<Accordion allowZeroExpanded={false} preExpanded={['a']}>
5540
{items.map((item) => (
5641
<AccordionItem key={item.uuid}>
5742
<AccordionItemHeading>

demo/src/index.tsx

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import Code from './components/Code';
1818
// tslint:disable-next-line no-import-side-effect ordered-imports
1919
import {
2020
ExampleDefault,
21-
ExampleAllowMultipleExpanded,
2221
ExampleAllowMultipleExpandedFalse,
23-
ExampleAllowZeroExpanded,
22+
ExampleAllowZeroExpandedFalse,
2423
ExamplePreExpanded,
2524
ExampleOnChange,
2625
ExampleAccordionItemState,
@@ -54,8 +53,7 @@ const App = (): JSX.Element => (
5453
<h2 className="u-margin-top">Default behaviour</h2>
5554

5655
<p>
57-
By default, only one item may be expanded and it can only be
58-
collapsed again by expanding another.
56+
By default, any number of items may be expanded at any given time.
5957
</p>
6058

6159
<Accordion>
@@ -73,32 +71,15 @@ const App = (): JSX.Element => (
7371

7472
<Code code={ExampleDefault} />
7573

76-
<h2 className="u-margin-top">Expanding multiple items at once</h2>
77-
78-
<p>
79-
If you set <strong>allowMultipleExpanded</strong> to{' '}
80-
<strong>true</strong> then the accordion will permit multiple items
81-
to be expanded at once.
82-
</p>
83-
84-
<Accordion allowMultipleExpanded={true}>
85-
{placeholders.map((placeholder: Placeholder) => (
86-
<AccordionItem key={placeholder.heading}>
87-
<AccordionItemHeading>
88-
<AccordionItemButton>
89-
{placeholder.heading}
90-
</AccordionItemButton>
91-
</AccordionItemHeading>
92-
<AccordionItemPanel>{placeholder.panel}</AccordionItemPanel>
93-
</AccordionItem>
94-
))}
95-
</Accordion>
96-
97-
<Code code={ExampleAllowMultipleExpanded} />
98-
9974
<h2 className="u-margin-top">
100-
Same as above except with allowMultipleExpanded=false
75+
Prevent multiple items being expanded at a time
10176
</h2>
77+
<p>
78+
<strong>Note:</strong> we do not recommend this behavior. Users may
79+
wish to view the content of more than one panel at once. Also,
80+
collapsing a panel when opening another can cause unexpected scroll
81+
position changes.
82+
</p>
10283

10384
<Accordion allowMultipleExpanded={false}>
10485
{placeholders.map((placeholder: Placeholder) => (
@@ -115,17 +96,25 @@ const App = (): JSX.Element => (
11596

11697
<Code code={ExampleAllowMultipleExpandedFalse} />
11798

118-
<h2 className="u-margin-top">Collapsing the last expanded item</h2>
99+
<h2 className="u-margin-top">Pre-expanded items</h2>
119100

120101
<p>
121-
If you set <strong>allowZeroExpanded</strong> to{' '}
122-
<strong>true</strong> then a solitary expanded item may be collapsed
123-
again.
102+
If you set <strong>preExpanded</strong>, then you can choose which
103+
items are expanded on mount.
104+
</p>
105+
106+
<p>
107+
The strings passed to <strong>preExpanded</strong> are directly
108+
related to the <strong>uuid</strong> props of{' '}
109+
<strong>AccordionItem</strong>.
124110
</p>
125111

126-
<Accordion allowZeroExpanded={true}>
112+
<Accordion preExpanded={[placeholders[0].uuid]}>
127113
{placeholders.map((placeholder: Placeholder) => (
128-
<AccordionItem key={placeholder.heading}>
114+
<AccordionItem
115+
key={placeholder.heading}
116+
uuid={placeholder.uuid}
117+
>
129118
<AccordionItemHeading>
130119
<AccordionItemButton>
131120
{placeholder.heading}
@@ -136,22 +125,27 @@ const App = (): JSX.Element => (
136125
))}
137126
</Accordion>
138127

139-
<Code code={ExampleAllowZeroExpanded} />
128+
<Code code={ExamplePreExpanded} />
140129

141-
<h2 className="u-margin-top">Pre-expanded items</h2>
130+
<h2 className="u-margin-top">Preventing the collapsing of all items</h2>
142131

143132
<p>
144-
If you set <strong>preExpanded</strong>, then you can choose which
145-
items are expanded on mount.
133+
If you set <strong>allowZeroExpanded</strong> to{' '}
134+
<strong>false</strong> then the user must have at least one panel
135+
open at a time.
146136
</p>
147-
148137
<p>
149-
The strings passed to <strong>preExpanded</strong> are directly
150-
related to the <strong>uuid</strong> props of{' '}
151-
<strong>AccordionItem</strong>.
138+
<strong>Note:</strong> we do not recommend this behavior. Users
139+
would be able to expand items but not necessarily collapse them,
140+
which might not match their expectations. If you do choose to use
141+
this setting, we recommend you pair it with having{' '}
142+
<strong>preExpanded</strong> item(s).
152143
</p>
153144

154-
<Accordion preExpanded={[placeholders[0].uuid]}>
145+
<Accordion
146+
allowZeroExpanded={false}
147+
preExpanded={[placeholders[0].uuid]}
148+
>
155149
{placeholders.map((placeholder: Placeholder) => (
156150
<AccordionItem
157151
key={placeholder.heading}
@@ -167,7 +161,7 @@ const App = (): JSX.Element => (
167161
))}
168162
</Accordion>
169163

170-
<Code code={ExamplePreExpanded} />
164+
<Code code={ExampleAllowZeroExpandedFalse} />
171165

172166
<h2 className="u-margin-top">Informative onChange</h2>
173167

integration/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
ReactDOM.render(
1212
<div id="classic-accordion">
13-
<Accordion>
13+
<Accordion allowMultipleExpanded={false} allowZeroExpanded={false}>
1414
<AccordionItem>
1515
<AccordionItemHeading>
1616
<AccordionItemButton>Heading One</AccordionItemButton>

integration/wai-aria.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ describe('WAI ARIA Spec', () => {
371371
const { browser, page, buttonsHandles } = await setup();
372372
expect(buttonsHandles.length).toEqual(3);
373373

374-
const [firstButtonHandle] = buttonsHandles;
374+
const firstButtonHandle = buttonsHandles[0];
375375
await firstButtonHandle.click();
376376

377377
const buttonAriaDisabled = await page.evaluate(

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-accessible-accordion",
3-
"version": "5.0.0",
3+
"version": "6.0.0",
44
"description": "Accessible Accordion component for React",
55
"main": "dist/umd/index.js",
66
"module": "dist/es/index.js",
@@ -73,6 +73,10 @@
7373
{
7474
"name": "Emilia Zapata",
7575
"url": "https://github.com/synecdokey"
76+
},
77+
{
78+
"name": "Liam Johnston",
79+
"url": "https://github.com/liamjohnston"
7680
}
7781
],
7882
"license": "MIT",

src/components/Accordion.spec.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ describe('Accordion', () => {
3939

4040
describe('expanding and collapsing: ', () => {
4141
describe('allowMultipleExpanded prop', () => {
42-
it('permits multiple items to be expanded when explicitly true', () => {
42+
it('prevents multiple items to be expanded when set to false', () => {
4343
const { getByTestId } = render(
44-
<Accordion allowMultipleExpanded={true}>
44+
<Accordion allowMultipleExpanded={false}>
4545
<AccordionItem>
4646
<AccordionItemHeading>
4747
<AccordionItemButton data-testid={UUIDS.FOO} />
@@ -60,13 +60,13 @@ describe('Accordion', () => {
6060

6161
expect(
6262
getByTestId(UUIDS.FOO).getAttribute('aria-expanded'),
63-
).toEqual('true');
63+
).toEqual('false');
6464
expect(
6565
getByTestId(UUIDS.BAR).getAttribute('aria-expanded'),
6666
).toEqual('true');
6767
});
6868

69-
it('prevents multiple items being expanded by default', () => {
69+
it('allows multiple items being expanded by default', () => {
7070
const { getByTestId } = render(
7171
<Accordion>
7272
<AccordionItem>
@@ -87,17 +87,17 @@ describe('Accordion', () => {
8787

8888
expect(
8989
getByTestId(UUIDS.FOO).getAttribute('aria-expanded'),
90-
).toEqual('false');
90+
).toEqual('true');
9191
expect(
9292
getByTestId(UUIDS.BAR).getAttribute('aria-expanded'),
9393
).toEqual('true');
9494
});
9595
});
9696

9797
describe('allowZeroExpanded prop', () => {
98-
it('permits the last-expanded item to be collapsed when explicitly true', () => {
98+
it('prevents the last-expanded item to be collapsed when explicitly false', () => {
9999
const { getByTestId } = render(
100-
<Accordion allowZeroExpanded={true}>
100+
<Accordion allowZeroExpanded={false}>
101101
<AccordionItem>
102102
<AccordionItemHeading>
103103
<AccordionItemButton data-testid={UUIDS.FOO} />
@@ -106,15 +106,17 @@ describe('Accordion', () => {
106106
</Accordion>,
107107
);
108108

109+
// open it
109110
fireEvent.click(getByTestId(UUIDS.FOO));
111+
// attempt to close it
110112
fireEvent.click(getByTestId(UUIDS.FOO));
111113

112114
expect(
113115
getByTestId(UUIDS.FOO).getAttribute('aria-expanded'),
114-
).toEqual('false');
116+
).toEqual('true');
115117
});
116118

117-
it('prevents the last-expanded item being collapsed by default', () => {
119+
it('allows the last-expanded to collapsed by default', () => {
118120
const { getByTestId } = render(
119121
<Accordion>
120122
<AccordionItem>
@@ -130,7 +132,7 @@ describe('Accordion', () => {
130132

131133
expect(
132134
getByTestId(UUIDS.FOO).getAttribute('aria-expanded'),
133-
).toEqual('true');
135+
).toEqual('false');
134136
});
135137
});
136138

src/components/AccordionContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export class Provider extends React.PureComponent<
4242
ProviderState
4343
> {
4444
static defaultProps: ProviderProps = {
45-
allowMultipleExpanded: false,
46-
allowZeroExpanded: false,
45+
allowMultipleExpanded: true,
46+
allowZeroExpanded: true,
4747
};
4848

4949
state: ProviderState = new AccordionStore({

0 commit comments

Comments
 (0)