Skip to content

Commit

Permalink
Allow setup of scroll handler in useEffect phase to ensure refs are…
Browse files Browse the repository at this point in the history
… populated
  • Loading branch information
adamseckel committed Oct 27, 2022
1 parent 1dabb2b commit e57bd31
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/framer-motion/src/value/use-scroll.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { scroll, ScrollOptions } from "@motionone/dom"
import { RefObject } from "react"
import { motionValue } from "."
import { useIsomorphicLayoutEffect } from "../three-entry"
import { useConstant } from "../utils/use-constant"
import { useEffect } from "react"
import { useIsomorphicLayoutEffect } from "../three-entry"

interface UseScrollOptions extends Omit<ScrollOptions, "container" | "target"> {
container?: RefObject<HTMLElement>
target?: RefObject<HTMLElement>
layoutEffect?: boolean
}

const createScrollMotionValues = () => ({
Expand All @@ -19,11 +21,16 @@ const createScrollMotionValues = () => ({
export function useScroll({
container,
target,
layoutEffect = true,
...options
}: UseScrollOptions = {}) {
const values = useConstant(createScrollMotionValues)

useIsomorphicLayoutEffect(() => {
const useLifecycleEffect = layoutEffect
? useIsomorphicLayoutEffect
: useEffect

useLifecycleEffect(() => {
return scroll(
({ x, y }) => {
values.scrollX.set(x.current)
Expand Down

0 comments on commit e57bd31

Please sign in to comment.