Skip to content

Commit e31d2a9

Browse files
authored
fix(files): let a mouse wheel scroll CSV and XLSX previews horizontally (#6563)
* fix(files): let a mouse wheel scroll CSV and XLSX previews horizontally The zoomable previews (docx, pdf, pptx, image) bind `bindPreviewWheelZoom`, whose horizontal branch maps a trackpad's `deltaX` — and Shift+`deltaY` on a plain mouse — onto the container's `scrollLeft`. The tabular previews never bound anything, which did not matter while their table fitted the frame. Now that it is wider, a mouse whose wheel reports only `deltaY` can reach the overflow solely by dragging the scrollbar; hovering the table and scrolling does nothing sideways. Extract that horizontal branch into `bindPreviewHorizontalWheel`, sharing the delta logic with the zooming variant rather than duplicating it, and bind it in all three tabular preview containers through a `useHorizontalWheelScroll` ref callback. The new binder deliberately ignores ctrl/cmd+wheel so browser page zoom still works over a table, since these previews have no zoom of their own. * fix(files): keep the vertical component of a diagonal wheel pan Cancelling a wheel event is all-or-nothing, so applying only `scrollLeft` after `preventDefault` dropped a diagonal trackpad pan's `deltaY` entirely. Apply the vertical component too, except under Shift, which remaps `deltaY` onto the horizontal axis and so has none left to spend. * fix(files): normalize wheel deltas to pixels and import the hook absolutely A wheel delta is only in pixels when `deltaMode` says so — Firefox reports mouse wheels in lines — while scroll offsets always are, so a three-line notch moved the table three pixels. Convert line and page deltas before applying them.
1 parent 1dd85eb commit e31d2a9

6 files changed

Lines changed: 233 additions & 8 deletions

File tree

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/csv-table-preview.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { memo } from 'react'
44
import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace'
5+
import { useHorizontalWheelScroll } from '@/app/workspace/[workspaceId]/files/components/file-viewer/use-horizontal-wheel-scroll'
56
import { useWorkspaceCsvPreview } from '@/hooks/queries/workspace-file-table'
67
import { useCsvTruncationImport } from './csv-import'
78
import { DataTable } from './data-table'
@@ -19,6 +20,7 @@ export const CsvTablePreview = memo(function CsvTablePreview({
1920
file: WorkspaceFileRecord
2021
workspaceId: string
2122
}) {
23+
const scrollRef = useHorizontalWheelScroll()
2224
const version = Number(new Date(file.updatedAt)) || file.size
2325
const {
2426
data,
@@ -42,7 +44,7 @@ export const CsvTablePreview = memo(function CsvTablePreview({
4244
}
4345

4446
return (
45-
<div className='flex flex-1 flex-col overflow-auto p-6'>
47+
<div ref={scrollRef} className='flex flex-1 flex-col overflow-auto p-6'>
4648
<DataTable headers={data.headers} rows={data.rows} />
4749
</div>
4850
)

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { memo, useEffect, useMemo, useRef, useState } from 'react'
44
import '@sim/emcn/components/code/code.css'
55
import { CSV_PREVIEW_MAX_ROWS } from '@/lib/api/contracts/workspace-file-table'
66
import { getFileExtension } from '@/lib/uploads/utils/file-utils'
7+
import { useHorizontalWheelScroll } from '@/app/workspace/[workspaceId]/files/components/file-viewer/use-horizontal-wheel-scroll'
78
import { type CsvImportFileDescriptor, useCsvTruncationImport } from './csv-import'
89
import { DataTable } from './data-table'
910
import { MermaidDiagram } from './mermaid-diagram'
@@ -264,6 +265,7 @@ const CsvPreview = memo(function CsvPreview({
264265
file: CsvImportFileDescriptor
265266
readOnly?: boolean
266267
}) {
268+
const scrollRef = useHorizontalWheelScroll()
267269
const { headers, rows, truncated } = useMemo(() => parseCsv(content), [content])
268270
useCsvTruncationImport(workspaceId, file, truncated, readOnly)
269271

@@ -276,7 +278,7 @@ const CsvPreview = memo(function CsvPreview({
276278
}
277279

278280
return (
279-
<div className='min-h-0 flex-1 overflow-auto p-6'>
281+
<div ref={scrollRef} className='min-h-0 flex-1 overflow-auto p-6'>
280282
<DataTable headers={headers} rows={rows} />
281283
</div>
282284
)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*
4+
* A mouse whose wheel reports only `deltaY` has no native gesture for reaching a preview
5+
* table's horizontal overflow short of dragging the scrollbar, so the tabular previews bind
6+
* `bindPreviewHorizontalWheel`. It must move the container on a horizontal gesture, stay out
7+
* of the way otherwise, and — unlike the zooming variant — leave ctrl/cmd+wheel to the browser
8+
* so page zoom still works over a table.
9+
*/
10+
import { beforeEach, describe, expect, it } from 'vitest'
11+
import { bindPreviewHorizontalWheel } from '@/app/workspace/[workspaceId]/files/components/file-viewer/preview-wheel-zoom'
12+
13+
/** jsdom does no layout, so scrollWidth/clientWidth are stubbed to model an overflowing container. */
14+
function makeContainer({ scrollWidth = 2000, clientWidth = 1000 } = {}): HTMLElement {
15+
const el = document.createElement('div')
16+
Object.defineProperty(el, 'scrollWidth', { value: scrollWidth, configurable: true })
17+
Object.defineProperty(el, 'clientWidth', { value: clientWidth, configurable: true })
18+
el.scrollLeft = 0
19+
document.body.appendChild(el)
20+
return el
21+
}
22+
23+
function wheel(el: HTMLElement, init: WheelEventInit): WheelEvent {
24+
const event = new WheelEvent('wheel', { bubbles: true, cancelable: true, ...init })
25+
el.dispatchEvent(event)
26+
return event
27+
}
28+
29+
describe('bindPreviewHorizontalWheel', () => {
30+
let container: HTMLElement
31+
let unbind: () => void
32+
33+
beforeEach(() => {
34+
document.body.innerHTML = ''
35+
container = makeContainer()
36+
unbind = bindPreviewHorizontalWheel(container)
37+
})
38+
39+
it("scrolls by a trackpad's horizontal delta", () => {
40+
const event = wheel(container, { deltaX: 120, deltaY: 0 })
41+
42+
expect(container.scrollLeft).toBe(120)
43+
expect(event.defaultPrevented).toBe(true)
44+
})
45+
46+
it('maps shift+wheel to horizontal for a vertical-only mouse', () => {
47+
const event = wheel(container, { deltaX: 0, deltaY: 120, shiftKey: true })
48+
49+
expect(container.scrollLeft).toBe(120)
50+
expect(event.defaultPrevented).toBe(true)
51+
})
52+
53+
/**
54+
* Cancelling a wheel event is all-or-nothing, so a diagonal trackpad pan must have its
55+
* vertical movement re-applied by hand — otherwise `preventDefault` silently eats it.
56+
*/
57+
it('keeps the vertical movement of a diagonal pan', () => {
58+
Object.defineProperty(container, 'scrollHeight', { value: 5000, configurable: true })
59+
Object.defineProperty(container, 'clientHeight', { value: 500, configurable: true })
60+
61+
wheel(container, { deltaX: 40, deltaY: 90 })
62+
63+
expect(container.scrollLeft).toBe(40)
64+
expect(container.scrollTop).toBe(90)
65+
})
66+
67+
it('does not also spend a shift gesture vertically', () => {
68+
wheel(container, { deltaX: 0, deltaY: 120, shiftKey: true })
69+
70+
expect(container.scrollLeft).toBe(120)
71+
expect(container.scrollTop).toBe(0)
72+
})
73+
74+
/**
75+
* `deltaMode` is not always pixels — Firefox reports mouse wheels in lines — while scroll
76+
* offsets always are, so a three-line notch added raw would move the table three pixels.
77+
*/
78+
it('converts a line-mode delta to pixels', () => {
79+
wheel(container, { deltaX: 3, deltaY: 0, deltaMode: WheelEvent.DOM_DELTA_LINE })
80+
81+
expect(container.scrollLeft).toBe(48)
82+
})
83+
84+
it('converts a page-mode delta to the container width', () => {
85+
wheel(container, { deltaX: 1, deltaY: 0, deltaMode: WheelEvent.DOM_DELTA_PAGE })
86+
87+
expect(container.scrollLeft).toBe(1000)
88+
})
89+
90+
it('leaves a plain vertical wheel alone so the container still scrolls down', () => {
91+
const event = wheel(container, { deltaX: 0, deltaY: 120 })
92+
93+
expect(container.scrollLeft).toBe(0)
94+
expect(event.defaultPrevented).toBe(false)
95+
})
96+
97+
/** Zoom is the browser's here — the tabular previews have no zoom of their own. */
98+
it.each([
99+
['ctrl', { ctrlKey: true }],
100+
['cmd', { metaKey: true }],
101+
])('leaves %s+wheel to the browser', (_label, modifier) => {
102+
const event = wheel(container, { deltaX: 120, deltaY: 0, ...modifier })
103+
104+
expect(container.scrollLeft).toBe(0)
105+
expect(event.defaultPrevented).toBe(false)
106+
})
107+
108+
it('does nothing when the container has no horizontal overflow', () => {
109+
const fitted = makeContainer({ scrollWidth: 1000, clientWidth: 1000 })
110+
const unbindFitted = bindPreviewHorizontalWheel(fitted)
111+
112+
const event = wheel(fitted, { deltaX: 120, deltaY: 0 })
113+
114+
expect(fitted.scrollLeft).toBe(0)
115+
expect(event.defaultPrevented).toBe(false)
116+
unbindFitted()
117+
})
118+
119+
it('stops scrolling once unbound', () => {
120+
unbind()
121+
122+
wheel(container, { deltaX: 120, deltaY: 0 })
123+
124+
expect(container.scrollLeft).toBe(0)
125+
})
126+
127+
it('scrolls a child gesture, since the listener captures', () => {
128+
const cell = document.createElement('td')
129+
container.appendChild(cell)
130+
131+
wheel(cell, { deltaX: 80, deltaY: 0 })
132+
133+
expect(container.scrollLeft).toBe(80)
134+
})
135+
})

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-wheel-zoom.ts

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,73 @@ interface BindPreviewWheelZoomOptions {
88
onPan?: (event: WheelEvent) => void
99
}
1010

11+
/**
12+
* Horizontal component of a wheel gesture: a trackpad's own `deltaX`, or `deltaY`
13+
* while Shift is held — the only horizontal gesture available on a mouse whose
14+
* wheel reports `deltaY` alone.
15+
*/
16+
function horizontalDeltaOf(event: WheelEvent): number {
17+
return event.deltaX !== 0 ? event.deltaX : event.shiftKey ? event.deltaY : 0
18+
}
19+
20+
/**
21+
* Vertical component still owed to the container once the horizontal one is taken.
22+
* Shift *remaps* `deltaY` onto the horizontal axis, so that gesture has no vertical
23+
* component left to spend; an ordinary diagonal trackpad pan does.
24+
*/
25+
function verticalDeltaOf(event: WheelEvent): number {
26+
return event.deltaX !== 0 ? event.deltaY : 0
27+
}
28+
29+
/**
30+
* Rough pixel height of one wheel "line". A wheel delta is only in pixels when `deltaMode`
31+
* says so — Firefox reports mouse wheels in lines — while scroll offsets are always pixels,
32+
* so a three-line notch added raw would move the table three pixels.
33+
*/
34+
const WHEEL_LINE_HEIGHT_PX = 16
35+
36+
/** Convert a wheel delta to pixels. `pageSize` is the container extent along that axis. */
37+
function toPixels(delta: number, event: WheelEvent, pageSize: number): number {
38+
if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return delta * WHEEL_LINE_HEIGHT_PX
39+
if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) return delta * pageSize
40+
return delta
41+
}
42+
43+
/**
44+
* Scroll `container` for a wheel gesture carrying a horizontal component. No-op when the
45+
* gesture is purely vertical or the container has nothing to scroll sideways, leaving the
46+
* event to scroll natively.
47+
*
48+
* Cancelling a wheel event is all-or-nothing, so once `preventDefault` is called this owes
49+
* the container *both* axes — a diagonal pan that only had its `deltaX` applied would lose
50+
* its vertical movement entirely.
51+
*/
52+
function applyHorizontalWheel(container: HTMLElement, event: WheelEvent): void {
53+
const horizontalDelta = horizontalDeltaOf(event)
54+
if (horizontalDelta === 0 || container.scrollWidth <= container.clientWidth) return
55+
56+
event.preventDefault()
57+
container.scrollLeft += toPixels(horizontalDelta, event, container.clientWidth)
58+
container.scrollTop += toPixels(verticalDeltaOf(event), event, container.clientHeight)
59+
}
60+
61+
/**
62+
* Bind horizontal wheel gestures for a preview scroll container that has no zoom of its
63+
* own — the tabular CSV/XLSX previews, whose table is wider than its frame. A mouse whose
64+
* wheel reports only `deltaY` otherwise has no way to reach that overflow short of dragging
65+
* the scrollbar. Unlike {@link bindPreviewWheelZoom} this leaves `ctrl`/`cmd`+wheel alone,
66+
* so browser page zoom still works over a table.
67+
*/
68+
export function bindPreviewHorizontalWheel(container: HTMLElement): () => void {
69+
const onWheel = (event: WheelEvent) => {
70+
if (event.ctrlKey || event.metaKey) return
71+
applyHorizontalWheel(container, event)
72+
}
73+
74+
container.addEventListener('wheel', onWheel, { capture: true, passive: false })
75+
return () => container.removeEventListener('wheel', onWheel, { capture: true })
76+
}
77+
1178
/**
1279
* Bind browser pinch/ctrl-wheel zoom and horizontal wheel gestures for preview
1380
* scroll containers. Trackpad pinch fires `wheel` with `ctrlKey=true`; without
@@ -34,11 +101,7 @@ export function bindPreviewWheelZoom(
34101
return
35102
}
36103

37-
const horizontalDelta = event.deltaX !== 0 ? event.deltaX : event.shiftKey ? event.deltaY : 0
38-
if (horizontalDelta === 0 || container.scrollWidth <= container.clientWidth) return
39-
40-
event.preventDefault()
41-
container.scrollLeft += horizontalDelta
104+
applyHorizontalWheel(container, event)
42105
}
43106

44107
container.addEventListener('wheel', onWheel, { capture: true, passive: false })
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use client'
2+
3+
import { useCallback, useRef } from 'react'
4+
import { bindPreviewHorizontalWheel } from '@/app/workspace/[workspaceId]/files/components/file-viewer/preview-wheel-zoom'
5+
6+
/**
7+
* Ref callback that gives a preview scroll container horizontal wheel scrolling.
8+
*
9+
* The tabular previews render a table wider than its frame, and a mouse whose wheel
10+
* reports only `deltaY` has no native way to reach the overflow short of dragging the
11+
* scrollbar. Binding is done through a ref callback rather than an effect so the
12+
* listener attaches with the node and detaches when React passes `null`.
13+
*/
14+
export function useHorizontalWheelScroll() {
15+
const unbindRef = useRef<(() => void) | null>(null)
16+
17+
return useCallback((node: HTMLDivElement | null) => {
18+
unbindRef.current?.()
19+
unbindRef.current = node ? bindPreviewHorizontalWheel(node) : null
20+
}, [])
21+
}

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/xlsx-preview.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { toError } from '@sim/utils/errors'
77
import type { WorkBook } from 'xlsx'
88
import { assertOoxmlPreviewWithinLimits } from '@/lib/file-parsers/ooxml-preview-guard'
99
import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace'
10+
import { useHorizontalWheelScroll } from '@/app/workspace/[workspaceId]/files/components/file-viewer/use-horizontal-wheel-scroll'
1011
import { DataTable } from './data-table'
1112
import { PreviewError, PreviewLoadingFrame, resolvePreviewError } from './preview-shared'
1213
import { useDocPreviewBinary } from './use-doc-preview-binary'
@@ -29,6 +30,7 @@ export const XlsxPreview = memo(function XlsxPreview({
2930
file: WorkspaceFileRecord
3031
workspaceId: string
3132
}) {
33+
const scrollRef = useHorizontalWheelScroll()
3234
const preview = useDocPreviewBinary(workspaceId, file)
3335
const fileData = preview.data
3436

@@ -130,7 +132,7 @@ export const XlsxPreview = memo(function XlsxPreview({
130132
))}
131133
</div>
132134
</div>
133-
<div className='flex-1 overflow-auto p-6'>
135+
<div ref={scrollRef} className='flex-1 overflow-auto p-6'>
134136
<DataTable headers={currentSheet.headers} rows={currentSheet.rows} />
135137
{currentSheet.truncated && (
136138
<p className='mt-3 text-center text-[12px] text-[var(--text-muted)]'>

0 commit comments

Comments
 (0)