@scania/tegel
Version:
Tegel Design System
87 lines (82 loc) • 3.75 kB
JavaScript
;
var index = require('./index-DGsdvbvx.js');
var isHeadingElement = require('./isHeadingElement-DihKNeqS.js');
require('./dfs-Clg9RCmq.js');
const headerDropdownListCss = () => `:host{display:block;width:190px;padding:0;margin:0;list-style:none;border-radius:unset;background-color:var(--tds-header-app-launcher-menu-bg)}@media all and (max-width: 384px){:host{width:100vw}}:host([size=lg]){width:320px}`;
const TdsHeaderDropdownList = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
// A Map to store the slots and their associated slotchange listeners.
this.slotListeners = new Map();
/** The size of the component. */
this.size = 'md';
}
componentWillLoad() {
var _a, _b;
const { children } = this.host;
// Set the size prop for each child, if they have such a property
for (let i = 0; i < children.length; i++) {
const child = children[i];
if ('size' in child) {
child.size = this.size;
}
}
let listRoot = this.host;
if (((_b = (_a = this.host) === null || _a === void 0 ? void 0 : _a.parentElement) === null || _b === void 0 ? void 0 : _b.tagName.toLowerCase()) === 'tds-header-launcher-list') {
listRoot = this.host.parentElement;
}
const headingEl = isHeadingElement.getPreviousNestedChildOfSiblingsMatching(listRoot, isHeadingElement.isHeadingElement);
if (headingEl) {
this.headingElement = headingEl;
}
else {
console.warn('Heading element for list not found');
}
}
componentDidLoad() {
var _a;
(_a = this.host.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelectorAll('slot').forEach((slot) => {
// Add the slotchange event listener.
const onSlotChange = (e) => {
this.processAssignedElements(e.target);
};
slot.addEventListener('slotchange', onSlotChange);
onSlotChange({ target: slot });
// Store the slot and its listener in the Map.
this.slotListeners.set(slot, onSlotChange);
});
}
processAssignedElements(slot) {
const nodes = slot.assignedElements();
nodes.forEach((node) => {
// Add a listitem role to the assigned element if needed.
if (node.tagName.toLowerCase() !== 'li' &&
node.tagName.toLowerCase() !== 'slot' &&
node.getAttribute('role') !== 'listitem') {
node.setAttribute('role', 'listitem');
}
// If the assigned element is a slot, process its assigned elements recursively.
if (node.tagName.toLowerCase() === 'slot') {
this.processAssignedElements(node);
}
});
}
disconnectedCallback() {
this.slotListeners.forEach((listener, slot) => {
// Remove the slotchange event listener and delete the entry from the Map.
slot.removeEventListener('slotchange', listener);
this.slotListeners.delete(slot);
});
}
render() {
var _a;
const attributes = {
'role': 'list',
'aria-labelledby': (_a = this.headingElement) === null || _a === void 0 ? void 0 : _a.id,
};
return (index.h(index.Host, Object.assign({ key: '87736a915ab274f1fcd30e6eb3ac2b594c323ba9' }, attributes), index.h("slot", { key: '61a2332a3da99fa2b0fa51a54547e9d5b66fa804' })));
}
get host() { return index.getElement(this); }
};
TdsHeaderDropdownList.style = headerDropdownListCss();
exports.tds_header_dropdown_list = TdsHeaderDropdownList;