Skip to content

Commit 695cb39

Browse files
authored
Merge pull request #3755 from motiondivision/fix-issue-2777
fix: throw an actionable invariant for custom components with non-DOM refs
2 parents a6ed094 + 33a1820 commit 695cb39

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as React from "react"
2+
import { motion } from "../.."
3+
import { render } from "../../jest.setup"
4+
5+
/**
6+
* Regression test for #2777
7+
*
8+
* When `motion.create()` wraps a custom component whose ref doesn't resolve to
9+
* a DOM element (e.g. the inner component is a class component, so its ref is
10+
* the class instance rather than a styleable element), the render loop would
11+
* throw `Cannot convert undefined or null to object` from deep inside the
12+
* frame loop, breaking every animation on the page.
13+
*
14+
* Motion should instead throw an actionable invariant at mount.
15+
*/
16+
describe("motion.create() wrapping a custom component with a non-DOM ref", () => {
17+
let consoleError: jest.SpyInstance
18+
19+
beforeEach(() => {
20+
// React logs uncaught errors thrown from refs, which is noise here.
21+
consoleError = jest.spyOn(console, "error").mockImplementation(() => {})
22+
})
23+
24+
afterEach(() => consoleError.mockRestore())
25+
26+
test("throws an actionable invariant when the ref isn't an element", () => {
27+
class ClassButton extends React.Component<any> {
28+
render() {
29+
return <button>{this.props.children}</button>
30+
}
31+
}
32+
33+
// Forwards the ref to a class component, so the mounted instance is the
34+
// class instance (no `.style`), mirroring the NextUI Button repro.
35+
const AnimateButton = React.forwardRef<any, any>((props, ref) => (
36+
<ClassButton ref={ref} {...props} />
37+
))
38+
39+
const MotionButton = motion.create(AnimateButton)
40+
41+
expect(() =>
42+
render(<MotionButton initial={{ opacity: 0 }}>BUY</MotionButton>)
43+
).toThrowError(
44+
"motion.create() components must forward their ref to a HTML or SVG element. For more information and steps for solving, visit https://motion.dev/troubleshooting/custom-component-ref"
45+
)
46+
})
47+
48+
test("mounts without error when the ref resolves to an element", () => {
49+
const AnimateButton = React.forwardRef<HTMLButtonElement, any>(
50+
(props, ref) => <button ref={ref} {...props} />
51+
)
52+
53+
const MotionButton = motion.create(AnimateButton)
54+
55+
expect(() =>
56+
render(<MotionButton initial={{ opacity: 0 }}>BUY</MotionButton>)
57+
).not.toThrow()
58+
})
59+
})

‎packages/motion-dom/src/render/html/HTMLVisualElement.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Box } from "motion-utils"
1+
import { invariant, type Box } from "motion-utils"
22
import type { AnyResolvedKeyframe } from "../../animation/types"
33
import { isCSSVariableName } from "../../animation/utils/is-css-variable"
44
import type { MotionNodeOptions } from "../../node/types"
@@ -28,6 +28,21 @@ export class HTMLVisualElement extends DOMVisualElement<
2828
> {
2929
type = "html"
3030

31+
mount(instance: HTMLElement) {
32+
/**
33+
* If a custom component forwards its ref to something other than a
34+
* HTML/SVG element (a class instance, an imperative handle) there's
35+
* nothing for Motion to style, measure or attach gestures to. #2777
36+
*/
37+
invariant(
38+
Boolean(instance.style),
39+
"motion.create() components must forward their ref to a HTML or SVG element",
40+
"custom-component-ref"
41+
)
42+
43+
super.mount(instance)
44+
}
45+
3146
readValueFromInstance(
3247
instance: HTMLElement,
3348
key: string

0 commit comments

Comments
 (0)