Open In App

Create 3D Radio Buttons in React

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

3D Radio Buttons are a fancier version of regular radio buttons. They have a cool 3D look with shadows and animations, which makes them attractive and appealing to the user. In this article, we will create 3d radio buttons with the help of React.

Preview of 3D Radio Buttons:

btn

Final Preview

Prerequisites:

Approach to Create 3D Radio Buttons:

  • Create a functional React components named RadioButton1 and RadioButton2.
  • Use the useState hook to manage the selected option’s state.
  • Define a function handleOptionChange to update the selected option.
  • Represents the radio button choices and labels within a div structure. Each radio button consists of an input, a div with a ball, and a label.
  • Applies dynamic styles based on the selected option, leveraging the checked attribute and conditional rendering in JSX.

Steps to Create the React App:

Step 1: Create a new React project using Create React App.

npx create-react-app radio-buttons
cd radio-buttons

Step 2: Create a folder “components” in src file and add the two different radio button files named RadioButton1.js and RadioButton2.js, along with their CSS files.

Project Structure:

btn-structure

File Structure

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

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"run": "^1.4.0",
"web-vitals": "^2.1.4"
}

Example: Write the following code in the respective file.

Javascript




// App.js
 
import React from "react";
import "./App.css";
import RadioButtons from "./Components/RadioButton1";
import RadioButtons2 from "./Components/RadioButton2";
 
function App() {
    return (
        <div>
            <h1 className="gfg">GeeksForGeeks</h1>
            <div className="main-cont">
                <h1 className="btn-title">Radio Buttons</h1>
                <div className="container">
                    <div className="in-cont">
                        <RadioButtons />
                    </div>
                    <div className="in-cont">
                        <RadioButtons2 />
                    </div>
                </div>
            </div>
        </div>
    );
}
 
export default App;


Javascript




// RadioButton1.js
 
import React, { useState } from "react";
import "./RadioButton1.css";
 
const RadioButtons = () => {
    const [isChecked, setIsChecked] = useState(false);
 
    const handleInputChange = () => {
        setIsChecked(!isChecked);
    };
 
    return (
        <div>
            <div
                className={`radio-input ${isChecked ? "checked" : ""}`}
                onClick={handleInputChange}
            >
                <input
                    name="btn"
                    type="radio"
                    className="input"
                    checked={isChecked}
                    onChange={() => {}}
                />
                <span className="span">
                    {isChecked ? "Active" : "Inactive"}
                </span>
                <div className={`dot ${isChecked ? "active" : ""}`}></div>
            </div>
            <div
                className={`radio-input ${!isChecked ? "checked" : ""}`}
                onClick={handleInputChange}
            >
                <input
                    name="btn"
                    type="radio"
                    className="input"
                    checked={isChecked}
                    onChange={() => {}}
                />
                <span className="span">
                    {!isChecked ? "Active" : "Inactive"}
                </span>
                <div className={`dot ${isChecked ? "active" : ""}`}></div>
            </div>
        </div>
    );
};
 
export default RadioButtons;


Javascript




// RadioButton2.js
 
import React, { useState } from 'react';
import './RadioButton2.css';
 
const RadioButtons2 = () => {
    const [selectedOption, setSelectedOption] = useState('one');
 
    const handleOptionChange = (value) => {
        setSelectedOption(value);
    };
 
    return (
        <div className="radio-input2">
            <div className="selector">
                <div className="choice">
                    <div>
                        <input
                            type="radio"
                            id="one"
                            name="number-selector"
                            value="one"
                            checked={selectedOption === 'one'}
                            onChange={() => handleOptionChange('one')}
                            className="choice-circle"
                        />
                        <div className="ball"></div>
                    </div>
                    <label className="choice-name" htmlFor="one">
                        1
                    </label>
                </div>
                <div className="choice">
                    <div>
                        <input
                            type="radio"
                            id="two"
                            name="number-selector"
                            value="two"
                            checked={selectedOption === 'two'}
                            onChange={() => handleOptionChange('two')}
                            className="choice-circle"
                        />
                        <div className="ball"></div>
                    </div>
                    <label className="choice-name" htmlFor="two">
                        2
                    </label>
                </div>
                <div className="choice">
                    <div>
                        <input
                            type="radio"
                            id="three"
                            name="number-selector"
                            value="three"
                            checked={selectedOption === 'three'}
                            onChange={() => handleOptionChange('three')}
                            className="choice-circle"
                        />
                        <div className="ball"></div>
                    </div>
                    <label className="choice-name" htmlFor="three">
                        3
                    </label>
                </div>
            </div>
        </div>
    );
};
 
export default RadioButtons2;


CSS




/* App.css */
 
body {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(240, 245, 240, 0.66);
    font-family: monospace;
}
 
.main-cont {
    width: 70vh;
    height: 68vh;
    padding: 20px 30px;
    text-align: center;
    background-color: #ccf3e7;
    border: 1px solid #ccc;
    border-radius: 15px;
    box-shadow: 10px 10px 5px 0px rgba(109, 95, 95, 0.5);
}
 
.btn-title {
    padding: 10px auto;
    margin: 30px auto;
    color: blue;
    font-size: 50px;
    border: 3px solid;
    box-shadow: 2px 0px 4px 4px rgb(64, 64, 241);
    border-radius: 20px;
 
}
 
.container {
    width: 100%;
    height: 45vh;
    display: grid;
    place-content: center;
    display: flex;
    justify-content: space-between;
 
}
 
.in-cont {
    width: 45%;
    height: 70%;
    margin: auto;
    border: 3px solid rgb(245, 58, 37);
    box-shadow: 6px 4px 3px 0px rgb(241, 101, 36);
    display: grid;
    place-content: center;
    border-radius: 20px;
    padding: 20px 0;
    background-color: rgb(241, 192, 170);
 
}
 
.gfg {
    margin-top: 30px;
    text-align: center;
    font-size: 35px;
    color: green;
}
 
 
 
/* ================== Media Query(Small Devices) ============== */
 
@media screen and (max-width: 800px) {
 
    .main-cont {
        width: 50vh;
    }
 
    .container {
        display: grid;
        grid-template-columns: repeat(1, 1fr);
        height: fit-content;
    }
 
    .in-cont {
        padding: 20px 20px;
    }
 
    .btn-title {
        width: 70%;
        padding: 5px 0;
        font-size: 40px;
    }
 
}


CSS




/* RadioButton1.css */
 
.radio-input {
    position: relative;
    display: flex;
    flex-direction: column;
    margin: 40px;
    width: 150px;
    height: 50px;
    cursor: pointer;
}
 
.radio-input .input {
    position: absolute;
    height: 100%;
    width: 100%;
    margin: 0;
    cursor: pointer;
    z-index: 10;
    opacity: 0;
}
 
.radio-input .span {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: #323232;
    border-radius: 10px;
    color: #eb0707;
    font-weight: 700;
    font-size: 17px;
    box-shadow: 0px 6px 0px 5px gray;
    border: 2px solid black;
    text-shadow: 1px 1px 0.5px rgba(247, 5, 5, 0.589);
}
 
.radio-input.checked .span {
    color: #00ff00;
    text-shadow: 1px 1px 0.5px rgba(0, 255, 0, 0.3);
    box-shadow: 0px -6px 1px 6px gray;
}
 
.radio-input .dot {
    height: 6px;
    width: 6px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
}
 
.radio-input.checked .dot {
    background: #00ff00;
}


CSS




/* RadioButton2.css */
 
.selector {
    display: flex;
    flex-direction: column;
}
 
.choice {
    margin: 10px 0 10px 0;
    display: flex;
    align-items: center;
}
 
.choice>div {
    position: relative;
    width: 41px;
    height: 41px;
    margin-right: 15px;
}
 
.choice-circle {
    appearance: none;
    height: 100%;
    width: 100%;
    border-radius: 100%;
    border-width: 9px;
    border-style: solid;
    border-color: rgba(245, 245, 245, 0.45);
    cursor: pointer;
 
}
 
.ball {
    z-index: 1;
    position: absolute;
    inset: 0px;
    transform: translate(6px, 3px);
    box-shadow: rgba(0, 0, 0, 0.17) 0px -10px 10px 0px inset,
        rgba(247, 14, 14, 0.09) 0px 4px 2px,
        rgba(0, 0, 0, 0.09) 0px 8px 4px,
        rgba(10, 142, 160, 0.09) 0px 16px 8px,
        rgba(17, 107, 167, 0.09) 0px 32px 16px,
        0px -1px 15px -8px rgba(13, 106, 228, 0.09);
    border-radius: 100%;
    transition: transform 800ms cubic-bezier(1, -0.4, 0, 1.4);
    background-color: rgb(250, 245, 245);
}
 
 
.choice-circle:checked+.ball {
    transform: translateX(0px);
    background-color: aqua;
    transform: translate(6px, 3px);
    z-index: 1;
}
 
 
.choice-name {
    color: rgb(105, 31, 224);
    font-size: 35px;
    font-weight: 900;
    font-family: monospace;
    cursor: pointer;
}


To run the application run the following command in the terminal

npm start

Output:

bf51f1c6-454b-49ca-a522-d7aee5965fb9-ezgifcom-crop

Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads