Skip to content

Commit

Permalink
Adding useComputed
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry committed Aug 21, 2023
1 parent 411be5b commit 24f4ae4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/framer-motion/src/animation/animators/Animation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class Animation {
startTime: number | null = null

holdTime: number | null = null

currentTime: number | null = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class AsyncAnimation extends Animation {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Animation } from "./Animation"

export class SyncAnimation extends Animation {}
13 changes: 13 additions & 0 deletions packages/framer-motion/src/value/use-computed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { MotionValue } from "."
import { useCombineMotionValues } from "./use-combine-values"

let collectMotionValues: MotionValue[] | undefined = undefined

export function useComputed<O>(compute: () => O): MotionValue<O> {
collectMotionValues = []
compute()
const value = useCombineMotionValues(collectMotionValues, compute)
collectMotionValues = undefined

return value
}
13 changes: 9 additions & 4 deletions packages/framer-motion/src/value/use-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,26 @@ export function useTransform<I, O>(
| MotionValue<string | number>[],
transformer: MultiTransformer<I, O>
): MotionValue<O>

export function useTransform<I, O>(transformer: () => O): MotionValue<O>
export function useTransform<I, O>(
input:
| MotionValue<I>
| MotionValue<string>[]
| MotionValue<number>[]
| MotionValue<string | number>[],
inputRangeOrTransformer: InputRange | Transformer<I, O>,
| MotionValue<string | number>[]
| (() => O),
inputRangeOrTransformer?: InputRange | Transformer<I, O>,
outputRange?: O[],
options?: TransformOptions<O>
): MotionValue<O> {
if (typeof input === "function") {
return useComputed(input)
}

const transformer =
typeof inputRangeOrTransformer === "function"
? inputRangeOrTransformer
: transform(inputRangeOrTransformer, outputRange!, options)
: transform(inputRangeOrTransformer!, outputRange!, options)

return Array.isArray(input)
? useListTransform(
Expand Down

0 comments on commit 24f4ae4

Please sign in to comment.