外观
ContextMenu 上下文菜单
右键或长按目标元素,在操作位置打开与当前对象相关的菜单。
import { SContextMenu } from '@icecreamqaq/shadcn/context-menu'使用示例
基础用法
context-menu.vue组合式 API vue
<template>
<s-context-menu>
<s-context-menu-trigger as="button" type="button" class="file">
<strong>会议纪要.md</strong>
<span>{{ favorite ? '已收藏' : '右键或长按查看操作' }}</span>
</s-context-menu-trigger>
<s-context-menu-content aria-label="文件操作">
<s-context-menu-item @select="favorite = !favorite">
{{ favorite ? '取消收藏' : '加入收藏' }}
</s-context-menu-item>
</s-context-menu-content>
</s-context-menu>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { SContextMenu, SContextMenuContent, SContextMenuItem, SContextMenuTrigger } from '@icecreamqaq/shadcn/context-menu'
const favorite = ref(false)
</script>
<style scoped>
.file { display: grid; gap: .5rem; min-width: 12rem; padding: 1.5rem; border: 1px dashed var(--sv-border); border-radius: var(--sv-radius); text-align: start; }
.file span { color: var(--sv-muted-foreground); font-size: .875rem; }
</style>vue
<template>
<s-context-menu>
<s-context-menu-trigger as="button" type="button" class="file">
<strong>会议纪要.md</strong>
<span>{{ favorite ? '已收藏' : '右键或长按查看操作' }}</span>
</s-context-menu-trigger>
<s-context-menu-content aria-label="文件操作">
<s-context-menu-item @select="favorite = !favorite">
{{ favorite ? '取消收藏' : '加入收藏' }}
</s-context-menu-item>
</s-context-menu-content>
</s-context-menu>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import {
SContextMenu,
SContextMenuContent,
SContextMenuItem,
SContextMenuTrigger,
} from '@icecreamqaq/shadcn/context-menu'
export default defineComponent({
components: { SContextMenu, SContextMenuContent, SContextMenuItem, SContextMenuTrigger },
data() {
return { favorite: false }
},
})
</script>
<style scoped>
.file { display: grid; gap: .5rem; min-width: 12rem; padding: 1.5rem; border: 1px dashed var(--sv-border); border-radius: var(--sv-radius); text-align: start; }
.file span { color: var(--sv-muted-foreground); font-size: .875rem; }
</style>常用 API
| 成员 | 类型 | 用法 |
|---|---|---|
SContextMenu.pressOpenDelay | number | 触摸或触控笔长按触发所需的毫秒数,默认 700。 |
SContextMenuTrigger.as | AsTag | Component | 指定触发区域的元素,默认渲染 span。 |
SContextMenuItem.@select | 事件 | 选择菜单项时执行对应操作。 |
SContextMenuItem.variant | "default" | "destructive" | 区分普通操作和破坏性操作的视觉样式。 |
完整 API
ContextMenu14 个成员
属性
| 名称 | 类型 |
|---|---|
pressOpenDelay | number |
open | boolean |
dir | Direction$4 |
modal | boolean |
unmountOnHide | boolean |
options | readonly MenuOption[] |
triggerLabel | string |
renderLabel | RenderContent<{ option: MenuOption; }> |
defaultOpen | boolean |
事件
| 名称 | 类型 |
|---|---|
select | ((option: MenuOption, event: Event) => any) |
update:open | ((payload: boolean) => any) |
插槽
| 名称 | 类型 |
|---|---|
default | ((scope: {}) => any) |
trigger | (() => any) |
label | ((scope: { option: MenuOption; }) => any) |
ContextMenuCheckboxItem11 个成员
属性
| 名称 | 类型 |
|---|---|
modelValue | CheckedState |
disabled | boolean |
textValue | string |
asChild | boolean |
as | AsTag | Component |
class | ClassValue |
defaultValue | CheckedState |
事件
| 名称 | 类型 |
|---|---|
select | ((event: Event) => any) |
update:modelValue | ((payload: boolean) => any) |
插槽
| 名称 | 类型 |
|---|---|
indicator-icon | ((props: {}) => any) |
default | ((props: {}) => any) |
ContextMenuContent28 个成员
属性
| 名称 | 类型 |
|---|---|
asChild | boolean |
as | AsTag | Component |
forceMount | boolean |
loop | boolean |
memoDependencies | unknown[] |
sideFlip | boolean |
fallbackAxisSideDirection | "none" | "start" | "end" |
alignOffset | number |
alignFlip | boolean |
avoidCollisions | boolean |
collisionBoundary | Element | (Element | null)[] | null |
collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> |
hideShiftedArrow | boolean |
sticky | "partial" | "always" |
hideWhenDetached | boolean |
positionStrategy | "absolute" | "fixed" |
disableUpdateOnLayoutShift | boolean |
prioritizePosition | boolean |
reference | ReferenceElement |
spaceExtendsTypeahead | boolean |
class | ClassValue |
事件
| 名称 | 类型 |
|---|---|
escapeKeyDown | ((event: KeyboardEvent) => any) |
pointerDownOutside | ((event: PointerDownOutsideEvent) => any) |
focusOutside | ((event: FocusOutsideEvent) => any) |
interactOutside | ((event: PointerDownOutsideEvent | FocusOutsideEvent) => any) |
openAutoFocus | ((event: Event) => any) |
closeAutoFocus | ((event: Event) => any) |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuGroup3 个成员
属性
| 名称 | 类型 |
|---|---|
asChild | boolean |
as | AsTag | Component |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuItem9 个成员
属性
| 名称 | 类型 |
|---|---|
disabled | boolean |
textValue | string |
asChild | boolean |
as | AsTag | Component |
class | ClassValue |
inset | boolean |
variant | "default" | "destructive" |
事件
| 名称 | 类型 |
|---|---|
select | ((event: Event) => any) |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuLabel5 个成员
属性
| 名称 | 类型 |
|---|---|
asChild | boolean |
as | AsTag | Component |
class | ClassValue |
inset | boolean |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuRadioGroup6 个成员
属性
| 名称 | 类型 |
|---|---|
modelValue | AcceptableValue |
asChild | boolean |
as | AsTag | Component |
defaultValue | AcceptableValue |
事件
| 名称 | 类型 |
|---|---|
update:modelValue | ((payload: AcceptableValue) => any) |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuRadioItem9 个成员
属性
| 名称 | 类型 |
|---|---|
value 必填 | AcceptableValue |
disabled | boolean |
textValue | string |
asChild | boolean |
as | AsTag | Component |
class | ClassValue |
事件
| 名称 | 类型 |
|---|---|
select | ((event: Event) => any) |
插槽
| 名称 | 类型 |
|---|---|
indicator-icon | ((props: {}) => any) |
default | ((props: {}) => any) |
ContextMenuSeparator3 个成员
属性
| 名称 | 类型 |
|---|---|
asChild | boolean |
as | AsTag | Component |
class | ClassValue |
ContextMenuShortcut2 个成员
属性
| 名称 | 类型 |
|---|---|
class | ClassValue |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuSub4 个成员
属性
| 名称 | 类型 |
|---|---|
defaultOpen | boolean |
open | boolean |
事件
| 名称 | 类型 |
|---|---|
update:open | ((payload: boolean) => any) |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuSubContent32 个成员
属性
| 名称 | 类型 |
|---|---|
forceMount | boolean |
asChild | boolean |
as | AsTag | Component |
loop | boolean |
memoDependencies | unknown[] |
sideOffset | number |
sideFlip | boolean |
fallbackAxisSideDirection | "none" | "start" | "end" |
alignOffset | number |
alignFlip | boolean |
avoidCollisions | boolean |
collisionBoundary | Element | (Element | null)[] | null |
collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> |
arrowPadding | number |
hideShiftedArrow | boolean |
sticky | "partial" | "always" |
hideWhenDetached | boolean |
positionStrategy | "absolute" | "fixed" |
updatePositionStrategy | "always" | "optimized" |
disableUpdateOnLayoutShift | boolean |
prioritizePosition | boolean |
reference | ReferenceElement |
spaceExtendsTypeahead | boolean |
class | ClassValue |
事件
| 名称 | 类型 |
|---|---|
escapeKeyDown | ((event: KeyboardEvent) => any) |
pointerDownOutside | ((event: PointerDownOutsideEvent) => any) |
focusOutside | ((event: FocusOutsideEvent) => any) |
interactOutside | ((event: PointerDownOutsideEvent | FocusOutsideEvent) => any) |
openAutoFocus | ((event: Event) => any) |
closeAutoFocus | ((event: Event) => any) |
entryFocus | ((event: Event) => any) |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuSubTrigger8 个成员
属性
| 名称 | 类型 |
|---|---|
graceDuration | number |
disabled | boolean |
textValue | string |
asChild | boolean |
as | AsTag | Component |
class | ClassValue |
inset | boolean |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
ContextMenuTrigger4 个成员
属性
| 名称 | 类型 |
|---|---|
disabled | boolean |
asChild | boolean |
as | AsTag | Component |
插槽
| 名称 | 类型 |
|---|---|
default | ((props: {}) => any) |
分组导出
模板标签为 <s-context-menu>。
组件 SContextMenu、SContextMenuCheckboxItem、SContextMenuContent、SContextMenuGroup、SContextMenuItem、SContextMenuLabel、SContextMenuRadioGroup、SContextMenuRadioItem、SContextMenuSeparator、SContextMenuShortcut、SContextMenuSub、SContextMenuSubContent、SContextMenuSubTrigger、SContextMenuTrigger