@graphcommerce/framer-utils 6.0.0 → 6.0.1-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 6.0.1-canary.1
4
+
5
+ ## 6.0.1-canary.0
6
+
7
+ ### Patch Changes
8
+
9
+ - [#1854](https://github.com/graphcommerce-org/graphcommerce/pull/1854) [`9cdce5786`](https://github.com/graphcommerce-org/graphcommerce/commit/9cdce578693782733d7bb3f4b7bf364e1cf26124) - Simplify useElementScroll component to a better standard to debug stuff ([@paales](https://github.com/paales))
10
+
3
11
  ## 6.0.0
4
12
 
5
13
  ### Patch Changes
@@ -1,68 +1,48 @@
1
- import { motionValue, MotionValue, useTransform } from 'framer-motion'
2
- import { equal } from '@wry/equality'
1
+ import { MotionValue, useMotionValue, useTransform } from 'framer-motion'
3
2
  import sync from 'framesync'
4
- import { RefObject, useMemo } from 'react'
3
+ import { RefObject } from 'react'
4
+ import { useConstant } from './useConstant'
5
5
  import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
6
6
 
7
- type ScrollMotionValue = { animating: boolean; x: number; y: number; xMax: number; yMax: number }
8
-
9
7
  export interface ScrollMotionValues {
8
+ animating: MotionValue<boolean>
10
9
  x: MotionValue<number>
11
10
  y: MotionValue<number>
12
11
  xProgress: MotionValue<number>
13
12
  yProgress: MotionValue<number>
14
13
  xMax: MotionValue<number>
15
14
  yMax: MotionValue<number>
16
- scroll: MotionValue<ScrollMotionValue>
17
- }
18
-
19
- const refScrollMap = new Map<
20
- RefObject<HTMLElement | undefined> | string,
21
- MotionValue<ScrollMotionValue>
22
- >()
23
-
24
- const initval = () => motionValue({ animating: false, x: 0, y: 0, xMax: 0, yMax: 0 })
25
-
26
- const getScrollMotion = (ref?: RefObject<HTMLElement | undefined>, sharedKey?: string) => {
27
- if (!ref) return initval()
28
-
29
- const key = sharedKey || ref
30
-
31
- if (!refScrollMap.has(key)) {
32
- const scroll = initval()
33
- refScrollMap.set(key, scroll)
34
- }
35
-
36
- return refScrollMap.get(key) as MotionValue<ScrollMotionValue>
37
15
  }
38
16
 
39
17
  export function useElementScroll(ref?: RefObject<HTMLElement | undefined>): ScrollMotionValues {
40
- const scroll = getScrollMotion(ref)
41
-
42
- const x = useTransform(scroll, (v) => v.x)
43
- const y = useTransform(scroll, (v) => v.y)
44
- const xProgress = useTransform(scroll, (v) => (!v.x && !v.xMax ? 0 : v.x / v.xMax))
45
- const yProgress = useTransform(scroll, (v) => (!v.y && !v.yMax ? 0 : v.y / v.yMax))
46
- const xMax = useTransform(scroll, (v) => v.xMax)
47
- const yMax = useTransform(scroll, (v) => v.yMax)
18
+ const animating = useMotionValue(false)
19
+ const x = useMotionValue(0)
20
+ const y = useMotionValue(0)
21
+ const xMax = useMotionValue(0)
22
+ const yMax = useMotionValue(0)
23
+ const xProgress = useTransform([x, xMax], (v: number[]) => (!v[0] && !v[1] ? 0 : v[0] / v[1]))
24
+ const yProgress = useTransform([y, yMax], (v: number[]) => (!v[0] && !v[1] ? 0 : v[0] / v[1]))
25
+
26
+ const scroll = useConstant<ScrollMotionValues>(() => ({
27
+ animating,
28
+ x,
29
+ y,
30
+ xProgress,
31
+ yProgress,
32
+ xMax,
33
+ yMax,
34
+ }))
48
35
 
49
36
  useIsomorphicLayoutEffect(() => {
50
37
  const element = ref?.current
51
38
  if (!element) return () => {}
52
39
 
53
40
  const updater = () => {
54
- const scrollValue: ScrollMotionValue = {
55
- ...scroll.get(),
56
- xMax: Math.max(0, element.scrollWidth - element.offsetWidth),
57
- yMax: Math.max(0, element.scrollHeight - element.offsetHeight),
58
- }
59
-
60
- if (!scroll.get().animating) {
61
- scrollValue.x = element.scrollLeft
62
- scrollValue.y = element.scrollTop
63
- }
41
+ xMax.set(Math.max(0, element.scrollWidth - element.offsetWidth))
42
+ yMax.set(Math.max(0, element.scrollHeight - element.offsetHeight))
64
43
 
65
- if (!equal(scrollValue, scroll.get())) scroll.set(scrollValue)
44
+ if (!animating.get()) y.set(element.scrollTop)
45
+ if (!animating.get()) x.set(element.scrollLeft)
66
46
  }
67
47
  updater()
68
48
 
@@ -76,10 +56,7 @@ export function useElementScroll(ref?: RefObject<HTMLElement | undefined>): Scro
76
56
  element.removeEventListener('scroll', updaterTimed)
77
57
  ro.disconnect()
78
58
  }
79
- }, [ref, scroll])
59
+ }, [animating, ref, x, xMax, y, yMax])
80
60
 
81
- return useMemo(
82
- () => ({ x, y, xProgress, yProgress, xMax, yMax, scroll }),
83
- [x, xMax, xProgress, y, yMax, yProgress, scroll],
84
- )
61
+ return scroll
85
62
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/framer-utils",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "6.0.0",
5
+ "version": "6.0.1-canary.1",
6
6
  "sideEffects": false,
7
7
  "scripts": {
8
8
  "dev": "tsc -W"
@@ -15,9 +15,9 @@
15
15
  }
16
16
  },
17
17
  "devDependencies": {
18
- "@graphcommerce/eslint-config-pwa": "6.0.0",
19
- "@graphcommerce/prettier-config-pwa": "6.0.0",
20
- "@graphcommerce/typescript-config-pwa": "6.0.0"
18
+ "@graphcommerce/eslint-config-pwa": "6.0.1-canary.1",
19
+ "@graphcommerce/prettier-config-pwa": "6.0.1-canary.1",
20
+ "@graphcommerce/typescript-config-pwa": "6.0.1-canary.1"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "framer-motion": "^10.0.0",