fix: use difference `layoutId` to fix date element layout transition, fixed #664

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei 2024-09-27 17:35:26 +08:00
parent 1eae565fb3
commit 3f170d050d
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
1 changed files with 22 additions and 6 deletions

View File

@ -1,7 +1,16 @@
import { m } from "framer-motion"
import { throttle } from "lodash-es"
import type { FC, PropsWithChildren } from "react"
import { memo, useCallback, useId, useLayoutEffect, useMemo, useRef, useState } from "react"
import {
cloneElement,
memo,
useCallback,
useId,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react"
import { useDebounceCallback } from "usehooks-ts"
import { SafeFragment } from "~/components/common/Fragment"
@ -79,10 +88,13 @@ const DateItemInner: FC<{
Wrapper?: FC<PropsWithChildren>
}> = ({ date, endTime, startTime, className, Wrapper }) => {
const rid = useId()
const RelativeElement = (
<m.span key="b" layout layoutId={rid}>
<RelativeDay date={date} />
</m.span>
const RelativeElement = useMemo(
() => (
<m.span key="b" layout layoutId={rid}>
<RelativeDay date={date} />
</m.span>
),
[date, rid],
)
const handleMarkAllAsRead = useMarkAllByRoute({
@ -108,6 +120,7 @@ const DateItemInner: FC<{
const { feedId } = useRouteParams()
const isList = isListSubscription(feedId)
const tooltipId = useRef(Math.random().toString())
return (
<div
className={cn(className, isSticky && "border-b")}
@ -122,7 +135,10 @@ const DateItemInner: FC<{
<span>
Mark
<span> </span>
{RelativeElement}
{useMemo(
() => cloneElement(RelativeElement, { layoutId: tooltipId.current }),
[RelativeElement],
)}
<span> </span>
as read
</span>