Skip to content

πŸŽ₯ Make videos programmatically with React

chore: address PR review β€” shared easing assert, docs anchor, comments

JonnyBurgerf7b310a5a911
diff --git a/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx b/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx
index 8ea0c37047..af9bbe42a7 100644
--- a/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx
+++ b/packages/animation-utils/src/transformation-helpers/interpolate-styles/index.tsx
@@ -1,4 +1,5 @@
import {
+ assertValidInterpolateEasingOption,
interpolate,
interpolateColors,
type EasingFunction,
@@ -19,32 +20,6 @@ type InterpolateStylesResolvedOptions = {
extrapolateRight: ExtrapolateType;
};
-function assertInterpolateStylesEasingOption(
- easing: EasingFunction | readonly EasingFunction[] | undefined,
- inputRangeLength: number,
-) {
- if (easing === undefined) {
- return;
- }
-
- if (typeof easing === 'function') {
- return;
- }
-
- const expectedLength = inputRangeLength - 1;
- if (easing.length !== expectedLength) {
- throw new Error(
- `When easing is an array, it must have one entry per segment between keyframes (length inputRange.length - 1 = ${expectedLength}), but got length ${easing.length}`,
- );
- }
-
- for (let i = 0; i < easing.length; i++) {
- if (typeof easing[i] !== 'function') {
- throw new Error(`easing[${i}] must be a function`);
- }
- }
-}
-
const interpolatedPropertyPart = ({
inputValue,
inputRange,
@@ -342,7 +317,7 @@ export const interpolateStyles = (
const initialStyle = outputStylesRange[startIndex];
const finalStyle = outputStylesRange[endIndex];
- assertInterpolateStylesEasingOption(options?.easing, inputRange.length);
+ assertValidInterpolateEasingOption(options?.easing, inputRange.length);
const easingOption = options?.easing;
const segmentEasing: EasingFunction =
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index ce45d452af..f17194b608 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -178,6 +178,7 @@ export {interpolateColors} from './interpolate-colors.js';
export {LogLevel} from './log.js';
export {Loop} from './loop/index.js';
export {
+ assertValidInterpolateEasingOption,
EasingFunction,
ExtrapolateType,
interpolate,
diff --git a/packages/core/src/interpolate.ts b/packages/core/src/interpolate.ts
index c193855db5..f091cfc12b 100644
--- a/packages/core/src/interpolate.ts
+++ b/packages/core/src/interpolate.ts
@@ -120,7 +120,7 @@ function checkInfiniteRange(name: string, arr: readonly number[]) {
}
}
-function checkValidEasingOption(
+export function assertValidInterpolateEasingOption(
easing: EasingFunction | readonly EasingFunction[] | undefined,
inputRangeLength: number,
) {
@@ -183,7 +183,7 @@ export function interpolate(
checkValidInputRange(inputRange);
- checkValidEasingOption(options?.easing, inputRange.length);
+ assertValidInterpolateEasingOption(options?.easing, inputRange.length);
const easingOption = options?.easing;
const defaultEasing = (num: number): number => num;
@@ -196,6 +196,7 @@ export function interpolate(
return easingOption;
}
+ // `segmentIndex` is in [0, inputRange.length - 2]; array length was validated above.
return easingOption[segmentIndex] as EasingFunction;
};
diff --git a/packages/core/src/no-react.ts b/packages/core/src/no-react.ts
index 7279812a45..0b69453879 100644
--- a/packages/core/src/no-react.ts
+++ b/packages/core/src/no-react.ts
@@ -6,6 +6,7 @@ export type {
} from './CompositionManager';
export {DownloadBehavior} from './download-behavior';
export {
+ assertValidInterpolateEasingOption,
EasingFunction,
ExtrapolateType,
interpolate,
diff --git a/packages/docs/docs/animation-utils/interpolate-styles.mdx b/packages/docs/docs/animation-utils/interpolate-styles.mdx
index e0e75e2ae1..47c7423878 100644
--- a/packages/docs/docs/animation-utils/interpolate-styles.mdx
+++ b/packages/docs/docs/animation-utils/interpolate-styles.mdx
@@ -41,7 +41,7 @@ A function that takes 3-4 arguments:
1. The input value.
2. The range of values that you expect the input to assume.
3. The range of output styles that you want the input to map to.
-4. Options object with the same keys as [`interpolate()`](/docs/interpolate#easing) (`extrapolateLeft`, `extrapolateRight`, `easing`). Optional.
+4. Options object with the same keys as [`interpolate()`](/docs/interpolate#easing) (`extrapolateLeft`, `extrapolateRight`, `easing`), including a [per-segment `easing` array](/docs/interpolate#per-segment-easing-array) when you have more than two keyframes. Optional.
## Return value
diff --git a/packages/docs/docs/interpolate.mdx b/packages/docs/docs/interpolate.mdx
index 99d76fd131..318f575a45 100644
--- a/packages/docs/docs/interpolate.mdx
+++ b/packages/docs/docs/interpolate.mdx
@@ -172,7 +172,7 @@ interpolate(frame, [0, 10, 40, 100], [0, 0.2, 0.6, 1], {
});
```
-#### Per-segment easing (array)<AvailableFrom v="4.0.462" />
+#### Per-segment easing (array) {#per-segment-easing-array}<AvailableFrom v="4.0.462" />
You can pass an array of easing functions with **one entry per segment** between consecutive keyframes. Its length must be `inputRange.length - 1` (the same as `outputRange.length - 1`). The first easing applies between the first and second keyframe, the second easing between the second and third, and so on.