@oslokommune/punkt-elements
Version:
Komponentbiblioteket til Punkt, et designsystem laget av Oslo Origo
101 lines (86 loc) • 3.24 kB
text/typescript
import { PktElement } from '@/base-elements/element'
import { html, nothing } from 'lit'
import { createRef, ref, Ref } from 'lit/directives/ref.js'
import { unsafeHTML } from 'lit/directives/unsafe-html.js'
import { classMap } from 'lit/directives/class-map.js'
import { customElement, property, state } from 'lit/decorators.js'
import { uuidish } from '@/utils/stringutils'
import specs from 'componentSpecs/input-wrapper.json'
import '@/components/icon'
import { PktSlotController } from '@/controllers/pkt-slot-controller'
('pkt-helptext')
export class PktHelptext extends PktElement {
defaultSlot: Ref<HTMLElement> = createRef()
constructor() {
super()
this.slotController = new PktSlotController(this, this.defaultSlot)
}
({ type: String, reflect: true })
forId: string = uuidish()
({ type: String })
helptext: string = ''
({ type: String })
helptextDropdown: string = ''
({ type: String })
helptextDropdownButton: string = specs.props.helptextDropdownButton.default
() isHelpTextOpen: boolean = false
() filledSlots: Set<string | null | undefined> = new Set()
updateSlots(filledSlots: Set<string | null | undefined>) {
this.filledSlots = new Set(filledSlots)
}
render() {
const toggleDropdown = () => {
const isOpen = !this.isHelpTextOpen
this.isHelpTextOpen = isOpen
this.dispatchEvent(
new CustomEvent('toggleHelpText', {
bubbles: true,
detail: { isOpen },
}),
)
}
const helptextClasses = classMap({
'pkt-inputwrapper__helptext-container': true,
'pkt-inputwrapper__has-helptext':
this.helptext || this.helptextDropdown || this.filledSlots.size > 0,
})
const helptextDropdown = () => {
if (this.helptextDropdown) {
return html`<div class="pkt-inputwrapper__helptext-expandable">
<button
class="pkt-link pkt-link--icon-right pkt-btn pkt-btn--small pkt-btn--tertiary pkt-btn--icon-right"
type="button"
@click=${toggleDropdown}
>
<pkt-icon
class="pkt-btn__icon"
name="${this.isHelpTextOpen ? 'chevron-thin-up' : 'chevron-thin-down'}"
></pkt-icon>
<span class="pkt-btn__text">${unsafeHTML(this.helptextDropdownButton)}</span>
</button>
<div
class="${classMap({
'pkt-inputwrapper__helptext': true,
'pkt-inputwrapper__helptext-expandable-open': this.isHelpTextOpen,
'pkt-inputwrapper__helptext-expandable-closed': !this.isHelpTextOpen,
})}"
>
${unsafeHTML(this.helptextDropdown)}
</div>
</div>`
} else {
return nothing
}
}
const helptextElement = () => {
return html`<div class="${helptextClasses}">
<div class="pkt-inputwrapper__helptext" id="${this.forId}-helptext">
<div class="pkt-contents" ${ref(this.defaultSlot)} name="helptext"></div>
${this.helptext && unsafeHTML(this.helptext)}
</div>
${helptextDropdown()}
</div>`
}
return html`${helptextElement()}`
}
}