Refactor #6647 - For ScrollPanel

pull/6674/head
tugcekucukoglu 2024-10-28 12:59:00 +03:00
parent 1578dc4bb0
commit 7cfe3d852e
2 changed files with 40 additions and 8 deletions

View File

@ -40,8 +40,8 @@
</template> </template>
<script> <script>
import { addClass, getHeight, removeClass } from '@primeuix/utils/dom';
import { UniqueComponentId } from '@primevue/core/utils'; import { UniqueComponentId } from '@primevue/core/utils';
import { getHeight, addClass, removeClass } from '@primeuix/utils/dom';
import BaseScrollPanel from './BaseScrollPanel.vue'; import BaseScrollPanel from './BaseScrollPanel.vue';
export default { export default {
@ -61,12 +61,14 @@ export default {
lastPageY: null, lastPageY: null,
timer: null, timer: null,
outsideClickListener: null, outsideClickListener: null,
mutationObserver: null,
data() { data() {
return { return {
id: this.$attrs.id, id: this.$attrs.id,
orientation: 'vertical', orientation: 'vertical',
lastScrollTop: 0, lastScrollTop: 0,
lastScrollLeft: 0 lastScrollLeft: 0,
isRTL: false
}; };
}, },
watch: { watch: {
@ -80,6 +82,9 @@ export default {
if (this.$el.offsetParent) { if (this.$el.offsetParent) {
this.initialize(); this.initialize();
} }
this.updateDirection();
this.observeDirectionChanges();
}, },
updated() { updated() {
if (!this.initialized && this.$el.offsetParent) { if (!this.initialized && this.$el.offsetParent) {
@ -92,8 +97,25 @@ export default {
if (this.frame) { if (this.frame) {
window.cancelAnimationFrame(this.frame); window.cancelAnimationFrame(this.frame);
} }
if (this.mutationObserver) {
this.mutationObserver.disconnect();
}
}, },
methods: { methods: {
updateDirection() {
this.isRTL = !!this.$el.closest('[dir="rtl"]');
},
observeDirectionChanges() {
const targetNode = document.documentElement;
const config = { attributes: true, attributeFilter: ['dir'] };
this.mutationObserver = new MutationObserver(() => {
this.updateDirection();
});
this.mutationObserver.observe(targetNode, config);
},
initialize() { initialize() {
this.moveBar(); this.moveBar();
this.bindDocumentResizeListener(); this.bindDocumentResizeListener();
@ -137,9 +159,14 @@ export default {
} else { } else {
this.$refs.xBar.setAttribute('data-p-scrollpanel-hidden', 'false'); this.$refs.xBar.setAttribute('data-p-scrollpanel-hidden', 'false');
!this.isUnstyled && removeClass(this.$refs.xBar, 'p-scrollpanel-hidden'); !this.isUnstyled && removeClass(this.$refs.xBar, 'p-scrollpanel-hidden');
if (this.isRTL) {
this.$refs.xBar.style.cssText = 'width:' + Math.max(this.scrollXRatio * 100, 10) + '%; right:' + (this.$refs.content.scrollLeft / totalWidth) * 100 + '%;bottom:' + bottom + 'px;';
} else {
this.$refs.xBar.style.cssText = 'width:' + Math.max(this.scrollXRatio * 100, 10) + '%; left:' + (this.$refs.content.scrollLeft / totalWidth) * 100 + '%;bottom:' + bottom + 'px;'; this.$refs.xBar.style.cssText = 'width:' + Math.max(this.scrollXRatio * 100, 10) + '%; left:' + (this.$refs.content.scrollLeft / totalWidth) * 100 + '%;bottom:' + bottom + 'px;';
} }
} }
}
if (this.$refs.yBar) { if (this.$refs.yBar) {
if (this.scrollYRatio >= 1) { if (this.scrollYRatio >= 1) {
@ -148,10 +175,16 @@ export default {
} else { } else {
this.$refs.yBar.setAttribute('data-p-scrollpanel-hidden', 'false'); this.$refs.yBar.setAttribute('data-p-scrollpanel-hidden', 'false');
!this.isUnstyled && removeClass(this.$refs.yBar, 'p-scrollpanel-hidden'); !this.isUnstyled && removeClass(this.$refs.yBar, 'p-scrollpanel-hidden');
if (this.isRTL) {
this.$refs.yBar.style.cssText =
'height:' + Math.max(this.scrollYRatio * 100, 10) + '%; top: calc(' + (this.$refs.content.scrollTop / totalHeight) * 100 + '% - ' + this.$refs.xBar.clientHeight + 'px);left:' + right + 'px;';
} else {
this.$refs.yBar.style.cssText = this.$refs.yBar.style.cssText =
'height:' + Math.max(this.scrollYRatio * 100, 10) + '%; top: calc(' + (this.$refs.content.scrollTop / totalHeight) * 100 + '% - ' + this.$refs.xBar.clientHeight + 'px);right:' + right + 'px;'; 'height:' + Math.max(this.scrollYRatio * 100, 10) + '%; top: calc(' + (this.$refs.content.scrollTop / totalHeight) * 100 + '% - ' + this.$refs.xBar.clientHeight + 'px);right:' + right + 'px;';
} }
} }
}
}); });
} }
}, },

View File

@ -31,7 +31,6 @@ const theme = ({ dt }) => `
cursor: pointer; cursor: pointer;
opacity: 0; opacity: 0;
outline-color: transparent; outline-color: transparent;
transition: outline-color ${dt('scrollpanel.transition.duration')};
background: ${dt('scrollpanel.bar.background')}; background: ${dt('scrollpanel.bar.background')};
border: 0 none; border: 0 none;
transition: outline-color ${dt('scrollpanel.transition.duration')}, opacity ${dt('scrollpanel.transition.duration')}; transition: outline-color ${dt('scrollpanel.transition.duration')}, opacity ${dt('scrollpanel.transition.duration')};
@ -45,12 +44,12 @@ const theme = ({ dt }) => `
.p-scrollpanel-bar-y { .p-scrollpanel-bar-y {
width: ${dt('scrollpanel.bar.size')}; width: ${dt('scrollpanel.bar.size')};
top: 0; inset-block-start: 0;
} }
.p-scrollpanel-bar-x { .p-scrollpanel-bar-x {
height: ${dt('scrollpanel.bar.size')}; height: ${dt('scrollpanel.bar.size')};
bottom: 0; inset-block-end: 0;
} }
.p-scrollpanel-hidden { .p-scrollpanel-hidden {