UNPKG

4.79 kBTypeScriptView Raw
1import { MotionValue } from "../value";
2import { TransformOptions } from "../utils/transform";
3export declare type InputRange = number[];
4declare type SingleTransformer<I, O> = (input: I) => O;
5declare type MultiTransformer<I, O> = (input: I[]) => O;
6/**
7 * Create a `MotionValue` that transforms the output of another `MotionValue` by mapping it from one range of values into another.
8 *
9 * @remarks
10 *
11 * Given an input range of `[-200, -100, 100, 200]` and an output range of
12 * `[0, 1, 1, 0]`, the returned `MotionValue` will:
13 *
14 * - When provided a value between `-200` and `-100`, will return a value between `0` and `1`.
15 * - When provided a value between `-100` and `100`, will return `1`.
16 * - When provided a value between `100` and `200`, will return a value between `1` and `0`
17 *
18 *
19 * The input range must be a linear series of numbers. The output range
20 * can be any value type supported by Framer Motion: numbers, colors, shadows, etc.
21 *
22 * Every value in the output range must be of the same type and in the same format.
23 *
24 * @library
25 *
26 * ```jsx
27 * export function MyComponent() {
28 * const x = useMotionValue(0)
29 * const xRange = [-200, -100, 100, 200]
30 * const opacityRange = [0, 1, 1, 0]
31 * const opacity = useTransform(x, xRange, opacityRange)
32 *
33 * return <Frame x={x} animate={{ x: 200 }} opacity={opacity} />
34 * }
35 * ```
36 *
37 * @motion
38 *
39 * ```jsx
40 * export const MyComponent = () => {
41 * const x = useMotionValue(0)
42 * const xRange = [-200, -100, 100, 200]
43 * const opacityRange = [0, 1, 1, 0]
44 * const opacity = useTransform(x, xRange, opacityRange)
45 *
46 * return (
47 * <motion.div
48 * animate={{ x: 200 }}
49 * style={{ opacity, x }}
50 * />
51 * )
52 * }
53 * ```
54 *
55 * @param inputValue - `MotionValue`
56 * @param inputRange - A linear series of numbers (either all increasing or decreasing)
57 * @param outputRange - A series of numbers, colors or strings. Must be the same length as `inputRange`.
58 * @param options -
59 *
60 * - clamp: boolean. Clamp values to within the given range. Defaults to `true`
61 * - ease: EasingFunction[]. Easing functions to use on the interpolations between each value in the input and output ranges. If provided as an array, the array must be one item shorter than the input and output ranges, as the easings apply to the transition between each.
62 *
63 * @returns `MotionValue`
64 *
65 * @public
66 */
67export declare function useTransform<I, O>(value: MotionValue<number>, inputRange: InputRange, outputRange: O[], options?: TransformOptions<O>): MotionValue<O>;
68/**
69 * Create a `MotionValue` that transforms the output of another `MotionValue` through a function.
70 * In this example, `y` will always be double `x`.
71 *
72 * @library
73 *
74 * ```jsx
75 * import * as React from "react"
76 * import { Frame, useMotionValue, useTransform } from "framer"
77 *
78 * export function MyComponent() {
79 * const x = useMotionValue(10)
80 * const y = useTransform(x, value => value * 2)
81 *
82 * return <Frame x={x} y={y} />
83 * }
84 * ```
85 *
86 * @motion
87 *
88 * ```jsx
89 * export const MyComponent = () => {
90 * const x = useMotionValue(10)
91 * const y = useTransform(x, value => value * 2)
92 *
93 * return <motion.div style={{ x, y }} />
94 * }
95 * ```
96 *
97 * @param input - A `MotionValue` that will pass its latest value through `transform` to update the returned `MotionValue`.
98 * @param transform - A function that accepts the latest value from `input` and returns a new value.
99 * @returns `MotionValue`
100 *
101 * @public
102 */
103export declare function useTransform<I, O>(input: MotionValue<I>, transformer: SingleTransformer<I, O>): MotionValue<O>;
104/**
105 * Pass an array of `MotionValue`s and a function to combine them. In this example, `z` will be the `x` multiplied by `y`.
106 *
107 * @library
108 *
109 * ```jsx
110 * import * as React from "react"
111 * import { Frame, useMotionValue, useTransform } from "framer"
112 *
113 * export function MyComponent() {
114 * const x = useMotionValue(0)
115 * const y = useMotionValue(0)
116 * const z = useTransform([x, y], [latestX, latestY] => latestX * latestY)
117 *
118 * return <Frame x={x} y={y} z={z} />
119 * }
120 * ```
121 *
122 * @motion
123 *
124 * ```jsx
125 * export const MyComponent = () => {
126 * const x = useMotionValue(0)
127 * const y = useMotionValue(0)
128 * const z = useTransform([x, y], [latestX, latestY] => latestX * latestY)
129 *
130 * return <motion.div style={{ x, y, z }} />
131 * }
132 * ```
133 *
134 * @param input - An array of `MotionValue`s that will pass their latest values through `transform` to update the returned `MotionValue`.
135 * @param transform - A function that accepts the latest values from `input` and returns a new value.
136 * @returns `MotionValue`
137 *
138 * @public
139 */
140export declare function useTransform<I, O>(input: MotionValue<string | number>[], transformer: MultiTransformer<I, O>): MotionValue<O>;
141export {};