Design an Animated Toggle Switch Button using framer-motion & React
Last Updated :
25 Jul, 2024
Animated Toggle Switch Button using framer-motion & React is a button that shows some animation/transitions and switches the states when clicked.
Prerequisites:
Approach:
To design an animated toggle switch button using framer motion in react we will be using the animated and motion components for showing transitions and useState hook to store, useEffect to render the components after state changes.
Steps to create React Application And Installing Module :
Step 1: Create a React application using the following command:
npx create-react-application demo
Step 2: After creating your project folder i.e. demo, move to it using the following command:
cd demo
Step 3: Install framer-motion from npm.
npm i framer-motion
Project Structure:

The updated list of dependencies after installing required modules:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"framer-motion": "^10.16.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: This example implemets animated toggle switch button using motion and animate components of framer-motion.
CSS
/* Filename - App.css */
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
/* for smooth transition*/
transition: 0.5s ease-in-out;
}
* {
box-sizing: border-box;
}
.toggleSwitch {
width: 170px;
height: 100px;
border-radius: 100px;
padding: 10px;
display: flex;
cursor: pointer;
z-index: 2;
}
/* CSS for switch when "on"*/
.toggleSwitch.on {
background-color: #1aad66;
justify-content: flex-end;
/* for smooth transition*/
transition: 0.5s ease-in-out;
}
/*CSS for switch when "off"*/
.toggleSwitch.off {
background-color: #dddddd;
justify-content: flex-start;
}
.toggleSwitch div {
width: 80px;
height: 80px;
border-radius: 100%;
}
img {
width: 80px;
height: 80px;
border-radius: 100%;
}
JavaScript
// Filename - App.js
import React from "react";
import { useState, useEffect } from "react";
import { motion } from "framer-motion";
import "./App.css";
const Switch = ({ isOn, ...rest }) => {
// initialize the customClassName according to the
// state of the "isOn" using ternary operator
const customClassName = `toggleSwitch ${
isOn ? "on" : "off"
}`;
// initialize the src according to the
// state of the "isOn" using ternary operator
const src = isOn
? "Toggle 1st Image link"
: "Toggle 2nd Image link";
return (
<motion.div
animate
className={customClassName}
{...rest}
>
<motion.div animate>
<img src={src} />
</motion.div>
</motion.div>
);
};
const App = () => {
// useState hook is used to manage the state of
// "isOn" that is used to change the className,
// background-color and img src accordingly
const [isOn, setIsOn] = useState(false);
useEffect(() => {
// background-color changes every time "isOn"
// changes using JavaScript DOM methods
document.body.style.backgroundColor = isOn
? "#1c1c1c"
: "#ffffff";
}, [isOn]);
return (
<Switch
isOn={isOn}
onClick={() => setIsOn(!isOn)}
/>
);
};
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
Similar Reads
Animated modal using react, framer-motion & styled-components In this article, we are going to learn how to create an animated model using react, framer-motion & styled-components.PrerequisitesJavaScript (ES6).HTMLCSSReactJS. React useStateSteps to Create React Application And Installing Module:Step 1: Now, you will start a new project using create-react-a
4 min read
Animated expanding card using framer-motion and ReactJS In this article, we are going to learn how to create an animated expanding card using react and framer.Prerequisites:Knowledge of JavaScript (ES6). JavaScript inbuilt methods we are going to make use are :Arrow function (ES6)Ternary operatorObjects in JavaScriptKnowledge of HTML/CSS.Basic knowledge
3 min read
Animated shared layout using framer-motion and React.js Animated shared layout using framer-motion and React.js involves items that toggle open and close show some animation/transitions and switch the states when clicked.Prerequisites:Node.js and NPMReact JSReact JS HooksApproach:To design an Animated shared layout using framer-motion and React.js we wil
3 min read
How to create a rolling die using React and framer-motion ? We can create a die using react with plain CSS and framer-motion library for animating, Framer Motion library helps to animate the UI elements.Prerequisites:JavaScriptHTMLCSSReactJSSteps to Create the React Application And Installing Module:Step 1: Create a React application using the following comm
3 min read
Animated sliding page gallery using framer-motion and React.js The following approach covers how to create an animated sliding page gallery using framer-motion and ReactJS.Prerequisites:Knowledge of JavaScript (ES6)Knowledge of HTML and CSS.Basic knowledge of ReactJS.Creating React Application And Installing Module:Step 1: Create a React application using the f
4 min read
How to create Tinder card swipe gesture using React and framer-motion ? In the world of modern dating, Tinder has become one of the most popular platforms for meeting new people. One of its defining features is the swipe gesture, where users can swipe left or right to indicate their interest or disinterest in a potential match. In this article, we'll explore how to crea
5 min read
Animated sliding image gallery using framer and ReactJS Animated sliding image gallery using Framer and React JS will have some image and show one by one by one with a sliding animation from right to left.Prerequisites:Node.js and NPM React JSReact JS HooksApproach:To design an animated sliding image gallery using Framer in React we will be using the Pag
2 min read
How to Create a Toggle Switch in React as a Reusable Component ? Creating a reusable toggle switch in React enhances the UI. This component can customized as required with the props and can be reused. It can be done using CSS or CSS frameworks like MUI and tailwindCSS.In this article, weâre going to create a Toggle Switch in React as a reusable component. Prerequ
3 min read
How to Ceate Swipe-able Button in Tailwind CSS and React? Creating swipe-enabled buttons in React applications can greatly improve user experience, especially on mobile devices, these buttons are typically used for actions like deleting, sharing, and liking items, in this article we'll show you how to create a swipe-enabled button using Tailwind CSS for st
3 min read
Create Buttons UI using React and Tailwind CSS Tailwind CSS is a utility-first CSS framework that allows developers to quickly build responsive and reusable components. We'll explore how to create buttons with different styles such as primary, secondary, and disabled states, and buttons of various sizes.PrerequisitesReact JavaScriptNodeJSTailwin
2 min read