-
-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4cc398a
commit 993d208
Showing
15 changed files
with
105 additions
and
184 deletions.
There are no files selected for viewing
Binary file removed
BIN
-21.1 KB
.yarn/cache/@motionone-animation-npm-10.10.1-d0255a2947-11c5361043.zip
Binary file not shown.
Binary file added
BIN
+17.6 KB
.yarn/cache/@motionone-animation-npm-10.12.0-1d89e3c156-1e7c230da2.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file renamed
BIN
+12.4 KB
...sing-npm-10.9.0-ba0b919b6e-b0c63b161e.zip → ...ing-npm-10.12.0-5be92ac408-81f3a31829.zip
Binary file not shown.
Binary file renamed
BIN
+31.6 KB
...tors-npm-10.9.0-c2d3b358e8-f1d8aa5064.zip → ...ors-npm-10.12.0-3107d354ec-9e55478e2c.zip
Binary file not shown.
Binary file renamed
BIN
+8.13 KB
...ypes-npm-10.9.0-0083d58cd6-aab2966c8a.zip → ...pes-npm-10.12.0-93c236a2da-91b53fa78f.zip
Binary file not shown.
Binary file renamed
BIN
+25.7 KB
...tils-npm-10.9.0-59a43b5270-b13d9eb6d6.zip → ...ils-npm-10.12.0-066c183e92-407e43e9bc.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 9 additions & 82 deletions
91
packages/framer-motion/src/value/scroll/use-element-scroll.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,11 @@ | ||
import { RefObject } from "react" | ||
import { useConstant } from "../../utils/use-constant" | ||
import { | ||
createScrollMotionValues, | ||
ScrollMotionValues, | ||
createScrollUpdater, | ||
} from "./utils" | ||
import { addDomEvent } from "../../events/use-dom-event" | ||
import { useIsomorphicLayoutEffect } from "../../utils/use-isomorphic-effect" | ||
import { invariant } from "hey-listen" | ||
|
||
const getElementScrollOffsets = (element: HTMLElement) => () => { | ||
return { | ||
xOffset: element.scrollLeft, | ||
yOffset: element.scrollTop, | ||
xMaxOffset: element.scrollWidth - element.offsetWidth, | ||
yMaxOffset: element.scrollHeight - element.offsetHeight, | ||
} | ||
} | ||
|
||
/** | ||
* Returns MotionValues that update when the provided element scrolls: | ||
* | ||
* - `scrollX` — Horizontal scroll distance in pixels. | ||
* - `scrollY` — Vertical scroll distance in pixels. | ||
* - `scrollXProgress` — Horizontal scroll progress between `0` and `1`. | ||
* - `scrollYProgress` — Vertical scroll progress between `0` and `1`. | ||
* | ||
* This element must be set to `overflow: scroll` on either or both axes to report scroll offset. | ||
* | ||
* ```jsx | ||
* export const MyComponent = () => { | ||
* const ref = useRef() | ||
* const { scrollYProgress } = useElementScroll(ref) | ||
* | ||
* return ( | ||
* <div ref={ref}> | ||
* <motion.div style={{ scaleX: scrollYProgress }} /> | ||
* </div> | ||
* ) | ||
* } | ||
* ``` | ||
* | ||
* @public | ||
*/ | ||
export function useElementScroll( | ||
ref: RefObject<HTMLElement> | ||
): ScrollMotionValues { | ||
const values = useConstant(createScrollMotionValues) | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
const element = ref.current | ||
|
||
invariant( | ||
!!element, | ||
"ref provided to useScroll must be passed into a HTML element." | ||
) | ||
if (!element) return | ||
|
||
const updateScrollValues = createScrollUpdater( | ||
values, | ||
getElementScrollOffsets(element) | ||
) | ||
|
||
const scrollListener = addDomEvent( | ||
element, | ||
"scroll", | ||
updateScrollValues | ||
) | ||
|
||
const resizeListener = addDomEvent( | ||
element, | ||
"resize", | ||
updateScrollValues | ||
) | ||
|
||
return () => { | ||
scrollListener && scrollListener() | ||
resizeListener && resizeListener() | ||
} | ||
}, []) | ||
|
||
return values | ||
import { warnOnce } from "../../utils/warn-once" | ||
import { useScroll } from "../use-scroll" | ||
|
||
export function useElementScroll(ref: RefObject<HTMLElement>) { | ||
warnOnce( | ||
false, | ||
"useElementScroll is deprecated. Convert to useScroll({ container: ref })." | ||
) | ||
return useScroll({ container: ref }) | ||
} |
70 changes: 5 additions & 65 deletions
70
packages/framer-motion/src/value/scroll/use-viewport-scroll.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,7 @@ | ||
import { | ||
createScrollMotionValues, | ||
createScrollUpdater, | ||
ScrollMotionValues, | ||
} from "./utils" | ||
import { addDomEvent } from "../../events/use-dom-event" | ||
import { useIsomorphicLayoutEffect } from "../../utils/use-isomorphic-effect" | ||
import { warnOnce } from "../../utils/warn-once" | ||
import { useScroll } from "../use-scroll" | ||
|
||
let viewportScrollValues: ScrollMotionValues | ||
|
||
function getViewportScrollOffsets() { | ||
return { | ||
xOffset: window.pageXOffset, | ||
yOffset: window.pageYOffset, | ||
xMaxOffset: document.body.clientWidth - window.innerWidth, | ||
yMaxOffset: document.body.clientHeight - window.innerHeight, | ||
} | ||
} | ||
|
||
let hasListeners = false | ||
|
||
function addEventListeners() { | ||
hasListeners = true | ||
|
||
const updateScrollValues = createScrollUpdater( | ||
viewportScrollValues, | ||
getViewportScrollOffsets | ||
) | ||
|
||
addDomEvent(window, "scroll", updateScrollValues) | ||
addDomEvent(window, "resize", updateScrollValues) | ||
} | ||
|
||
/** | ||
* Returns MotionValues that update when the viewport scrolls: | ||
* | ||
* - `scrollX` — Horizontal scroll distance in pixels. | ||
* - `scrollY` — Vertical scroll distance in pixels. | ||
* - `scrollXProgress` — Horizontal scroll progress between `0` and `1`. | ||
* - `scrollYProgress` — Vertical scroll progress between `0` and `1`. | ||
* | ||
* **Warning:** Setting `body` or `html` to `height: 100%` or similar will break the `Progress` | ||
* values as this breaks the browser's capability to accurately measure the page length. | ||
* | ||
* ```jsx | ||
* export const MyComponent = () => { | ||
* const { scrollYProgress } = useViewportScroll() | ||
* return <motion.div style={{ scaleX: scrollYProgress }} /> | ||
* } | ||
* ``` | ||
* | ||
* @public | ||
*/ | ||
export function useViewportScroll(): ScrollMotionValues { | ||
/** | ||
* Lazy-initialise the viewport scroll values | ||
*/ | ||
if (!viewportScrollValues) { | ||
viewportScrollValues = createScrollMotionValues() | ||
} | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
!hasListeners && addEventListeners() | ||
}, []) | ||
|
||
return viewportScrollValues | ||
export function useViewportScroll() { | ||
warnOnce(false, "useViewportScroll is deprecated. Convert to useScroll().") | ||
return useScroll() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { scroll, ScrollOptions } from "@motionone/dom" | ||
import { RefObject } from "react" | ||
import { motionValue } from "." | ||
import { useIsomorphicLayoutEffect } from "../three-entry" | ||
import { useConstant } from "../utils/use-constant" | ||
|
||
interface UseScrollOptions extends Omit<ScrollOptions, "container" | "target"> { | ||
container?: RefObject<HTMLElement> | ||
target?: RefObject<HTMLElement> | ||
} | ||
|
||
const createScrollMotionValues = () => ({ | ||
scrollX: motionValue(0), | ||
scrollY: motionValue(0), | ||
scrollXProgress: motionValue(0), | ||
scrollYProgress: motionValue(0), | ||
}) | ||
|
||
export function useScroll({ | ||
container, | ||
target, | ||
...options | ||
}: UseScrollOptions = {}) { | ||
const values = useConstant(createScrollMotionValues) | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
return scroll( | ||
({ x, y }) => { | ||
values.scrollX.set(x.current) | ||
values.scrollXProgress.set(x.progress) | ||
values.scrollY.set(y.current) | ||
values.scrollYProgress.set(y.progress) | ||
}, | ||
{ | ||
...options, | ||
container: container?.current || undefined, | ||
target: target?.current || undefined, | ||
} | ||
) | ||
}, []) | ||
|
||
return values | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters