Skip to content

Commit d330c75

Browse files
committed
feat(aria/accordion): Extend public api with open/close methods
1 parent ecd9039 commit d330c75

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/aria/accordion/accordion.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ export class AccordionPanel {
7676
this._deferredContentAware.contentVisible.set(!this._pattern.hidden());
7777
});
7878
}
79+
80+
/** Opens this item. */
81+
open(itemValue: string) {
82+
this.accordionTrigger()?.expansionControl.open();
83+
}
84+
85+
/** Closes this item. */
86+
close(itemValue: string) {
87+
this.accordionTrigger()?.expansionControl.close();
88+
}
89+
90+
/** Toggles the expansion state of this item. */
91+
toggle(itemValue: string) {
92+
this.accordionTrigger()?.expansionControl.toggle();
93+
}
7994
}
8095

8196
/**
@@ -135,6 +150,21 @@ export class AccordionTrigger {
135150
accordionGroup: computed(() => this._accordionGroup._pattern),
136151
accordionPanel: this.accordionPanel,
137152
});
153+
154+
/** Opens this item. */
155+
open(itemValue: string) {
156+
this._pattern.expansionControl.open();
157+
}
158+
159+
/** Closes this item. */
160+
close(itemValue: string) {
161+
this._pattern.expansionControl.close();
162+
}
163+
164+
/** Toggles the expansion state of this item. */
165+
toggle(itemValue: string) {
166+
this._pattern.expansionControl.toggle();
167+
}
138168
}
139169

140170
/**
@@ -204,6 +234,49 @@ export class AccordionGroup {
204234
}
205235
});
206236
}
237+
238+
/** Opens the accordion panel with the specified value. */
239+
open(itemValue: string) {
240+
const trigger = this._findTriggerPatternByValue(itemValue);
241+
242+
if (trigger) {
243+
this._pattern.expansionManager.open(trigger);
244+
}
245+
}
246+
247+
/** Closes the accordion panel with the specified value. */
248+
close(itemValue: string) {
249+
const trigger = this._findTriggerPatternByValue(itemValue);
250+
251+
if (trigger) {
252+
this._pattern.expansionManager.close(trigger);
253+
}
254+
}
255+
256+
/** Toggles the expansion state of the accordion panel with the specified value. */
257+
toggle(itemValue: string) {
258+
const trigger = this._findTriggerPatternByValue(itemValue);
259+
260+
if (trigger) {
261+
this._pattern.expansionManager.toggle(trigger);
262+
}
263+
}
264+
265+
/** Opens all accordion panels if multi-expandable. */
266+
openAll() {
267+
this._pattern.expansionManager.openAll();
268+
}
269+
270+
/** Closes all accordion panels. */
271+
closeAll() {
272+
this._pattern.expansionManager.closeAll();
273+
}
274+
275+
_findTriggerPatternByValue(value: string) {
276+
const trigger = this._triggers().find(t => t.value() === value);
277+
278+
return trigger?._pattern;
279+
}
207280
}
208281

209282
/**

0 commit comments

Comments
 (0)