Skip to content

Commit

Permalink
Merge pull request #831 from framer/feature/framer-tree-animations
Browse files Browse the repository at this point in the history
Enable framer magic motion transitions
  • Loading branch information
mergetron[bot] committed Oct 28, 2020
2 parents 2b82149 + 8b26eaa commit dc571a6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions api/framer-motion.api.md
Expand Up @@ -260,6 +260,11 @@ export interface FeatureProps extends MotionProps {
// @public (undocumented)
export type ForwardRefComponent<T, P> = ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>;

// Warning: (ae-internal-missing-underscore) The name "FramerTreeContext" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
export const FramerTreeContext: import("react").Context<SyncLayoutBatcher | SharedLayoutSyncMethods>;

// @public (undocumented)
export type GestureHandlers = PanHandlers & TapHandlers & HoverHandlers;

Expand Down
7 changes: 7 additions & 0 deletions src/components/AnimateSharedLayout/SharedLayoutContext.ts
Expand Up @@ -97,3 +97,10 @@ export function isSharedLayout(
export const SharedLayoutContext = createContext<
SyncLayoutBatcher | SharedLayoutSyncMethods
>(createBatcher())

/**
* @internal
*/
export const FramerTreeLayoutContext = createContext<
SyncLayoutBatcher | SharedLayoutSyncMethods
>(createBatcher())
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -139,6 +139,7 @@ export {
export {
SharedLayoutSyncMethods,
SharedLayoutContext,
FramerTreeLayoutContext,
SyncLayoutLifecycles,
createBatcher,
} from "./components/AnimateSharedLayout/SharedLayoutContext"
Expand Down
15 changes: 13 additions & 2 deletions src/motion/features/layout/Measure.tsx
Expand Up @@ -6,10 +6,12 @@ import {
SharedLayoutSyncMethods,
isSharedLayout,
SharedLayoutContext,
FramerTreeLayoutContext,
} from "../../../components/AnimateSharedLayout/SharedLayoutContext"

interface SyncProps extends FeatureProps {
syncLayout: SharedLayoutSyncMethods | SyncLayoutBatcher
framerSyncLayout: SharedLayoutSyncMethods | SyncLayoutBatcher
}

/**
Expand All @@ -20,8 +22,10 @@ class Measure extends React.Component<SyncProps> {
* If this is a child of a SyncContext, register the VisualElement with it on mount.
*/
componentDidMount() {
const { syncLayout, visualElement } = this.props
const { syncLayout, framerSyncLayout, visualElement } = this.props
isSharedLayout(syncLayout) && syncLayout.register(visualElement)
isSharedLayout(framerSyncLayout) &&
framerSyncLayout.register(visualElement)
}

/**
Expand Down Expand Up @@ -62,7 +66,14 @@ class Measure extends React.Component<SyncProps> {

function MeasureContextProvider(props: FeatureProps) {
const syncLayout = useContext(SharedLayoutContext)
return <Measure {...props} syncLayout={syncLayout} />
const framerSyncLayout = useContext(FramerTreeLayoutContext)
return (
<Measure
{...props}
syncLayout={syncLayout}
framerSyncLayout={framerSyncLayout}
/>
)
}

export const MeasureLayout: MotionFeature = {
Expand Down
6 changes: 6 additions & 0 deletions src/motion/features/layout/use-snapshot-on-unmount.ts
Expand Up @@ -2,18 +2,24 @@ import { VisualElement } from "../../../render/VisualElement"
import { useContext } from "react"
import {
SharedLayoutContext,
FramerTreeLayoutContext,
isSharedLayout,
} from "../../../components/AnimateSharedLayout/SharedLayoutContext"
import { useIsomorphicLayoutEffect } from "../../../utils/use-isomorphic-effect"

export function useSnapshotOnUnmount(visualElement: VisualElement) {
const syncLayout = useContext(SharedLayoutContext)
const framerSyncLayout = useContext(FramerTreeLayoutContext)

useIsomorphicLayoutEffect(
() => () => {
if (isSharedLayout(syncLayout)) {
syncLayout.remove(visualElement as any)
}

if (isSharedLayout(framerSyncLayout)) {
framerSyncLayout.remove(visualElement as any)
}
},
[]
)
Expand Down

0 comments on commit dc571a6

Please sign in to comment.