0
0
Vueframework~10 mins

Mouse button modifiers in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mouse button modifiers
User clicks mouse
Vue event listener detects click
Check mouse button modifier
Run left
Execute handler based on button
When a user clicks, Vue listens for the event, checks which mouse button was pressed, and runs the matching handler.
Execution Sample
Vue
<template>
  <button @click.left="onLeftClick" @click.right.prevent="onRightClick">Click me</button>
</template>

<script setup>
function onLeftClick() { alert('Left click!') }
function onRightClick() { alert('Right click!') }
</script>
This Vue button runs different functions depending on left or right mouse clicks.
Execution Table
StepMouse Button ClickedModifier DetectedHandler CalledPrevent Default
1Leftclick.leftonLeftClickNo
2Rightclick.rightonRightClickYes (prevented)
3MiddleNo matching modifierNo handlerNo
💡 No handler runs if the mouse button modifier does not match any listener.
Variable Tracker
VariableStartAfter Left ClickAfter Right ClickAfter Middle Click
Handler CalledNoneonLeftClickonRightClickNone
Prevent DefaultFalseFalseTrueFalse
Key Moments - 2 Insights
Why doesn't the middle mouse button trigger any handler?
Because the code only listens for .left and .right modifiers, so middle clicks have no matching handler (see execution_table row 3).
What does the .prevent modifier do on the right click?
It stops the browser's default context menu from showing on right click, as shown by 'Prevent Default' being Yes in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which handler runs when the left mouse button is clicked?
AonLeftClick
BonRightClick
CNo handler
DonMiddleClick
💡 Hint
Check the 'Handler Called' column for Step 1 in execution_table.
At which step does the prevent default action happen?
AStep 1
BStep 3
CStep 2
DNo step
💡 Hint
Look at the 'Prevent Default' column in execution_table.
If we add @click.middle="onMiddleClick", what changes in the execution_table?
AStep 1 handler changes to onMiddleClick
BStep 3 handler changes to onMiddleClick
CNo change
DStep 2 handler changes to onMiddleClick
💡 Hint
Look at Step 3 where middle click currently has no handler.
Concept Snapshot
Vue mouse button modifiers let you run code for specific mouse buttons.
Use .left, .right, .middle after @click.
Example: @click.left triggers on left click only.
.prevent stops default browser actions like context menu.
No handler runs if no matching modifier is used.
Full Transcript
In Vue, mouse button modifiers let you run different code depending on which mouse button the user clicks. When a user clicks, Vue checks if the click event has a modifier like .left or .right. If it matches, Vue runs the handler for that button. For example, @click.left runs only on left clicks. The .prevent modifier stops the browser's default action, like the right-click menu. If no modifier matches, no handler runs. This helps create buttons that respond differently to left, right, or middle clicks.