import { classMap } from 'lit/directives/class-map.js' import { customElement, property, state } from 'lit/decorators.js' import { html, nothing, PropertyValues } from 'lit' import { PktElementWithSlot } from '@/base-elements/element-with-slot' import { slotContent } from '@/directives/slot-content' import type { TAriaLive, TAlertSkin } from 'shared-types' import { updateClassAttribute } from '@/utils/classutils' import specs from 'componentSpecs/alert.json' import '@/components/icon' import '@/components/button' export type { TAlertSkin } export interface IPktAlert { skin?: TAlertSkin closeAlert?: boolean title?: string date?: string | null ariaLive?: TAriaLive | null /** * @deprecated Bruk size i stedet. Uten eksplisitt size gir compact størrelsen 'small'. */ compact?: boolean role?: string size?: 'medium' | 'small' | 'xsmall' } export class PktAlert extends PktElementWithSlot implements IPktAlert { constructor() { super() this._isClosed = false } // Properties @property({ type: Boolean, reflect: false }) compact = specs.deprecated.compact.default @property({ type: String, reflect: true }) title: string = '' @property({ type: String, reflect: true }) skin: TAlertSkin = specs.props.skin .default as TAlertSkin @property({ type: String }) ariaLive: TAriaLive = specs.props.ariaLive.default as TAriaLive @property({ type: String, reflect: true, attribute: 'aria-live' }) ariaLiveAttr: TAriaLive | null = null @property({ type: Boolean, reflect: true }) closeAlert = specs.props.closeAlert.default @property({ type: String, reflect: true }) date: string | null = null @property({ type: String, reflect: true }) role: string = 'status' @property({ type: String, attribute: 'size' }) size?: 'medium' | 'small' | 'xsmall' @state() _isClosed: boolean = false // Lifecycle connectedCallback(): void { super.connectedCallback() this.ariaLiveAttr = (this.getAttribute('aria-live') as TAriaLive) || this.ariaLive } attributeChangedCallback(name: string, _old: string | null, value: string | null): void { if (name === 'ariaLive') { this.ariaLiveAttr = value as TAriaLive } super.attributeChangedCallback(name, _old, value) } protected updated(_changedProperties: PropertyValues): void { super.updated(_changedProperties) if (_changedProperties.has('ariaLive')) { this.ariaLiveAttr = this.ariaLive } if (_changedProperties.has('_isClosed')) { updateClassAttribute(this, 'pkt-hide', this._isClosed) } } // Render render() { const alertSize = this.size ?? (this.compact ? 'small' : 'medium') const classes = { 'pkt-alert': true, [`pkt-alert--${alertSize}`]: true, [`pkt-alert--${this.skin}`]: true, 'pkt-hide': this._isClosed, } const gridClasses = { 'pkt-alert__grid': true, 'pkt-alert__noTitle': !this.title, 'pkt-alert__noDate': !this.date, } return html`