How to Ceate Swipe-able Button in Tailwind CSS and React?
Last Updated :
03 Oct, 2024
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 styling and React for functionality.
Approach
In this approach, we first create a React project and set up Tailwind CSS for styling. Framer Motion is used to detect swipe gestures, allowing us to add interactive animations for a swipe-enabled button. The button responds to a swipe gesture by moving and revealing additional actions like "Delete." We also handle the swipe state in the component using React's useState hook, ensuring the button's behavior and appearance change based on user interaction. This combination of tools makes it easy to create responsive, mobile-friendly UI elements.
Steps to Create & Configure the Project
Step 1: Create a New React Project
Firstly we have to run the given command to create a new React project.
npx create-react-app swipe-button-demo
cd swipe-button-demo
Step 2: Install Tailwind CSS
When we create a new project We will need to install Tailwind CSS and create a configuration file named tailwind.config.js, we can do this by running following command.
npm install tailwindcss postcss autoprefixer
npx tailwindcss init
Project Structure:

Updated Dependencies:
dependencies: {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"autoprefixer": "^10.4.20",
"framer-motion": "^11.9.0",
"postcss": "^8.4.47",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"tailwindcss": "^3.4.13",
"web-vitals": "^2.1.4"
}
Step 3: Configure Template Paths
Edit tailwind.config.js file to add content paths for your React files.
JavaScript
/*tailwind.config.js*/
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Step 4: Add Tailwind Directives
In src/index.css file, add base, components and utilities layers of Tailwind:
@tailwind base;
@tailwind components;
@tailwind utilities;
Step 5: Install Framer Motion
We will use Framer Motion for detecting swipe gestures and animating button.
npm install framer-motion
Step 6: Create the Swipe Button Component
Now create a new file SwipeButton.js in src directory and add following code.
JavaScript
//components/SwipeButton.js
import React, { useState } from 'react';
import { motion } from 'framer-motion';
const SwipeButton = () => {
const [swiped, setSwiped] = useState(false);
const handleSwipe = (event, info) => {
if (info.offset.x < -100) {
setSwiped(true);
}
};
return (
<div className="relative w-full
max-w-sm mx-auto mt-12">
<motion.div
className={`p-4 bg-green-600
text-white rounded-lg
shadow-lg ${swiped ? 'translate-x-[-100%]' : ''
}`}
drag="x"
dragConstraints={{ left: 0, right: 0 }}
onDragEnd={handleSwipe}
initial={{ x: 0 }}
animate={{ x: swiped ? -100 : 0 }}
>
Swipe Me
</motion.div>
{swiped && (
<div className="absolute top-0
right-0 p-4 bg-red-500
text-white rounded-lg">
<button onClick={() => setSwiped(false)}>Delete</button>
</div>
)}
</div>
);
};
export default SwipeButton;
Step 7: Add the SwipeButton Component to the App
Open src/App.js
and import SwipeButton
component.
JavaScript
//App.js
import React from 'react';
import SwipeButton from './components/SwipeButton';
import './index.css';
function App() {
return (
<div className="min-h-screen bg-gray-100 flex
flex-col items-center justify-center">
{/* Green GeeksforGeeks text */}
<h1 className="text-green-600
text-4xl font-bold
mb-8">
GeeksforGeeks
</h1>
{/* Swipeable Button */}
<SwipeButton />
</div>
);
}
export default App;
Step 8: Run the Application
Now you can start your development server.
npm start
Output: Open your browser and navigate to http://localhost:3000
.
Similar Reads
How to Skew a Button in Tailwind CSS?
Skewed buttons can add a dynamic and modern look to your website, Tailwind CSS provides utility classes to easily apply 2D transformations like tilt, rotation, and scaling, we'll explain how to tilt, buttons using Tailwind CSS and create a simple project to demonstrate this. Approach To Skew A Butto
2 min read
How to Create a Sidebar in NextJS & Tailwind CSS?
In web development, sidebars are commonly used to organize navigation elements and provide quick access to different sections of a website. A sidebar is a vertical menu or panel that is typically positioned on the left or right side of the main content area. Prerequisites:Next.jsTailwindJavaScriptSt
5 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 JavaScriptNodeJSTailwi
2 min read
How to Create Tailwind CSS Profile Card in React?
TailwindCSS can be integrated with React to create highly customizable and responsive profile cards by using utility-first classes for styling. This approach allows you to design visually appealing cards with gradients, rounded corners, and interactive animations, all while ensuring seamless respons
3 min read
How to Create Animated Loading Button using Tailwind CSS?
An Animated Loading Button is a button that displays a loading spinner or animation to indicate an ongoing process, such as form submission. Using Tailwind CSS, you can create this by combining utility classes for styling and animations to visually enhance the button during loading. Table of Content
2 min read
How to Create Floating Button in Tailwind CSS?
A floating button is a popular UI design feature used to quickly access actions like adding, saving, or navigating to website users. We can easily create a floating button in Tailwind CSS. Tailwind CSS makes it very easy to create a floating button with its utility-first approach, which allows us to
4 min read
How to Install Tailwind CSS with Create React App?
We will see how to set up Tailwind CSS in a React project using the Create React App. Tailwind CSS is a utility-first CSS framework that allows for rapid development of custom user interfaces. When combined with Create React App, it offers a flexible approach to build modern React applications. Prer
2 min read
Create Animated Scroll-to-Top Button in Tailwind CSS
An animated scroll-to-top button is a user interface element that appears on a webpage when the user scrolls down. It allows the user to quickly return to the top of the page with a single click or tap. This feature is often implemented using CSS animations or transitions, and it can enhance the use
2 min read
How to create a Responsive Navigation Bar in Tailwind CSS ?
A Responsive Navigation Bar with collapsible elements is a crucial component of modern web design, allowing users to navigate seamlessly across various screen sizes. In this article, we'll explore how to implement such a navigation bar using Tailwind CSS, a utility-first CSS framework that enables r
2 min read
Create an Animated Social Media Icons in React and Tailwind CSS
Adding animated social media icons using React and Tailwind CSS improves user experience by enhancing functionality and visual appeal. The icons will have a smooth and appealing animation, making them visually engaging for users. Output Preview:Let us have a look at how the final output will look li
4 min read