Skip to content

Commit

Permalink
Merge pull request #308 from benhowell/fix-resize-in-safari
Browse files Browse the repository at this point in the history
Fix horizontal filling after resizing in Arc and Safari
  • Loading branch information
itoldya authored Apr 29, 2024
2 parents d28e501 + deeb07c commit 10bd771
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 84 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-grid-gallery",
"version": "1.0.0",
"version": "1.0.1-alpha.0",
"description": "Justified gallery component for React.",
"types": "dist/react-grid-gallery.d.ts",
"main": "dist/react-grid-gallery.cjs.js",
Expand Down
28 changes: 6 additions & 22 deletions src/Gallery.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect, useCallback, useRef, MouseEvent } from "react";
import { MouseEvent } from "react";
import { Image } from "./Image";
import { ResizeListener } from "./ResizeListener";
import { useContainerWidth } from "./useContainerWidth";
import { buildLayoutFlat } from "./buildLayout";
import { Image as ImageInterface, GalleryProps } from "./types";
import * as styles from "./styles";
Expand All @@ -20,24 +20,9 @@ export const Gallery = <T extends ImageInterface>({
tagStyle,
thumbnailImageComponent,
}: GalleryProps<T>): JSX.Element => {
const galleryRef = useRef(null);

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

const handleResize = useCallback(() => {
if (!galleryRef.current) {
return;
}
let width = galleryRef.current.clientWidth;
try {
width = galleryRef.current.getBoundingClientRect().width;
} catch (err) {}
setContainerWidth(Math.floor(width));
}, []);

useEffect(() => {
handleResize();
}, []);
const { containerRef, containerWidth } = useContainerWidth(
defaultContainerWidth
);

const thumbnails = buildLayoutFlat<T>(images, {
containerWidth,
Expand All @@ -56,8 +41,7 @@ export const Gallery = <T extends ImageInterface>({
};

return (
<div id={id} className="ReactGridGallery" ref={galleryRef}>
<ResizeListener onResize={handleResize} />
<div id={id} className="ReactGridGallery" ref={containerRef}>
<div style={styles.gallery}>
{thumbnails.map((item, index) => (
<Image
Expand Down
59 changes: 0 additions & 59 deletions src/ResizeListener.tsx

This file was deleted.

35 changes: 35 additions & 0 deletions src/useContainerWidth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useRef, useCallback, useState } from "react";

export function useContainerWidth(defaultContainerWidth: number) {
const ref = useRef<HTMLElement | null>(null);
const observerRef = useRef<ResizeObserver>();

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

const containerRef = useCallback((node: HTMLElement | null) => {
observerRef.current?.disconnect();
observerRef.current = undefined;

ref.current = node;

const updateWidth = () => {
if (!ref.current) {
return;
}
let width = ref.current.clientWidth;
try {
width = ref.current.getBoundingClientRect().width;
} catch (err) {}
setContainerWidth(Math.floor(width));
};

updateWidth();

if (node && typeof ResizeObserver !== "undefined") {
observerRef.current = new ResizeObserver(updateWidth);
observerRef.current.observe(node);
}
}, []);

return { containerRef, containerWidth };
}

0 comments on commit 10bd771

Please sign in to comment.