UNPKG

@scania/tegel

Version:
81 lines (75 loc) 5.44 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-ca8040ad.js'); const generateUniqueId = require('./generateUniqueId-e3fc9863.js'); const hasSlot = require('./hasSlot-1c90e9ba.js'); const headerDropdownCss = ":host{--tds-scrollbar-width-standard:thin;--tds-scrollbar-width:10px;--tds-scrollbar-height:10px;--tds-scrollbar-thumb-border-width:3px;--tds-scrollbar-thumb-border-hover-width:2px}body{scrollbar-width:thin}:host{height:var(--tds-header-height);position:relative}:host .menu{flex-direction:column;overflow-y:auto;max-height:calc(100vh - var(--tds-header-height));box-shadow:var(--tds-nav-dropdown-menu-box);background-color:var(--tds-header-app-launcher-menu-background);border-top-right-radius:0;border-top-left-radius:0}:host .menu:hover::-webkit-scrollbar-thumb{border:var(--tds-scrollbar-thumb-border-hover-width) solid transparent;background:var(--tds-scrollbar-hover-thumb-color);background-clip:padding-box}:host .menu::-webkit-scrollbar{width:var(--tds-scrollbar-width)}:host .menu::-webkit-scrollbar-track{background:var(--tds-scrollbar-track-color)}:host .menu::-webkit-scrollbar-thumb{border-radius:40px;background:var(--tds-scrollbar-thumb-color);border:var(--tds-scrollbar-thumb-border-width) solid transparent;background-clip:padding-box}:host .menu::-webkit-scrollbar-button{height:0;width:0}@supports not selector(::-webkit-scrollbar){:host .menu{scrollbar-color:var(--tds-scrollbar-thumb-color) var(--tds-scrollbar-track-color);scrollbar-width:var(--tds-scrollbar-width-standard)}}:host .state-open .dropdown-icon{transform:rotate(180deg)}:host .state-open .button{position:relative;z-index:901}"; const TdsHeaderDropdownStyle0 = headerDropdownCss; const TdsHeaderDropdown = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.uuid = generateUniqueId.generateUniqueId(); this.handleSlottedItemClick = (event) => { const eventSource = event.target.tagName.toLowerCase(); if (['a', 'button'].includes(eventSource)) { this.open = false; } }; this.label = undefined; this.noDropdownIcon = false; this.selected = false; this.tdsAriaLabel = undefined; this.open = false; this.buttonEl = undefined; } onAnyClick(event) { // Source: https://lamplightdev.com/blog/2021/04/10/how-to-detect-clicks-outside-of-a-web-component/ const isClickOutside = !event.composedPath().includes(this.host); if (isClickOutside) { this.open = false; } } handleKeyDown(event) { if (event.key === 'Escape' && this.open) { this.open = false; this.buttonEl.focus(); } } toggleDropdown() { this.open = !this.open; if (this.open) { requestAnimationFrame(() => { const selectors = "a, [tabindex='0']"; const firstFocusableElement = this.host.shadowRoot.querySelector(selectors) || this.host.querySelector(selectors); if (firstFocusableElement instanceof HTMLElement) { firstFocusableElement.focus(); } }); } } connectedCallback() { const hasLabelSlot = hasSlot.hasSlot('label', this.host); if (!this.tdsAriaLabel && !hasLabelSlot) { console.warn('Tegel Header Dropdown component: use label slot or specify tdsAriaLabel prop'); } } render() { return (index.h(index.Host, { key: '6ab1d0f1d475d6d0a486e1909cad300d89eb39a0' }, index.h("div", { key: 'bfe11cdc8c2bf592cec7f3cf8888555071781afe', class: { 'state-open': this.open, } }, index.h("tds-header-item", { key: '27b6d2680858443a26d7108b5318386df76db788', class: "button", active: this.open, selected: this.selected }, index.h("button", { key: '09d0ccb3bc5a31a897af65f5fcf4d47ee8b35192', ref: (el) => { this.buttonEl = el; }, "aria-expanded": `${this.open}`, "aria-controls": `launcher-${this.uuid}`, "aria-current": this.selected ? 'location' : 'false', onClick: () => { this.toggleDropdown(); }, "aria-label": this.tdsAriaLabel }, index.h("slot", { key: '50c872c297116ece9dd5da41398aac3bd88cd529', name: "icon" }), this.label, index.h("slot", { key: '141a43cdc4c8aeade4d547bbb91a28f62542bb98', name: "label" }), !this.noDropdownIcon && (index.h("tds-icon", { key: '13f2f8d6b264f09dea45e9b7809adab1b83db683', class: "dropdown-icon", name: "chevron_down", size: "16px", svgTitle: "Dropdown icon" })))), this.buttonEl && (index.h("tds-popover-canvas", { key: '10b18c15ae1d2c0fbafaa82bed4ae7759c857c54', id: `tds-dropdown-${this.uuid}`, class: "menu", referenceEl: this.buttonEl, placement: "bottom-start", show: this.open, offsetDistance: 0, modifiers: [ { name: 'flip', options: { fallbackPlacements: [], }, }, ] }, this.open ? (index.h("span", { onClick: (e) => this.handleSlottedItemClick(e), onKeyDown: (e) => e.key === 'Enter' && this.handleSlottedItemClick(e) }, index.h("slot", null))) : null))))); } get host() { return index.getElement(this); } }; TdsHeaderDropdown.style = TdsHeaderDropdownStyle0; exports.tds_header_dropdown = TdsHeaderDropdown;