/* eslint-disable @typescript-eslint/no-explicit-any */ import React, { useEffect, useMemo, useRef, useState } from 'react' import useSize from '~/code/hook/useSize' import shuffleArray from '~/code/utility/shuffleArray' import C from '~/code/classes' type ItemType = { element: React.ReactNode; h: number; w: number } type MasonryPropsType = { className?: string columnBreakpoints: { [key: number]: number default: number } // required, string // Custom attributes, however it is advised against // using these to prevent unintended issues and future conflicts // ...any other attribute, will be added to the container columnClassName?: string gap?: number // optional, string items: Array } export function Column({ children, gap, className }: ColumnProps) { const list = [C.flex, C.column, C.width100P, className].filter(x => x) return (
{children}
) } export type ColumnProps = { children: React.ReactNode className?: string gap: number } export const Grid = React.forwardRef(GridBase) export function GridBase( { children, gap, className }: GridProps, ref: React.ForwardedRef, ) { const list = [C.flex, C.width100P, className].filter(x => x) return (
{children}
) } export type GridProps = { children: React.ReactNode className?: string gap: number } // Sort the images by aspect ratio descending. function aspectRatioDescending(a: ItemType, b: ItemType) { return b.h / b.w - a.h / a.w } export default function Masonry({ columnBreakpoints, columnClassName, items, className, gap = 0, }: MasonryPropsType) { const [columnCount, setColumnCount] = useState( columnBreakpoints.default, ) const grid = useRef(null) const size = useSize(grid) useEffect(() => { if (!size) { return } let matchedBreakpoint = Infinity let newColumnCount = columnBreakpoints.default for (let breakpoint in columnBreakpoints) { const optBreakpoint = parseInt(breakpoint) const isCurrentBreakpoint = optBreakpoint > 0 && size.width <= optBreakpoint if (isCurrentBreakpoint && optBreakpoint < matchedBreakpoint) { matchedBreakpoint = optBreakpoint newColumnCount = columnBreakpoints[breakpoint] } } newColumnCount = Math.max(1, newColumnCount ?? 1) setColumnCount(newColumnCount) // eslint-disable-next-line react-hooks/exhaustive-deps }, [columnCount, size?.width, columnBreakpoints]) const columns: Array> = useMemo(() => { if (!size) { return [] } const currentColumnCount = columnCount const columns: Array> = [] const columnHeights: Array = new Array( currentColumnCount, ).fill(0) const thumbnailWidth = (size.width - gap) / currentColumnCount - gap items.sort(aspectRatioDescending) // Assign each image to a column. for (const item of items) { // Find the shortest column. let shortest = 0 for (let j = 1; j < currentColumnCount; ++j) { if (columnHeights[j] < columnHeights[shortest]) { shortest = j } } // Put the image there. columnHeights[shortest] += gap + thumbnailWidth * (item.h / item.w) columns[shortest] = columns[shortest] ?? [] columns[shortest].push(item.element) } columns.forEach(column => shuffleArray(column)) shuffleArray(columns) return columns // eslint-disable-next-line react-hooks/exhaustive-deps }, [columnCount, items, gap, size?.width]) return ( {columns.map((elements, i) => ( {elements} ))} ) }