feat(dropdown): 新增上下文菜单
This commit is contained in:
parent
1a721dfbf7
commit
8bb7d448f7
@ -8,7 +8,8 @@
|
|||||||
.layui-dropdown dl {
|
.layui-dropdown dl {
|
||||||
display: none;
|
display: none;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 0;
|
left: var(--layui-dropdown-left);
|
||||||
|
top: var(--layui-dropdown-top);
|
||||||
margin-top: 2px;
|
margin-top: 2px;
|
||||||
z-index: 899;
|
z-index: 899;
|
||||||
min-width: 100%;
|
min-width: 100%;
|
||||||
|
@ -6,7 +6,7 @@ export default {
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import "./index.less";
|
import "./index.less";
|
||||||
import { provide, ref } from "vue";
|
import { computed, provide, ref } from "vue";
|
||||||
import { onClickOutside } from "@vueuse/core";
|
import { onClickOutside } from "@vueuse/core";
|
||||||
import { DropdownTrigger } from "./interface";
|
import { DropdownTrigger } from "./interface";
|
||||||
|
|
||||||
@ -22,13 +22,25 @@ const props = withDefaults(defineProps<LayDropdownProps>(), {
|
|||||||
const emit = defineEmits(["open", "hide"]);
|
const emit = defineEmits(["open", "hide"]);
|
||||||
const openState = ref(false);
|
const openState = ref(false);
|
||||||
const dropdownRef = ref<null | HTMLElement>(null);
|
const dropdownRef = ref<null | HTMLElement>(null);
|
||||||
|
const dropdownX = ref<number | string>(0);
|
||||||
|
const dropdownY = ref<number | string>("auto");
|
||||||
|
|
||||||
onClickOutside(dropdownRef, (event) => {
|
onClickOutside(dropdownRef, (event) => {
|
||||||
openState.value = false;
|
openState.value = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
const open = function () {
|
const open = function (event?: Event) {
|
||||||
if (props.disabled === false) {
|
if (props.disabled === false) {
|
||||||
|
if(event){
|
||||||
|
const el = event.currentTarget;
|
||||||
|
// @ts-ignore
|
||||||
|
const rect = el.getBoundingClientRect();
|
||||||
|
// @ts-ignore
|
||||||
|
dropdownX.value = event.clientX - rect.left + "px";
|
||||||
|
// @ts-ignore
|
||||||
|
dropdownY.value = event.clientY - rect.top + "px";
|
||||||
|
}
|
||||||
|
|
||||||
openState.value = true;
|
openState.value = true;
|
||||||
emit("open");
|
emit("open");
|
||||||
}
|
}
|
||||||
@ -39,15 +51,20 @@ const hide = function () {
|
|||||||
emit("hide");
|
emit("hide");
|
||||||
};
|
};
|
||||||
|
|
||||||
const toggle = function () {
|
const toggle = function (event?: Event) {
|
||||||
if (props.disabled === false)
|
if (props.disabled === false)
|
||||||
if (openState.value) {
|
if (openState.value) {
|
||||||
hide();
|
hide();
|
||||||
} else {
|
} else {
|
||||||
open();
|
open(event);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const dropdownStyle = computed(() => ({
|
||||||
|
'--layui-dropdown-left': dropdownX.value,
|
||||||
|
'--layui-dropdown-top': dropdownY.value,
|
||||||
|
}))
|
||||||
|
|
||||||
provide("openState", openState);
|
provide("openState", openState);
|
||||||
|
|
||||||
defineExpose({ open, hide, toggle });
|
defineExpose({ open, hide, toggle });
|
||||||
@ -60,8 +77,12 @@ defineExpose({ open, hide, toggle });
|
|||||||
@mouseenter="trigger === 'hover' && open()"
|
@mouseenter="trigger === 'hover' && open()"
|
||||||
@mouseleave="trigger === 'hover' && hide()"
|
@mouseleave="trigger === 'hover' && hide()"
|
||||||
:class="{ 'layui-dropdown-up': openState }"
|
:class="{ 'layui-dropdown-up': openState }"
|
||||||
|
:style="dropdownStyle"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
@click="trigger === 'click' && toggle()"
|
||||||
|
@contextmenu.prevent="trigger === 'contextMenu' && toggle($event)"
|
||||||
>
|
>
|
||||||
<div @click="trigger === 'click' && toggle()">
|
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
</div>
|
</div>
|
||||||
<dl class="layui-anim layui-anim-upbit">
|
<dl class="layui-anim layui-anim-upbit">
|
||||||
|
@ -1 +1 @@
|
|||||||
export type DropdownTrigger = "click" | "hover";
|
export type DropdownTrigger = "click" | "hover" | "contextMenu";
|
||||||
|
@ -66,6 +66,17 @@ export default {
|
|||||||
</lay-dropdown-menu>
|
</lay-dropdown-menu>
|
||||||
</template>
|
</template>
|
||||||
</lay-dropdown>
|
</lay-dropdown>
|
||||||
|
|
||||||
|
<lay-dropdown trigger="contextMenu">
|
||||||
|
<lay-button>contextMenu 触发</lay-button>
|
||||||
|
<template #content>
|
||||||
|
<lay-dropdown-menu>
|
||||||
|
<lay-dropdown-menu-item>选项一</lay-dropdown-menu-item>
|
||||||
|
<lay-dropdown-menu-item>选项二</lay-dropdown-menu-item>
|
||||||
|
<lay-dropdown-menu-item>选项三</lay-dropdown-menu-item>
|
||||||
|
</lay-dropdown-menu>
|
||||||
|
</template>
|
||||||
|
</lay-dropdown>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@ -154,7 +165,7 @@ export default {
|
|||||||
|
|
||||||
| 属性 | 描述 | 可选值 |
|
| 属性 | 描述 | 可选值 |
|
||||||
| ------- | -------- | --------------- |
|
| ------- | -------- | --------------- |
|
||||||
| trigger | 触发方式 | `click` `hover` |
|
| trigger | 触发方式 | `click` `hover` `contextMenu` |
|
||||||
| disabled | 是否禁用触发 | `true` `false` |
|
| disabled | 是否禁用触发 | `true` `false` |
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
Loading…
x
Reference in New Issue
Block a user