Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-url-preview-scroll-arrows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

Fix URL preview scroll arrows appearing when there is no content to scroll
68 changes: 26 additions & 42 deletions src/app/components/url-preview/UrlPreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import { Box, Icon, IconButton, Icons, Scroll, Spinner, Text, as, color, config } from 'folds';
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { useMatrixClient } from '$hooks/useMatrixClient';
import {
getIntersectionObserverEntry,
useIntersectionObserver,
} from '$hooks/useIntersectionObserver';
import { mxcUrlToHttp, downloadMedia } from '$utils/matrix';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import * as css from './UrlPreviewCard.css';
Expand All @@ -19,7 +15,7 @@

const openMediaInNewTab = async (url: string | undefined) => {
if (!url) {
console.warn('Attempted to open an empty url');

Check warning on line 18 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
const blob = await downloadMedia(url);
Expand Down Expand Up @@ -62,14 +58,14 @@
);
const handleAuxClick = (ev: React.MouseEvent) => {
if (!prev['og:image']) {
console.warn('No image');

Check warning on line 61 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
if (ev.button === 1) {
ev.preventDefault();
const mxcUrl = mxcUrlToHttp(mx, prev['og:image'], /* useAuthentication */ true);
if (!mxcUrl) {
console.error('Error converting mxc:// url.');

Check warning on line 68 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
openMediaInNewTab(mxcUrl);
Expand Down Expand Up @@ -242,43 +238,34 @@

export const UrlPreviewHolder = as<'div'>(({ children, ...props }, ref) => {
const scrollRef = useRef<HTMLDivElement>(null);
const backAnchorRef = useRef<HTMLDivElement>(null);
const frontAnchorRef = useRef<HTMLDivElement>(null);
const [backVisible, setBackVisible] = useState(true);
const [frontVisible, setFrontVisible] = useState(true);
const innerBoxRef = useRef<HTMLDivElement>(null);
const [canScrollLeft, setCanScrollLeft] = useState(false);
const [canScrollRight, setCanScrollRight] = useState(false);

const intersectionObserver = useIntersectionObserver(
useCallback((entries) => {
const backAnchor = backAnchorRef.current;
const frontAnchor = frontAnchorRef.current;
const backEntry = backAnchor && getIntersectionObserverEntry(backAnchor, entries);
const frontEntry = frontAnchor && getIntersectionObserverEntry(frontAnchor, entries);
if (backEntry) {
setBackVisible(backEntry.isIntersecting);
}
if (frontEntry) {
setFrontVisible(frontEntry.isIntersecting);
}
}, []),
useCallback(
() => ({
root: scrollRef.current,
rootMargin: '10px',
}),
[]
)
);
const updateArrows = useCallback(() => {
const scroll = scrollRef.current;
if (!scroll) return;
const { scrollLeft, scrollWidth, clientWidth } = scroll;
setCanScrollLeft(scrollLeft > 1);
setCanScrollRight(scrollLeft + clientWidth < scrollWidth - 1);
}, []);

useEffect(() => {
const backAnchor = backAnchorRef.current;
const frontAnchor = frontAnchorRef.current;
if (backAnchor) intersectionObserver?.observe(backAnchor);
if (frontAnchor) intersectionObserver?.observe(frontAnchor);
const scroll = scrollRef.current;
if (!scroll) return undefined;

updateArrows();
scroll.addEventListener('scroll', updateArrows, { passive: true });

const resizeObserver = new ResizeObserver(updateArrows);
resizeObserver.observe(scroll);
if (innerBoxRef.current) resizeObserver.observe(innerBoxRef.current);

return () => {
if (backAnchor) intersectionObserver?.unobserve(backAnchor);
if (frontAnchor) intersectionObserver?.unobserve(frontAnchor);
scroll.removeEventListener('scroll', updateArrows);
resizeObserver.disconnect();
};
}, [intersectionObserver]);
}, [updateArrows]);

const handleScrollBack = () => {
const scroll = scrollRef.current;
Expand Down Expand Up @@ -308,8 +295,7 @@
>
<Scroll ref={scrollRef} direction="Horizontal" size="0" visibility="Hover" hideTrack>
<Box shrink="No" alignItems="Center">
<div ref={backAnchorRef} />
{!backVisible && (
{canScrollLeft && (
<>
<div className={css.UrlPreviewHolderGradient({ position: 'Left' })} />
<IconButton
Expand All @@ -324,10 +310,9 @@
</IconButton>
</>
)}
<Box alignItems="Inherit" gap="200">
<Box ref={innerBoxRef} alignItems="Inherit" gap="200">
{children}

{!frontVisible && (
{canScrollRight && (
<>
<div className={css.UrlPreviewHolderGradient({ position: 'Right' })} />
<IconButton
Expand All @@ -342,7 +327,6 @@
</IconButton>
</>
)}
<div ref={frontAnchorRef} />
</Box>
</Box>
</Scroll>
Expand Down
Loading