Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll callback "direction" output suggestion #48

Closed
devenini opened this issue Nov 3, 2022 · 3 comments
Closed

Scroll callback "direction" output suggestion #48

devenini opened this issue Nov 3, 2022 · 3 comments

Comments

@devenini
Copy link

devenini commented Nov 3, 2022

Hi!

After using the scroll callback, I've been wondering if the returned direction parameter should output the direction of the user's scroll input instead of the global instance direction.

Current behavior

import Lenis from '@studio-freight/lenis'

const lenis = new Lenis({
  	direction: 'vertical', 
  	smooth: true,
})

lenis.on('scroll', ({ direction }) => {
  	console.log({direction}); // In that case, it'll always return `vertical` (lenis.direction)
})

Suggested behavior

import Lenis from '@studio-freight/lenis'

const lenis = new Lenis({
  	direction: 'vertical', 
	smooth: true,
})

lenis.on('scroll', ({ direction }) => {
  console.log({direction}); // returns `1` or `-1`. Based on user scroll event
})

Would return 1 or -1 depending if the user is scrolling up and down (vertical) or left and right (horizontal).

I think this solution would make more sense since all other properties of that event are related to a scroll action, not the global instance.

Workaround

I've found a workaround to reproduce the suggested behavior. But I would like to know your thoughts on having this behavior built-in the library!

import Lenis from '@studio-freight/lenis'

let scrollDirection = 1
let lastProgress = 0

const lenis = new Lenis({
	direction: 'vertical', 
 	smooth: true,
})

lenis.on('scroll', ({ progress }) => {
	//get direction
	if (progress > lastProgress) {
		if (scrollDirection != 1) {
			scrollDirection = 1;
		}
	} else {
		if (scrollDirection != -1) {
			scrollDirection = -1;
		}
	}
})
@arzafran
Copy link
Contributor

arzafran commented Nov 4, 2022

hey! I've made a PR to add this functionality already, we're discussing if direction is semantic enough. would love to know your thoughts too. will land soon!

@arzafran
Copy link
Contributor

arzafran commented Nov 7, 2022

landed in v0.2.20

@arzafran arzafran closed this as completed Nov 7, 2022
@joshmeads
Copy link

Good improvement for sure. My workaround was simply checking if velocity was < or > 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants