Skip to content

Commit 1c38cc0

Browse files
Helios docs site - Add example to Accordion docs for persisting item state (#3367)
Co-authored-by: Cristiano Rastelli <cristiano.rastelli@hashicorp.com>
1 parent 44a9ef5 commit 1c38cc0

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

website/docs/components/accordion/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,33 @@
66
import Component from '@glimmer/component';
77
import { action } from '@ember/object';
88
import { tracked } from '@glimmer/tracking';
9+
import { service } from '@ember/service';
910

11+
const STORAGE_KEY = 'website:accordion:item-2-state';
1012
export default class Index extends Component {
11-
@tracked state = 'close';
13+
@tracked accordionState = 'close'; // for expand/collapse all example
14+
@tracked itemState; // for persisting item state example
15+
@service fastboot;
1216

17+
// Store Item state in session storage to persist across page reloads
18+
constructor(owner, args) {
19+
super(owner, args);
20+
if (!this.fastboot.isFastBoot) {
21+
this.itemState = sessionStorage.getItem(STORAGE_KEY) ?? 'open';
22+
}
23+
}
24+
25+
// handle Accordion expand/collapse all
26+
@action
27+
toggleAccordionState() {
28+
this.accordionState = this.accordionState === 'open' ? 'close' : 'open';
29+
}
30+
31+
// handle Item toggle and persist state in session storage
1332
@action
14-
toggleState() {
15-
this.state = this.state === 'open' ? 'close' : 'open';
33+
onItemToggle() {
34+
this.itemState = this.itemState === 'open' ? 'close' : 'open';
35+
sessionStorage.setItem(STORAGE_KEY, this.itemState);
1636
}
1737
}
38+

website/docs/components/accordion/partials/code/how-to-use.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ The `@forceState` argument enables you to implement expand/collapse all function
147147
<div class="doc-accordion-flex-layout">
148148
<Hds::Text::Display @size="300">Examination period</Hds::Text::Display>
149149
<Hds::Button
150-
@text={{if (eq this.state "open") "Collapse all" "Expand all"}}
151-
@icon={{if (eq this.state "open") "unfold-close" "unfold-open"}}
152-
@color="tertiary" @size="small" {{on "click" this.toggleState}}
150+
@text={{if (eq this.accordionState "open") "Collapse all" "Expand all"}}
151+
@icon={{if (eq this.accordionState "open") "unfold-close" "unfold-open"}}
152+
@color="tertiary" @size="small" {{on "click" this.toggleAccordionState}}
153153
/>
154154
</div>
155-
<Hds::Accordion @forceState={{this.state}} as |A|>
155+
<Hds::Accordion @forceState={{this.accordionState}} as |A|>
156156
<A.Item>
157157
<:toggle>Exam experience</:toggle>
158158
<:content>
@@ -186,6 +186,55 @@ The `@forceState` argument enables you to implement expand/collapse all function
186186
</Hds::Accordion>
187187
```
188188

189+
### Persist Item state
190+
191+
The `@forceState` argument can be used to programmatically control individual Accordion Items. For example, use `@onClickToggle` to respond to the user’s click, save the open/close state, then use `@forceState` to persist the state if the screen refreshes.
192+
193+
```handlebars
194+
<Hds::Accordion as |A|>
195+
<A.Item>
196+
<:toggle>Item one</:toggle>
197+
<:content>
198+
Additional content for item one
199+
</:content>
200+
</A.Item>
201+
<A.Item
202+
@onClickToggle={{this.onItemToggle}}
203+
@forceState={{this.itemState}}
204+
>
205+
<:toggle>Item two</:toggle>
206+
<:content>
207+
Item open on page load. Click to close then refresh the window.
208+
The Item will remember its state and remain closed.
209+
</:content>
210+
</A.Item>
211+
<A.Item>
212+
<:toggle>Item three</:toggle>
213+
<:content>
214+
Additional content for item three
215+
</:content>
216+
</A.Item>
217+
</Hds::Accordion>
218+
```
219+
220+
#### JavaScript code
221+
222+
```javascript{data-execute=false}
223+
@tracked itemState;
224+
225+
// Store Item state in session storage to persist across page reloads
226+
constructor(owner, args) {
227+
super(owner, args);
228+
this.itemState = sessionStorage.getItem(STORAGE_KEY) ?? 'open';
229+
}
230+
231+
@action
232+
onItemToggle() {
233+
this.itemState = this.itemState === 'open' ? 'close' : 'open';
234+
sessionStorage.setItem(STORAGE_KEY, this.itemState);
235+
}
236+
```
237+
189238
### Accessible name
190239

191240
The `ariaLabel` value is applied to the HTML button which controls visibility of the content block. The text does not display in the UI. The default value is "Toggle display" but you can set a custom value useful for translated text for example.

0 commit comments

Comments
 (0)