Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

reactjs - Is it possible to use Framer Motion in React with pug

I am using multiple technologies for my advanced React web page development I previously used AOS for animating my react components simply, also tried GreenSock once but it didn't fit my needs... I am using Next js and pug js in Next js through https://github.com/pugjs/babel-plugin-transform-react-pug

And want to use Framer motion for the same but motion.div changes to <motion className="div"> Could anyone please help me

question from:https://stackoverflow.com/questions/65879863/is-it-possible-to-use-framer-motion-in-react-with-pug

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Next.js Application, go to .babelrc fil at the root(if not create one and add

{
  "plugins": [
    ["transform-jsx-classname-components", {
      "objects": ["motion"]
    }]
  ]
}

That's good, your final .babelrc should look as

{
  "presets": ["next/babel"],
  "plugins": [
    "transform-react-pug",
    [
      "transform-jsx-classname-components",
      {
        "objects": ["motion"]
      }
    ],
    "transform-react-jsx"
  ]
}

This is good to go for React's framer motion using pug And also please note : There is no syntax highlighting for the same as I tried over the time

.. In case of create-react-app please make sure to add the .babelrc contents in package.json after ejecting (npm eject), You will have a "babel" field in package.json, just add these plugins property inside that field..

Still this isn't the exact solution,

Now you will have to use motion.Div wherever you want animations instead of motion.div (i.e, uppercase)

Anyways this means that you cannot animate elements directy but you can animate elements, you will always need to use motion.Div or motion.Span...

Hope this Helps ??

Edit: You could also add into import => as

Example import {motion as Motion } from 'framer-motion';

But you still have to use Motion.Div in pug


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...