clean up masonry algorithm

This commit is contained in:
Chuck Dries 2022-06-28 21:17:00 -07:00
parent 5ada3c3b0d
commit ad21989140
No known key found for this signature in database
GPG Key ID: A00B7AEAE1DC5BE6

View File

@ -43,11 +43,10 @@ const MasonryGallery = ({
[images] [images]
); );
const rowsByBreakpoint = React.useMemo( const targetAspect = aspectTargetsByBreakpoint[breakpoint];
const rows = React.useMemo(
() => () =>
R.map(
R.pipe( R.pipe(
(targetAspect) =>
R.reduce( R.reduce(
(acc, currentAspect) => { (acc, currentAspect) => {
const currentRow = acc.pop(); const currentRow = acc.pop();
@ -76,17 +75,13 @@ const MasonryGallery = ({
}, },
]; ];
}, },
[{ aspect: 0, startIndex: 0, images: 0 }], [{ aspect: 0, startIndex: 0, images: 0 }]
aspectRatios
), ),
R.indexBy(R.prop("startIndex")) R.indexBy(R.prop("startIndex"))
) )(aspectRatios),
)(aspectTargetsByBreakpoint), [aspectRatios, targetAspect]
[aspectRatios, aspectTargetsByBreakpoint]
); );
const rows = rowsByBreakpoint[breakpoint];
let cursor = 0; let cursor = 0;
return ( return (
<div <div
@ -104,10 +99,11 @@ const MasonryGallery = ({
const rowAspectRatioSum = currentRow.aspect; const rowAspectRatioSum = currentRow.aspect;
const ar = getAspectRatio(image); const ar = getAspectRatio(image);
let width; let width;
let height = `calc(100vw / ${rowAspectRatioSum} - 10px)` let height = `calc(100vw / ${rowAspectRatioSum} - 10px)`;
if (ar === rowAspectRatioSum) { if (rowAspectRatioSum < targetAspect / 2) {
width = `calc(300px * ${ar})`; // incomplete row, render stuff at "ideal" sizes instead of filling width
height = 'unset' width = `calc(100vw / ${targetAspect / ar})`;
height = "unset";
} else { } else {
const widthNumber = ((ar / rowAspectRatioSum) * 100).toFixed(7); const widthNumber = ((ar / rowAspectRatioSum) * 100).toFixed(7);
width = `${widthNumber}%`; width = `${widthNumber}%`;