Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Gallery changes. Smooth scroll, small button changes, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sol25 authored and Sol25 committed Oct 12, 2023
1 parent 26d17ae commit b43e5ef
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 43 deletions.
86 changes: 57 additions & 29 deletions frontend/src/Components/RoadDetails/ImageGallery.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useRef, useEffect, useState } from 'react';

interface Image {
id: number;
Expand Down Expand Up @@ -70,28 +70,52 @@ const images: Image[] = [

const ImageGallery: React.FC = () => {
const [scrollPosition, setScrollPosition] = useState(0);
const containerRef = useRef<HTMLDivElement>(null);
const [showArrows, setShowArrows] = useState(true);

const openImageInNewTab = (imageUrl: string) => {
window.open(imageUrl, '_blank');
};

const handleScroll = (scrollOffset: number) => {
const newScrollPosition = scrollPosition + scrollOffset;
if (!containerRef.current) return;

// Calculate the maximum scroll limit based on the total image width
const maxScrollLimit = (images.length - 11.4) * 124; // 120 is the width of each image
const containerWidth = containerRef.current.clientWidth;
const totalImageWidth = images.length * 124; // 120 for the image width and 4 for margin

// Ensure the scroll stays within bounds
const boundedScrollPosition = Math.max(
0,
Math.min(newScrollPosition, maxScrollLimit),
);
const centerOffset = (containerWidth - totalImageWidth) / 2;

setScrollPosition(boundedScrollPosition);
let newScrollPosition = scrollPosition + scrollOffset;

// If scrolling to the left, don't go past half the container's width
if (scrollOffset < 0) {
newScrollPosition = Math.max(newScrollPosition, centerOffset);
}
// If scrolling to the right, don't go past the width of the images minus half the container's width
else {
newScrollPosition = Math.min(
newScrollPosition,
totalImageWidth - containerWidth + centerOffset,
);
}

setScrollPosition(newScrollPosition);
};

useEffect(() => {
if (containerRef.current) {
const containerWidth = containerRef.current.clientWidth;
const totalImageWidth = images.length * 124;
if (totalImageWidth <= containerWidth) {
setShowArrows(false);
} else {
setShowArrows(true);
}
}
}, []);

return (
<div className="image-gallery-container">
<div className="image-gallery-container" ref={containerRef}>
<div className="image-gallery-page">
<div
className="image-container"
Expand All @@ -108,24 +132,28 @@ const ImageGallery: React.FC = () => {
))}
</div>
</div>
<button
className={`arrow-button left-arrow ${
scrollPosition === 0 ? 'disabled' : ''
}`}
onClick={() => handleScroll(-120)}
disabled={scrollPosition === 0}
>
&#9658;
</button>
<button
className={`arrow-button right-arrow ${
scrollPosition === (images.length - 1) * 124 ? 'disabled' : ''
}`}
onClick={() => handleScroll(120)}
disabled={scrollPosition === (images.length - 1) * 124}
>
&#9658;
</button>
{showArrows && (
<>
<button
className={`arrow-button left-arrow ${
scrollPosition === 0 ? 'disabled' : ''
}`}
onClick={() => handleScroll(-120)}
disabled={scrollPosition === 0}
>
&#9658;
</button>
<button
className={`arrow-button right-arrow ${
scrollPosition === (images.length - 1) * 124 ? 'disabled' : ''
}`}
onClick={() => handleScroll(120)}
disabled={scrollPosition === (images.length - 1) * 124}
>
&#9658;
</button>
</>
)}
</div>
);
};
Expand Down
51 changes: 37 additions & 14 deletions frontend/src/css/road_details.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* src/styles.css */

/* Define styles for the top bar */
.top-bar {
background-color: #333; /* Example color for the top bar */
height: 50px;
Expand Down Expand Up @@ -170,22 +167,32 @@ input[type='checkbox'] {
}

.image-gallery-container {
display: flex; /* Add display: flex */
flex-direction: column; /* Stack children vertically */
justify-content: center; /* Center children vertically */
display: flex;
justify-content: center;
align-items: center;
position: relative;
overflow: hidden;
width: 100%;
padding-left: 15px;
padding-left: 15px;
padding-right: 15px;
}

.image-gallery-page {
display: flex;
overflow-x: auto; /* Enable horizontal scrolling */
max-width: 100%; /* Set a maximum width */
justify-content: center;
align-items: center;
overflow-x: scroll;
padding-left: 15px;
padding-right: 15px;
}

.image-container {
display: flex;
align-items: center;
margin-left: 0.5vh;
justify-content: center; /* This will center the images */
flex-wrap: nowrap; /* This will prevent images from wrapping to the next line */
transition: transform 0.3s ease;
overflow-y: hidden; /*Added this to stop vertical scroll rect to appear when hovering over images - SL*/
}

.image-thumbnail {
Expand All @@ -194,19 +201,24 @@ input[type='checkbox'] {
object-fit: cover; /* Preserve aspect ratio and cover the container */
cursor: pointer;
transition: transform 0.3s ease-in-out;
margin-right: 4px;
margin-right: 2px;
margin-top: 0.5vh;
text-align: center;
margin-left: 2px;
flex-shrink: 0;
display: inline-block;
}

.arrow-button {
background-color: #fff;
background-color: rgba(255, 255, 255, 0.8);
border: none;
height: 50px;
width: 50px;
top: 30%;
border-radius: 50%;
position: absolute;
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.2);
box-sizing: border-box;
cursor: pointer;
display: flex;
align-items: center;
Expand All @@ -216,10 +228,21 @@ input[type='checkbox'] {
}

.left-arrow {
left: 10px;
left: 50px;
transform: rotate(180deg);
transform: translateY(-50%) rotate(180deg);
}

.right-arrow {
right: 10px;
right: 50px;
transform: translateY(-50%);
}

.arrow-button.disabled {
opacity: 0;
pointer-events: none;
}

.image-thumbnail:first-child {
margin-left: 0;
}

0 comments on commit b43e5ef

Please sign in to comment.