|
| 1 | +import { LitElement, html, css } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js'; |
| 2 | + |
| 3 | +const detailsExpanded = !!JSON.parse(localStorage.getItem('details-expanded')); |
| 4 | + |
| 5 | +export class DetailsToggle extends LitElement { |
| 6 | + static properties = { |
| 7 | + _detailsExpanded: { state: true, type: Boolean }, |
| 8 | + }; |
| 9 | + |
| 10 | + constructor() { |
| 11 | + super(); |
| 12 | + this._detailsExpanded = detailsExpanded; |
| 13 | + document.addEventListener('detailstoggled', e => { |
| 14 | + this._detailsExpanded = !!e?.detail?.expanding; |
| 15 | + this.render(); |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + static styles = css` |
| 20 | + :host { |
| 21 | + cursor: pointer; |
| 22 | + transform: translateY(3px); |
| 23 | + } |
| 24 | + `; |
| 25 | + |
| 26 | + toggleDetails() { |
| 27 | + this._detailsExpanded = !this._detailsExpanded; |
| 28 | + localStorage.setItem('details-expanded', JSON.stringify(this._detailsExpanded)); |
| 29 | + if (this._detailsExpanded) { |
| 30 | + for (const details of document.getElementsByTagName('details')) { |
| 31 | + details.setAttribute('open', 'true'); |
| 32 | + } |
| 33 | + } else { |
| 34 | + for (const details of document.getElementsByTagName('details')) { |
| 35 | + details.removeAttribute('open'); |
| 36 | + } |
| 37 | + } |
| 38 | + document.dispatchEvent(new CustomEvent('detailstoggled', { detail: { expanding: this._detailsExpanded } })); |
| 39 | + } |
| 40 | + |
| 41 | + render() { |
| 42 | + const icon = this._detailsExpanded ? 'carbon:collapse-categories' : 'carbon:expand-categories'; |
| 43 | + const title = this._detailsExpanded ? 'Collapse details' : 'Expand details'; |
| 44 | + return html` |
| 45 | + <iconify-icon width="24" height="24" icon="${icon}" title="${title}" style="color:var(--header-link-color)" @click=${this.toggleDetails}></iconify-icon> |
| 46 | + `; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +customElements.define('fsdocs-details-toggle', DetailsToggle); |
0 commit comments