Download react-spring/packages/shared/src/dom-events/scroll/ScrollHandler.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-spring/packages/shared/src/dom-events/scroll/ScrollHandler.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-spring/packages/shared/src/dom-events/scroll/ScrollHandler.ts
-
curl -L -o ScrollHandler.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-spring/packages/shared/src/dom-events/scroll/ScrollHandler.ts
1.68 kB
| import { progress } from '../../progress' | |
| import type { OnScrollCallback } from './index' | |
| const SCROLL_KEYS = { | |
| x: { | |
| length: 'Width', | |
| position: 'Left', | |
| }, | |
| y: { | |
| length: 'Height', | |
| position: 'Top', | |
| }, | |
| } as const | |
| /** | |
| * Whilst user's may not need the scrollLength, it's easier to return | |
| * the whole state we're storing and let them pick what they want. | |
| */ | |
| export interface ScrollAxis { | |
| current: number | |
| progress: number | |
| scrollLength: number | |
| } | |
| export interface ScrollInfo { | |
| time: number | |
| x: ScrollAxis | |
| y: ScrollAxis | |
| } | |
| /** | |
| * Why use a class? More extensible in the future. | |
| */ | |
| export class ScrollHandler { | |
| protected callback: OnScrollCallback | |
| protected container: HTMLElement | |
| protected info: ScrollInfo | |
| constructor(callback: OnScrollCallback, container: HTMLElement) { | |
| this.callback = callback | |
| this.container = container | |
| this.info = { | |
| time: 0, | |
| x: this.createAxis(), | |
| y: this.createAxis(), | |
| } | |
| } | |
| private createAxis = (): ScrollAxis => ({ | |
| current: 0, | |
| progress: 0, | |
| scrollLength: 0, | |
| }) | |
| private updateAxis = (axisName: keyof Pick<ScrollInfo, 'x' | 'y'>) => { | |
| const axis = this.info[axisName] | |
| const { length, position } = SCROLL_KEYS[axisName] | |
| axis.current = this.container[`scroll${position}`] | |
| axis.scrollLength = | |
| this.container[`scroll${length}`] - this.container[`client${length}`] | |
| axis.progress = progress(0, axis.scrollLength, axis.current) | |
| } | |
| private update = () => { | |
| this.updateAxis('x') | |
| this.updateAxis('y') | |
| } | |
| private sendEvent = () => { | |
| this.callback(this.info) | |
| } | |
| advance = () => { | |
| this.update() | |
| this.sendEvent() | |
| } | |
| } | |