-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Problem:** When rearranging grid elements, they should animate along the X/Y axii to indicate the completed movement. Moreover, we don't have a way to animate rendered canvas elements. **Fix:** This PR introduces a way to animate canvas elements programmatically, without having to inject anything inside the rendered components themselves. After doing that, the PR uses the new logic to animate the grid rearrange as a first test subject. The goal is to be able to arbitrarily animate canvas elements using the same framer motion API we use elsewhere, explicitly. In order to avoid manipulating the rendered elements, the targeting capabilities of `useAnimate` are used here. 1. Create the animation scope and animation function with `useAnimate` 2. Connect the scope with the `DesignPanelRoot` component 3. Store the animation function in a new `AnimationContext` context 4. `useCanvasAnimation` can now be used to target specific canvas elements and animate them, selecting them from the DOM by their `data-uid` prop 5. 🎈 https://github.com/concrete-utopia/utopia/assets/1081051/ab70b016-a91b-47f8-95ec-bec00bdca838
- Loading branch information
Showing
7 changed files
with
187 additions
and
35 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
66 changes: 66 additions & 0 deletions
66
editor/src/components/canvas/ui-jsx-canvas-renderer/animation-context.ts
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,66 @@ | ||
import type { | ||
ElementOrSelector, | ||
DOMKeyframesDefinition, | ||
DynamicAnimationOptions, | ||
AnimationPlaybackControls, | ||
AnimationScope, | ||
} from 'framer-motion' | ||
import React, { useContext } from 'react' | ||
import type { ElementPath } from 'utopia-shared/src/types' | ||
import { MetadataUtils } from '../../../core/model/element-metadata-utils' | ||
import { getUtopiaID } from '../../../core/shared/uid-utils' | ||
import { Substores, useEditorState } from '../../editor/store/store-hook' | ||
import { mapDropNulls } from '../../../core/shared/array-utils' | ||
|
||
export type AnimationCtx = { | ||
scope: AnimationScope | null | ||
animate: | ||
| (( | ||
value: ElementOrSelector, | ||
keyframes: DOMKeyframesDefinition, | ||
options?: DynamicAnimationOptions | undefined, | ||
) => AnimationPlaybackControls) | ||
| null | ||
} | ||
|
||
export const AnimationContext = React.createContext<AnimationCtx>({ | ||
scope: null, | ||
animate: null, | ||
}) | ||
|
||
export function useCanvasAnimation(paths: ElementPath[]) { | ||
const ctx = useContext(AnimationContext) | ||
|
||
const uids = useEditorState( | ||
Substores.metadata, | ||
(store) => { | ||
return mapDropNulls((path) => { | ||
const element = MetadataUtils.findElementByElementPath(store.editor.jsxMetadata, path) | ||
if (element == null) { | ||
return null | ||
} | ||
return getUtopiaID(element) | ||
}, paths) | ||
}, | ||
'useCanvasAnimation uids', | ||
) | ||
|
||
const selector = React.useMemo(() => { | ||
return uids.map((uid) => `[data-uid='${uid}']`).join(',') | ||
}, [uids]) | ||
|
||
const elements = React.useMemo( | ||
() => (selector === '' ? [] : document.querySelectorAll(selector)), | ||
[selector], | ||
) | ||
|
||
return React.useCallback( | ||
(keyframes: DOMKeyframesDefinition, options?: DynamicAnimationOptions) => { | ||
if (ctx.animate == null || elements.length === 0) { | ||
return | ||
} | ||
void ctx.animate(elements, keyframes, options) | ||
}, | ||
[ctx, elements], | ||
) | ||
} |
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