From 903c05a848ad15d6283763e560fe9660ed3ac0b7 Mon Sep 17 00:00:00 2001 From: kooriookami <38392315+kooriookami@users.noreply.github.com> Date: Tue, 26 Jan 2021 08:01:49 -0600 Subject: [PATCH] perf(scrollbar): when scrollbar isn't shown, hide the bar (#1342) * perf(scrollbar): when scrollbar isn't shown, hide the bar * fix: fix right click bar will return top * perf: store and restore onselectstart * perf: update --- packages/scrollbar/src/bar.ts | 86 ---------------- packages/scrollbar/src/bar.vue | 125 ++++++++++++++++++++++++ packages/scrollbar/src/index.vue | 45 ++++----- packages/theme-chalk/src/scrollbar.scss | 24 ++--- 4 files changed, 156 insertions(+), 124 deletions(-) delete mode 100644 packages/scrollbar/src/bar.ts create mode 100644 packages/scrollbar/src/bar.vue diff --git a/packages/scrollbar/src/bar.ts b/packages/scrollbar/src/bar.ts deleted file mode 100644 index 68d8fbb1c5..0000000000 --- a/packages/scrollbar/src/bar.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { defineComponent, h, computed, ref, getCurrentInstance, onUnmounted, inject, Ref } from 'vue' -import { on, off } from '@element-plus/utils/dom' -import { renderThumbStyle, BAR_MAP } from './util' - -export default defineComponent({ - name: 'Bar', - - props: { - vertical: Boolean, - size: String, - move: Number, - }, - - setup(props) { - const instance = getCurrentInstance() - const thumb = ref(null) - const wrap = inject('scroll-bar-wrap', {} as Ref>) - const bar = computed(() => { - return BAR_MAP[props.vertical ? 'vertical' : 'horizontal'] - }) - const barStore = ref({}) - const cursorDown = ref(null) - const clickThumbHandler= e => { - // prevent click event of right button - if (e.ctrlKey || e.button === 2) { - return - } - startDrag(e) - barStore.value[bar.value.axis] = (e.currentTarget[bar.value.offset] - (e[bar.value.client] - e.currentTarget.getBoundingClientRect()[bar.value.direction])) - } - - const clickTrackHandler = e => { - const offset = Math.abs(e.target.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) - const thumbHalf = (thumb.value[bar.value.offset] / 2) - const thumbPositionPercentage = ((offset - thumbHalf) * 100 / instance.vnode.el[bar.value.offset]) - - wrap.value[bar.value.scroll] = (thumbPositionPercentage * wrap.value[bar.value.scrollSize] / 100) - } - const startDrag = e => { - e.stopImmediatePropagation() - cursorDown.value = true - on(document, 'mousemove', mouseMoveDocumentHandler) - on(document, 'mouseup', mouseUpDocumentHandler) - document.onselectstart = () => false - } - - const mouseMoveDocumentHandler = e => { - if (cursorDown.value === false) return - const prevPage = barStore.value[bar.value.axis] - - if (!prevPage) return - - const offset = ((instance.vnode.el.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) * -1) - const thumbClickPosition = (thumb.value[bar.value.offset] - prevPage) - const thumbPositionPercentage = ((offset - thumbClickPosition) * 100 / instance.vnode.el[bar.value.offset]) - wrap.value[bar.value.scroll] = (thumbPositionPercentage * wrap.value[bar.value.scrollSize] / 100) - } - - function mouseUpDocumentHandler() { - cursorDown.value = false - barStore.value[bar.value.axis] = 0 - off(document, 'mousemove', mouseMoveDocumentHandler) - document.onselectstart = null - } - - onUnmounted(() => { - off(document, 'mouseup', mouseUpDocumentHandler) - }) - - return () => h('div', { - class: ['el-scrollbar__bar', 'is-' + bar.value.key], - onMousedown: clickTrackHandler, - }, - h('div', { - ref: thumb, - class: 'el-scrollbar__thumb', - onMousedown: clickThumbHandler, - style: renderThumbStyle({ - size: props.size, - move: props.move, - bar: bar.value, - }), - }), - ) - }, -}) diff --git a/packages/scrollbar/src/bar.vue b/packages/scrollbar/src/bar.vue new file mode 100644 index 0000000000..3e68588858 --- /dev/null +++ b/packages/scrollbar/src/bar.vue @@ -0,0 +1,125 @@ + + + diff --git a/packages/scrollbar/src/index.vue b/packages/scrollbar/src/index.vue index 5ed86c20f7..405e87e794 100644 --- a/packages/scrollbar/src/index.vue +++ b/packages/scrollbar/src/index.vue @@ -1,5 +1,5 @@