Skip to content

Instantly share code, notes, and snippets.

@eladcandroid
Created August 2, 2019 10:03
Show Gist options
  • Save eladcandroid/7f6aae68cceb0ef6fac69381a3c5818e to your computer and use it in GitHub Desktop.
Save eladcandroid/7f6aae68cceb0ef6fac69381a3c5818e to your computer and use it in GitHub Desktop.
Vue 3 Function API - Mixin
<template>
<div class="container">
<p v-if="isLoading" class="loading">Posts loading...</p>
<template v-else>
<div class="topbar" :class="{ open: pageOffset > 120 }">
<div class="container content">
<h3>User #{{ id }} posts</h3>
<span class="count">{{ count }} items</span>
</div>
</div>
<h1>User #{{ id }} posts</h1>
<span class="count">{{ count }} items</span>
<div v-for="post in posts" :key="post.id" class="box">
<h3>{{ post.title }}</h3>
<p>{{ post.body }}</p>
</div>
</template>
</div>
</template>
<script>
const scrollMixin = {
data() {
return {
pageOffset: 0
}
},
mounted() {
window.addEventListener('scroll', this.update)
},
destroyed() {
window.removeEventListener('scroll', this.update)
},
methods: {
update() {
this.pageOffset = window.pageYOffset
}
}
}
export default {
mixins: [scrollMixin],
props: {
id: Number,
isLoading: Boolean,
posts: Array,
count: Number
}
}
</script>
<style lang="scss" scoped>
.box {
padding: 0.5em 1.5em;
margin: 1em 0;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
border-radius: 10px;
background: #fff;
}
.count {
background: #48d8bb;
padding: 4px 10px;
font-size: 14px;
border-radius: 100px;
color: #fff;
}
.loading {
text-align: center;
}
.topbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
overflow-y: hidden;
background: #fff;
box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
max-height: 0;
transition: all 0.5s ease-out;
&.open {
max-height: 100px;
}
.content {
display: flex;
align-items: center;
justify-content: space-between;
}
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment