UNPKG

@openproject/primer-view-components

Version:

ViewComponents of the Primer Design System for OpenProject

57 lines (56 loc) 2.11 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { attr, target, targets } from '@github/catalyst'; // eslint-disable-next-line custom-elements/expose-class-on-global export class CollapsibleElement extends HTMLElement { constructor() { super(...arguments); this.collapsed = false; } toggle() { this.collapsed = !this.collapsed; } attributeChangedCallback(name) { if (name === 'data-collapsed') { if (this.collapsed) { this.hideAll(); } else { this.expandAll(); } } } hideAll() { // For whatever reason, setting `hidden` directly does not work on the SVGs this.arrowDown?.removeAttribute('hidden'); this.arrowUp?.setAttribute('hidden', ''); for (const el of this.collapsibleElements) { el.hidden = true; } this.classList.add(`${this.baseClass}--collapsed`); } expandAll() { this.arrowUp?.removeAttribute('hidden'); this.arrowDown?.setAttribute('hidden', ''); for (const el of this.collapsibleElements) { el.hidden = false; } this.classList.remove(`${this.baseClass}--collapsed`); } } __decorate([ target ], CollapsibleElement.prototype, "arrowDown", void 0); __decorate([ target ], CollapsibleElement.prototype, "arrowUp", void 0); __decorate([ targets ], CollapsibleElement.prototype, "collapsibleElements", void 0); __decorate([ attr ], CollapsibleElement.prototype, "collapsed", void 0);