Skip to content

Commit

Permalink
Updating approach
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry committed Sep 24, 2020
1 parent 65fa9f4 commit b4319c7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 67 deletions.
51 changes: 0 additions & 51 deletions src/index-legacy.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Components
*/
export { motion } from "./render/dom"
export { motion, createDomMotionComponent } from "./render/dom"
export { m } from "./render/dom/minimal-component"
export { AnimatePresence } from "./components/AnimatePresence"
export { AnimateSharedLayout } from "./components/AnimateSharedLayout"
Expand Down
25 changes: 25 additions & 0 deletions src/motion/__tests__/create-component.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { render } from "../../../jest.setup"
import { createDomMotionComponent } from "../../render/dom"
import * as React from "react"
import { motionValue } from "../../value"

const motion = { div: createDomMotionComponent("div") }
describe("Legacy component", () => {
test("Animates", async () => {
const promise = new Promise((resolve) => {
const x = motionValue(0)
const onComplete = () => resolve(x.get())
const Component = () => (
<motion.div
animate={{ x: 20 }}
style={{ x }}
onAnimationComplete={onComplete}
/>
)
const { rerender } = render(<Component />)
rerender(<Component />)
})

return expect(promise).resolves.toBe(20)
})
})
55 changes: 40 additions & 15 deletions src/render/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ export type CustomDomComponent<Props> = React.ForwardRefExoticComponent<
React.RefAttributes<SVGElement | HTMLElement>
>

const allMotionFeatures = [
MeasureLayout,
Animation,
Drag,
Gestures,
Exit,
AnimateLayout,
]

const domBaseConfig = {
useVisualElement: useDomVisualElement as any,
render: render as any,
animationControlsConfig: {
makeTargetAnimatable: parseDomVariant,
},
}

/**
* Convert any React component into a `motion` component. The provided component
* **must** use `React.forwardRef` to the underlying DOM component you want to animate.
Expand All @@ -37,20 +54,15 @@ export type CustomDomComponent<Props> = React.ForwardRefExoticComponent<
*
* @public
*/

export function createMotionProxy(defaultFeatures: MotionFeature[]) {
type CustomMotionComponent = { custom: typeof custom }
type Motion = HTMLMotionComponents &
SVGMotionComponents &
CustomMotionComponent

const config: MotionComponentConfig<HTMLElement | SVGElement> = {
...domBaseConfig,
defaultFeatures,
useVisualElement: useDomVisualElement as any,
render: render as any,
animationControlsConfig: {
makeTargetAnimatable: parseDomVariant,
},
}

function custom<Props>(
Expand All @@ -73,19 +85,32 @@ export function createMotionProxy(defaultFeatures: MotionFeature[]) {
return new Proxy({ custom }, { get }) as Motion
}

export const allMotionFeatures = [
MeasureLayout,
Animation,
Drag,
Gestures,
Exit,
AnimateLayout,
]

/**
* HTML & SVG components, optimised for use with gestures and animation. These can be used as
* drop-in replacements for any HTML & SVG component, all CSS & SVG properties are supported.
*
* @public
*/
export const motion = /*@__PURE__*/ createMotionProxy(allMotionFeatures)

/**
* Create a DOM `motion` component with the provided string. This is primarily intended
* as a full alternative to `motion` for consumers who have to support environments that don't
* support `Proxy`.
*
* ```javascript
* import { createDomMotionComponent } from "framer-motion"
*
* const motion = {
* div: createDomMotionComponent('div')
* }
* ```
*
* @public
*/
export function createDomMotionComponent(key: string) {
return createMotionComponent(key, {
...domBaseConfig,
defaultFeatures: allMotionFeatures,
})
}

0 comments on commit b4319c7

Please sign in to comment.