Skip to content

Commit d730560

Browse files
authored
fix(Bug): resolve text-rotator twitch every time when text updates. (#8117)
1 parent 5bd5d58 commit d730560

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

src/components/TextRotater/TextRotater.jsx

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Import External Dependencies
1+
import { clsx } from "clsx";
22
import PropTypes from "prop-types";
33
import { Children, PureComponent, cloneElement } from "react";
44

@@ -20,11 +20,12 @@ export default class TextRotater extends PureComponent {
2020
state = {
2121
currentIndex: 0,
2222
contentHeight: 0,
23+
isAnimating: false,
2324
};
2425

2526
render() {
2627
const { children, maxWidth } = this.props;
27-
const { currentIndex, contentHeight } = this.state;
28+
const { currentIndex, contentHeight, isAnimating } = this.state;
2829
const childrenCount = Children.count(children);
2930

3031
const currentChild = cloneElement(children[currentIndex], {
@@ -48,8 +49,10 @@ export default class TextRotater extends PureComponent {
4849
"
4950
>
5051
<div
51-
className="inline-flex flex-col text-left"
52-
ref={(trw) => (this.textRotatorWrap = trw)}
52+
className={clsx(
53+
"inline-flex flex-col text-left",
54+
isAnimating && "text-rotater--slide-up",
55+
)}
5356
onTransitionEnd={this._handleTransitionEnd}
5457
style={{ height: contentHeight, width: maxWidth }}
5558
>
@@ -63,48 +66,45 @@ export default class TextRotater extends PureComponent {
6366
componentDidMount() {
6467
const { delay } = this.props;
6568

66-
setTimeout(() => {
69+
this.heightTimeout = setTimeout(() => {
6770
this._calculateContentHeight();
6871
}, 50);
6972

70-
setTimeout(() => {
71-
if (this.textRotatorWrap) {
72-
this.textRotatorWrap.classList.add("text-rotater--slide-up");
73-
}
73+
this.animationTimeout = setTimeout(() => {
74+
this.setState({ isAnimating: true });
7475
}, delay);
7576

7677
window.addEventListener("resize", this._calculateContentHeight);
7778
}
7879

7980
componentWillUnmount() {
81+
clearTimeout(this.heightTimeout);
82+
clearTimeout(this.animationTimeout);
83+
clearTimeout(this.repeatTimeout);
8084
window.removeEventListener("resize", this._calculateContentHeight);
8185
}
8286

8387
_calculateContentHeight = () => {
84-
this.setState({
85-
contentHeight: this.content.clientHeight,
86-
});
88+
if (this.content) {
89+
this.setState({
90+
contentHeight: this.content.clientHeight,
91+
});
92+
}
8793
};
8894

8995
_handleTransitionEnd = () => {
9096
const { children, repeatDelay } = this.props;
9197

92-
if (this.textRotatorWrap) {
93-
this.textRotatorWrap.classList.remove("text-rotater--slide-up");
94-
95-
this.setState(
96-
{
97-
currentIndex:
98-
(this.state.currentIndex + 1) % Children.count(children),
99-
},
100-
() => {
101-
setTimeout(() => {
102-
if (this.textRotatorWrap) {
103-
this.textRotatorWrap.classList.add("text-rotater--slide-up");
104-
}
105-
}, repeatDelay);
106-
},
107-
);
108-
}
98+
this.setState(
99+
{
100+
currentIndex: (this.state.currentIndex + 1) % Children.count(children),
101+
isAnimating: false,
102+
},
103+
() => {
104+
this.repeatTimeout = setTimeout(() => {
105+
this.setState({ isAnimating: true });
106+
}, repeatDelay);
107+
},
108+
);
109109
};
110110
}

0 commit comments

Comments
 (0)