Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add scroll to bottom button
Signed-off-by: Marco Ambrosini <[email protected]>
  • Loading branch information
marcoambrosini committed Aug 28, 2020
commit 404106855b08888e3f1a223ace25d238a4d840c8
46 changes: 40 additions & 6 deletions src/components/MessagesList/MessagesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ get the messagesList array and loop through the list to generate the messages.
type="messages"
:count="15" />
</template>
<transition name="fade">
<button v-show="!isScrolledToBottom"
class="scroll-to-bottom"
@click="scrollToBottom">
<ChevronDown :size="24" fill-color="#fff" />
</button>
</transition>
</div>
</template>

Expand All @@ -65,12 +72,14 @@ import isInLobby from '../../mixins/isInLobby'
import debounce from 'debounce'
import { EventBus } from '../../services/EventBus'
import LoadingPlaceholder from '../LoadingPlaceholder'
import ChevronDown from 'vue-material-design-icons/ChevronDown'

export default {
name: 'MessagesList',
components: {
LoadingPlaceholder,
MessagesGroup,
ChevronDown,
},

mixins: [
Expand Down Expand Up @@ -200,6 +209,10 @@ export default {
chatIdentifier() {
return this.token + ':' + this.isParticipant + ':' + this.isInLobby
},

scroller() {
return document.querySelector('.scroller')
},
},

watch: {
Expand Down Expand Up @@ -503,17 +516,16 @@ export default {

debounceHandleScroll: debounce(function() {
this.handleScroll()
}, 600),
}, 200),
/**
* When the div is scrolled, this method checks if it's been scrolled to the top
* or to the bottom of the list bottom.
*/
async handleScroll() {
const scroller = document.querySelector('.scroller')
const scrollHeight = scroller.scrollHeight
const scrollTop = scroller.scrollTop
const scrollHeight = this.scroller.scrollHeight
const scrollTop = this.scroller.scrollTop
const scrollOffset = scrollHeight - scrollTop
const elementHeight = scroller.clientHeight
const elementHeight = this.scroller.clientHeight
const tolerance = 10
if (scrollOffset < elementHeight + tolerance && scrollOffset > elementHeight - tolerance) {
this.isScrolledToBottom = true
Expand Down Expand Up @@ -549,8 +561,9 @@ export default {
*/
scrollToBottom() {
this.$nextTick(function() {
document.querySelector('.scroller').scrollTop = document.querySelector('.scroller').scrollHeight
this.scroller.scrollTop = this.scroller.scrollHeight
})
this.isScrolledToBottom = true
},

/**
Expand Down Expand Up @@ -581,14 +594,35 @@ export default {
</script>

<style lang="scss" scoped>
@import '../../assets/variables.scss';

.scroller {
flex: 1 0;
overflow-y: auto;
scroll-behavior: smooth;
&__loading {
height: 50px;
display: flex;
justify-content: center;
}
}

.scroll-to-bottom {
position: absolute;
width: 44px;
height: 44px;
background-color: var(--color-primary-element);
bottom: 76px;
right: 24px;
z-index: 2;
padding: 0;
margin: 0;
&:hover,
&:focus {
background-color: var(--color-primary-element-light);
opacity: 1 !important;
border: none;
}
}

</style>