How can I add a fade-in animation to <div>fading-in text</div>
using only inline styles?
class Practise extends Component {
state = { show: false };
componentDidMount() {
setTimeout(() => {
this.setState({ show: true });
}, 2000);
}
render() {
if (!this.state.show) return <div>default regular text</div>;
return <div>fading-in text</div>;
}
}
(No library solutions please, I want to figure it out natively)
Set the onMouseEnter and onMouseLeave props on the element. When the user hovers over or out of the element, update a state variable. Conditionally set inline styles on the element.
Fade Component adds a fade animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Fade Component in ReactJS using the following approach.
The setState
method has a callback as second(optional) parameter. So once you set your this.state.show
to true
you can increment your opacity
using this callback parameter. The callback function may look like below:
fadingIn(){
const timer = setInterval(() => {
if (this.state.opacity === 1) {
clearInterval(timer);
}
this.setState({ opacity: this.state.opacity + 0.1 })
}, 100);
}
So as you already added componentDidMount
you can trigger it there
componentDidMount(){
setTimeout(() => this.setState({ show: true }, this.fadingIn), 1000)
}
render(){
return <div>
{!this.state.show
? <div>Regular</div>
: <div style={{opacity: this.state.opacity}}>Fade In</div>}
</div>
}
Worked Example
UPDATE
Try something like this:
const withFading = ({ Faded, duration, isOut }) => {
const inEffect = `
@keyframes react-fade-in {
0% { opacity: 0; }
50% { opacity: 0; }
100% { opacity: 1; }
}
`;
const outEffect = `
@keyframes react-fade-out {
0% { opacity: 1; }
50% { opacity: 0; }
100% { opacity: 0; }
}
`;
return (
<div>
// Here we add style tag with the css for fadeIn & fadeOut
// depends on a input value of isOut parameter.
// It does same thing as an example from below
// <style>
// body { your css rules }
// </style>
// with react we can pass `body { ... }` as a child into
// style tag as i did.
<style children={isOut ? outEffect : inEffect} />
<div style={{
animationDuration: `${duration}s`,
animationIterationCount: 1,
animationName: `react-fade-${(isOut ? 'out' : 'in')}`,
animationTimingFunction: isOut ? 'ease-out' : 'ease-in'
}}
><Faded /></div>
</div>
)
}
const Hello = () => <div>Hello</div>
const FadedHello = withFading({ Faded: Hello, duration: 2, isOut: false});
Worked example
I try to answer your question in the "The Reason's" comment section (this question: can this be done using opacity and transition instead of keyFrames...") As far as I know, we have to use add a trigger for the transition (such as hover pseudo-class in CSS or onMouseEnter and onMouseLeave props in react events)
Here is my answer and I have tested it
import React, {useState} from 'react'
function App() {
const [isHovered, setIsHovered] = useState(false)
return (
<div
style={{
backgroundColor: isHovered ? 'orange' : 'green',
opacity: isHovered ? 1 : 0,
height: isHovered ? 400 : 200,
width: isHovered ? 400 : 200,
transition: 'all 1s',
}}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
</div>
);
}
export default App;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With