accounts-frontend/packages/app/components/ui/motion/SlideMotion.tsx

113 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
import React from 'react';
2017-07-22 21:27:38 +05:30
import { Motion, spring } from 'react-motion';
import MeasureHeight from 'app/components/MeasureHeight';
2017-07-22 21:27:38 +05:30
2017-11-19 23:46:15 +05:30
import styles from './slide-motion.scss';
2017-07-22 21:27:38 +05:30
2019-12-10 13:17:32 +05:30
interface Props {
2020-05-24 04:38:24 +05:30
activeStep: number;
children: React.ReactNode;
2019-12-10 13:17:32 +05:30
}
2019-12-07 16:58:52 +05:30
interface State {
2020-05-24 04:38:24 +05:30
version: string;
prevChildren: React.ReactNode | undefined;
stepsHeights: Record<Props['activeStep'], number>;
2019-12-07 16:58:52 +05:30
}
2019-12-10 13:17:32 +05:30
class SlideMotion extends React.PureComponent<Props, State> {
2020-05-24 04:38:24 +05:30
state: State = {
prevChildren: undefined, // to track version updates
version: `${this.props.activeStep}.0`,
stepsHeights: [],
};
private isHeightMeasured: boolean;
2017-07-22 21:27:38 +05:30
2020-05-24 04:38:24 +05:30
static getDerivedStateFromProps(props: Props, state: State) {
let [, version] = state.version.split('.').map(Number);
2017-07-22 21:27:38 +05:30
2020-05-24 04:38:24 +05:30
if (props.children !== state.prevChildren) {
version++;
}
2019-12-10 13:17:32 +05:30
2020-05-24 04:38:24 +05:30
// mark this view as dirty to re-measure height
return {
prevChildren: props.children,
version: `${props.activeStep}.${version}`,
};
2019-12-10 13:17:32 +05:30
}
2020-05-24 04:38:24 +05:30
render() {
const { activeStep, children } = this.props;
const { version } = this.state;
const activeStepHeight = this.state.stepsHeights[activeStep] || 0;
// a hack to disable height animation on first render
const { isHeightMeasured } = this;
this.isHeightMeasured = isHeightMeasured || activeStepHeight > 0;
const motionStyle = {
transform: spring(activeStep * 100, {
stiffness: 500,
damping: 50,
precision: 0.5,
}),
height: isHeightMeasured
? spring(activeStepHeight, {
stiffness: 500,
damping: 20,
precision: 0.5,
})
: activeStepHeight,
};
return (
<Motion style={motionStyle}>
{(interpolatingStyle) => (
<div
style={{
overflow: 'hidden',
height: `${interpolatingStyle.height}px`,
}}
>
<div
className={styles.container}
style={{
2021-03-26 08:49:04 +05:30
// TODO: inverse for RTL language
2020-05-24 04:38:24 +05:30
WebkitTransform: `translateX(-${interpolatingStyle.transform}%)`,
transform: `translateX(-${interpolatingStyle.transform}%)`,
}}
>
{React.Children.map(children, (child, index) => (
<MeasureHeight
className={styles.item}
onMeasure={this.onStepMeasure(index)}
state={version}
key={index}
>
{child}
</MeasureHeight>
))}
</div>
</div>
)}
</Motion>
);
}
2017-07-22 21:27:38 +05:30
2020-05-24 04:38:24 +05:30
onStepMeasure = (step: number) => (height: number) => {
this.setState({
stepsHeights: {
...this.state.stepsHeights,
[step]: height,
},
});
};
2017-07-22 21:27:38 +05:30
}
2019-12-07 16:58:52 +05:30
export default SlideMotion;