feat(dropdown): 新增上下文菜单
This commit is contained in:
@@ -8,7 +8,8 @@
|
||||
.layui-dropdown dl {
|
||||
display: none;
|
||||
position: absolute;
|
||||
left: 0;
|
||||
left: var(--layui-dropdown-left);
|
||||
top: var(--layui-dropdown-top);
|
||||
margin-top: 2px;
|
||||
z-index: 899;
|
||||
min-width: 100%;
|
||||
|
||||
@@ -6,7 +6,7 @@ export default {
|
||||
|
||||
<script setup lang="ts">
|
||||
import "./index.less";
|
||||
import { provide, ref } from "vue";
|
||||
import { computed, provide, ref } from "vue";
|
||||
import { onClickOutside } from "@vueuse/core";
|
||||
import { DropdownTrigger } from "./interface";
|
||||
|
||||
@@ -22,13 +22,25 @@ const props = withDefaults(defineProps<LayDropdownProps>(), {
|
||||
const emit = defineEmits(["open", "hide"]);
|
||||
const openState = ref(false);
|
||||
const dropdownRef = ref<null | HTMLElement>(null);
|
||||
const dropdownX = ref<number | string>(0);
|
||||
const dropdownY = ref<number | string>("auto");
|
||||
|
||||
onClickOutside(dropdownRef, (event) => {
|
||||
openState.value = false;
|
||||
});
|
||||
|
||||
const open = function () {
|
||||
const open = function (event?: Event) {
|
||||
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;
|
||||
emit("open");
|
||||
}
|
||||
@@ -39,15 +51,20 @@ const hide = function () {
|
||||
emit("hide");
|
||||
};
|
||||
|
||||
const toggle = function () {
|
||||
const toggle = function (event?: Event) {
|
||||
if (props.disabled === false)
|
||||
if (openState.value) {
|
||||
hide();
|
||||
} else {
|
||||
open();
|
||||
open(event);
|
||||
}
|
||||
};
|
||||
|
||||
const dropdownStyle = computed(() => ({
|
||||
'--layui-dropdown-left': dropdownX.value,
|
||||
'--layui-dropdown-top': dropdownY.value,
|
||||
}))
|
||||
|
||||
provide("openState", openState);
|
||||
|
||||
defineExpose({ open, hide, toggle });
|
||||
@@ -60,8 +77,12 @@ defineExpose({ open, hide, toggle });
|
||||
@mouseenter="trigger === 'hover' && open()"
|
||||
@mouseleave="trigger === 'hover' && hide()"
|
||||
:class="{ 'layui-dropdown-up': openState }"
|
||||
:style="dropdownStyle"
|
||||
>
|
||||
<div @click="trigger === 'click' && toggle()">
|
||||
<div
|
||||
@click="trigger === 'click' && toggle()"
|
||||
@contextmenu.prevent="trigger === 'contextMenu' && toggle($event)"
|
||||
>
|
||||
<slot></slot>
|
||||
</div>
|
||||
<dl class="layui-anim layui-anim-upbit">
|
||||
|
||||
@@ -1 +1 @@
|
||||
export type DropdownTrigger = "click" | "hover";
|
||||
export type DropdownTrigger = "click" | "hover" | "contextMenu";
|
||||
|
||||
Reference in New Issue
Block a user