How to create a translucent text input in ReactJS ?
Last Updated :
22 Jul, 2024
We are going to learn how to create a translucent text input in React JS. We are going to create a translucent animated text input using framer-motion and styled components.
Prerequisites:
Approach:
- We are going to create a translucent animated text input using framer-motion and styled components.
- Wrapper, Input, Label, Underline are the styled components used to make the text input box collectively in Component.jsx file.
- In Component.jsx file, we use framer-motion with custom animation variants from the Component.motion.js file to animate the text input box.
- React useState hook is used to manage the state of value that is used as a placeholder attribute & also to set it as a label when active.
- Framer-motion useCycle hook is similar to react useState hook. It cycles through a series of visual properties used for animation. It is used to toggle between or cycle through animation variants.
Steps to create React Application and Installing Modules:
Step 1: Now, you will start a new project using create-react-app so open your terminal and type:
npx create-react-app translucent-input-box
Step 2: After creating your project folder i.e. translucent-input-box , move to it using the following command.
cd translucent-input-box
Step 3: Add the npm packages you will need during the project:
npm install framer-motion styled-components
Project Structure:
Project structureThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: Below is the implementation of the above approach
CSS
.App {
font-family: "Times New Roman", Times, serif;
text-align: center;
width: auto;
height: 98vh;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
background: #1e9600;
/* fallback for old browsers */
background: -webkit-linear-gradient(to right,
#ff0000,
#fff200,
#1e9600);
/* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right,
#ff0000,
#fff200,
#1e9600);
}
.container {
border-radius: 25px;
width: 50vw;
height: 20vh;
display: flex;
justify-content: center;
align-items: center;
opacity: 0.5;
background-color: #f1f1f1;
}
Input {
text-decoration: none;
background-color: #f1f1f1;
width: 40%;
}
JavaScript
import React, { useState } from "react";
import "./App.css";
import Input from "./Input";
const App = () => {
// The useState hook is used to manage the state of
// "value" that is used as placeholder attribute
// and also to set it as a label when clicked
const [value, setValue] = useState("");
return (
<div className="App">
<div className="container">
{/* "Input" component created using styled-components
and animated using framer-motion
*/}
<Input
value={value}
onChange={(id, value) => setValue(value)}
label={"First name"}
/>
</div>
</div>
);
};
export default App;
JavaScript
import React from "react";
import {
Wrapper, Input,
Label, Underline
}
from "./Component.styles";
import {
motionLabel,
motionUnderline
}
from "./Component.motion";
import { useCycle } from "framer-motion";
export default ({ label, value, onChange, id, errors }) => {
const onTapStart = (event, info) => {
focus === "inactive" && cycleFocus();
return blur === "inactive" && cycleBlur();
};
const onBlur = event => {
value === "" && cycleFocus();
cycleBlur();
};
const [focus, cycleFocus] = useCycle("inactive", "active");
const [blur, cycleBlur] = useCycle("inactive", "active");
return (
<div>
{/* Wrapper,Label,Underline - custom styled-components with
some of its attributes
*/}
<Wrapper>
<Input
onTap={onTapStart}
placeholder={label}
onBlur={e => onBlur(id)}
onChange={e => onChange(id, e.target.value)}
type={"text"}
required
value={value}
/>
<Label {...motionLabel(focus)}>{label}</Label>
<Underline {...motionUnderline(blur)} />
</Wrapper>
</div>
);
}
JavaScript
//Component.motion.js
const variantsWrapper = {
initial: {},
in: {},
out: {},
hover: {},
tap: {}
};
const variantsLabel = {
active: {
x: -15,
y: -20,
scale: 0.7
},
inactive: { x: 0, y: 0, scale: 1 }
};
const variantsUnderline = {
active: {
width: "100%",
transition: {
ease: "easeIn",
duration: 0.2
}
},
inactive: {
width: "0",
transition: {
ease: "easeIn",
duration: 0.1
}
}
};
export const motionLabel = state =& gt; {
return {
animate: state,
variants: variantsLabel
};
};
export const motionUnderline = state =& gt; {
return {
animate: state,
variants: variantsUnderline
};
};
export const animationWrapper = {
initial: "initial",
animate: "in",
exit: "out",
whileHover: "hover",
whileTap: "tap",
variants: variantsWrapper
};
JavaScript
//Component.styles.js
import styled from "styled-components";
import { motion } from "framer-motion";
// Below are the styled-components used to
// make the animated text input box
export const Wrapper = styled(motion.div)`
position: relative;
width: 80%;
padding: 18px;
padding-bottom: 30px;
border-bottom: 1px solid #2f528f;
`;
export const Label = styled(motion.span)`
align-self: center;
position: absolute;
left: 0;
top: 50%;
grid-area: input;
font-family: Montserrat;
font-size: 18px;
line-height: 18px;
text-align: left;
pointer-events: none;
font-weight: normal;
/* background: green; */
`;
export const Input = styled(motion.input)`
height: 18px;
font-size: 18px;
-webkit-appearance: none;
background: transparent !important;
position: absolute;
left: 0;
top: 50%;
padding: 0;
padding-bottom: 5px;
margin: 0;
color: black;
border: none;
box-shadow: none !important;
font-weight: normal;
&:focus {
outline: none;
}
&::placeholder {
color: #f1f1f1;
}
`;
export const Underline = styled(motion.div)`
position: absolute;
background-color: #2f528f;
bottom: 0;
left: 0;
width: 100%;
height: 3px;
`;
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
Similar Reads
How to create basic text input in React Native ? Text input is a basic component that allows users to input text through the keyboard. It is widely used in mobile applications for taking usernames, passwords, user details, and much more.In React Native, we have a TextInput component to achieve this. It comes with many props that help us to modify
4 min read
How to restrict user character input limit in ReactJS ? To restrict the user character input limit in React JS we can simply set a max input limit to the input box or we can also monitor the input length and make a custom function to set the restriction. Pre-requisite:NPM & Node.jsReact.jsReact HooksHTML onchange EventApproaches to restrict user char
3 min read
How to Create Smaller `Input` in React-Bootstrap ? ReactJS is one of the most favorite libraries for building attractive applications. Combining Bootstrap with ReactJS is a powerful tool for making the application more interactive. A Smaller Input in React-Bootstrap refers to reducing the size of an input field component, typically achieved by apply
4 min read
How to create OTP Input Box Using React ? Let us explore the implementation of OTP (One-Time Password) login functionality using ReactJS. OTP-based logins add an extra layer of security to your applications, making them more resilient against unauthorized access.Prerequisites:ReactHTML, CSS and JavaScriptApproach:Create a React functional c
3 min read
How to create Bold Specific Text In React JS ? ReactJS has eÂmerged as one of the most widely adopted JavaScript libraries for deÂsigning user interfaces. TheÂy often encounter scenarios that demand speÂcialized text styling capabilities. This includeÂs the need to eÂmphasize certain words or phrases by applying bold formatting. In this article
3 min read
How to set input box to be a floating number in ReactJS ? When working with forms in ReactJS, you may encounter scenarios where you need to accept floating-point numbers as input from users. We will set an input box to accept floating numbers in ReactJS and handle the validation of the entered values. PrerequisitesReact JS NPM and Node.jsApproach If we wan
2 min read
How to add code input in React JS? In this article, we are going to learn how we can add Code Input in React JS. React is a front-end JavaScript library for constructing user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Approach to add code input: To incorporate our
2 min read
How to Create Typewriter Effect in ReactJS? Learn how to enhance your React JS applications with a captivating typewriter effect using CSS and JavaScript, leveraging the typewriter-effect package for seamless and engaging UI design.ApproachTo create a typewriter effect in React, we will be using the typewriter-effect npm package. Import and u
5 min read
How to Set Text Color in ReactJS ? React provides you the ability to create interactive and dynamic useÂr interfaces. Within these interfaces, the choice of text color holds significant importance as it enhanceÂs the visual appeal and engageÂment for users. A foundational aspect of styling revolveÂs around modifying text color. In t
3 min read
How to add styles to autocomplete in ReactJS ? Autocomplete functionality in web applications is common for predictive user input. This article focuses on enhancing the visual appearance of ReactJS autocomplete components using Material-UI's package, offering customization options for seamless integration with the overall application design.Prer
3 min read