import {classMap} from "lit/directives/class-map.js"; import {type CSSResultGroup, html, type PropertyValues, unsafeCSS} from 'lit'; import {HasSlotController} from "../../internal/slot"; import {property} from 'lit/decorators.js'; import {watch} from "../../internal/watch"; import ZincElement from '../../internal/zinc-element'; import type ZnNavbar from "../navbar"; import styles from './header.scss'; /** * @summary Short summary of the component's intended use. * @documentation https://zinc.style/components/header * @status experimental * @since 1.0 * * @dependency zn-example * * @event zn-event-name - Emitted as an example. * * @slot - The default slot. * @slot example - An example slot. * * @csspart base - The component's base wrapper. * * @cssproperty --example - An example CSS custom property. */ export default class ZnHeader extends ZincElement { static styles: CSSResultGroup = unsafeCSS(styles); private readonly hasSlotController = new HasSlotController(this, '[default]', 'nav', 'breadcrumb'); @property({attribute: 'full-location'}) fullLocation: string; @property({attribute: 'entity-id'}) entityId: string; @property({attribute: 'entity-id-show', type: Boolean}) entityIdShow: boolean; @property({type: Boolean}) transparent: boolean = false; @property() caption: string; @property() icon: string; @property() description: string; @property({type: Array}) navigation = []; @property({attribute: 'full-width', type: Boolean}) fullWidth: boolean; @property({attribute: 'previous-path'}) previousPath: string; @property({attribute: 'previous-target'}) previousTarget: string; private navbar: ZnNavbar; // Attach any event listeners you may need connectedCallback() { super.connectedCallback(); window.addEventListener('alt-press', this.handleAltPress); window.addEventListener('alt-up', this.handleAltUp); } // Clean up event listeners disconnectedCallback() { super.disconnectedCallback(); window.removeEventListener('alt-press', this.handleAltPress); window.removeEventListener('alt-up', this.handleAltUp); } protected firstUpdated(_changedProperties: PropertyValues) { super.firstUpdated(_changedProperties); this.navbar = this.querySelector('zn-navbar')!; this.updateNav(); } handleAltPress = () => { this.classList.toggle('alt-pressed', true); } handleAltUp = () => { this.classList.toggle('alt-pressed', false); } @watch('navigation', {waitUntilFirstUpdate: true}) updateNav() { if (!this.navbar && this.navigation && this.navigation.length > 0) { this.navbar = document.createElement('zn-navbar'); const nc = this.shadowRoot?.querySelector('#nav-container'); if (nc) { nc.appendChild(this.navbar); } } if (this.navbar) { (this.navbar as ZnNavbar).navigation = this.navigation; this.navbar.setAttribute('baseless', ''); } } render() { const hasDefaultSlot = this.hasSlotController.test('[default]'); const hasNavigationSlot = this.hasSlotController.test('nav'); const hasNavigation = this.navigation && this.navigation.length > 0 || hasNavigationSlot; const hasPreviousPath = this.previousPath; const hasEntityId = this.entityId; const hasFullLocation = this.fullLocation; const hasBreadcrumb = this.hasSlotController.test('breadcrumb'); // Do not add formatting within breadcrumb or navigation - css:empty in use const header = html`
${hasFullLocation || hasEntityId ? html`
${hasFullLocation ? html` ` : null} ${hasEntityId ? html` ` : null} ${hasFullLocation ? html` ` : null}
` : null}
${hasBreadcrumb ? html` ` : null} ${hasPreviousPath ? html` ` : null} ${hasDefaultSlot ? html`
` : ''}
${this.icon ? html` ` : null} ${this.caption} ${this.description ? html` ${this.description}` : ''}
${hasNavigation ? html` ` : null}
`; if (this.fullWidth) { return html` ${header}`; } return header; } }