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.
Practice
.right modifier do when used with a Vue @click event?Solution
Step 1: Understand mouse button modifiers in Vue
Vue provides modifiers like.left,.right, and.middleto specify which mouse button triggers an event.Step 2: Identify the effect of
The.rightmodifier.rightmodifier restricts the event to trigger only on right mouse button clicks.Final Answer:
It triggers the event only when the right mouse button is clicked. -> Option AQuick Check:
Mouse button modifier.right= right click only [OK]
- Confusing .right with .left or .middle
- Thinking .right triggers on any click
- Using .right without @click event
Solution
Step 1: Recall Vue mouse button modifier syntax
Vue uses@click.middleto listen for middle mouse button clicks.Step 2: Check each option's syntax
Only@click.middleis valid syntax; others are incorrect or invalid modifiers.Final Answer:
@click.middle="handleClick" -> Option AQuick Check:
Correct syntax for middle click = @click.middle [OK]
- Placing .middle before click
- Using incorrect modifier names
- Adding underscores or camelCase in modifiers
<button @click.left="leftClick" @click.right="rightClick">Click me</button>
What happens when the user clicks the button with the middle mouse button?
Solution
Step 1: Analyze the event modifiers on the button
The button listens for@click.leftand@click.rightevents only, meaning left and right mouse clicks trigger handlers.Step 2: Determine what happens on middle mouse click
Since there is no@click.middlehandler, the middle mouse button click triggers no method.Final Answer:
No method is called. -> Option CQuick Check:
Middle click without handler = no action [OK]
- Assuming middle click triggers left or right handlers
- Ignoring missing middle click handler
- Thinking all clicks trigger all handlers
<div @click.left="handleLeft" @click.right.prevent="handleRight">Test</div>
Why might the
handleRight method not run when right-clicking the div?Solution
Step 1: Understand the effect of
The.preventmodifier.preventmodifier callsevent.preventDefault()to stop default browser actions like navigation or form submission, but does not stop event handlers from running.Step 2: Consider browser default behavior on right-click
Right-click usually opens the context menu, which can interfere with event handling unless prevented explicitly.Step 3: Identify why
If the context menu appears, it may block the event or user interaction, so the handler might not seem to run.handleRightmight not runFinal Answer:
Because the browser's context menu blocks the event. -> Option DQuick Check:
Right-click triggers context menu unless prevented [OK]
- Thinking .prevent stops event handler
- Assuming @click.right is invalid
- Ignoring browser default context menu
Solution
Step 1: Identify required mouse buttons
The component should respond only to left and middle clicks, ignoring right clicks.Step 2: Check each option's event listeners
<div @click.left="onLeft" @click.middle="onMiddle">Click</div> uses separate listeners for.leftand.middle, correctly handling both buttons.Step 3: Evaluate other options
<div @click.left.middle="onLeftMiddle">Click</div> uses an invalid combined modifier.left.middle. <div @click.right="onRight">Click</div> listens only to right clicks, which should be ignored. <div @click="onAnyClick">Click</div> listens to all clicks, including right clicks.Final Answer:
<div @click.left="onLeft" @click.middle="onMiddle">Click</div> -> Option BQuick Check:
Separate .left and .middle listeners handle clicks correctly [OK]
- Trying to combine .left and .middle in one modifier
- Listening to right clicks when not needed
- Using generic @click without filtering buttons
Ready to prove it?
Pass the test and this pattern moves unknown
