r/reactjs • u/maxs_dev • Apr 27 '20
Show /r/reactjs Declarative Magnet Animation w/ Framer Motion
3
u/MindOnTheBlink Apr 27 '20
This is impressive. Great job!
As a dev who is currently learning Framer X, I have been wanting give Framer Motion a whirl. How easy it is to adopt if you already have a pretty extensive React background? What were some of your pain points with it?
11
u/maxs_dev Apr 27 '20
I'm actually a Product Designer and started learning to code with OG Framer (it was using coffeescript). Learning React alongside their animation library felt really natural since it's literally just a prop to get things to animate. Some advice is digging into their variants, which can be really powerful as you can orchestrate complex animations a lot easier. Also, if you're using Styled Components you can use Framer Motion alongside like "styled(motion.div)".
5
u/sickcodebruh420 Apr 27 '20
I absolutely fucking love Framer Motion. I use it in React and am currently building my second massive feature that relies on it for a number of heavily orchestrated animations. Works wonderfully with Styled Components, too.
The closest thing to a pain point was and still is thinking about how to build components with animation states in mind, but that’s not unique to Framer Motion. I find that I have to structure some components a bit differently to ensure that the transitions can run in the right order, or I’ll have to position things absolutely when I’d normally just slap
flexon a div and let them settle into place. The library itself makes things so easy to express, I’ve never felt limited by it. The fact that it’s written in TypeScript is an added bonus.
7
2
2
u/Maguspk Apr 27 '20 edited Apr 28 '20
awesome demo, also my first time seeing variable!
feedback: it’d be awesome if you used a drag handler instead of mouse events, that way you could drag it around on a phone (fun!)
edit: and just to drive home how awesome framer motion is, I was able to make a 2 line edit to get this to behave correctly for touch events (added to the magnet div)
onPan={handleMouseMove}
drag1
0
-4
30
u/maxs_dev Apr 27 '20 edited Apr 27 '20
Here's the code and demo, happy to answer any questions!
https://variable.app/p/0088wbsgZacahMVEORaY