UNPKG

@postnord/web-components

Version:

PostNord Web Components

194 lines (193 loc) 7.55 kB
/*! * Built with Stencil * By PostNord. */ import { en, awaitTopbar } from "../../../globals/helpers"; import { h, Host, forceUpdate } from "@stencil/core"; import { arrow_left } from "pn-design-assets/pn-assets/icons.js"; const translations = { sv: 'Tillbaka', en: 'Back', da: 'Tilbage', fi: 'Takaisin', no: 'Tilbake', }; /** * @slot breadcrumb - This slot is reserved for a `pn-text-link` or another complex breadcrumb links structure. * Do not use this slot and the `go-back-href` prop at the same time. * @slot buttons - This slot is used to align multiple `pn-button` elements. However, we recommend a single button. * @slot menu - This slot is reserved for a `pn-tablist`. * This slot allows you to use the `href` prop on `pn-tab`, turning them into links instead of buttons. */ export class PnHeader { constructor() { this.heading = undefined; this.maxWidth = undefined; this.goBackHref = undefined; this.goBackText = undefined; this.language = null; this.goBackTextTranslated = undefined; } mo; hasBreadcrumb = false; hasMenu = false; hasButtons = false; hostElement; setMaxWidth() { if (!this.maxWidth) return; this.hostElement.style.setProperty('--max-width', this.maxWidth); } setLanguage() { this.goBackTextTranslated = this.goBackText || translations[this.language || en]; } async componentWillLoad() { if (this.mo) this.mo.disconnect(); this.mo = new MutationObserver(() => { forceUpdate(this.hostElement); this.checkSlots(); }); this.mo.observe(this.hostElement, { childList: true, subtree: true }); this.checkSlots(); this.setMaxWidth(); this.setLanguage(); if (this.language) return; await awaitTopbar(this.hostElement); } checkSlots() { this.hasBreadcrumb = !!this.hostElement.querySelector('[slot="breadcrumb"]'); this.hasMenu = !!this.hostElement.querySelector('[slot="menu"]'); this.hasButtons = !!this.hostElement.querySelector('[slot="buttons"]'); } showBreadcrumb() { return this.hasBreadcrumb || !!this.goBackHref?.length; } render() { return (h(Host, { key: 'cd686ea2ca013f125a319f5f2788c2c225dd9365' }, h("header", { key: '7e74404829674f2fe5ad3d3a4750eb747d186e7a', class: "pn-header" }, h("div", { key: '3ee0964a234ef68381ea198a17ced4798f5da125', class: "pn-header-container" }, h("div", { key: 'e356e0d7c0258bdbb1ebbe3cf3918e1bcf9d0b6a', class: "pn-header-breadcrumbs", hidden: !this.showBreadcrumb() }, this.goBackHref && (h("pn-text-link", { key: 'cab2b189eddbc8fa17dbe9da0a4bb48b071e826a', label: this.goBackTextTranslated, href: this.goBackHref, icon: arrow_left, leftIcon: true })), h("slot", { key: '414093ad797665dd8d0e6f23fcd12f095f487565', name: "breadcrumb" })), h("div", { key: '6e2ddd40d188968e23d3fdb92bf3d45ae6241856', class: "pn-header-content" }, this.heading && h("h1", { key: '0e92c8b16cc4a616dde6f2b14ccdff04c802c76b' }, this.heading), h("slot", { key: '1d4f83a9f13fa526a61442e4d4664587bd37ae83' })), h("div", { key: '31f07187103ed34d6122ee53d4665d96a24099e6', class: "pn-header-buttons", hidden: !this.hasButtons }, h("slot", { key: '855d81a1ea7c821ebe3d996ddae20b666c6200ef', name: "buttons" })), h("slot", { key: '0553519937c5937322c2e45f35790e38f91ef390', name: "menu" }))))); } static get is() { return "pn-header"; } static get originalStyleUrls() { return { "$": ["pn-header.scss"] }; } static get styleUrls() { return { "$": ["pn-header.css"] }; } static get properties() { return { "heading": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Heading text." }, "attribute": "heading", "reflect": false }, "maxWidth": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Set a inner container max-width, any width CSS value is accepted." }, "attribute": "max-width", "reflect": false }, "goBackHref": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The href for the go back link. If this is provided a button be displayed." }, "attribute": "go-back-href", "reflect": false }, "goBackText": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The text for the back link." }, "attribute": "go-back-text", "reflect": false }, "language": { "type": "string", "mutable": false, "complexType": { "original": "PnLanguages", "resolved": "\"\" | \"da\" | \"en\" | \"fi\" | \"no\" | \"sv\"", "references": { "PnLanguages": { "location": "import", "path": "@/globals/types", "id": "src/globals/types.ts::PnLanguages" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Set the language manually, only use this prop if the pnTopbar is not loaded. Supported: sv, en, da, fi and no." }, "attribute": "language", "reflect": false, "defaultValue": "null" } }; } static get states() { return { "goBackTextTranslated": {} }; } static get elementRef() { return "hostElement"; } static get watchers() { return [{ "propName": "maxWidth", "methodName": "setMaxWidth" }, { "propName": "language", "methodName": "setLanguage" }]; } } //# sourceMappingURL=pn-header.js.map