AnimatedPostCard

This commit is contained in:
srgooglo 2022-03-14 22:57:23 +01:00
parent 0115e34c5d
commit 1dc446d5d1
2 changed files with 59 additions and 4 deletions

View File

@ -7,8 +7,19 @@ import classnames from "classnames"
import { User } from "models" import { User } from "models"
import CSSMotion from "rc-animate/lib/CSSMotion"
import useLayoutEffect from "rc-util/lib/hooks/useLayoutEffect"
import "./index.less" import "./index.less"
const getCurrentHeight = (node) => ({ height: node.offsetHeight })
const getMaxHeight = (node) => {
return { height: node.scrollHeight }
}
const getCollapsedHeight = () => ({ height: 0, opacity: 0 })
function PostHeader(props) { function PostHeader(props) {
const [timeAgo, setTimeAgo] = React.useState(0) const [timeAgo, setTimeAgo] = React.useState(0)
@ -98,7 +109,7 @@ function PostActions(props) {
</div> </div>
} }
export default class PostCard extends React.Component { export class PostCard extends React.Component {
state = { state = {
loading: true, loading: true,
selfId: null, selfId: null,
@ -188,4 +199,48 @@ export default class PostCard extends React.Component {
</div> </div>
</div> </div>
} }
} }
export const PostCardAnimated = ({
data,
onAppear,
motionAppear,
}, ref,) => {
const motionRef = React.useRef(false)
useLayoutEffect(() => {
return () => {
if (motionRef.current) {
onAppear()
}
}
}, [])
return <CSSMotion
ref={ref}
motionName="motion"
motionAppear={motionAppear}
onAppearStart={getCollapsedHeight}
onAppearActive={node => {
motionRef.current = true;
return getMaxHeight(node);
}}
onAppearEnd={onAppear}
onLeaveStart={getCurrentHeight}
onLeaveActive={getCollapsedHeight}
onLeaveEnd={() => {
onLeave(id)
}}
>
{(props, passedMotionRef) => {
return <PostCard
ref={passedMotionRef}
data={data}
/>
}}
</CSSMotion>
}
export const ForwardedPostCardAnimated = React.forwardRef(PostCardAnimated)
export default ForwardedPostCardAnimated

View File

@ -48,7 +48,7 @@
} }
svg { svg {
fill: var(--appColor); fill : var(--appColor);
margin-left: 6px; margin-left: 6px;
} }
@ -69,7 +69,7 @@
margin : 0; margin : 0;
font-family: "DM Mono", monospace; font-family: "DM Mono", monospace;
align-self : start; align-self : start;
cursor: pointer; cursor : pointer;
} }
>div { >div {