UNPKG

@shopware-ag/meteor-component-library

Version:

The meteor component library is a Vue component library developed by Shopware. It is based on the [Meteor Design System](https://shopware.design/).

675 lines (674 loc) 21.7 kB
import './mt-action-menu-item.css'; import { defineComponent, watchPostEffect, openBlock, createBlock, unref, withCtx, renderSlot, normalizeProps, guardReactiveProps, ref, createVNode, mergeProps, nextTick, onUnmounted, inject, onMounted, computed, resolveDynamicComponent, normalizeClass, createCommentVNode, createElementBlock, Fragment, renderList, toDisplayString } from "vue"; import { h as injectPopperRootContext, j as injectMenuContentContext, k as useCollection, l as isMouseEvent, c as injectMenuRootContext, m as SELECTION_KEYS, I as ITEM_SELECT, i as injectMenuContext, d as injectMenuSubContext, u as useId, n as getOpenState, o as SUB_OPEN_KEYS, a as useEmitAsProps } from "./MenuSub-7ab99f3b.mjs"; import { b as useForwardExpose, P as Primitive } from "./Primitive-bb21d4ce.mjs"; import { _ as _sfc_main$1 } from "./mt-icon.vue_vue_type_style_index_0_lang-2cc5f73e.mjs"; import { useI18n } from "vue-i18n"; import { _ as _export_sfc } from "./_plugin-vue_export-helper-cc2b3d55.mjs"; var PopperAnchor_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ __name: "PopperAnchor", props: { reference: { type: null, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false } }, setup(__props) { const props = __props; const { forwardRef, currentElement } = useForwardExpose(); const rootContext = injectPopperRootContext(); watchPostEffect(() => { rootContext.onAnchorChange(props.reference ?? currentElement.value); }); return (_ctx, _cache) => { return openBlock(), createBlock(unref(Primitive), { ref: unref(forwardRef), as: _ctx.as, "as-child": _ctx.asChild }, { default: withCtx(() => [renderSlot(_ctx.$slots, "default")]), _: 3 }, 8, ["as", "as-child"]); }; } }); var PopperAnchor_default = PopperAnchor_vue_vue_type_script_setup_true_lang_default; var MenuAnchor_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ __name: "MenuAnchor", props: { reference: { type: null, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false } }, setup(__props) { const props = __props; return (_ctx, _cache) => { return openBlock(), createBlock(unref(PopperAnchor_default), normalizeProps(guardReactiveProps(props)), { default: withCtx(() => [renderSlot(_ctx.$slots, "default")]), _: 3 }, 16); }; } }); var MenuAnchor_default = MenuAnchor_vue_vue_type_script_setup_true_lang_default; var MenuItemImpl_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ inheritAttrs: false, __name: "MenuItemImpl", props: { disabled: { type: Boolean, required: false }, textValue: { type: String, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false } }, setup(__props) { const props = __props; const contentContext = injectMenuContentContext(); const { forwardRef } = useForwardExpose(); const { CollectionItem } = useCollection(); const isFocused = ref(false); async function handlePointerMove(event) { if (event.defaultPrevented) return; if (!isMouseEvent(event)) return; if (props.disabled) contentContext.onItemLeave(event); else { const defaultPrevented = contentContext.onItemEnter(event); if (!defaultPrevented) { const item = event.currentTarget; item == null ? void 0 : item.focus({ preventScroll: true }); } } } async function handlePointerLeave(event) { await nextTick(); if (event.defaultPrevented) return; if (!isMouseEvent(event)) return; contentContext.onItemLeave(event); } return (_ctx, _cache) => { return openBlock(), createBlock(unref(CollectionItem), { value: { textValue: _ctx.textValue } }, { default: withCtx(() => [createVNode(unref(Primitive), mergeProps({ ref: unref(forwardRef), role: "menuitem", tabindex: "-1" }, _ctx.$attrs, { as: _ctx.as, "as-child": _ctx.asChild, "aria-disabled": _ctx.disabled || void 0, "data-disabled": _ctx.disabled ? "" : void 0, "data-highlighted": isFocused.value ? "" : void 0, onPointermove: handlePointerMove, onPointerleave: handlePointerLeave, onFocus: _cache[0] || (_cache[0] = async (event) => { await nextTick(); if (event.defaultPrevented || _ctx.disabled) return; isFocused.value = true; }), onBlur: _cache[1] || (_cache[1] = async (event) => { await nextTick(); if (event.defaultPrevented) return; isFocused.value = false; }) }), { default: withCtx(() => [renderSlot(_ctx.$slots, "default")]), _: 3 }, 16, [ "as", "as-child", "aria-disabled", "data-disabled", "data-highlighted" ])]), _: 3 }, 8, ["value"]); }; } }); var MenuItemImpl_default = MenuItemImpl_vue_vue_type_script_setup_true_lang_default; var MenuItem_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ __name: "MenuItem", props: { disabled: { type: Boolean, required: false }, textValue: { type: String, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false } }, emits: ["select"], setup(__props, { emit: __emit }) { const props = __props; const emits = __emit; const { forwardRef, currentElement } = useForwardExpose(); const rootContext = injectMenuRootContext(); const contentContext = injectMenuContentContext(); const isPointerDownRef = ref(false); async function handleSelect() { const menuItem = currentElement.value; if (!props.disabled && menuItem) { const itemSelectEvent = new CustomEvent(ITEM_SELECT, { bubbles: true, cancelable: true }); emits("select", itemSelectEvent); await nextTick(); if (itemSelectEvent.defaultPrevented) isPointerDownRef.value = false; else rootContext.onClose(); } } return (_ctx, _cache) => { return openBlock(), createBlock(MenuItemImpl_default, mergeProps(props, { ref: unref(forwardRef), onClick: handleSelect, onPointerdown: _cache[0] || (_cache[0] = () => { isPointerDownRef.value = true; }), onPointerup: _cache[1] || (_cache[1] = async (event) => { var _a; await nextTick(); if (event.defaultPrevented) return; if (!isPointerDownRef.value) (_a = event.currentTarget) == null ? void 0 : _a.click(); }), onKeydown: _cache[2] || (_cache[2] = async (event) => { const isTypingAhead = unref(contentContext).searchRef.value !== ""; if (_ctx.disabled || isTypingAhead && event.key === " ") return; if (unref(SELECTION_KEYS).includes(event.key)) { event.currentTarget.click(); event.preventDefault(); } }) }), { default: withCtx(() => [renderSlot(_ctx.$slots, "default")]), _: 3 }, 16); }; } }); var MenuItem_default = MenuItem_vue_vue_type_script_setup_true_lang_default; var MenuSubTrigger_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ __name: "MenuSubTrigger", props: { disabled: { type: Boolean, required: false }, textValue: { type: String, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false } }, setup(__props) { const props = __props; const menuContext = injectMenuContext(); const rootContext = injectMenuRootContext(); const subContext = injectMenuSubContext(); const contentContext = injectMenuContentContext(); const openTimerRef = ref(null); subContext.triggerId || (subContext.triggerId = useId(void 0, "reka-menu-sub-trigger")); function clearOpenTimer() { if (openTimerRef.value) window.clearTimeout(openTimerRef.value); openTimerRef.value = null; } onUnmounted(() => { clearOpenTimer(); }); function handlePointerMove(event) { if (!isMouseEvent(event)) return; const defaultPrevented = contentContext.onItemEnter(event); if (defaultPrevented) return; if (!props.disabled && !menuContext.open.value && !openTimerRef.value) { contentContext.onPointerGraceIntentChange(null); openTimerRef.value = window.setTimeout(() => { menuContext.onOpenChange(true); clearOpenTimer(); }, 100); } } async function handlePointerLeave(event) { var _a, _b; if (!isMouseEvent(event)) return; clearOpenTimer(); const contentRect = (_a = menuContext.content.value) == null ? void 0 : _a.getBoundingClientRect(); if (contentRect == null ? void 0 : contentRect.width) { const side = (_b = menuContext.content.value) == null ? void 0 : _b.dataset.side; const rightSide = side === "right"; const bleed = rightSide ? -5 : 5; const contentNearEdge = contentRect[rightSide ? "left" : "right"]; const contentFarEdge = contentRect[rightSide ? "right" : "left"]; contentContext.onPointerGraceIntentChange({ area: [ { x: event.clientX + bleed, y: event.clientY }, { x: contentNearEdge, y: contentRect.top }, { x: contentFarEdge, y: contentRect.top }, { x: contentFarEdge, y: contentRect.bottom }, { x: contentNearEdge, y: contentRect.bottom } ], side }); window.clearTimeout(contentContext.pointerGraceTimerRef.value); contentContext.pointerGraceTimerRef.value = window.setTimeout(() => contentContext.onPointerGraceIntentChange(null), 300); } else { const defaultPrevented = contentContext.onTriggerLeave(event); if (defaultPrevented) return; contentContext.onPointerGraceIntentChange(null); } } async function handleKeyDown(event) { var _a; const isTypingAhead = contentContext.searchRef.value !== ""; if (props.disabled || isTypingAhead && event.key === " ") return; if (SUB_OPEN_KEYS[rootContext.dir.value].includes(event.key)) { menuContext.onOpenChange(true); await nextTick(); (_a = menuContext.content.value) == null ? void 0 : _a.focus(); event.preventDefault(); } } return (_ctx, _cache) => { return openBlock(), createBlock(MenuAnchor_default, { "as-child": "" }, { default: withCtx(() => [createVNode(MenuItemImpl_default, mergeProps(props, { id: unref(subContext).triggerId, ref: (vnode) => { var _a; (_a = unref(subContext)) == null ? void 0 : _a.onTriggerChange(vnode == null ? void 0 : vnode.$el); return void 0; }, "aria-haspopup": "menu", "aria-expanded": unref(menuContext).open.value, "aria-controls": unref(subContext).contentId, "data-state": unref(getOpenState)(unref(menuContext).open.value), onClick: _cache[0] || (_cache[0] = async (event) => { if (props.disabled || event.defaultPrevented) return; event.currentTarget.focus(); if (!unref(menuContext).open.value) unref(menuContext).onOpenChange(true); }), onPointermove: handlePointerMove, onPointerleave: handlePointerLeave, onKeydown: handleKeyDown }), { default: withCtx(() => [renderSlot(_ctx.$slots, "default")]), _: 3 }, 16, [ "id", "aria-expanded", "aria-controls", "data-state" ])]), _: 3 }); }; } }); var MenuSubTrigger_default = MenuSubTrigger_vue_vue_type_script_setup_true_lang_default; var DropdownMenuItem_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ __name: "DropdownMenuItem", props: { disabled: { type: Boolean, required: false }, textValue: { type: String, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false } }, emits: ["select"], setup(__props, { emit: __emit }) { const props = __props; const emits = __emit; const emitsAsProps = useEmitAsProps(emits); useForwardExpose(); return (_ctx, _cache) => { return openBlock(), createBlock(unref(MenuItem_default), normalizeProps(guardReactiveProps({ ...props, ...unref(emitsAsProps) })), { default: withCtx(() => [renderSlot(_ctx.$slots, "default")]), _: 3 }, 16); }; } }); var DropdownMenuItem_default = DropdownMenuItem_vue_vue_type_script_setup_true_lang_default; var DropdownMenuSubTrigger_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ __name: "DropdownMenuSubTrigger", props: { disabled: { type: Boolean, required: false }, textValue: { type: String, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false } }, setup(__props) { const props = __props; useForwardExpose(); return (_ctx, _cache) => { return openBlock(), createBlock(unref(MenuSubTrigger_default), normalizeProps(guardReactiveProps(props)), { default: withCtx(() => [renderSlot(_ctx.$slots, "default")]), _: 3 }, 16); }; } }); var DropdownMenuSubTrigger_default = DropdownMenuSubTrigger_vue_vue_type_script_setup_true_lang_default; const _hoisted_1 = { key: 2, class: "mt-action-menu-item__arrow" }; const _hoisted_2 = { key: 3, class: "mt-action-menu-item__external-link" }; const _sfc_main = /* @__PURE__ */ defineComponent({ __name: "mt-action-menu-item", props: { variant: { default: "default" }, icon: { default: void 0 }, disabled: { type: Boolean, default: false }, shortcut: { default: void 0 }, isSubTrigger: { type: Boolean }, as: { default: void 0 }, link: { default: void 0 } }, setup(__props) { const MAC_SYMBOLS = { mod: "⌘", ctrl: "⌃", alt: "⌥", meta: "⌘", up: "↑", down: "↓", left: "←", right: "→" }; const SPECIAL_KEYS = [ "enter", "esc", "tab", "space", "backspace", "delete", "up", "down", "left", "right" ]; const MODIFIER_PRIORITY = { mod: 1, ctrl: 1, meta: 1, alt: 2, shift: 3 }; const ARIA_MODIFIER_KEYS = { mod: { mac: "Meta", pc: "Control" }, ctrl: { mac: "Control", pc: "Control" }, alt: { mac: "Alt", pc: "Alt" }, shift: { mac: "Shift", pc: "Shift" }, meta: { mac: "Meta", pc: "Meta" } }; const ARIA_SPECIAL_KEYS = { enter: "Enter", esc: "Escape", tab: "Tab", space: "Space", backspace: "Backspace", delete: "Delete", up: "ArrowUp", down: "ArrowDown", left: "ArrowLeft", right: "ArrowRight" }; const { t } = useI18n({ useScope: "global", messages: { en: { "mt-action-menu-item": { keys: { shift: "Shift", ctrl: "Ctrl", alt: "Alt", meta: "Win", enter: "Enter", esc: "Esc", tab: "Tab", space: "Space", backspace: "Backspace", delete: "Delete" } } }, de: { "mt-action-menu-item": { keys: { shift: "Umschalt", ctrl: "Strg", alt: "Alt", meta: "Win", enter: "Eingabe", esc: "Esc", tab: "Tab", space: "Leertaste", backspace: "Rücktaste", delete: "Entf" } } } } }); const props = __props; const group = inject( "mt-action-menu-group", null ); onMounted(() => { if (group) { group.registerItem(!!props.icon); } }); const isMac = typeof navigator !== "undefined" && /Mac|iPod|iPhone|iPad/.test(navigator.platform); const getKeyLabel = (key, forMac) => { if (forMac && MAC_SYMBOLS[key]) return MAC_SYMBOLS[key]; if (["up", "down", "left", "right"].includes(key)) return MAC_SYMBOLS[key]; if (key === "mod") return t("mt-action-menu-item.keys.ctrl"); return t(`mt-action-menu-item.keys.${key}`); }; const formatShortcut = (def) => { const { modifiers = [], key } = def; const sortedModifiers = [...modifiers].sort( (a, b) => MODIFIER_PRIORITY[a] - MODIFIER_PRIORITY[b] ); const isSpecial = SPECIAL_KEYS.includes(key); return { pc: [ ...sortedModifiers.map((m) => getKeyLabel(m, false)), isSpecial ? getKeyLabel(key, false) : key.toUpperCase() ], mac: [ ...sortedModifiers.map((m) => getKeyLabel(m, true)), isSpecial ? getKeyLabel(key, true) : key.toUpperCase() ] }; }; const shortcutKeys = computed(() => { if (!props.shortcut) return []; const formatted = formatShortcut(props.shortcut); return isMac ? formatted.mac : formatted.pc; }); const ariaKeyShortcuts = computed(() => { if (!props.shortcut) return void 0; const { modifiers = [], key } = props.shortcut; const sortedModifiers = [...modifiers].sort( (a, b) => MODIFIER_PRIORITY[a] - MODIFIER_PRIORITY[b] ); const platform = isMac ? "mac" : "pc"; const parts = sortedModifiers.map((m) => ARIA_MODIFIER_KEYS[m][platform]); const isSpecial = SPECIAL_KEYS.includes(key); parts.push(isSpecial ? ARIA_SPECIAL_KEYS[key] : key.toUpperCase()); return parts.join("+"); }); return (_ctx, _cache) => { return openBlock(), createBlock(resolveDynamicComponent(_ctx.isSubTrigger ? unref(DropdownMenuSubTrigger_default) : unref(DropdownMenuItem_default)), { as: _ctx.link ? "a" : _ctx.as, href: _ctx.link, target: _ctx.link ? "_blank" : void 0, rel: _ctx.link ? "noopener noreferrer" : void 0, class: normalizeClass(["mt-action-menu-item", `mt-action-menu-item--variant-${_ctx.variant}`]), disabled: _ctx.disabled, "data-has-icon": !!_ctx.icon || void 0, "aria-keyshortcuts": ariaKeyShortcuts.value }, { default: withCtx(() => [ !!_ctx.icon ? (openBlock(), createBlock(_sfc_main$1, { key: 0, name: _ctx.icon, size: "14", mode: "solid" }, null, 8, ["name"])) : createCommentVNode("", true), renderSlot(_ctx.$slots, "default", {}, void 0, true), shortcutKeys.value.length && !_ctx.isSubTrigger && !_ctx.link ? (openBlock(), createElementBlock("kbd", { key: 1, "aria-hidden": "true", class: normalizeClass([ "mt-action-menu-item__shortcut", { "mt-action-menu-item__shortcut--disabled": _ctx.disabled } ]) }, [ (openBlock(true), createElementBlock(Fragment, null, renderList(shortcutKeys.value, (key, index) => { return openBlock(), createElementBlock("span", { key: index }, toDisplayString(key), 1); }), 128)) ], 2)) : createCommentVNode("", true), _ctx.isSubTrigger && !_ctx.link ? (openBlock(), createElementBlock("div", _hoisted_1, [ createVNode(_sfc_main$1, { name: "chevron-right-s", size: "10", color: "var(--color-icon-primary-default)", mode: "regular" }) ])) : createCommentVNode("", true), _ctx.link ? (openBlock(), createElementBlock("div", _hoisted_2, [ createVNode(_sfc_main$1, { name: "external-link-s", size: "10", color: "var(--color-icon-secondary-default)", mode: "regular" }) ])) : createCommentVNode("", true) ]), _: 3 }, 8, ["as", "href", "target", "rel", "class", "disabled", "data-has-icon", "aria-keyshortcuts"]); }; } }); const mtActionMenuItem_vue_vue_type_style_index_0_scoped_de4ae728_lang = ""; const mtActionMenuItem = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-de4ae728"]]); export { MenuAnchor_default as M, mtActionMenuItem as m }; //# sourceMappingURL=mt-action-menu-item-982cf55b.mjs.map