-
Notifications
You must be signed in to change notification settings - Fork 23
fix(Carousel): Use event handler instead of useEffect for scroll #683
Conversation
useEffect was causing problems on React 18, so this change reworks the scrolling mechanism to explicitly call `scrollTo` on every single manual event (clicks, keyboard) as well as when the `currentIndex` prop changes. It also sets `isManuallyUpdating` to true. After that we listen to the 'scroll' event and reset `isManuallyUpdating` to false after the very last scroll event. The intersection observer is used for syncing the active dot with scroll events that are triggered natively (e.g. dragging on mobile). We check for `isManuallyUpdating` here to avoid messing with manual `scrollTo` calls.
scrollTimeout.current = window.setTimeout(() => { | ||
isManuallyUpdating.current = false; | ||
}, 100); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only part that I'm not sure about. I had to do this because there is no scrollend
event. To make things worse, React doesn't support passive
scroll events, which is the recommended approach.
But at least the delay doesn't cause any usability problems (in my testing).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldnt IntersectionObserver
help here? You could know, when element is fully visible (so assume scoll has ended)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how would it work? we already use intersection observers and this code is directly interacting with it
scrollTimeout.current = window.setTimeout(() => { | ||
isManuallyUpdating.current = false; | ||
}, 100); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldnt IntersectionObserver
help here? You could know, when element is fully visible (so assume scoll has ended)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works,
I dont like timeout, but we can revisit it later, if issues appear.
…in/iTwinUI-react#683) useEffect was causing problems on React 18, so this change reworks the scrolling mechanism to explicitly call scrollTo on every single manual event (clicks, keyboard) as well as when the currentIndex prop changes. It also sets isManuallyUpdating to true. And then we listen to the 'scroll' event and reset isManuallyUpdating to false after the very last scroll event. The intersection observer is used for syncing the active dot with scroll events that are triggered natively (e.g. dragging on mobile). We check for isManuallyUpdating here to avoid messing with manual scrollTo calls.
useEffect was causing problems on React 18 (#656 (review)), so this change reworks the scrolling mechanism to explicitly call
scrollTo
on every single manual event (clicks, keyboard) as well as when thecurrentIndex
prop changes. It also setsisManuallyUpdating
to true.After that we listen to the 'scroll' event and reset
isManuallyUpdating
to false after the very last scroll event.The intersection observer is used for syncing the active dot with scroll events that are triggered natively (e.g. dragging on mobile). We check for
isManuallyUpdating
here to avoid messing with manualscrollTo
calls.