Visitar URL original
feat(ContextMenu): add `open` and `pressOpenDelay` props (#7148) · nuxt/ui@73c489b · GitHub
Skip to content

Commit 73c489b

Browse files
feat(ContextMenu): add open and pressOpenDelay props (#7148)
1 parent 378873a commit 73c489b

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script setup lang="ts">
2+
import type { ContextMenuItem } from '@nuxt/ui'
3+
4+
const open = ref(false)
5+
6+
defineShortcuts({
7+
o: () => open.value = !open.value
8+
})
9+
10+
const items: ContextMenuItem[] = [
11+
{
12+
label: 'Profile',
13+
icon: 'i-lucide-user'
14+
}, {
15+
label: 'Billing',
16+
icon: 'i-lucide-credit-card'
17+
}, {
18+
label: 'Settings',
19+
icon: 'i-lucide-cog'
20+
}
21+
]
22+
</script>
23+
24+
<template>
25+
<UContextMenu v-model:open="open" :items="items" :ui="{ content: 'w-48' }">
26+
<div class="flex items-center justify-center rounded-md border border-dashed border-accented text-sm aspect-video w-72">
27+
Right click here
28+
</div>
29+
</UContextMenu>
30+
</template>

‎docs/content/docs/2.components/context-menu.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,21 @@ name: 'context-menu-color-items-example'
326326
---
327327
::
328328

329+
### Control open state :badge{label="Soon" class="align-text-top"}
330+
331+
You can control the open state by using the `v-model:open` directive. When opened programmatically, the menu is anchored to the trigger instead of the cursor.
332+
333+
::component-example
334+
---
335+
collapse: true
336+
name: 'context-menu-open-example'
337+
---
338+
::
339+
340+
::note
341+
In this example, leveraging [`defineShortcuts`](/docs/composables/define-shortcuts), you can toggle the ContextMenu by pressing :kbd{value="O"}.
342+
::
343+
329344
### With custom slot
330345

331346
Use the `slot` property to customize a specific item.

‎src/runtime/components/ContextMenu.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface ContextMenuItem extends Omit<LinkProps, 'type' | 'raw' | 'custo
4343
[key: string]: any
4444
}
4545
46-
export interface ContextMenuProps<T extends ArrayOrNested<ContextMenuItem> = ArrayOrNested<ContextMenuItem>> extends Omit<ContextMenuRootProps, 'dir' | 'open' | 'pressOpenDelay'> {
46+
export interface ContextMenuProps<T extends ArrayOrNested<ContextMenuItem> = ArrayOrNested<ContextMenuItem>> extends Omit<ContextMenuRootProps, 'dir'> {
4747
/**
4848
* @defaultValue 'md'
4949
*/
@@ -137,7 +137,7 @@ const props = useComponentProps<ContextMenuProps<T>>('contextMenu', _props)
137137
138138
const appConfig = useAppConfig() as ContextMenu['AppConfig']
139139
140-
const rootProps = useForwardProps(reactivePick(props, 'modal'), emits)
140+
const rootProps = useForwardProps(reactivePick(props, 'open', 'pressOpenDelay', 'modal'), emits)
141141
const contentProps = toRef(() => props.content)
142142
const getProxySlots = () => omit(slots, ['default'])
143143

‎test/components/ContextMenu.spec.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { h, defineComponent } from 'vue'
2-
import { describe, it, expect, test } from 'vitest'
2+
import { describe, it, expect, test, expectTypeOf } from 'vitest'
33
import { axe } from 'vitest-axe'
44
import { mountSuspended } from '@nuxt/test-utils/runtime'
55
import type { AppConfig } from '@nuxt/schema'
6+
import { ContextMenuRoot } from 'reka-ui'
67
import ContextMenu from '../../src/runtime/components/ContextMenu.vue'
8+
import type { ContextMenuProps } from '../../src/runtime/components/ContextMenu.vue'
9+
import Theme from '../../src/runtime/components/Theme.vue'
710
import type { ComponentConfig } from '../../src/runtime/types/tv'
811
import { expectSlotProps } from '../utils/types'
912
import { renderEach } from '../component-render'
@@ -143,6 +146,17 @@ describe('ContextMenu', () => {
143146
}
144147
)
145148

149+
it('resolves open and pressOpenDelay from Theme props', async () => {
150+
const wrapper = await mountSuspended({
151+
components: { UTheme: Theme, ContextMenuWrapper },
152+
setup: () => ({ props }),
153+
template: `<UTheme :props="{ contextMenu: { open: true, pressOpenDelay: 100 } }"><ContextMenuWrapper v-bind="props" /></UTheme>`
154+
})
155+
156+
expect(wrapper.find('[data-slot="content"]').exists()).toBe(true)
157+
expect(wrapper.findComponent(ContextMenuRoot).props('pressOpenDelay')).toBe(100)
158+
})
159+
146160
it('passes accessibility tests', async () => {
147161
const wrapper = await mountSuspended(ContextMenuWrapper, {
148162
props
@@ -154,6 +168,9 @@ describe('ContextMenu', () => {
154168
})
155169

156170
test('should have the correct types', () => {
171+
expectTypeOf<ContextMenuProps['open']>().toEqualTypeOf<boolean | undefined>()
172+
expectTypeOf<ContextMenuProps['pressOpenDelay']>().toEqualTypeOf<number | undefined>()
173+
157174
// normal
158175
expectSlotProps('item', () => ContextMenu({
159176
items: [{ label: 'foo', value: 'bar' }]

0 commit comments

Comments
 (0)