UNPKG

@postnord/web-components

Version:
207 lines (206 loc) 8.09 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 { mo; hasBreadcrumb = false; hasMenu = false; hasButtons = false; hostElement; /** Heading text. */ heading; /** Set a inner container max-width, any width CSS value is accepted. */ maxWidth; setMaxWidth() { if (!this.maxWidth) return; this.hostElement.style.setProperty('--max-width', this.maxWidth); } /** The href for the go back link. If this is provided a button be displayed. */ goBackHref; /** The text for the back link. */ goBackText; /** Set the language manually, only use this prop if the pnTopbar is not loaded. Supported: sv, en, da, fi and no. */ language = null; setLanguage() { this.goBackTextTranslated = this.goBackText || translations[this.language || en]; } goBackTextTranslated; 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: '108e455b065af488f5d7695e42b560e623c0f5a1' }, h("header", { key: 'cac8db1772de2161130573e6c3cfb59034d526bc', class: "pn-header" }, h("div", { key: 'd9e0080de9e0c49a3bb61d3f2b0703f06987dd59', class: "pn-header-container" }, h("div", { key: '710e2af325f7fc2e978b10a55681cd63c0318820', class: "pn-header-breadcrumbs", hidden: !this.showBreadcrumb() }, this.goBackHref && (h("pn-text-link", { key: '015afb6977cd80e7f05fc4c6c8570c46d71cba41', label: this.goBackTextTranslated, href: this.goBackHref, icon: arrow_left, leftIcon: true })), h("slot", { key: '9927d7dcc6dfdb7af7224f711c8fdb85815a4cf4', name: "breadcrumb" })), h("div", { key: '46eb3efb3beb5c483cb440bcae59a20e2f2ccf2c', class: "pn-header-content" }, this.heading && h("h1", { key: 'f5aa3c6dd4376a193cc2f2019a399d04b2851f74' }, this.heading), h("slot", { key: '38838b2560a7123e4b643aaeeace129b324712ed' })), h("div", { key: '2b174446cb67d0c3b0ec675911c7b072785f6868', class: "pn-header-buttons", hidden: !this.hasButtons }, h("slot", { key: 'ad4b20d2658ab680ac1a39cba4d4631a7ceecd8c', name: "buttons" })), h("slot", { key: '793f3704789ec91c5589e4c7e5829d9b65ab33cc', 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." }, "getter": false, "setter": false, "reflect": false, "attribute": "heading" }, "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." }, "getter": false, "setter": false, "reflect": false, "attribute": "max-width" }, "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." }, "getter": false, "setter": false, "reflect": false, "attribute": "go-back-href" }, "goBackText": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The text for the back link." }, "getter": false, "setter": false, "reflect": false, "attribute": "go-back-text" }, "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." }, "getter": false, "setter": false, "reflect": false, "attribute": "language", "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