summaryrefslogtreecommitdiff
blob: 2a1ab888b1916dc9e745e560833b83d76e229163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * External dependencies
 */
import { chunk, drop, take } from 'lodash';

/**
 * Internal dependencies
 */
import Row from './row';
import Column from './column';
import Gallery from './gallery';
import { MAX_COLUMNS } from '../constants';

export default function Square( { columns, renderedImages } ) {
	const columnCount = Math.min( MAX_COLUMNS, columns );

	const remainder = renderedImages.length % columnCount;

	return (
		<Gallery>
			{ [
				...( remainder ? [ take( renderedImages, remainder ) ] : [] ),
				...chunk( drop( renderedImages, remainder ), columnCount ),
			].map( ( imagesInRow, rowIndex ) => (
				<Row key={ rowIndex } className={ `columns-${ imagesInRow.length }` }>
					{ imagesInRow.map( ( image, colIndex ) => (
						<Column key={ colIndex }>{ image }</Column>
					) ) }
				</Row>
			) ) }
		</Gallery>
	);
}