From c884b9b963c09a9031f1dca90004ae301b013a22 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 18 Nov 2024 11:01:14 -0600 Subject: [PATCH] PreviewCard: set the ref differently, don't set the same ref on two different elements --- src/components/preview-card.tsx | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/components/preview-card.tsx b/src/components/preview-card.tsx index 76d99220d..c9f70c981 100644 --- a/src/components/preview-card.tsx +++ b/src/components/preview-card.tsx @@ -4,7 +4,7 @@ import playerPlayIcon from '@tabler/icons/outline/player-play.svg'; import zoomInIcon from '@tabler/icons/outline/zoom-in.svg'; import clsx from 'clsx'; import { List as ImmutableList } from 'immutable'; -import { useState, useEffect } from 'react'; +import { useState, useEffect, useRef } from 'react'; import Blurhash from 'soapbox/components/blurhash.tsx'; import HStack from 'soapbox/components/ui/hstack.tsx'; @@ -40,6 +40,8 @@ const PreviewCard: React.FC = ({ onOpenMedia, horizontal, }): JSX.Element => { + const ref = useRef(null); + const [width, setWidth] = useState(defaultWidth); const [embedded, setEmbedded] = useState(false); @@ -52,6 +54,14 @@ const PreviewCard: React.FC = ({ const trimmedTitle = trim(card.title, maxTitle); const trimmedDescription = trim(card.description, maxDescription); + useEffect(() => { + if (ref.current) { + const { offsetWidth } = ref.current; + cacheWidth?.(offsetWidth); + setWidth(offsetWidth); + } + }, [ref.current]); + const handlePhotoClick = () => { const attachment = normalizeAttachment({ type: 'image', @@ -78,16 +88,6 @@ const PreviewCard: React.FC = ({ } }; - const setRef: React.RefCallback = c => { - if (c) { - if (cacheWidth) { - cacheWidth(c.offsetWidth); - } - - setWidth(c.offsetWidth); - } - }; - const renderVideo = () => { const content = { __html: addAutoPlay(card.html) }; const ratio = getRatio(card); @@ -95,7 +95,6 @@ const PreviewCard: React.FC = ({ return (
= ({ } return ( -
+
}> {embed} {description}
@@ -246,7 +245,7 @@ const PreviewCard: React.FC = ({ className={clsx(className, 'cursor-pointer hover:bg-gray-100 hover:no-underline dark:hover:bg-primary-800/30')} target='_blank' rel='noopener' - ref={setRef} + ref={ref as React.RefObject} onClick={e => e.stopPropagation()} > {embed}