diff --git a/packages/varlet-cli/site/pc/App.vue b/packages/varlet-cli/site/pc/App.vue index 39906697b49..12931fa783f 100644 --- a/packages/varlet-cli/site/pc/App.vue +++ b/packages/varlet-cli/site/pc/App.vue @@ -512,7 +512,7 @@ iframe { display: block; content: ''; background: @color-primary; - width: 2px; + width: 4px; height: 40px; position: absolute; left: 0; diff --git a/packages/varlet-ui/src/menu/Menu.vue b/packages/varlet-ui/src/menu/Menu.vue index 48a7fef990a..ca9761f5661 100644 --- a/packages/varlet-ui/src/menu/Menu.vue +++ b/packages/varlet-ui/src/menu/Menu.vue @@ -34,31 +34,40 @@ export default defineComponent({ inheritAttrs: false, props, setup(props) { - const { zIndex } = useZIndex(props, 'show', 1) - const host: Ref = ref(null) const menu: Ref = ref(null) - const top: Ref = ref(0) const left: Ref = ref(0) + const { zIndex } = useZIndex(props, 'show', 1) + let clickSelf = false + const computeTop = (alignment: string): number => { return alignment === 'top' ? getTop(host.value as HTMLElement) : getTop(host.value as HTMLElement) - (menu.value as HTMLElement).offsetHeight } - let clickSelf = false - const handleClick = () => { clickSelf = true } + const handleMenuBlur = () => { + if (clickSelf) { + clickSelf = false + return + } + + props['onUpdate:show']?.(false) + props.onBlur?.() + } + watch( () => props.alignment, (newValue: string) => { props.show === true && (top.value = computeTop(newValue)) } ) + watch( () => props.show, async (newValue: boolean) => { @@ -69,22 +78,13 @@ export default defineComponent({ { immediate: true } ) - const handleMenuBlur = () => { - if (clickSelf) { - clickSelf = false - return - } - - props['onUpdate:show']?.(false) - props.onBlur?.() - } - onMounted(() => { top.value = computeTop(props.alignment) left.value = getLeft(host.value as HTMLElement) document.addEventListener('click', handleMenuBlur) }) + onUnmounted(() => { document.removeEventListener('click', handleMenuBlur) }) diff --git a/packages/varlet-ui/src/menu/example/index.vue b/packages/varlet-ui/src/menu/example/index.vue index 6a16dd748c9..b74bc733c8e 100644 --- a/packages/varlet-ui/src/menu/example/index.vue +++ b/packages/varlet-ui/src/menu/example/index.vue @@ -1,9 +1,5 @@