forked from motiondivision/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
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
ed7ea87
commit d07f81a
Showing
22 changed files
with
404 additions
and
204 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,62 @@ | ||
import { createContext, useContext } from "react" | ||
import { createContext, useContext, useMemo } from "react" | ||
import { VisualElement } from "../../render/types" | ||
import { | ||
checkIfControllingVariants, | ||
isVariantLabel, | ||
} from "../../render/utils/variants" | ||
import { MotionProps } from "../types" | ||
|
||
export const MotionContext = createContext<VisualElement | undefined>(undefined) | ||
export interface MotionContextProps { | ||
visualElement?: VisualElement | ||
initial?: false | string | string[] | ||
animate?: string | string[] | ||
} | ||
|
||
export const MotionContext = createContext<MotionContextProps>({}) | ||
|
||
export function useVisualElementContext() { | ||
return useContext(MotionContext) | ||
return useContext(MotionContext).visualElement | ||
} | ||
|
||
export function getCurrentTreeVariants( | ||
props: MotionProps, | ||
context: MotionContextProps | ||
): MotionContextProps { | ||
if (checkIfControllingVariants(props)) { | ||
const { initial, animate } = props | ||
return { | ||
initial: | ||
initial === false || isVariantLabel(initial) | ||
? (initial as any) | ||
: undefined, | ||
animate: isVariantLabel(animate) ? animate : undefined, | ||
} | ||
} | ||
return props.inherit !== false ? context : {} | ||
} | ||
|
||
function variantLabelsAsDependency( | ||
prop: undefined | string | string[] | boolean | ||
) { | ||
return Array.isArray(prop) ? prop.join(" ") : prop | ||
} | ||
|
||
export function useCreateMotionContext( | ||
props: MotionProps, | ||
isStatic: boolean | ||
): MotionContextProps { | ||
const { initial, animate } = getCurrentTreeVariants( | ||
props, | ||
useContext(MotionContext) | ||
) | ||
|
||
return useMemo( | ||
() => ({ initial, animate }), | ||
isStatic | ||
? [ | ||
variantLabelsAsDependency(initial), | ||
variantLabelsAsDependency(animate), | ||
] | ||
: [] | ||
) | ||
} |
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,75 @@ | ||
import { animationControls } from "../../../animation/animation-controls" | ||
import { getCurrentTreeVariants } from "../MotionContext" | ||
|
||
describe("getCurrentTreeVariants", () => { | ||
test("It returns the correct variant to render currently", () => { | ||
expect( | ||
getCurrentTreeVariants( | ||
{ | ||
initial: { opacity: 0 }, | ||
animate: { opacity: 1 }, | ||
}, | ||
{ | ||
initial: "a", | ||
animate: "b", | ||
} | ||
) | ||
).toEqual({ initial: "a", animate: "b" }) | ||
|
||
expect( | ||
getCurrentTreeVariants( | ||
{ | ||
initial: { opacity: 0 }, | ||
animate: { opacity: 1 }, | ||
inherit: false, | ||
}, | ||
{ | ||
initial: "a", | ||
animate: "b", | ||
} | ||
) | ||
).toEqual({ initial: undefined, animate: undefined }) | ||
|
||
expect( | ||
getCurrentTreeVariants( | ||
{ | ||
initial: false, | ||
animate: ["a", "b"], | ||
}, | ||
{ | ||
initial: "c", | ||
animate: "b", | ||
} | ||
) | ||
).toEqual({ initial: false, animate: ["a", "b"] }) | ||
|
||
expect( | ||
getCurrentTreeVariants( | ||
{ | ||
initial: ["c", "d"], | ||
animate: ["a", "b"], | ||
}, | ||
{ | ||
initial: ["e", "f"], | ||
animate: ["g", "h"], | ||
} | ||
) | ||
).toEqual({ | ||
initial: ["c", "d"], | ||
animate: ["a", "b"], | ||
}) | ||
|
||
expect( | ||
getCurrentTreeVariants( | ||
{ | ||
initial: false, | ||
animate: animationControls(), | ||
}, | ||
{ | ||
initial: "a", | ||
animate: "b", | ||
} | ||
) | ||
).toEqual({ initial: false, animate: undefined }) | ||
}) | ||
}) |
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
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,83 @@ | ||
import { useContext, useMemo } from "react" | ||
import { isAnimationControls } from "../../animation/animation-controls" | ||
import { PresenceContext } from "../../components/AnimatePresence/PresenceContext" | ||
import { ResolvedValues } from "../../render/types" | ||
import { | ||
checkIfControllingVariants, | ||
resolveVariantFromProps, | ||
} from "../../render/utils/variants" | ||
import { isMotionValue } from "../../value/utils/is-motion-value" | ||
import { MotionContextProps } from "../context/MotionContext" | ||
import { MotionProps } from "../types" | ||
import { isForcedMotionValue } from "./is-forced-motion-value" | ||
|
||
/** | ||
* | ||
*/ | ||
export function useCreateVisualState( | ||
props: MotionProps, | ||
context: MotionContextProps, | ||
isStatic: boolean | ||
): ResolvedValues { | ||
const createVisualState = () => { | ||
const values: ResolvedValues = {} | ||
const presenceContext = useContext(PresenceContext) | ||
const blockInitialAnimation = presenceContext?.initial === false | ||
|
||
/** | ||
* TODO: Make this renderer specific using the scrapMotionProps | ||
* | ||
* const motionValues = scrapeMotionValues() | ||
* | ||
*/ | ||
const { style } = props | ||
for (const key in style) { | ||
if (isMotionValue(style[key])) { | ||
values[key] = style[key].get() | ||
} else if (isForcedMotionValue(key, props)) { | ||
values[key] = style[key] | ||
} | ||
} | ||
|
||
let { initial, animate } = props | ||
const isControllingVariants = checkIfControllingVariants(props) | ||
const isVariantNode = isControllingVariants || props.variants | ||
|
||
if ( | ||
context && | ||
isVariantNode && | ||
!isControllingVariants && | ||
props.inherit !== false | ||
) { | ||
initial ??= context.initial | ||
animate ??= context.animate | ||
} | ||
|
||
const variantToSet = | ||
blockInitialAnimation || initial === false ? animate : initial | ||
|
||
if ( | ||
variantToSet && | ||
typeof variantToSet !== "boolean" && | ||
!isAnimationControls(variantToSet) | ||
) { | ||
const list = Array.isArray(variantToSet) | ||
? variantToSet | ||
: [variantToSet] | ||
list.forEach((definition) => { | ||
const resolved = resolveVariantFromProps(props, definition) | ||
if (!resolved) return | ||
|
||
const { transitionEnd, transition, ...target } = resolved | ||
|
||
for (const key in target) values[key] = target[key] | ||
for (const key in transitionEnd) | ||
values[key] = transitionEnd[key] | ||
}) | ||
} | ||
|
||
return values | ||
} | ||
|
||
return isStatic ? createVisualState() : useMemo(createVisualState, []) | ||
} |
Oops, something went wrong.