Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UBER-1159: fixed horizontal scrolling in Scroller #3945

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Changes from all commits
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
159 changes: 80 additions & 79 deletions packages/ui/src/components/Scroller.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -162,60 +162,55 @@

const onScroll = (event: MouseEvent): void => {
scrolling = false
if (isScrolling && divBar && divScroll) {
const rectScroll = divScroll.getBoundingClientRect()
if (isScrolling === 'vertical') {
let Y = event.clientY - dXY
if (Y < rectScroll.top + shiftTop + 2) Y = rectScroll.top + shiftTop + 2
if (Y > rectScroll.bottom - divBar.clientHeight - shiftBottom - 2) {
Y = rectScroll.bottom - divBar.clientHeight - shiftBottom - 2
}
divBar.style.top = Y - rectScroll.y + 'px'
const topBar = Y - rectScroll.y - shiftTop - 2
const heightScroll = rectScroll.height - 4 - divBar.clientHeight - shiftTop - shiftBottom
const procBar = topBar / heightScroll
divScroll.scrollTop = (divScroll.scrollHeight - divScroll.clientHeight) * procBar
} else {
let X = event.clientX - dXY
if (X < rectScroll.left + 2 + shiftLeft) X = rectScroll.left + 2 + shiftLeft
if (X > rectScroll.right - divBarH.clientWidth - (mask !== 'none' ? 12 : 2) - shiftRight) {
X = rectScroll.right - divBarH.clientWidth - (mask !== 'none' ? 12 : 2) - shiftRight
}
divBarH.style.left = X - rectScroll.x + 'px'
const leftBar = X - rectScroll.x - shiftLeft - 2
const widthScroll =
rectScroll.width - 2 - (mask !== 'none' ? 12 : 2) - divBarH.clientWidth - shiftLeft - shiftRight
const procBar = leftBar / widthScroll
divScroll.scrollLeft = (divScroll.scrollWidth - divScroll.clientWidth) * procBar
if (
(divBar == null && isScrolling === 'vertical') ||
(divBarH == null && isScrolling === 'horizontal') ||
divScroll == null
) {
return
}
const rectScroll = divScroll.getBoundingClientRect()
if (isScrolling === 'vertical') {
let Y = event.clientY - dXY
if (Y < rectScroll.top + shiftTop + 2) Y = rectScroll.top + shiftTop + 2
if (Y > rectScroll.bottom - divBar.clientHeight - shiftBottom - 2) {
Y = rectScroll.bottom - divBar.clientHeight - shiftBottom - 2
}
}
}
const onScrollEnd = (event: MouseEvent): void => {
const el: HTMLElement = event.currentTarget as HTMLElement
if (el && isScrolling) {
document.removeEventListener('mousemove', onScroll)
// document.body.style.pointerEvents = 'all'
document.body.style.userSelect = 'auto'
document.body.style.webkitUserSelect = 'auto'
}
divBar.style.top = Y - rectScroll.y + 'px'
const topBar = Y - rectScroll.y - shiftTop - 2
const heightScroll = rectScroll.height - 4 - divBar.clientHeight - shiftTop - shiftBottom
const procBar = topBar / heightScroll
divScroll.scrollTop = (divScroll.scrollHeight - divScroll.clientHeight) * procBar
} else {
let X = event.clientX - dXY
if (X < rectScroll.left + 2 + shiftLeft) X = rectScroll.left + 2 + shiftLeft
if (X > rectScroll.right - divBarH.clientWidth - (mask !== 'none' ? 12 : 2) - shiftRight) {
X = rectScroll.right - divBarH.clientWidth - (mask !== 'none' ? 12 : 2) - shiftRight
}
divBarH.style.left = X - rectScroll.x + 'px'
const leftBar = X - rectScroll.x - shiftLeft - 2
const widthScroll =
rectScroll.width - 2 - (mask !== 'none' ? 12 : 2) - divBarH.clientWidth - shiftLeft - shiftRight
const procBar = leftBar / widthScroll
divScroll.scrollLeft = (divScroll.scrollWidth - divScroll.clientWidth) * procBar
}
}
const onScrollEnd = (): void => {
document.removeEventListener('mousemove', onScroll)
document.body.style.userSelect = 'auto'
document.body.style.webkitUserSelect = 'auto'
document.removeEventListener('mouseup', onScrollEnd)
isScrolling = false
}
const onScrollStart = (event: MouseEvent, direction: 'vertical' | 'horizontal'): void => {
if (divScroll == null) return
scrolling = false
const el: HTMLElement = event.currentTarget as HTMLElement
if (el && divScroll) {
dXY =
direction === 'vertical'
? event.clientY - el.getBoundingClientRect().y
: event.clientX - el.getBoundingClientRect().x
document.addEventListener('mouseup', onScrollEnd)
document.addEventListener('mousemove', onScroll)
// document.body.style.pointerEvents = 'none'
document.body.style.userSelect = 'none'
document.body.style.webkitUserSelect = 'none'
isScrolling = direction
}
dXY = direction === 'vertical' ? event.offsetY : event.offsetX
document.addEventListener('mouseup', onScrollEnd)
document.addEventListener('mousemove', onScroll)
document.body.style.userSelect = 'none'
document.body.style.webkitUserSelect = 'none'
isScrolling = direction
}

const renderFade = () => {
Expand Down Expand Up @@ -456,37 +451,43 @@
ev: MouseEvent & { currentTarget: EventTarget & HTMLDivElement },
horizontal: boolean = false
) => {
if (!isScrolling && divBar && divScroll) {
const rectScroll = divScroll.getBoundingClientRect()
if (horizontal) {
const x = ev.offsetX
const trackWidth = ev.currentTarget.clientWidth
const barWidth = divBarH.clientWidth
const leftBar =
x - barWidth / 2 <= 0
? rectScroll.left + shiftLeft + 2
: x + barWidth / 2 >= trackWidth
? rectScroll.right - barWidth - shiftRight - (mask !== 'none' ? 12 : 2)
: ev.clientX - barWidth / 2
divBarH.style.left = `${leftBar}px`
const widthScroll = rectScroll.width - 2 - (mask !== 'none' ? 12 : 2) - barWidth - shiftLeft - shiftRight
const procBar = (leftBar - rectScroll.left - shiftLeft - 2) / widthScroll
divScroll.scrollLeft = (divScroll.scrollWidth - divScroll.clientWidth) * procBar
} else {
const y = ev.offsetY
const trackHeight = ev.currentTarget.clientHeight
const barHeight = divBar.clientHeight
const topBar =
y - barHeight / 2 <= 0
? rectScroll.top + shiftTop + 2
: y + barHeight / 2 >= trackHeight
? rectScroll.bottom - barHeight - shiftBottom - 2
: ev.clientY - barHeight / 2
divBar.style.top = `${topBar}px`
const heightScroll = rectScroll.height - 4 - barHeight - shiftTop - shiftBottom
const procBar = (topBar - rectScroll.top - shiftTop - 2) / heightScroll
divScroll.scrollTop = (divScroll.scrollHeight - divScroll.clientHeight) * procBar
}
if (
(divBar == null && !horizontal) ||
(divBarH == null && horizontal) ||
divScroll == null ||
isScrolling !== false
) {
return
}
const rectScroll = divScroll.getBoundingClientRect()
if (horizontal) {
const x = ev.offsetX
const trackWidth = ev.currentTarget.clientWidth
const barWidth = divBarH.clientWidth
const leftBar =
x - barWidth / 2 <= 0
? rectScroll.left + shiftLeft + 2
: x + barWidth / 2 >= trackWidth
? rectScroll.right - barWidth - shiftRight - (mask !== 'none' ? 12 : 2)
: ev.clientX - barWidth / 2
divBarH.style.left = `${leftBar}px`
const widthScroll = rectScroll.width - 2 - (mask !== 'none' ? 12 : 2) - barWidth - shiftLeft - shiftRight
const procBar = (leftBar - rectScroll.left - shiftLeft - 2) / widthScroll
divScroll.scrollLeft = (divScroll.scrollWidth - divScroll.clientWidth) * procBar
} else {
const y = ev.offsetY
const trackHeight = ev.currentTarget.clientHeight
const barHeight = divBar.clientHeight
const topBar =
y - barHeight / 2 <= 0
? rectScroll.top + shiftTop + 2
: y + barHeight / 2 >= trackHeight
? rectScroll.bottom - barHeight - shiftBottom - 2
: ev.clientY - barHeight / 2
divBar.style.top = `${topBar}px`
const heightScroll = rectScroll.height - 4 - barHeight - shiftTop - shiftBottom
const procBar = (topBar - rectScroll.top - shiftTop - 2) / heightScroll
divScroll.scrollTop = (divScroll.scrollHeight - divScroll.clientHeight) * procBar
}
}

Expand Down