Skip to content

Commit

Permalink
fix(ui): use updated algo to calc images per row in gallery for hotke…
Browse files Browse the repository at this point in the history
…y nav
  • Loading branch information
psychedelicious committed Sep 19, 2024
1 parent f8f6fb9 commit 13db18f
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,36 @@ import type { ImageDTO } from 'services/api/types';
* Gets the number of images per row in the gallery by grabbing their DOM elements.
*/
const getImagesPerRow = (): number => {
const widthOfGalleryImage =
document.querySelector(`.${GALLERY_IMAGE_CLASS_NAME}`)?.getBoundingClientRect().width ?? 1;
const imageEl = document.querySelector(`.${GALLERY_IMAGE_CLASS_NAME}`);
const gridEl = document.querySelector(`.${GALLERY_GRID_CLASS_NAME}`);

const widthOfGalleryGrid = document.querySelector(`.${GALLERY_GRID_CLASS_NAME}`)?.getBoundingClientRect().width ?? 0;
if (!imageEl || !gridEl) {
return 0;
}
const container = gridEl.parentElement;
if (!container) {
return 0;
}

const imageRect = imageEl.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();

// We need to account for the gap between images
const gridElStyle = window.getComputedStyle(gridEl);
const gap = parseFloat(gridElStyle.gap);

const imagesPerRow = Math.round(widthOfGalleryGrid / widthOfGalleryImage);
let imagesPerRow = 0;
let spaceUsed = 0;

// Floating point precision can cause imagesPerRow to be 1 too small. Adding 1px to the container size fixes
// this, without the possibility of accidentally adding an extra column.
while (spaceUsed + imageRect.width <= containerRect.width + 1) {
imagesPerRow++; // Increment the number of images
spaceUsed += imageRect.width; // Add image size to the used space
if (spaceUsed + gap <= containerRect.width) {
spaceUsed += gap; // Add gap size to the used space after each image except after the last image
}
}

return imagesPerRow;
};
Expand Down

0 comments on commit 13db18f

Please sign in to comment.