import { css, html } from 'lit' import type { PropertyValueMap } from 'lit' import { property, state } from 'lit/decorators.js' import { when } from 'lit/directives/when.js' import { classMap } from 'lit/directives/class-map.js' import type { DirectiveClass, DirectiveResult } from 'lit/directive.js' import { UsBaseElement } from '../base/UsBaseElement' import { customElementIfNotExist } from '../../utils/customElementIfNotExist' import { dispatchCustomEvent } from '../../utils/dispatchCustomEvent' import type { AccordionVariants, IAccordionProps } from './model/IAccordionProps' import UsAccordion_css from './accordion.pcss' import vars_css from './vars.pcss' // import '../icon/UsIcon' import '../tooltip/UsTooltip' declare global { interface HTMLElementTagNameMap { 'us-accordion': UsAccordion } } @customElementIfNotExist('us-accordion') export class UsAccordion extends UsBaseElement implements IAccordionProps { static styles = [ UsBaseElement.styles, css([vars_css] as TemplateStringsArray | any), css([UsAccordion_css] as TemplateStringsArray | any), ] @property({ type: String }) widgetView: IAccordionProps['widgetView'] = 'vertical' @property({ type: String }) variant: AccordionVariants = 'primary' @property({ type: Boolean }) alternativeBackground = false @property({ type: String }) iconName = '' @property({ type: String }) title = '' @property({ type: String }) titleClose = '' @property({ type: Boolean }) infoTooltip = false @property({ type: Boolean }) isAccordionOpen = false @property({ type: String, attribute: 'data-testid', reflect: true }) dataTestId = 'accordion' @state() showInfo = false @state() showContent = false @state() hovered = false connectedCallback() { super.connectedCallback() } updated(changedProperties: PropertyValueMap) { if (changedProperties.has('isAccordionOpen')) this.showContent = this.isAccordionOpen } render() { return html` ${this.renderTypeOfAccordion(this.variant)} ${when(this.showContent, () => html` `)} ` } renderTypeOfAccordion(type: AccordionVariants) { switch (type) { case 'primary': return html` e.stopPropagation()}> ${when(this.iconName.length, () => html` `)} ${this.title} ${when(this.infoTooltip, () => html` e.stopPropagation()} @focus=${this.handleFocus} @blur=${this.handleBlur}> `, () => html``)} ` case 'secondary': return html` ${this.title} ` case 'link': return html` ${!this.showContent ? this.title : (this.titleClose || this.title)} ` case 'button': return html` ${this.title} ` } } getAccordionClasses(type: string): DirectiveResult { return classMap({ 'accordion-header': true, 'accordion-header__alternative': this.alternativeBackground, 'open': this.showContent, [type]: type.length, }) } toggleAccordion(e: KeyboardEvent | MouseEvent) { if (e.type === 'click') this.showContent = !this.showContent else if (e.type === 'keydown' && (e as KeyboardEvent).key === 'Enter') this.showContent = !this.showContent dispatchCustomEvent('showContent', this.showContent, this) } onHover() { this.hovered = !this.hovered } handleFocus(e: any) { if (e?.sourceCapabilities !== null) this.showInfo = true } handleBlur() { this.showInfo = false } }