import { provide } from '@lit/context' import { html, LitElement } from 'lit' import { customElement, property, state } from 'lit/decorators.js' import { classMap } from 'lit/directives/class-map.js' import type { TAccordionSkin } from 'shared-types' import { accordionContext, type AccordionContext } from './accordion-context' export type TPktAccordionSkin = TAccordionSkin export interface IPktAccordion { compact?: boolean skin?: TPktAccordionSkin ariaLabelledBy?: string name?: string /** * @description A unique identifier to connect the accordion with a heading */ } export class PktAccordion extends LitElement implements IPktAccordion { // Properties @property({ type: String, reflect: true, attribute: 'aria-labelledby' }) ariaLabelledBy: string = '' @property({ type: Boolean, reflect: true, attribute: 'compact' }) compact: boolean = false @property({ type: String, reflect: true, attribute: 'skin' }) skin: TPktAccordionSkin = 'borderless' @property({ type: String, reflect: true, attribute: 'name' }) name: string = '' // Deler skin med pkt-accordion-item-children via context @provide({ context: accordionContext }) @state() private context: AccordionContext = { skin: this.skin } willUpdate(changedProperties: Map) { if (changedProperties.has('skin')) { this.context = { ...this.context, skin: this.skin } } } updated(changedProperties: Map) { if (changedProperties.has('name')) { this.updateAccordionItemNames() } } private updateAccordionItemNames() { if (this.name) { const slot = this.renderRoot?.querySelector('slot') if (slot) { const assignedElements = slot.assignedElements() assignedElements.forEach((element) => { if (element.tagName.toLowerCase() === 'pkt-accordion-item') { if (!element.hasAttribute('name')) { element.setAttribute('name', this.name) } } }) } } } firstUpdated() { const slot = this.renderRoot?.querySelector('slot') if (slot) { slot.addEventListener('slotchange', () => { this.updateAccordionItemNames() }) } } render() { const accordionClasses = { 'pkt-accordion': true, 'pkt-accordion--compact': this.compact, [`pkt-accordion--${this.skin}`]: this.skin, } return html`
` } } export default PktAccordion try { customElement('pkt-accordion')(PktAccordion) } catch (e) { console.warn('Forsøker å definere , men den er allerede definert') }