Skip to content

Commit

Permalink
feature: Support for responsive default expanded items
Browse files Browse the repository at this point in the history
As well as using a class attribute, elements can be expanded by default using `data-collapsable-default-expanded-media`. This data attribute should contain a media query string (e.g. `(min-width: 992px)`), which is then evaluated using `window.matchMedia`. If it matches, the item will also be expanded by default, regardless of class. It is therefore intended to be used without the default expanded class name.
  • Loading branch information
zipper committed Jan 18, 2024
1 parent d7b3c12 commit afd0875
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/CollapsableItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,16 @@ export class CollapsableItem {
}

public get isDefaultExpanded(): boolean {
return this.element.classList.contains(this.collapsable.options.classNames.defaultExpanded)
const defaultExpandedClass = this.element.classList.contains(this.collapsable.options.classNames.defaultExpanded)
const mediaDataset = this.element.dataset.defaultCollapsableExpandedMedia

if (defaultExpandedClass || !mediaDataset) {
return defaultExpandedClass
}

const defaultExpandedMedia = window.matchMedia(mediaDataset)

return defaultExpandedMedia.matches || defaultExpandedClass
}

public get isExpanded(): boolean {
Expand Down

0 comments on commit afd0875

Please sign in to comment.