Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to setup Tailwind CSS with Vite ?
Next article icon

How to use Tailwind Forms Plugin with React Select ?

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Tailwind CSS, a utility-first styling library for web design, offers Tailwind forms as plugins that provide a foundational reset for form styles. This simplifies the styling process for form elements, allowing easy customization with utilities. React Select, a select component library, is employed for crafting Select Input controls in React with built-in multiselect and async support.

Prerequisites:

  • React JS
  • Tailwind CSS

Steps to create React Project and install required modules:

Step 1: Create a React application using the following command:

npx create-react-app <<Project Name>>

Step 2: After creating your project folder i.e. folder_name, move to it using the following command:

cd <<Project Name>>

Step 3: Install react-select and clsx writing the following command.

 npm install react-select clsx

Step 4: Install TailwindCSS by writing the following command.

npm install -D tailwindcss @tailwindcss/forms
npx tailwindcss init

Step 5: Add the paths of your template file and import @tailwindcss/forms in tailwind.config.js

module.exports = {
content: ["./src/**/*.{html,js,jsx}"],
theme: {
extend: {},
},
plugins: [require('@tailwindcss/forms')],
}

Step 6: Add the tailwind directives to your index.css file

@tailwind base;
@tailwind components;
@tailwind utilities;

Step 7: Compile your index.css file to scan the template

npx tailwindcss -i ./src/index.css -o ./public/output.css

Step 8: Add your compiled CSS file to the <head> and start using Tailwind’s utility classes to style your content.

<link href="/public/output.css" rel="stylesheet">

Now TailwindCSS and react-select and tailwind-form plugin is now installed and ready to be used in the project

Project Structure:

PS_RST

The updated dependencies in package.json file will look like:

"dependencies": {
"clsx": "^2.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"react-select": "^5.8.0",
"web-vitals": "^2.1.4",
}

Example 1: In this, we will use basic styling to display react-select components using tailwind and tailwindcss forms. In react-select, there are 20 internal components that we can style. Few are given below:

  • Control: It is used to style the base of the select component.
  • Input: It is used to style the input search of the component.
  • Menu: To style the dropdown list.
  • Option: To style the list when they are selected or focused.
JavaScript
// SelectTailwind.jsx import clsx from "clsx"; import React from "react"; import Select from "react-select"; const SelectTailwind = () => {     const options = [         { value: "chocolate", label: "Chocolate" },         { value: "strawberry", label: "Strawberry" },         { value: "vanilla", label: "Vanilla" },     ];     return (         <div className=" bg-white text-black                            flex flex-col                            justify-center                           items-center                           h-screen w-full">             <h1 className=" flex flex-row                              text-green-500                              font-semibold                              text-2xl">                 GFG React-Select and Tailwind Forms Plugin             </h1>             <label htmlFor="wotailwind">Without Tailwind</label>             <Select id="wotailwind" options={options} />             <label htmlFor="wtailwind">With Tailwind</label>             <Select                 id="wtailwind"                 unstyled                 options={options}                 classNames={{                     input: () => "[&_input:focus]:ring-0",                     control: () => "form-multiselect p-6 ",                     menu: () => "bg-slate-200 ",                     option: ({ isFocused, isSelected }) =>                         clsx(                             isFocused && `hover:cursor-pointer                                  hover:bg-slate-500                                  px-3 py-2 rounded`,                             isSelected && "bg-slate-300"                         ),                 }}             />         </div>     ); }; export default SelectTailwind; 

Steps to run: To run the project enter the following command in Terminal.

 npm start

Output:



Example 2: In this example, we will use tailwind css styling to style the components and use variables instead of writing directly in classNames.

JavaScript
//MultipleTailwind.jsx import clsx from "clsx"; import React from "react"; import Select from "react-select";  const MultipleTailwind = () => {     const options = [         { value: "chocolate", label: "Chocolate" },         { value: "strawberry", label: "Strawberry" },         { value: "vanilla", label: "Vanilla" },     ];     const baseStyle =         `form-multiselect text-blue-800              border p-5 rounded-lg bg-red-400              hover:cursor-pointer`;     const menuStyle =         `text-blue-900 border            rounded-lg bg-red-400            hover:cursor-pointer`;     const optionStyles = {         focus: `hover:cursor-pointer                  hover:bg-white                  hover:text-black`,         Selected: "bg-slate-300",     };     return (         <div className=" bg-white text-black                            flex flex-col                            justify-center                           items-center                          h-screen w-full">             <h1 className="text-green-500                             font-semibold                             text-2xl">                 GFG React-MultiSelect and Tailwind Forms Plugin             </h1>             <label className="flavour">Multiselect :</label>             <Select                 classNames={{                     input: () => "[&_input:focus]:ring-0",                     control: () => baseStyle,                     menu: () => menuStyle,                     option: ({ isFocused, isSelected }) =>                         clsx(                             isFocused && optionStyles.focus,                             isSelected && optionStyles.Selected                         ),                 }}                 unstyled                 id="flavour"                 isMulti                 styles={{                     input: (base) => ({                         ...base,                         "input:focus": {                             boxShadow: "none",                         },                     }),                 }}                 options={options}             />         </div>     ); }; export default MultipleTailwind; 

Steps to Run: To run the project enter the following command in Terminal.

 npm start

Output:



Next Article
How to setup Tailwind CSS with Vite ?
author
ameyabavkar
Improve
Article Tags :
  • Web Technologies
  • CSS
  • ReactJS
  • Tailwind CSS
  • React-Questions
  • Tailwind CSS-Questions

Similar Reads

  • How to setup Tailwind 3 in React with CRA 5 ?
    In this article, we will see how to set up Tailwind 3 in React with CRA 5, but before that, we need some basic ideas about these technologies. React JS: A free and open-source front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook.TailwindCSS: A high
    3 min read
  • How to use Ant Design with Tailwind CSS in a React Project ?
    The integration of Ant Design and Tailwind CSS in a React project presents challenges due to their differing styling conventions. Ant Design offers a feature-rich component library with its own class names, while Tailwind CSS provides a utility-first framework for custom designs. Combining both libr
    2 min read
  • How to use HTML <select> tag in ReactJS ?
    HTML <select> tag in react is a common web development component used for dynamic form input or as a select menu. It provides multiple selection options in the form of a dropdown menu. It enables the web page to render a select box with the options. The <select>  tag is used as an outer
    2 min read
  • How to setup Tailwind CSS with Vite ?
    Tailwind CSS is a popular CSS framework that simplifies the process of designing user interfaces with its utility-first approach. Vite is a build tool that provides a fast development experience for modern web projects. Together, they make front-end development efficient and enjoyable. In this artic
    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
  • How to Implement Reveal on Scroll in React using Tailwind CSS ?
    In React, the reveal on the scroll is a technique, where elements on a page gradually appear or animate as the user scrolls down. Prerequisites:NPM & Node.jsReact JSTailwind CSSTo implement reveal in scroll in react using Tailwind CSS we have these methods: Table of Content Using the Intersectio
    4 min read
  • How To Build Restaurant Hero Section In React With Tailwind CSS?
    Restaurant Hero Section means when we open the landing page of a restaurant website, the first and foremost content that appears from the navigation bar about the restaurant or some text with a picture is the hero section. We use React to create UI and in this article, we will learn how to Build Res
    3 min read
  • How to Disable Text Selection in ReactJS ?
    In this article, we will see how to disable the text selection in ReactJS. Prerequisites:Introduction to ReactReact HooksNPM or NPXWeb applications ofte­n provide a valuable feature­ called text sele­ction, enabling users to easily highlight and copy content from a webpage. However, there are situat
    4 min read
  • Create Select Menus UI using React and Tailwind CSS
    We will build a Select Menu UI using React and Tailwind CSS. Select menus are dropdowns that allow users to choose one option from a predefined list. We'll style the dropdown using Tailwind CSS for a modern, clean look and integrate React Icons to improve user experience. PrerequisitesReact.jsTailwi
    5 min read
  • How To Set Default Value In Select using ReactJS?
    When building forms in ReactJS, it is common to work with select dropdowns where users can choose an option from a list. Sometimes, you may want to set a default value in the select dropdown to pre-select an option when the form loads. In this article, we’ll explore different approaches to set a def
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences