Line data Source code
1 16 : <script setup lang="ts">
2 : import { createReusableTemplate } from '@vueuse/core'
3 : import { DropdownMenuCheckboxItem, DropdownMenuItem } from 'reka-ui'
4 : import { computed, onBeforeUnmount, toValue } from 'vue'
5 :
6 : import { cn } from '@comfyorg/tailwind-utils'
7 :
8 : import TextTooltip from '@comfyorg/design-system/tooltip/TextTooltip'
9 : import { whileMouseDown } from './mouseDownUtil'
10 :
11 : import { menuItemClass } from './menuStyles'
12 : import type { MenuItemAction } from './types'
13 :
14 : const { allowCommandless = false, item } = defineProps<{
15 : allowCommandless?: boolean
16 : item: MenuItemAction
17 : }>()
18 :
19 4699 : const emit = defineEmits<{
20 : select: []
21 : activate: [item: MenuItemAction]
22 : }>()
23 :
24 4699 : const [DefineItem, ReuseItem] = createReusableTemplate()
25 :
26 4699 : const disabled = computed(() => toValue(item.disabled) === true)
27 4699 : const focusableDisabled = computed(
28 : () => disabled.value && item.focusableWhenDisabled === true
29 : )
30 4699 : const unfocusable = computed(() =>
31 : disabled.value
32 : ? !item.focusableWhenDisabled
33 : : !item.command && !allowCommandless
34 : )
35 :
36 4699 : let pointerRepeating = false
37 4699 : let stopRepeating: (() => void) | undefined
38 :
39 4699 : onBeforeUnmount(() => stopRepeating?.())
40 :
41 433 : function mouseDown(event: MouseEvent) {
42 433 : if (
43 : event.button !== 0 ||
44 : toValue(item.disabled) ||
45 : !item.command ||
46 : item.pressAndHoldInterval === undefined
47 : )
48 432 : return
49 433 : stopRepeating?.()
50 433 : pointerRepeating = false
51 433 : const { dispose } = whileMouseDown(
52 : event,
53 : () => {
54 4 : const firstRepeat = !pointerRepeating
55 4 : pointerRepeating = true
56 4 : void item.command?.({ originalEvent: event, item })
57 4 : if (firstRepeat) emit('activate', item)
58 : },
59 : item.pressAndHoldInterval
60 : )
61 433 : stopRepeating = dispose
62 : }
63 :
64 436 : function select(event: Event) {
65 436 : if (disabled.value || !item.command) {
66 0 : event.preventDefault()
67 0 : return
68 : }
69 436 : if (pointerRepeating) {
70 1 : pointerRepeating = false
71 1 : event.preventDefault()
72 1 : return
73 : }
74 435 : if (item.checked !== undefined) event.preventDefault()
75 435 : void item.command({ originalEvent: event, item })
76 435 : emit('activate', item)
77 435 : if (event.defaultPrevented) return
78 394 : if (item.pressAndHoldInterval !== undefined) {
79 0 : event.preventDefault()
80 0 : return
81 : }
82 383 : emit('select')
83 : }
84 4699 : </script>
85 :
86 : <template>
87 : <DefineItem>
88 : <component
89 : :is="
90 : item.checked === undefined ? DropdownMenuItem : DropdownMenuCheckboxItem
91 : "
92 : :aria-label="toValue(item.label)"
93 : :aria-description="toValue(item.description)"
94 : :disabled="unfocusable"
95 : :as-child="focusableDisabled"
96 : :class="
97 : cn(
98 : menuItemClass,
99 : item.variant === 'destructive' && 'text-destructive',
100 : focusableDisabled && 'opacity-50',
101 : toValue(item.class)
102 : )
103 : "
104 : :model-value="toValue(item.checked)"
105 : @mousedown="mouseDown"
106 : @keydown.capture="pointerRepeating = false"
107 : @select="select"
108 : >
109 : <div v-if="focusableDisabled" aria-disabled="true">
110 : <slot />
111 : </div>
112 : <slot v-else />
113 : </component>
114 : </DefineItem>
115 : <TextTooltip v-if="item.tooltip" :text="item.tooltip" side="right">
116 : <ReuseItem />
117 : </TextTooltip>
118 : <ReuseItem v-else />
119 : </template>
|