-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): support context menu API
- Loading branch information
Showing
12 changed files
with
177 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ | |
} | ||
} | ||
|
||
.k-menu-item { | ||
.k-tab-menu-item { | ||
display: block; | ||
position: relative; | ||
cursor: pointer; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<template> | ||
<template v-for="{ id, styles } of ctx.internal.activeMenus" :key="id"> | ||
<k-menu | ||
:id="id" | ||
:style="styles" | ||
:ref="el => elements[id] = (el as ComponentPublicInstance)" | ||
></k-menu> | ||
</template> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { Dict, useContext } from '@koishijs/client' | ||
import { ComponentPublicInstance, shallowReactive } from 'vue' | ||
import { useEventListener } from '@vueuse/core' | ||
import KMenu from './menu.vue' | ||
const ctx = useContext() | ||
const elements = shallowReactive<Dict<ComponentPublicInstance>>({}) | ||
useEventListener('click', () => { | ||
ctx.internal.activeMenus.splice(0) | ||
}) | ||
useEventListener('contextmenu', () => { | ||
ctx.internal.activeMenus.splice(0) | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<template> | ||
<div | ||
class="k-menu-item" | ||
v-if="forced || !disabled" | ||
:class="{ disabled }" | ||
@click.prevent="action?.action(ctx.internal.createScope())" | ||
> | ||
{{ toValue(label) }} | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { MaybeGetter, MenuItem, useContext } from '@koishijs/client' | ||
import { computed } from 'vue' | ||
const props = defineProps<MenuItem & { prefix: string }>() | ||
const ctx = useContext() | ||
const forced = computed(() => props.id.startsWith('!')) | ||
const action = computed(() => { | ||
let id = props.id.replace(/^!/, '') | ||
if (id.startsWith('.')) id = props.prefix + id | ||
return ctx.internal.actions[id] | ||
}) | ||
const disabled = computed(() => { | ||
if (!action.value) return true | ||
if (!action.value.disabled) return false | ||
return toValue(action.value.disabled) | ||
}) | ||
function toValue<T>(getter: MaybeGetter<T>): T { | ||
if (typeof getter !== 'function') return getter | ||
return (getter as any)(ctx.internal.createScope()) | ||
} | ||
</script> | ||
|
||
<style lang="scss"> | ||
.k-menu-item { | ||
user-select: none; | ||
padding: 0.25rem 1.5rem; | ||
cursor: pointer; | ||
transition: var(--color-transition); | ||
&.disabled { | ||
color: var(--k-text-light); | ||
pointer-events: none; | ||
} | ||
&:not(.disabled):hover { | ||
background-color: var(--k-hover-bg); | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<template> | ||
<div ref="menu" class="k-menu"> | ||
<template v-for="item of ctx.internal.menus[id]"> | ||
<div class="k-menu-separator" v-if="item.id === '@separator'"></div> | ||
<menu-item v-else v-bind="{ prefix: id, ...item }"></menu-item> | ||
</template> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { useContext } from '@koishijs/client' | ||
import MenuItem from './menu-item.vue' | ||
defineProps<{ | ||
id: string | ||
}>() | ||
const ctx = useContext() | ||
</script> | ||
|
||
<style lang="scss"> | ||
.k-menu { | ||
position: fixed; | ||
z-index: 1000; | ||
min-width: 12rem; | ||
padding: var(--k-menu-padding-vertical) 0; | ||
border-radius: 6px; | ||
background-color: var(--k-menu-bg); | ||
box-shadow: var(--k-menu-shadow); | ||
transition: var(--color-transition); | ||
font-size: 14px; | ||
.k-menu-separator { | ||
margin: var(--k-menu-padding-vertical) 0; | ||
border-top: 1px solid var(--k-color-border); | ||
&:first-child, &:last-child { | ||
display: none; | ||
} | ||
& + & { | ||
display: none; | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters