-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathGallery.tsx
More file actions
executable file
·67 lines (61 loc) · 1.79 KB
/
Gallery.tsx
File metadata and controls
executable file
·67 lines (61 loc) · 1.79 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { MouseEvent } from "react";
import { Image } from "./Image";
import { useContainerWidth } from "./useContainerWidth";
import { buildLayoutFlat } from "./buildLayout";
import { Image as ImageInterface, GalleryProps } from "./types";
import * as styles from "./styles";
export const Gallery = <T extends ImageInterface>({
images,
id = "ReactGridGallery",
enableImageSelection = true,
onSelect = () => {},
rowHeight = 180,
maxRows,
margin = 2,
defaultContainerWidth = 0,
onClick = () => {},
tileViewportStyle,
thumbnailStyle,
tagStyle,
thumbnailImageComponent,
}: GalleryProps<T>): JSX.Element => {
const { containerRef, containerWidth } = useContainerWidth(
defaultContainerWidth
);
const thumbnails = buildLayoutFlat<T>(images, {
containerWidth,
maxRows,
rowHeight,
margin,
});
const handleSelect = (index: number, event: MouseEvent<HTMLElement>) => {
event.preventDefault();
onSelect(index, images[index], event);
};
const handleClick = (index: number, event: MouseEvent<HTMLElement>) => {
onClick(index, images[index], event);
};
return (
<div id={id} className="ReactGridGallery" ref={containerRef}>
<div style={styles.gallery}>
{thumbnails.map((item, index) => (
<Image
key={item.key || index}
item={item}
index={index}
margin={margin}
height={rowHeight}
isSelectable={enableImageSelection}
onClick={handleClick}
onSelect={handleSelect}
tagStyle={tagStyle}
tileViewportStyle={tileViewportStyle}
thumbnailStyle={thumbnailStyle}
thumbnailImageComponent={thumbnailImageComponent}
/>
))}
</div>
</div>
);
};
Gallery.displayName = "Gallery";