There isn't a known method to move the common component around the page freely. Typically, this is achieved in the jQuery realm, but it often leads to messy and difficult-to-troubleshoot code.
@Nils suggested using v-if
in the comments section, which prevents the floating component from being rendered in the DOM. This means that it won't add weight to the page, resulting in a perceived faster experience for users.
In theory, Vue.js should handle rendering thousands of rows without any issues. More information can be found at Stefan Krause's blog:
In the example below, the "Do something" button only appears in the DOM when the cursor is placed inside one of the comment fields. Until then, it remains unrendered.
Vue.component('comment-block', {
props: ["comment"],
template: `
<div class="comment" @mouseenter="displayOptions=true" @mouseleave="displayOptions=false">
<div class="author">{{comment.author}} wrote:</div>
<input type='text' v-model='comment.message'/>
<div class="options" v-if="displayOptions"><button>Do something</button></div>
</div>`,
data: function(){
return {
displayOptions: false
}
}
});
new Vue({
el: '#app',
data: {
comments: [
{id: 0, author: 'Bob', message: "This is my comment #1"},
{id: 1, author: 'Frank', message: "Comment #2"},
{id: 2, author: 'Joe', message: "One more comment in this page (#3)"}
]
}
});
body {
margin: 20px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.comment {
padding: 5px;
}
.comment .author {
display: inline-block;
width: 120px;
text-align: right;
}
.comment input {
display: inline-block;
width: 240px;
}
.comment .options {
display: inline-block;
}
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afd9dacaef9d819e819a">[email protected]</a>/dist/vue.js"></script>
<body>
<div id="app">
<p>List of comments:</p>
<comment-block :comment="comment" v-for="comment in comments"></comment-block>
</div>
</body>
Note: The "Do something" button currently has no functionality besides displaying on the screen in this example.
This approach helps maintain a lightweight interface. However, as mentioned in the performance analysis blog post referenced earlier, this micro-optimization typically doesn't have a significant impact unless specific issues arise.