@postnord/web-components
Version:
PostNord Web Components
194 lines (193 loc) • 7.55 kB
JavaScript
/*!
* 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: '8493b17c1f553672941e7bddf7382e3e80377c71' }, h("header", { key: '2026f9c75dde59abda45a0e376c24907657fe8e7', class: "pn-header" }, h("div", { key: 'dbf01bc588ed38cccb0f951fed4047c9e8192354', class: "pn-header-container" }, h("div", { key: '115a52c44b0e5addf49e4807b21adeb84a531de9', class: "pn-header-breadcrumbs", hidden: !this.showBreadcrumb() }, this.goBackHref && (h("pn-text-link", { key: 'b2cce5ed46b6dcdb10a72bb806c5a406b6ff5e15', label: this.goBackTextTranslated, href: this.goBackHref, icon: arrow_left, leftIcon: true })), h("slot", { key: '27a3d3c38b30cba6d51c79a2c0bd18c6afa13a4c', name: "breadcrumb" })), h("div", { key: '8134ae8b844e9db548cb3db6bf995479c404fff1', class: "pn-header-content" }, this.heading && h("h1", { key: '3bf46740c66ca137adbc50fe6cdde178e82bfdbc' }, this.heading), h("slot", { key: '94ee53b3dee5a388ef84ae91c54a5b96c14a2661' })), h("div", { key: 'd44728888d295fd84285b28102b348da90ba5d80', class: "pn-header-buttons", hidden: !this.hasButtons }, h("slot", { key: '7af9da3f81bedb69639f7f723c24d93caebaba24', name: "buttons" })), h("slot", { key: '6c55a22b94293e59c8fdb33906c2d4612d067cc4', 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