Skip to content

Commit

Permalink
feat: implement accordeon exclusivity
Browse files Browse the repository at this point in the history
  • Loading branch information
spectrachrome committed Aug 23, 2023
1 parent f400d4d commit 16b4f0a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions elements/itemfilter/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export class ElementConfig {
* The property of the result items used for display
*/
public titleProperty = "title";

/**
* Allow opening multiple `details` accordeons in parallel
* @default false
*/
public expandMultiple?: boolean = false;
}

@customElement("eox-itemfilter")
Expand Down Expand Up @@ -279,6 +285,23 @@ export class EOxItemFilter extends TemplateElement {
this.search();
}

toggleAccordion(event: Event) {
const detailsElement = event.target as HTMLDetailsElement;

// Return early if multiple expansions are allowed or if accordion is being closed.
if (!detailsElement.open || this.config.expandMultiple) return;

// If we reach this point, it means detailsElement is being opened. Grab all `details`
// elements and close all other than the current one.
this.shadowRoot!
.querySelectorAll('details')
.forEach(details => {
if (details !== detailsElement) {
details.removeAttribute('open');
}
});
}

render() {
return html`
<style>
Expand Down Expand Up @@ -382,6 +405,7 @@ export class EOxItemFilter extends TemplateElement {
),
(aggregationProperty) => html`<details
class="details-results"
@toggle=${this.toggleAccordion}
open
>
<summary>
Expand Down

0 comments on commit 16b4f0a

Please sign in to comment.