Change gallery to be a grid

This commit is contained in:
niki committed 2026-07-03 15:30:24 +03:00
1 parent ea9a392982
commit 2dd1dc2ba3
5 files changed
+157 -128

No files matched your search

+5 -10
View File
@@ -10,7 +10,7 @@ import { useSetting } from '$state/hooks/settings';
import { settingsAtom, CaptionPosition } from '$state/settings';
import type { HTMLReactParserOptions } from 'html-react-parser';
import type { Opts } from 'linkifyjs';
import { Box, config, toRem } from 'folds';
import { Box, config } from 'folds';
import {
AudioContent,
DownloadFile,
@@ -264,16 +264,11 @@ function RenderMessageContentInternal({
style={{
display: 'flex',
flexDirection: attachmentDirection,
height: '100%',
width: '100%',
}}
>
<div
style={{
// height: '100%',
backgroundColor: '#00AA00',
}}
>
{attachment}
</div>
{attachment}
{renderCaption()}
</div>
);
@@ -283,7 +278,7 @@ function RenderMessageContentInternal({
renderCaptionedAttachment(
<MFile
content={content as Record<string, never> & { msgtype: MsgType.File }}
fitParent={isGallery}
//fitParent={isGallery}
renderFileContent={({ body, mimeType, info, encInfo, url }) => (
<FileContent
body={body}
+1 -1
View File
@@ -4,7 +4,7 @@ import { DefaultReset } from 'folds';
export const Image = style([
DefaultReset,
{
objectFit: 'contain',
objectFit: 'cover',
width: '100%',
height: '100%',
},
+42 -7
View File
@@ -6,18 +6,53 @@ export const GalleryHolder = style({
marginTop: config.space.S200,
});
export const GalleryImageGrid = recipe({
base: [
DefaultReset,
{
display: 'grid',
gap: '0.5rem',
maxWidth: toRem(600),
height: '100%',
width: '100%',
gridAutoColumns: toRem(100),
},
],
variants: {
type: {
ThreeItems: {
gridTemplateColumns: '1fr 1fr',
gridTemplateRows: `repeat(2, ${toRem(200)})`,
},
ThreeByThree: {
gridTemplateColumns: 'repeat(3,minmax(0,1fr))',
},
TwoByTwo: {
gridTemplateColumns: 'repeat(2,minmax(0,1fr))',
},
OneByOne: {
maxHeight: toRem(300),
gridTemplateColumns: '1fr',
},
},
},
});
export const GalleryItem = recipe({
base: [
DefaultReset,
{
display: 'flex',
maxWidth: toRem(450),
flexShrink: 0,
flexGrow: 1,
overflow: 'hidden',
borderRadius: config.radii.R300,
alignSelf: 'stretch',
backgroundColor: '#AA00AA',
minHeight: toRem(175),
minWidth: toRem(175),
width: '100%',
height: '100%',
aspectRatio: '1/1',
selectors: {
[`${GalleryImageGrid.classNames.variants.type.ThreeItems} &:nth-child(1)`]: {
gridRow: 'span 2',
},
},
},
],
});
+105 -107
View File
@@ -1,12 +1,12 @@
import type { ReactNode } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { Box, Icon, IconButton, Icons, Scroll } from 'folds';
import { Box } from 'folds';
import { MsgType, type IContent } from 'matrix-js-sdk';
import type { IGalleryContent, IGalleryItem } from '$types/matrix/common';
import {
getIntersectionObserverEntry,
useIntersectionObserver,
} from '$hooks/useIntersectionObserver';
import type {
IGalleryContent,
IGalleryImageItem,
IGalleryItem,
IGalleryVideoItem,
} from '$types/matrix/common';
import * as css from './MGallery.css';
function galleryItemToContent(item: IGalleryItem): IContent {
@@ -20,113 +20,111 @@ type MGalleryProps = {
renderCaption?: () => ReactNode;
};
type PartitionedMediaItem = {
items: (IGalleryImageItem | IGalleryVideoItem)[];
type: 'ThreeByThree' | 'TwoByTwo' | 'OneByOne' | 'ThreeItems';
};
export function MGallery({ content, renderItem, renderCaption }: MGalleryProps) {
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 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',
}),
[]
)
);
useEffect(() => {
const backAnchor = backAnchorRef.current;
const frontAnchor = frontAnchorRef.current;
if (backAnchor) intersectionObserver?.observe(backAnchor);
if (frontAnchor) intersectionObserver?.observe(frontAnchor);
return () => {
if (backAnchor) intersectionObserver?.unobserve(backAnchor);
if (frontAnchor) intersectionObserver?.unobserve(frontAnchor);
};
}, [intersectionObserver]);
const handleScrollBack = () => {
const scroll = scrollRef.current;
if (!scroll) return;
const { offsetWidth, scrollLeft } = scroll;
scroll.scrollTo({
left: scrollLeft - offsetWidth / 1.3,
behavior: 'smooth',
});
};
const handleScrollFront = () => {
const scroll = scrollRef.current;
if (!scroll) return;
const { offsetWidth, scrollLeft } = scroll;
scroll.scrollTo({
left: scrollLeft + offsetWidth / 1.3,
behavior: 'smooth',
});
};
const items = content.itemtypes;
let mediaItems = items.filter(
(item) => item.itemtype == MsgType.Video || item.itemtype == MsgType.Image
);
const columnItems = items.filter(
(item) => item.itemtype == MsgType.File || item.itemtype == MsgType.Audio
);
let partitionedMediaItems: PartitionedMediaItem[] = [];
while (mediaItems.length > 0) {
if (mediaItems.length >= 9) {
partitionedMediaItems.unshift({
items: mediaItems.slice(-9),
type: 'ThreeByThree',
});
mediaItems = mediaItems.slice(0, mediaItems.length - 9);
continue;
}
if (mediaItems.length >= 6) {
partitionedMediaItems.unshift({
items: mediaItems.slice(-6),
type: 'ThreeByThree',
});
mediaItems = mediaItems.slice(0, mediaItems.length - 6);
continue;
}
if (mediaItems.length >= 4) {
partitionedMediaItems.unshift({
items: mediaItems.slice(-4),
type: 'TwoByTwo',
});
mediaItems = mediaItems.slice(0, mediaItems.length - 4);
continue;
}
if (mediaItems.length > 3) {
partitionedMediaItems.unshift({
items: mediaItems.slice(-3),
type: 'ThreeByThree',
});
mediaItems = mediaItems.slice(0, mediaItems.length - 3);
continue;
}
if (mediaItems.length == 3) {
partitionedMediaItems.unshift({
items: mediaItems.slice(-3),
type: 'ThreeItems',
});
mediaItems = mediaItems.slice(0, mediaItems.length - 3);
continue;
}
if (mediaItems.length >= 2) {
partitionedMediaItems.unshift({
items: mediaItems.slice(-2),
type: 'TwoByTwo',
});
mediaItems = mediaItems.slice(0, mediaItems.length - 2);
continue;
}
if (mediaItems.length >= 1) {
partitionedMediaItems.unshift({
items: mediaItems.slice(-1),
type: 'OneByOne',
});
mediaItems = mediaItems.slice(0, mediaItems.length - 1);
continue;
}
}
return (
<Box direction="Column">
<Box className={css.GalleryHolder} direction="Column" style={{ position: 'relative' }}>
<Scroll ref={scrollRef} direction="Horizontal" size="0" visibility="Hover" hideTrack>
<Box shrink="No" alignItems="Center">
<div ref={backAnchorRef} />
{!backVisible && (
<>
<div className={css.GalleryHolderGradient({ position: 'Left' })} />
<IconButton
className={css.GalleryHolderBtn({ position: 'Left' })}
variant="Secondary"
radii="Pill"
size="300"
outlined
onClick={handleScrollBack}
<Box display="Flex" direction="Column">
<Box display="Flex" direction="Column" gap="400">
<Box direction="Column" gap="200">
{partitionedMediaItems.map((item) => (
<div
key={crypto.randomUUID()}
className={css.GalleryImageGrid({
type: item.type,
})}
>
{item.items.map((mediaItem, index) => (
<div
key={mediaItem.url ?? mediaItem.file?.url ?? index}
className={css.GalleryItem()}
>
<Icon size="300" src={Icons.ArrowLeft} />
</IconButton>
</>
)}
<Box alignItems="Inherit" gap="200">
{items.map((item, index) => (
<div key={item.url ?? item.file?.url ?? index} className={css.GalleryItem()}>
{renderItem(galleryItemToContent(item), index)}
{renderItem(galleryItemToContent(mediaItem), index)}
</div>
))}
{!frontVisible && (
<>
<div className={css.GalleryHolderGradient({ position: 'Right' })} />
<IconButton
className={css.GalleryHolderBtn({ position: 'Right' })}
variant="Primary"
radii="Pill"
size="300"
outlined
onClick={handleScrollFront}
>
<Icon size="300" src={Icons.ArrowRight} />
</IconButton>
</>
)}
<div ref={frontAnchorRef} />
</Box>
</Box>
</Scroll>
</div>
))}
</Box>
<Box alignItems="Start" gap="200" direction="Column">
{columnItems.map((item, index) => (
<div key={item.url ?? item.file?.url ?? index}>
{renderItem(galleryItemToContent(item), index)}
</div>
))}
</Box>
</Box>
{renderCaption?.()}
</Box>
@@ -454,15 +454,15 @@ export function MImage({ content, renderImageContent, outlined, fitParent }: MIm
flexGrow: 1,
flexShrink: 0,
width: fitParent ? '100%' : toRem(displayWidth),
height: fitParent ? MAX_SIZE : 'auto',
height: fitParent ? '100%' : 'auto',
}}
outlined={outlined}
>
<AttachmentBox
style={{
flexGrow: 1,
aspectRatio,
width: fitParent ? 'auto' : toRem(displayWidth),
aspectRatio: fitParent ? undefined : aspectRatio,
width: fitParent ? '100%' : toRem(displayWidth),
height: fitParent ? '100%' : toRem(height < 48 ? 48 : height),
}}
>
@@ -545,6 +545,7 @@ export function MVideo({
</AttachmentHeader>
<AttachmentBox
style={{
flexGrow: 1,
width: fitParent ? 'auto' : toRem(displayWidth),
height: fitParent ? '100%' : toRem(height < 48 ? 48 : height),
}}