Skip to content

Commit

Permalink
feat: add functions scrollLeft and scrollRight
Browse files Browse the repository at this point in the history
  • Loading branch information
valmisson committed Mar 3, 2024
1 parent 4f0f9ea commit 79451ab
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ Scroll to the top of the page

Scroll to the end of the page

#### `scrollLeft(options?)`

Scroll to the end of left the page

#### `scrollRight(options?)`

Scroll to the end of right the page

#### `scrollElement(reference, options?)`

Scroll to element with smooth animation.
Expand Down
63 changes: 61 additions & 2 deletions src/functions/scrolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,61 @@ export const scrollBottom = async (
onDone && onDone({ offset, duration, endPosition })
}

/**
* Scroll to the end of left the page
*
* @param options - An optional param with global options
*/

export const scrollLeft = async (
options?: Partial<GlobalOptions>
): Promise<void> => {
const opts = Object.assign(globalOptions, options)
const endPosition = { x: 0, y: 0 }

const { duration, offset, onStart, onDone } = opts

onStart && onStart({ offset, duration, endPosition })

await scrolling(endPosition, opts)

onDone && onDone({ offset, duration, endPosition })
}

/**
* Scroll to the end of right the page
*
* @param options - An optional param with global options
*/

export const scrollRight = async (
options?: Partial<GlobalOptions>
): Promise<void> => {
const opts = Object.assign(globalOptions, options)

const { duration, offset, onStart, onDone } = opts

const body = document.body
const html = document.documentElement

const endPosition = {
x: Math.max(
body.scrollWidth,
body.offsetWidth,
html.scrollWidth,
html.clientWidth,
html.offsetWidth
),
y: 0
}

onStart && onStart({ offset, duration, endPosition })

await scrolling(endPosition, opts)

onDone && onDone({ offset, duration, endPosition })
}

/**
* Scroll to element
*
Expand Down Expand Up @@ -106,13 +161,17 @@ export const scrollElement = async (
*/

export const scrollPosition = async (
position: Coord,
position: Coord | number,
options?: Partial<GlobalOptions>
): Promise<void> => {
if (!position || typeof position !== 'number') {
if (!position) {
throw new Error('scrollPosition require a position value valid')
}

if (typeof position === 'number') {
position = { x: 0, y: position }
}

const opts = Object.assign(globalOptions, options)
const endPosition = position
const { duration, offset, onStart, onDone } = opts
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as scrollTo } from './actions/ScrollTo'
export { default as scrollRef } from './actions/ScrollRef'
export { default as setGlobalOptions } from './internal/globalOptions'
export { scrollTop, scrollBottom, scrollElement, scrollPosition } from './functions/scrolling'
export { scrollTop, scrollBottom, scrollLeft, scrollRight, scrollElement, scrollPosition } from './functions/scrolling'

0 comments on commit 79451ab

Please sign in to comment.