|
| 1 | +--- |
| 2 | +chapter: 26 |
| 3 | +pageNumber: 255 |
| 4 | +description: Utilisation de Framer Motion pour les animations dans les applications React. |
| 5 | +--- |
| 6 | + |
| 7 | +## Animation à l’aide de Framer Motion |
| 8 | + |
| 9 | +Framer Motion est une bibliothèque de mouvements prête pour la production pour React. Il facilite la création d’animations complexes. |
| 10 | + |
| 11 | +**Installation** |
| 12 | + |
| 13 | +Vous pouvez inclure Framer Motion dans votre projet à l’aide de npm: |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install framer-motion |
| 17 | +``` |
| 18 | + |
| 19 | +**Animation de base** |
| 20 | + |
| 21 | +Voici un exemple simple d’utilisation de Framer Motion pour animer un composant: |
| 22 | + |
| 23 | +```javascript |
| 24 | +import React from 'react'; |
| 25 | +import { motion } from 'framer-motion'; |
| 26 | + |
| 27 | +const AnimatedComponent = () => { |
| 28 | + return ( |
| 29 | + <motion.div animate={{ x: 100 }} transition={{ duration: 1 }}> |
| 30 | + Je vais me déplacer vers la droite |
| 31 | + </motion.div> |
| 32 | + ); |
| 33 | +}; |
| 34 | + |
| 35 | +export default AnimatedComponent; |
| 36 | +``` |
| 37 | + |
| 38 | +**Animation avancée** |
| 39 | + |
| 40 | +Framer Motion fournit diverses fonctionnalités pour les animations avancées, telles que les images clés, les mouvements et les animations de mise en page. |
| 41 | + |
| 42 | + |
| 43 | +- **Images clés:** |
| 44 | +Les images clés vous permettent de définir plusieurs étapes d’une animation. Voici un exemple : |
| 45 | + |
| 46 | +```javascript |
| 47 | +import React from 'react'; |
| 48 | +import { motion } from 'framer-motion'; |
| 49 | + |
| 50 | +const KeyframeComponent = () => { |
| 51 | + return ( |
| 52 | + <motion.div |
| 53 | + animate={{ x: [0, 100, 0] }} |
| 54 | + transition={{ duration: 2, ease: 'easeInOut' }} |
| 55 | + > |
| 56 | + Je vais faire des allers-retours |
| 57 | + </motion.div> |
| 58 | + ); |
| 59 | +}; |
| 60 | + |
| 61 | +export default KeyframeComponent; |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +- **Gestes** |
| 66 | +Framer Motion vous permet de créer des animations basées sur les gestes de l’utilisateur. Voici un exemple: |
| 67 | + |
| 68 | +```javascript |
| 69 | +import React from 'react'; |
| 70 | +import { motion } from 'framer-motion'; |
| 71 | + |
| 72 | +const GestureComponent = () => { |
| 73 | + return ( |
| 74 | + <motion.div |
| 75 | + drag |
| 76 | + dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }} |
| 77 | + > |
| 78 | + Traîne-moi partout |
| 79 | + </motion.div> |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +export default GestureComponent; |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | +- **Animations de mise en page** |
| 88 | +Framer Motion facilite l’animation des modifications de mise en page. Voici un exemple: |
| 89 | + |
| 90 | +```javascript |
| 91 | +import React, { useState } from 'react'; |
| 92 | +import { motion } from 'framer-motion'; |
| 93 | + |
| 94 | +const LayoutAnimationComponent = () => { |
| 95 | + const [isOpen, setIsOpen] = useState(false); |
| 96 | + |
| 97 | + return ( |
| 98 | + <motion.div layout onClick={() => setIsOpen(!isOpen)} style={{ background: 'lightblue', padding: '10px' }}> |
| 99 | + {isOpen ? 'Click to collapse' : 'Click to expand'} |
| 100 | + </motion.div> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +export default LayoutAnimationComponent; |
| 105 | +``` |
| 106 | + |
| 107 | +{% hint style="info" %} |
| 108 | +Pour plus de détails et d’exemples, consultez la documentation de Framer Motion. |
| 109 | +{% endhint %} |
0 commit comments