Skip to content

Commit

Permalink
feat: add flag passive as a global option (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
valmisson committed Oct 21, 2023
1 parent 68484cf commit 1f3032a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Scroll to a position on the page
| `duration` | `500` | Duration (in milliseconds) of the animation. |
| `offset` | `0` | Offset that should be applied when scrolling. |
| `easing` | `cubicInOut` | Easing function to be used when animating. Use any easing from [`svelte/easing`][svelte-easing] or a custom easing function. |
| `passive` | `true` | A boolean value that, if true, indicates that the function specified by listener will never call preventDefault(). |
| `onStart` | `noop` | A callback function that should be called when scrolling has started. Receives the element, offset, duration and endPosition as a parameter. |
| `onDone` | `noop` | A callback function that should be called when scrolling has started. Receives the element, offset, duration and endPosition as a parameter. |

Expand Down
8 changes: 4 additions & 4 deletions src/actions/ScrollTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const elementsList = get(elements)

// handle with scrolling
const handle = async (event: Event, options: ScrollToOptions): Promise<void> => {
event.preventDefault()
if (!options.passive) event.preventDefault()

const { ref, onDone, onStart } = options

Expand Down Expand Up @@ -70,10 +70,10 @@ const scrollTo = ( // eslint-disable-line @typescript-eslint/explicit-module-bou
node.style.cursor = 'pointer'
}

const _handler = (event) => handle(event, opts)
const _handler = (event: MouseEvent | TouchEvent) => handle(event, opts)

node.addEventListener('click', _handler)
node.addEventListener('touchstart', _handler)
node.addEventListener('click', _handler, { passive: opts.passive })
node.addEventListener('touchstart', _handler, { passive: opts.passive })

return {
destroy () {
Expand Down
1 change: 1 addition & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const elements = writable<Array<ElementReference>>([])
export const globalOptions = writable<GlobalOptions>({
offset: 0,
duration: 500,
passive: true,
easing: cubicInOut,
onStart: () => { },
onDone: () => { }
Expand Down
1 change: 1 addition & 0 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface HooksOptions {
export interface GlobalOptions {
offset: number
duration: number
passive?: boolean
easing: (t: number) => number
onStart?: (options: HooksOptions) => void
onDone?: (options: HooksOptions) => void
Expand Down

0 comments on commit 1f3032a

Please sign in to comment.