Skip to content

Commit

Permalink
feat: support scrollcontrols prepend
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed May 30, 2024
1 parent 9099cdf commit 8e5a82a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ type ScrollControlsProps = {
* then a maxSpeed of e.g. 0.1 which will clamp the speed to 0.1 units per second, it may now
* take much longer than damping to reach the target if it is far away. Default: Infinity */
maxSpeed?: number
/** If true attaches the scroll container before the canvas */
prepend?: boolean
enabled?: boolean
style?: React.CSSProperties
children: React.ReactNode
Expand Down
7 changes: 6 additions & 1 deletion src/web/ScrollControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export type ScrollControlsProps = {
* then a maxSpeed of e.g. 3 which will clamp the speed to 3 units per second, it may now
* take much longer than damping to reach the target if it is far away. Default: Infinity */
maxSpeed?: number
/** If true attaches the scroll container before the canvas */
prepend?: boolean
enabled?: boolean
style?: React.CSSProperties
children: React.ReactNode
Expand Down Expand Up @@ -59,6 +61,7 @@ export function ScrollControls({
distance = 1,
damping = 0.25,
maxSpeed = Infinity,
prepend = false,
style = {},
children,
}: ScrollControlsProps) {
Expand Down Expand Up @@ -126,7 +129,9 @@ export function ScrollControls({
fill.style.width = horizontal ? `${pages * distance * 100}%` : '100%'
fill.style.pointerEvents = 'none'
el.appendChild(fill)
target.appendChild(el)

if (prepend) target.prepend(el)
else target.appendChild(el)

// Init scroll one pixel in to allow upward/leftward scroll
el[horizontal ? 'scrollLeft' : 'scrollTop'] = 1
Expand Down

0 comments on commit 8e5a82a

Please sign in to comment.