Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix horizontal filling after resizing in Arc and Safari #308

Merged
merged 2 commits into from
Apr 29, 2024
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
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 };
}
Loading