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

Configurable thumbnail dimensions in product gallery #1120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Added the ability to configure the width and height for product thumbnails in the carousel component. Improved page performance and SEO by ensuring appropriate image sizes are used, reducing unnecessary data load.

## [3.175.1] - 2024-09-11

### Fixed
Expand Down
6 changes: 6 additions & 0 deletions react/components/ProductImages/Wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const ProductImagesWrapper = props => {
showPaginationDots,
contentOrder,
placeholder,
thumbCustomWidth,
thumbCustomHeight,
} = useResponsiveValues(
pick(
[
Expand All @@ -25,6 +27,8 @@ const ProductImagesWrapper = props => {
'showPaginationDots',
'contentOrder',
'placeholder',
'thumbCustomWidth',
'thumbCustomHeight',
],
props
)
Expand Down Expand Up @@ -93,6 +97,8 @@ const ProductImagesWrapper = props => {
ModalZoomElement={props.ModalZoom}
contentType={props.contentType}
showImageLabel={props.showImageLabel}
thumbCustomWidth={thumbCustomWidth}
thumbCustomHeight={thumbCustomHeight}
// Deprecated
zoomProps={props.zoomProps}
displayMode={props.displayMode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ const CSS_HANDLES = [
]

const Thumbnail = props => {
const { alt, isVideo, thumbUrl, handles, aspectRatio = 'auto' } = props
const {
alt,
isVideo,
thumbUrl,
handles,
aspectRatio = 'auto',
thumbCustomWidth,
thumbCustomHeight,
} = props

return (
<>
Expand All @@ -39,7 +47,14 @@ const Thumbnail = props => {
)} w-100 h-auto db`}
itemProp="thumbnail"
alt={alt}
src={imageUrl(thumbUrl, THUMB_SIZE, THUMB_MAX_SIZE, aspectRatio)}
src={imageUrl(
thumbUrl,
THUMB_SIZE,
THUMB_MAX_SIZE,
aspectRatio,
thumbCustomWidth,
thumbCustomHeight
)}
/>
</figure>
<div
Expand Down Expand Up @@ -67,6 +82,8 @@ const ThumbnailSwiper = props => {
thumbnailAspectRatio,
thumbnailMaxHeight,
displayThumbnailsArrows,
thumbCustomWidth,
thumbCustomHeight,
...swiperProps
} = props

Expand Down Expand Up @@ -151,9 +168,6 @@ const ThumbnailSwiper = props => {
touchRatio={1}
threshold={8}
navigation={navigationConfig}
/* Slides are grouped when thumbnails arrows are enabled
* so that clicking on next/prev will scroll more than
* one thumbnail */
slidesPerGroup={displayThumbnailsArrows ? 4 : 1}
freeMode={false}
mousewheel={false}
Expand Down Expand Up @@ -183,6 +197,8 @@ const ThumbnailSwiper = props => {
alt={slide.alt}
thumbUrl={slide.thumbUrl || thumbUrls[i]}
aspectRatio={thumbnailAspectRatio}
thumbCustomWidth={thumbCustomWidth}
thumbCustomHeight={thumbCustomHeight}
/>
</SwiperSlide>
)
Expand Down
6 changes: 6 additions & 0 deletions react/components/ProductImages/components/Carousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ class Carousel extends Component {
showNavigationArrows = true,
thumbnailVisibility,
displayThumbnailsArrows = false,
thumbCustomWidth,
thumbCustomHeight,
} = this.props

const hasSlides = slides && slides.length > 0
Expand Down Expand Up @@ -325,6 +327,8 @@ class Carousel extends Component {
displayThumbnailsArrows={displayThumbnailsArrows}
slides={slides}
position={position}
thumbCustomWidth={thumbCustomWidth}
thumbCustomHeight={thumbCustomHeight}
/>
)

Expand Down Expand Up @@ -414,6 +418,8 @@ Carousel.propTypes = {
THUMBS_VISIBILITY.VISIBLE,
THUMBS_VISIBILITY.HIDDEN,
]),
thumbCustomWidth: PropTypes.number,
thumbCustomHeight: PropTypes.number,
}

export default withCssHandles(CSS_HANDLES)(Carousel)
4 changes: 4 additions & 0 deletions react/components/ProductImages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const ProductImages = ({
zoomFactor,
ModalZoomElement,
contentType = 'all',
thumbCustomWidth,
thumbCustomHeight,
// Deprecated
zoomProps,
displayMode,
Expand Down Expand Up @@ -152,6 +154,8 @@ const ProductImages = ({
thumbnailsOrientation={thumbnailsOrientation}
displayThumbnailsArrows={displayThumbnailsArrows}
thumbnailVisibility={thumbnailVisibility}
thumbCustomWidth={thumbCustomWidth}
thumbCustomHeight={thumbCustomHeight}
// Deprecated
zoomProps={zoomProps}
/>
Expand Down
45 changes: 24 additions & 21 deletions react/components/ProductImages/utils/aspectRatioUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,33 @@ export const imageUrl = (
src: string,
size: number,
maxSize: number,
aspectRatio?: AspectRatio
aspectRatio?: AspectRatio,
customWidth?: number,
customHeight?: number
// eslint-disable-next-line max-params
) => {
let width = size
let height: number | string = 'auto'

if (aspectRatio && aspectRatio !== 'auto') {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
height = size * (parseAspectRatio(aspectRatio) || 1)

if (width > maxSize) {
height /= width / maxSize
width = maxSize
let width = customWidth || size
let height: number | string = customHeight || 'auto'

if (!customWidth || !customHeight) {
if (aspectRatio && aspectRatio !== 'auto') {
height = size * (parseAspectRatio(aspectRatio) || 1)

if (width > maxSize) {
height /= width / maxSize
width = maxSize
}

if (height > maxSize) {
width /= height / maxSize
height = maxSize
}

width = Math.round(width)
height = Math.round(height)
} else {
width = Math.min(maxSize, width)
}

if (height > maxSize) {
width /= height / maxSize
height = maxSize
}

width = Math.round(width)
height = Math.round(height)
} else {
width = Math.min(maxSize, width)
}

return changeImageUrlSize(src, width, height)
Expand Down