Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"sideEffects": false,
"scripts": {
"prepublishOnly": "npm run build",
"clean": "rimraf dist",
"clean": "rimraf dist && rimraf node_modules/.cache",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rollup was not seeing my changes to types.ts and I had to manually delete this directory to fix it. Figured it'd be good to automate

"rollup:build": "rollup -c",
"watch": "rollup -c -w",
"build": "npm run clean && npm run rollup:build",
Expand Down
20 changes: 8 additions & 12 deletions src/CheckButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { useState } from "react";
import * as styles from "./styles";
import { CheckButtonProps } from "./types";

export const CheckButton = (props: CheckButtonProps): JSX.Element => {
const { isSelected, isVisible, onClick } = props;
const { selectedColor, hoverColor, color } = props;

export const CheckButton = ({
isSelected = false,
isVisible = true,
onClick,
color = "#FFFFFFB2",
selectedColor = "#4285F4FF",
hoverColor = "#FFFFFFFF",
}: CheckButtonProps): JSX.Element => {
const [hover, setHover] = useState(false);

const circleStyle = { display: isSelected ? "block" : "none" };
Expand Down Expand Up @@ -57,11 +61,3 @@ export const CheckButton = (props: CheckButtonProps): JSX.Element => {
</div>
);
};

CheckButton.defaultProps = {
isSelected: false,
isVisible: true,
color: "#FFFFFFB2",
selectedColor: "#4285F4FF",
hoverColor: "#FFFFFFFF",
};
45 changes: 22 additions & 23 deletions src/Gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import { buildLayoutFlat } from "./buildLayout";
import { Image as ImageInterface, GalleryProps } from "./types";
import * as styles from "./styles";

export const Gallery = <T extends ImageInterface>(
props: GalleryProps<T>
): JSX.Element => {
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 galleryRef = useRef(null);

const { maxRows, rowHeight, margin, enableImageSelection } = props;
const { defaultContainerWidth, images } = props;

const [containerWidth, setContainerWidth] = useState(defaultContainerWidth);

const handleResize = useCallback(() => {
Expand All @@ -39,15 +48,15 @@ export const Gallery = <T extends ImageInterface>(

const handleSelect = (index: number, event: MouseEvent<HTMLElement>) => {
event.preventDefault();
props.onSelect(index, images[index], event);
onSelect(index, images[index], event);
};

const handleClick = (index: number, event: MouseEvent<HTMLElement>) => {
props.onClick(index, images[index], event);
onClick(index, images[index], event);
};

return (
<div id={props.id} className="ReactGridGallery" ref={galleryRef}>
<div id={id} className="ReactGridGallery" ref={galleryRef}>
<ResizeListener onResize={handleResize} />
<div style={styles.gallery}>
{thumbnails.map((item, index) => (
Expand All @@ -60,10 +69,10 @@ export const Gallery = <T extends ImageInterface>(
isSelectable={enableImageSelection}
onClick={handleClick}
onSelect={handleSelect}
tagStyle={props.tagStyle}
tileViewportStyle={props.tileViewportStyle}
thumbnailStyle={props.thumbnailStyle}
thumbnailImageComponent={props.thumbnailImageComponent}
tagStyle={tagStyle}
tileViewportStyle={tileViewportStyle}
thumbnailStyle={thumbnailStyle}
thumbnailImageComponent={thumbnailImageComponent}
/>
))}
</div>
Expand All @@ -72,13 +81,3 @@ export const Gallery = <T extends ImageInterface>(
};

Gallery.displayName = "Gallery";

Gallery.defaultProps = {
id: "ReactGridGallery",
enableImageSelection: true,
rowHeight: 180,
margin: 2,
defaultContainerWidth: 0,
onClick: () => {},
onSelect: () => {},
};
63 changes: 38 additions & 25 deletions src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,52 @@ import { ImageExtended, ImageProps } from "./types";
import * as styles from "./styles";
import { getStyle } from "./styles";

export const Image = <T extends ImageExtended>(
props: ImageProps<T>
): JSX.Element => {
const { item, thumbnailImageComponent: ThumbnailImageComponent } = props;
export const Image = <T extends ImageExtended>({
item,
thumbnailImageComponent: ThumbnailImageComponent,
isSelectable = true,
thumbnailStyle,
tagStyle,
tileViewportStyle,
margin,
index,
onSelect,
onClick,
}: ImageProps<T>): JSX.Element => {
const styleContext = { item };

const [hover, setHover] = useState(false);

const thumbnailProps = {
key: props.index,
key: index,
"data-testid": "grid-gallery-item_thumbnail",
src: item.src,
alt: item.alt ? item.alt : "",
title: typeof item.caption === "string" ? item.caption : null,
style: getStyle(props.thumbnailStyle, styles.thumbnail, styleContext),
style: getStyle(thumbnailStyle, styles.thumbnail, styleContext),
};

const handleCheckButtonClick = (event: MouseEvent<HTMLElement>) => {
if (!props.isSelectable) {
if (!isSelectable) {
return;
}
props.onSelect(props.index, event);
onSelect(index, event);
};

const handleViewportClick = (event: MouseEvent<HTMLElement>) => {
props.onClick(props.index, event);
onClick(index, event);
};

const thumbnailImageProps = {
item,
index,
margin,
onSelect,
onClick,
isSelectable,
tileViewportStyle,
thumbnailStyle,
tagStyle,
};

return (
Expand All @@ -38,15 +58,15 @@ export const Image = <T extends ImageExtended>(
data-testid="grid-gallery-item"
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
style={styles.galleryItem({ margin: props.margin })}
style={styles.galleryItem({ margin })}
>
<div
className="ReactGridGallery_tile-icon-bar"
style={styles.tileIconBar}
>
<CheckButton
isSelected={item.isSelected}
isVisible={item.isSelected || (props.isSelectable && hover)}
isVisible={item.isSelected || (isSelectable && hover)}
onClick={handleCheckButtonClick}
/>
</div>
Expand All @@ -62,9 +82,7 @@ export const Image = <T extends ImageExtended>(
title={tag.title}
style={styles.tagItemBlock}
>
<span
style={getStyle(props.tagStyle, styles.tagItem, styleContext)}
>
<span style={getStyle(tagStyle, styles.tagItem, styleContext)}>
{tag.value}
</span>
</div>
Expand All @@ -84,22 +102,21 @@ export const Image = <T extends ImageExtended>(
<div
className="ReactGridGallery_tile-overlay"
style={styles.tileOverlay({
showOverlay: hover && !item.isSelected && props.isSelectable,
showOverlay: hover && !item.isSelected && isSelectable,
})}
/>

<div
className="ReactGridGallery_tile-viewport"
data-testid="grid-gallery-item_viewport"
style={getStyle(
props.tileViewportStyle,
styles.tileViewport,
styleContext
)}
style={getStyle(tileViewportStyle, styles.tileViewport, styleContext)}
onClick={handleViewportClick}
>
{ThumbnailImageComponent ? (
<ThumbnailImageComponent {...props} imageProps={thumbnailProps} />
<ThumbnailImageComponent
{...thumbnailImageProps}
imageProps={thumbnailProps}
/>
) : (
<img {...thumbnailProps} />
)}
Expand All @@ -115,7 +132,3 @@ export const Image = <T extends ImageExtended>(
</div>
);
};

Image.defaultProps = {
isSelectable: true,
};
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ export interface ImageProps<T extends ImageExtended = ImageExtended> {
item: T;
index: number;
margin: number;
height: number;
isSelectable: boolean;
onClick: (index: number, event: MouseEvent<HTMLElement>) => void;
onSelect: (index: number, event: MouseEvent<HTMLElement>) => void;
tileViewportStyle: StyleProp<T>;
thumbnailStyle: StyleProp<T>;
tagStyle: StyleProp<T>;
thumbnailImageComponent: ComponentType<ThumbnailImageProps>;
height?: number;
thumbnailImageComponent?: ComponentType<ThumbnailImageProps>;
}

export interface ThumbnailImageComponentImageProps {
Expand Down Expand Up @@ -104,7 +104,7 @@ export interface CheckButtonProps {
isSelected: boolean;
isVisible: boolean;
onClick: (event: MouseEvent<HTMLElement>) => void;
color: string;
selectedColor: string;
hoverColor: string;
color?: string;
selectedColor?: string;
hoverColor?: string;
}