2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2023-08-02 12:02:36 +00:00
|
|
|
<transition name="p-scrolltop" appear @enter="onEnter" @after-leave="onAfterLeave" v-bind="ptm('transition')">
|
2024-02-11 23:48:21 +00:00
|
|
|
<button v-if="visible" :ref="containerRef" :class="cx('root')" @click="onClick" type="button" :aria-label="scrollTopAriaLabel" v-bind="ptmi('root')">
|
2023-05-25 14:42:59 +00:00
|
|
|
<slot name="icon" :class="cx('icon')">
|
|
|
|
<component :is="icon ? 'span' : 'ChevronUpIcon'" :class="[cx('icon'), icon]" v-bind="ptm('icon')" />
|
2023-04-07 23:06:45 +00:00
|
|
|
</slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</button>
|
|
|
|
</transition>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-04-18 12:53:43 +00:00
|
|
|
import ChevronUpIcon from 'primevue/icons/chevronup';
|
2022-09-14 11:26:01 +00:00
|
|
|
import { DomHandler, ZIndexUtils } from 'primevue/utils';
|
2023-05-25 14:42:59 +00:00
|
|
|
import BaseScrollTop from './BaseScrollTop.vue';
|
2023-04-21 13:42:00 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
export default {
|
|
|
|
name: 'ScrollTop',
|
2023-05-24 10:09:45 +00:00
|
|
|
extends: BaseScrollTop,
|
2024-02-11 23:48:21 +00:00
|
|
|
inheritAttrs: false,
|
2022-09-06 12:03:37 +00:00
|
|
|
scrollListener: null,
|
|
|
|
container: null,
|
2022-09-14 11:26:01 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
visible: false
|
|
|
|
};
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
mounted() {
|
2022-09-14 11:26:01 +00:00
|
|
|
if (this.target === 'window') this.bindDocumentScrollListener();
|
|
|
|
else if (this.target === 'parent') this.bindParentScrollListener();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
beforeUnmount() {
|
2022-09-14 11:26:01 +00:00
|
|
|
if (this.target === 'window') this.unbindDocumentScrollListener();
|
|
|
|
else if (this.target === 'parent') this.unbindParentScrollListener();
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
if (this.container) {
|
|
|
|
ZIndexUtils.clear(this.container);
|
|
|
|
this.overlay = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onClick() {
|
|
|
|
let scrollElement = this.target === 'window' ? window : this.$el.parentElement;
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
scrollElement.scroll({
|
|
|
|
top: 0,
|
|
|
|
behavior: this.behavior
|
|
|
|
});
|
|
|
|
},
|
|
|
|
checkVisibility(scrollY) {
|
2022-09-14 11:26:01 +00:00
|
|
|
if (scrollY > this.threshold) this.visible = true;
|
|
|
|
else this.visible = false;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
bindParentScrollListener() {
|
|
|
|
this.scrollListener = () => {
|
|
|
|
this.checkVisibility(this.$el.parentElement.scrollTop);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.$el.parentElement.addEventListener('scroll', this.scrollListener);
|
|
|
|
},
|
|
|
|
bindDocumentScrollListener() {
|
|
|
|
this.scrollListener = () => {
|
|
|
|
this.checkVisibility(DomHandler.getWindowScrollTop());
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('scroll', this.scrollListener);
|
|
|
|
},
|
|
|
|
unbindParentScrollListener() {
|
|
|
|
if (this.scrollListener) {
|
|
|
|
this.$el.parentElement.removeEventListener('scroll', this.scrollListener);
|
|
|
|
this.scrollListener = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unbindDocumentScrollListener() {
|
|
|
|
if (this.scrollListener) {
|
|
|
|
window.removeEventListener('scroll', this.scrollListener);
|
|
|
|
this.scrollListener = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onEnter(el) {
|
|
|
|
ZIndexUtils.set('overlay', el, this.$primevue.config.zIndex.overlay);
|
|
|
|
},
|
|
|
|
onAfterLeave(el) {
|
|
|
|
ZIndexUtils.clear(el);
|
|
|
|
},
|
|
|
|
containerRef(el) {
|
|
|
|
this.container = el;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2022-12-08 11:04:25 +00:00
|
|
|
scrollTopAriaLabel() {
|
|
|
|
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.scrollTop : undefined;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2023-04-07 23:06:45 +00:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
ChevronUpIcon
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
</script>
|