kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
PreviewCard: set the ref differently, don't set the same ref on two different elements
rodzic
70a9886410
commit
c884b9b963
|
@ -4,7 +4,7 @@ import playerPlayIcon from '@tabler/icons/outline/player-play.svg';
|
||||||
import zoomInIcon from '@tabler/icons/outline/zoom-in.svg';
|
import zoomInIcon from '@tabler/icons/outline/zoom-in.svg';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { List as ImmutableList } from 'immutable';
|
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 Blurhash from 'soapbox/components/blurhash.tsx';
|
||||||
import HStack from 'soapbox/components/ui/hstack.tsx';
|
import HStack from 'soapbox/components/ui/hstack.tsx';
|
||||||
|
@ -40,6 +40,8 @@ const PreviewCard: React.FC<IPreviewCard> = ({
|
||||||
onOpenMedia,
|
onOpenMedia,
|
||||||
horizontal,
|
horizontal,
|
||||||
}): JSX.Element => {
|
}): JSX.Element => {
|
||||||
|
const ref = useRef<HTMLElement>(null);
|
||||||
|
|
||||||
const [width, setWidth] = useState(defaultWidth);
|
const [width, setWidth] = useState(defaultWidth);
|
||||||
const [embedded, setEmbedded] = useState(false);
|
const [embedded, setEmbedded] = useState(false);
|
||||||
|
|
||||||
|
@ -52,6 +54,14 @@ const PreviewCard: React.FC<IPreviewCard> = ({
|
||||||
const trimmedTitle = trim(card.title, maxTitle);
|
const trimmedTitle = trim(card.title, maxTitle);
|
||||||
const trimmedDescription = trim(card.description, maxDescription);
|
const trimmedDescription = trim(card.description, maxDescription);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (ref.current) {
|
||||||
|
const { offsetWidth } = ref.current;
|
||||||
|
cacheWidth?.(offsetWidth);
|
||||||
|
setWidth(offsetWidth);
|
||||||
|
}
|
||||||
|
}, [ref.current]);
|
||||||
|
|
||||||
const handlePhotoClick = () => {
|
const handlePhotoClick = () => {
|
||||||
const attachment = normalizeAttachment({
|
const attachment = normalizeAttachment({
|
||||||
type: 'image',
|
type: 'image',
|
||||||
|
@ -78,16 +88,6 @@ const PreviewCard: React.FC<IPreviewCard> = ({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const setRef: React.RefCallback<HTMLElement> = c => {
|
|
||||||
if (c) {
|
|
||||||
if (cacheWidth) {
|
|
||||||
cacheWidth(c.offsetWidth);
|
|
||||||
}
|
|
||||||
|
|
||||||
setWidth(c.offsetWidth);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const renderVideo = () => {
|
const renderVideo = () => {
|
||||||
const content = { __html: addAutoPlay(card.html) };
|
const content = { __html: addAutoPlay(card.html) };
|
||||||
const ratio = getRatio(card);
|
const ratio = getRatio(card);
|
||||||
|
@ -95,7 +95,6 @@ const PreviewCard: React.FC<IPreviewCard> = ({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={setRef}
|
|
||||||
className='relative w-full flex-none overflow-hidden'
|
className='relative w-full flex-none overflow-hidden'
|
||||||
dangerouslySetInnerHTML={content}
|
dangerouslySetInnerHTML={content}
|
||||||
style={{ height }}
|
style={{ height }}
|
||||||
|
@ -218,7 +217,7 @@ const PreviewCard: React.FC<IPreviewCard> = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={className} ref={setRef}>
|
<div className={className} ref={ref as React.RefObject<HTMLDivElement>}>
|
||||||
{embed}
|
{embed}
|
||||||
{description}
|
{description}
|
||||||
</div>
|
</div>
|
||||||
|
@ -246,7 +245,7 @@ const PreviewCard: React.FC<IPreviewCard> = ({
|
||||||
className={clsx(className, 'cursor-pointer hover:bg-gray-100 hover:no-underline dark:hover:bg-primary-800/30')}
|
className={clsx(className, 'cursor-pointer hover:bg-gray-100 hover:no-underline dark:hover:bg-primary-800/30')}
|
||||||
target='_blank'
|
target='_blank'
|
||||||
rel='noopener'
|
rel='noopener'
|
||||||
ref={setRef}
|
ref={ref as React.RefObject<HTMLAnchorElement>}
|
||||||
onClick={e => e.stopPropagation()}
|
onClick={e => e.stopPropagation()}
|
||||||
>
|
>
|
||||||
{embed}
|
{embed}
|
||||||
|
|
Ładowanie…
Reference in New Issue