<script setup lang="ts">
import { AnimatePresence, motion } from 'motion-v'
import {
ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuItem, ContextMenuLabel, ContextMenuSeparator,
ContextMenuShortcut, ContextMenuTrigger, useReducedMotion
} from 'beui-vue'
import { ref, defineComponent, h, type PropType } from 'vue'
const ICONS: Record<string, string> = {
ChevronDown: '<path d="m6 9 6 6 6-6"/>',
CreditCard: '<rect width="20" height="14" x="2" y="5" rx="2"/><line x1="2" x2="22" y1="10" y2="10"/>',
Link2: '<path d="M9 17H7A5 5 0 0 1 7 7h2"/><path d="M15 7h2a5 5 0 1 1 0 10h-2"/><line x1="8" x2="16" y1="12" y2="12"/>',
LogOut: '<path d="m16 17 5-5-5-5"/><path d="M21 12H9"/><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>',
Mail: '<path d="m22 7-8.991 5.727a2 2 0 0 1-2.009 0L2 7"/><rect x="2" y="4" width="20" height="16" rx="2"/>',
MessageSquare: '<path d="M22 17a2 2 0 0 1-2 2H6.828a2 2 0 0 0-1.414.586l-2.202 2.202A.71.71 0 0 1 2 21.286V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2z"/>',
Settings: '<path d="M9.671 4.136a2.34 2.34 0 0 1 4.659 0 2.34 2.34 0 0 0 3.319 1.915 2.34 2.34 0 0 1 2.33 4.033 2.34 2.34 0 0 0 0 3.831 2.34 2.34 0 0 1-2.33 4.033 2.34 2.34 0 0 0-3.319 1.915 2.34 2.34 0 0 1-4.659 0 2.34 2.34 0 0 0-3.32-1.915 2.34 2.34 0 0 1-2.33-4.033 2.34 2.34 0 0 0 0-3.831A2.34 2.34 0 0 1 6.35 6.051a2.34 2.34 0 0 0 3.319-1.915"/><circle cx="12" cy="12" r="3"/>',
Share2: '<circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" x2="15.42" y1="13.51" y2="17.49"/><line x1="15.41" x2="8.59" y1="6.51" y2="10.49"/>',
User: '<path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/>',
Check: '<path d="M20 6 9 17l-5-5"/>',
Copy: '<rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>',
Download: '<path d="M12 15V3"/><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5"/>',
Eye: '<path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0"/><circle cx="12" cy="12" r="3"/>',
Pencil: '<path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/><path d="m15 5 4 4"/>',
Trash2: '<path d="M10 11v6"/><path d="M14 11v6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M3 6h18"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>',
}
export const MenuIcon = defineComponent({
name: 'MenuIcon',
props: { name: { type: String as PropType<keyof typeof ICONS>, required: true } },
setup: props => () => h('svg', {
xmlns: 'http://www.w3.org/2000/svg', width: 24, height: 24, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor',
'stroke-width': 2, 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': 'true', innerHTML: ICONS[props.name] ?? '',
}),
})
const swapSpring = { type: 'spring', stiffness: 460, damping: 30, mass: 0.55 } as const
const reduce = useReducedMotion()
const message = ref<string | null>(null)
const offline = ref(false)
</script>
<template>
<div class="ctx-stage">
<ContextMenu>
<ContextMenuTrigger>
<button type="button" class="ctx-target">
<div class="folder">
<div class="ctx-tab" />
<div class="ctx-back" />
<div class="ctx-front" />
<div class="ctx-rule" />
</div>
<span class="title">Right click on me</span>
<div class="status">
<AnimatePresence mode="wait" :initial="false">
<motion.span
v-if="message" :key="message" class="note"
:initial="reduce ? { opacity: 0 } : { opacity: 0, y: 3, filter: 'blur(2px)' }" :animate="{ opacity: 1, y: 0, filter: 'blur(0px)' }"
:exit="reduce ? { opacity: 0 } : { opacity: 0, y: -2, filter: 'blur(2px)' }" :transition="reduce ? { duration: 0.1 } : swapSpring"
>
<MenuIcon name="Check" class="tick" />
{{ message }}
</motion.span>
<span v-else key="hint" class="hint">or long-press · Shift + F10</span>
</AnimatePresence>
</div>
</button>
</ContextMenuTrigger>
<ContextMenuContent aria-label="Folder actions" style="width: 15rem">
<ContextMenuLabel>Project files</ContextMenuLabel>
<ContextMenuItem text-value="Open" @select="message = 'Folder opened'">
<MenuIcon name="Eye" />
Open
<ContextMenuShortcut>↵</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem text-value="Rename" @select="message = 'Ready to rename'">
<MenuIcon name="Pencil" />
Rename
<ContextMenuShortcut>R</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem text-value="Duplicate" @select="message = 'Folder duplicated'">
<MenuIcon name="Copy" />
Duplicate
<ContextMenuShortcut>⌘D</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem text-value="Download" @select="message = 'Download started'">
<MenuIcon name="Download" />
Download
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuCheckboxItem
text-value="Keep offline" :checked="offline" :close-on-select="false"
@checked-change="(checked: boolean) => { offline = checked; message = checked ? 'Available offline' : 'Online only' }"
>
Keep offline
</ContextMenuCheckboxItem>
<ContextMenuSeparator />
<ContextMenuItem tone="destructive" text-value="Move to trash" @select="message = 'Moved to trash'">
<MenuIcon name="Trash2" />
Move to trash
<ContextMenuShortcut>⌘⌫</ContextMenuShortcut>
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
</div>
</template>
<style scoped>
.ctx-stage { display: flex; width: 100%; min-height: 360px; align-items: center; justify-content: center; box-sizing: border-box; }
.ctx-target { display: flex; flex-direction: column; align-items: center; border: 0; background: none; padding: 0; color: inherit; font: inherit; outline: none; cursor: default; }
.folder { position: relative; width: 8rem; height: 6rem; transition: transform 0.15s; }
.ctx-target:active .folder { transform: scale(0.98); }
.ctx-target:focus-visible .folder { border-radius: 1rem; box-shadow: 0 0 0 4px var(--beui-background), 0 0 0 6px color-mix(in srgb, var(--beui-foreground) 20%, transparent); }
.ctx-tab { position: absolute; left: 0.25rem; top: 0.25rem; width: 3.5rem; height: 1.75rem; border-radius: 10px 10px 0 0; background: #d4a84f; }
.ctx-back { position: absolute; inset: 1.25rem 0 0; border-radius: 14px; background: #e7bb61; box-shadow: 0 14px 24px -16px rgba(90, 58, 8, 0.75); }
.ctx-front { position: absolute; inset: 2.25rem 0 0; border-radius: 14px; background: #efc86f; }
.ctx-rule { position: absolute; inset: auto 1.25rem 1rem; height: 1px; background: rgb(0 0 0 / 0.1); }
:global(.dark .ctx-tab) { background: #a77d2f; }
:global(.dark .ctx-back) { background: #bd8d36; }
:global(.dark .ctx-front) { background: #cb9a41; }
:global(.dark .ctx-rule) { background: rgb(255 255 255 / 0.1); }
.title { margin-top: 1rem; font-size: 0.875rem; line-height: 1.25rem; font-weight: 500; color: var(--beui-foreground); }
.status { margin-top: 0.25rem; height: 1rem; }
.hint, .note { font-size: 10px; line-height: 1rem; color: var(--beui-muted-foreground); }
.note { display: flex; align-items: center; gap: 0.25rem; }
.tick { width: 0.75rem; height: 0.75rem; color: oklch(70% 0.18 155); }
</style>