@postnord/web-components
Version:
PostNord Web Components
207 lines (206 loc) • 8.09 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 {
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: '07db4e2989bad2c31854e3027e758d34c3aaf192' }, h("header", { key: '465b0e73e01985282ee142ae539c432630173529', class: "pn-header" }, h("div", { key: 'a65dcf2d28781308e3d91433987e3a773b30da12', class: "pn-header-container" }, h("div", { key: '18d6d93d83ce7f681f52782ace7daa9c75b71ffc', class: "pn-header-breadcrumbs", hidden: !this.showBreadcrumb() }, this.goBackHref && (h("pn-text-link", { key: '35cbde24fb1bcc9d9d327e20acf5be4afb08e17e', label: this.goBackTextTranslated, href: this.goBackHref, icon: arrow_left, leftIcon: true })), h("slot", { key: 'f4716b9ca6e7ae4c8be1cc08ced559ee6b73092c', name: "breadcrumb" })), h("div", { key: '501fc1cef17951ad68745d42b59d97f814ad7681', class: "pn-header-content" }, this.heading && h("h1", { key: 'e07f0524c2f6e9d623c7f5143c2b0c069836f0f7' }, this.heading), h("slot", { key: 'e0c67ecc93e7e93a7ecf05d92d9051cd0da18e72' })), h("div", { key: '4e483ade9e3704872f1cba25d80229f19078c503', class: "pn-header-buttons", hidden: !this.hasButtons }, h("slot", { key: '24fa16a74d9f5879f14b9b032d567bd28630f070', name: "buttons" })), h("slot", { key: '2ee40b1721ccc2f076525d32bf74720b9311a5f3', 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,
"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."
},
"getter": false,
"setter": false,
"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."
},
"getter": false,
"setter": false,
"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."
},
"getter": false,
"setter": false,
"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."
},
"getter": false,
"setter": false,
"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