Files
medusa-store/www/apps/ui/specs/examples/dropdown-menu-shortcuts.tsx
Shahed Nasser d1a1135328 docs: migrate UI docs (#13245)
* docs: create a new UI docs project (#13233)

* docs: create a new UI docs project

* fix installation errors

* docs: migrate UI docs content to new project (#13241)

* Fix content

* added examples for some components

* finish adding examples

* lint fix

* fix build errors

* delete empty files

* path fixes + refactor

* fix build error
2025-08-20 11:42:25 +03:00

66 lines
1.8 KiB
TypeScript

import { useEffect, useCallback } from "react"
import { DropdownMenu, IconButton, toast, Toaster } from "@medusajs/ui"
import { Keyboard } from "@medusajs/icons"
function getOsShortcut() {
const isMacOs =
typeof navigator !== "undefined"
? navigator.userAgent.toLowerCase().indexOf("mac") !== 0
: true
return isMacOs ? "⌘" : "Ctrl"
}
export default function DropdownMenuWithShortcuts() {
const osShortcut = getOsShortcut()
const handleEdit = useCallback(() => {
toast.success("Success", {
description: "Edit shortcut triggered!",
})
}, [])
const handleDelete = useCallback(() => {
toast.success("Success", {
description: "Delete shortcut triggered!",
})
}, [])
useEffect(() => {
function handleKeydown(e: KeyboardEvent) {
if (e.metaKey && e.key.toLowerCase() === "e") {
e.preventDefault()
handleEdit()
}
if (e.metaKey && e.key.toLowerCase() === "d") {
e.preventDefault()
handleDelete()
}
}
window.addEventListener("keydown", handleKeydown)
return () => window.removeEventListener("keydown", handleKeydown)
}, [handleEdit, handleDelete])
return (
<>
<DropdownMenu>
<DropdownMenu.Trigger asChild>
<IconButton>
<Keyboard />
</IconButton>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Item onSelect={handleEdit}>
Edit
<DropdownMenu.Shortcut>{osShortcut}E</DropdownMenu.Shortcut>
</DropdownMenu.Item>
<DropdownMenu.Item onSelect={handleDelete}>
Delete
<DropdownMenu.Shortcut>{osShortcut}D</DropdownMenu.Shortcut>
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu>
<Toaster />
</>
)
}