vira
Version:
A simple and highly versatile design system using element-vir.
67 lines (66 loc) • 1.85 kB
JavaScript
import { filterMap, joinWithFinalConjunction, } from '@augment-vir/common';
import { html, listen } from 'element-vir';
import { ViraMenuItem } from '../elements/pop-up/vira-menu-item.element.js';
/**
* Verifies that all items have unique ids.
*
* @category Internal
*/
export function assertUniqueIdProps(items) {
const usedIds = new Set();
const duplicateIds = [];
items.forEach((option) => {
if (usedIds.has(option.id)) {
duplicateIds.push(option.id);
}
else {
usedIds.add(option.id);
}
});
if (duplicateIds.length) {
throw new Error(`Duplicate option ids were given: ${joinWithFinalConjunction(duplicateIds)}`);
}
}
/**
* Handles toggling pop up state for `ViraDropdown`.
*
* @category Internal
*/
export function triggerPopUpState({ open, callback, popUpManager, host, options, }) {
if (open) {
const showPopUpResult = popUpManager.showPopUp(host, options);
callback?.(showPopUpResult);
}
else {
popUpManager.removePopUp();
callback?.(undefined);
}
}
/**
* A helper for rendering a bunch of menu items.
*
* @category PopUp
*/
export function renderMenuItemEntries(items) {
return filterMap(items, (item, index) => {
return html `
<${ViraMenuItem.assign({
...item,
})}
${listen('click', async (event) => {
if (item.disabled) {
event.stopImmediatePropagation();
event.preventDefault();
return;
}
await item.onClick?.({
event,
index,
});
})}
>
${item.content}
</${ViraMenuItem}>
`;
}, (template, menuItem) => !menuItem.hidden);
}