📖 Scenario: You are building a simple interactive webpage where users can click a button with different mouse buttons to see different messages.This helps understand how mouse button modifiers work in Vue.
🎯 Goal: Create a Vue component that shows a button. When the user clicks the button with the left, middle, or right mouse button, a message updates to show which button was clicked.
📋 What You'll Learn
Create a Vue component with a button and a message display
Use Vue's mouse button modifiers .left, .middle, and .right on the button's click event
Update a reactive message variable to show which mouse button was clicked
Ensure the component uses the Composition API with <script setup>
💡 Why This Matters
🌍 Real World
Mouse button modifiers help create interactive web apps that respond differently to left, middle, or right clicks, such as custom context menus or special controls.
💼 Career
Understanding event modifiers in Vue is essential for frontend developers to build intuitive and accessible user interfaces.
Progress0 / 4 steps
1
Set up the Vue component with a message variable
Create a Vue component using <script setup>. Inside it, create a reactive variable called message initialized to the string 'Click a button'. Also add a <template> with a <button> and a <p> that displays the message variable.
Vue
Hint
Use ref from Vue to create a reactive variable called message.
2
Add click handler functions for each mouse button
Inside the <script setup>, add three functions: leftClick, middleClick, and rightClick. Each function should update the message variable to the strings 'Left button clicked', 'Middle button clicked', and 'Right button clicked' respectively.
Vue
Hint
Define three functions that set message.value to the correct string.
3
Add mouse button modifiers to the button click events
In the <template>, add three click event listeners to the <button> using Vue's mouse button modifiers: @click.left, @click.middle, and @click.right. Bind them to the functions leftClick, middleClick, and rightClick respectively.
Vue
Hint
Use Vue's mouse button modifiers on the button's click event to call the correct functions.
4
Add contextmenu event to prevent default right-click menu
In the <template>, add an event listener @contextmenu.prevent to the <button> to prevent the browser's default right-click menu from appearing when the right mouse button is clicked.
Vue
Hint
Add @contextmenu.prevent on the button to stop the default right-click menu.
Practice
(1/5)
1. What does the .right modifier do when used with a Vue @click event?
easy
A. It triggers the event only when the right mouse button is clicked.
B. It triggers the event only when the left mouse button is clicked.
C. It triggers the event only when the middle mouse button is clicked.
D. It triggers the event for any mouse button clicked.
Solution
Step 1: Understand mouse button modifiers in Vue
Vue provides modifiers like .left, .right, and .middle to specify which mouse button triggers an event.
Step 2: Identify the effect of .right modifier
The .right modifier 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 A
Quick Check:
Mouse button modifier .right = right click only [OK]
Hint: Remember .right means right mouse button only [OK]
Common Mistakes:
Confusing .right with .left or .middle
Thinking .right triggers on any click
Using .right without @click event
2. Which of the following is the correct syntax to listen for a middle mouse button click in Vue?
easy
A. @click.middle="handleClick"
B. @middle.click="handleClick"
C. @click.middleButton="handleClick"
D. @click.middle_click="handleClick"
Solution
Step 1: Recall Vue mouse button modifier syntax
Vue uses @click.middle to listen for middle mouse button clicks.
Step 2: Check each option's syntax
Only @click.middle is valid syntax; others are incorrect or invalid modifiers.
Final Answer:
@click.middle="handleClick" -> Option A
Quick Check:
Correct syntax for middle click = @click.middle [OK]
Hint: Use @click.middle for middle mouse button events [OK]
Why might the handleRight method not run when right-clicking the div?
medium
A. Because .prevent modifier stops the event from firing.
B. Because @click.right is invalid syntax.
C. Because handleRight is not defined.
D. Because the browser's context menu blocks the event.
Solution
Step 1: Understand the effect of .prevent modifier
The .prevent modifier calls event.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 handleRight might not run
If the context menu appears, it may block the event or user interaction, so the handler might not seem to run.
Final Answer:
Because the browser's context menu blocks the event. -> Option D
Quick Check:
Right-click triggers context menu unless prevented [OK]
Hint: Right-click triggers context menu unless prevented [OK]
Common Mistakes:
Thinking .prevent stops event handler
Assuming @click.right is invalid
Ignoring browser default context menu
5. You want to create a Vue component that only reacts to left and middle mouse clicks but ignores right clicks. Which is the best way to write the event listeners?
hard
A. <div @click.left.middle="onLeftMiddle">Click</div>
B. <div @click.left="onLeft" @click.middle="onMiddle">Click</div>
C. <div @click.right="onRight">Click</div>
D. <div @click="onAnyClick">Click</div>
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 .left and .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 B
Quick Check:
Separate .left and .middle listeners handle clicks correctly [OK]
Hint: Use separate @click.left and @click.middle listeners [OK]
Common Mistakes:
Trying to combine .left and .middle in one modifier
Listening to right clicks when not needed
Using generic @click without filtering buttons
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.