Mouse button modifiers help you run code only when a specific mouse button is clicked. This makes your app respond exactly how you want.
Mouse button modifiers in Vue
<element @click.left="handler">...</element> <element @click.right="handler">...</element> <element @click.middle="handler">...</element>
Use .left, .right, or .middle after the event name to specify the mouse button.
This works with mouse events like click, mousedown, and mouseup.
sayLeft only when the left mouse button is clicked.<button @click.left="sayLeft">Left Click</button>showMenu only on right mouse button down.<div @mousedown.right="showMenu">Right Click Here</div>middleAction only when the middle mouse button is released.<span @mouseup.middle="middleAction">Middle Click</span>This Vue component has three buttons. Each button listens for clicks from a specific mouse button using modifiers. The message below changes depending on which button is clicked. The right click also uses .prevent to stop the browser's context menu.
<template>
<div>
<button @click.left="handleLeftClick">Left Click Me</button>
<button @click.right.prevent="handleRightClick">Right Click Me</button>
<button @click.middle="handleMiddleClick">Middle Click Me</button>
<p>{{ message }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('Click a button')
function handleLeftClick() {
message.value = 'Left button clicked!'
}
function handleRightClick() {
message.value = 'Right button clicked!'
}
function handleMiddleClick() {
message.value = 'Middle button clicked!'
}
</script>The .right modifier often needs .prevent to stop the browser's context menu from showing.
Mouse button modifiers only work with mouse events, not keyboard events.
Test your app on different devices because some mice or touchpads may behave differently.
Mouse button modifiers let you run code only for left, right, or middle mouse clicks.
Use .left, .right, or .middle after event names like @click.
They help make your app respond exactly to the mouse button the user presses.