mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 22:16:58 +00:00
Fixes image alt errors (#1084)
This commit is contained in:
parent
650c9a4e28
commit
4203fc8f32
@ -36,7 +36,6 @@ export function GridTileImage({
|
|||||||
'transition duration-300 ease-in-out hover:scale-105': isInteractive
|
'transition duration-300 ease-in-out hover:scale-105': isInteractive
|
||||||
})}
|
})}
|
||||||
{...props}
|
{...props}
|
||||||
alt={props.title || ''}
|
|
||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
{labels ? (
|
{labels ? (
|
||||||
|
@ -6,13 +6,13 @@ import Image from 'next/image';
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
export function Gallery({ images }: { images: { src: string; altText: string }[] }) {
|
export function Gallery({ images }: { images: { src: string; altText: string }[] }) {
|
||||||
const [currentImage, setCurrentImage] = useState(0);
|
const [currentImageIndex, setCurrentImageIndex] = useState(0);
|
||||||
|
|
||||||
function handleNavigate(direction: 'next' | 'previous') {
|
function handleNavigate(direction: 'next' | 'previous') {
|
||||||
if (direction === 'next') {
|
if (direction === 'next') {
|
||||||
setCurrentImage(currentImage + 1 < images.length ? currentImage + 1 : 0);
|
setCurrentImageIndex(currentImageIndex + 1 < images.length ? currentImageIndex + 1 : 0);
|
||||||
} else {
|
} else {
|
||||||
setCurrentImage(currentImage === 0 ? images.length - 1 : currentImage - 1);
|
setCurrentImageIndex(currentImageIndex === 0 ? images.length - 1 : currentImageIndex - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,13 +22,14 @@ export function Gallery({ images }: { images: { src: string; altText: string }[]
|
|||||||
return (
|
return (
|
||||||
<div className="mr-8 h-full">
|
<div className="mr-8 h-full">
|
||||||
<div className="relative mb-12 h-full max-h-[550px] overflow-hidden">
|
<div className="relative mb-12 h-full max-h-[550px] overflow-hidden">
|
||||||
{images[currentImage] && (
|
{images[currentImageIndex] && (
|
||||||
<Image
|
<Image
|
||||||
className="relative h-full w-full object-contain"
|
className="relative h-full w-full object-contain"
|
||||||
height={600}
|
height={600}
|
||||||
width={600}
|
width={600}
|
||||||
alt={images[currentImage]?.altText as string}
|
alt={images[currentImageIndex]?.altText as string}
|
||||||
src={images[currentImage]?.src as string}
|
src={images[currentImageIndex]?.src as string}
|
||||||
|
priority={true}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@ -58,16 +59,16 @@ export function Gallery({ images }: { images: { src: string; altText: string }[]
|
|||||||
{images.length > 1 ? (
|
{images.length > 1 ? (
|
||||||
<div className="flex items-center justify-center gap-2 overflow-auto py-1">
|
<div className="flex items-center justify-center gap-2 overflow-auto py-1">
|
||||||
{images.map((image, index) => {
|
{images.map((image, index) => {
|
||||||
const isActive = index === currentImage;
|
const isActive = index === currentImageIndex;
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
aria-label="Enlarge product image"
|
aria-label="Enlarge product image"
|
||||||
key={image.src}
|
key={image.src}
|
||||||
className="h-auto w-20"
|
className="h-auto w-20"
|
||||||
onClick={() => setCurrentImage(index)}
|
onClick={() => setCurrentImageIndex(index)}
|
||||||
>
|
>
|
||||||
<GridTileImage
|
<GridTileImage
|
||||||
alt={image?.altText}
|
alt={image.altText}
|
||||||
src={image.src}
|
src={image.src}
|
||||||
width={600}
|
width={600}
|
||||||
height={600}
|
height={600}
|
||||||
|
@ -23,6 +23,7 @@ import {
|
|||||||
Cart,
|
Cart,
|
||||||
Collection,
|
Collection,
|
||||||
Connection,
|
Connection,
|
||||||
|
Image,
|
||||||
Menu,
|
Menu,
|
||||||
Page,
|
Page,
|
||||||
Product,
|
Product,
|
||||||
@ -151,6 +152,18 @@ const reshapeCollections = (collections: ShopifyCollection[]) => {
|
|||||||
return reshapedCollections;
|
return reshapedCollections;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const reshapeImages = (images: Connection<Image>, productTitle: string) => {
|
||||||
|
const flattened = removeEdgesAndNodes(images);
|
||||||
|
|
||||||
|
return flattened.map((image) => {
|
||||||
|
const filename = image.url.match(/.*\/(.*)\..*/)[1];
|
||||||
|
return {
|
||||||
|
...image,
|
||||||
|
altText: image.altText || `${productTitle} - ${filename}`
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const reshapeProduct = (product: ShopifyProduct, filterHiddenProducts: boolean = true) => {
|
const reshapeProduct = (product: ShopifyProduct, filterHiddenProducts: boolean = true) => {
|
||||||
if (!product || (filterHiddenProducts && product.tags.includes(HIDDEN_PRODUCT_TAG))) {
|
if (!product || (filterHiddenProducts && product.tags.includes(HIDDEN_PRODUCT_TAG))) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -160,7 +173,7 @@ const reshapeProduct = (product: ShopifyProduct, filterHiddenProducts: boolean =
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...rest,
|
...rest,
|
||||||
images: removeEdgesAndNodes(images),
|
images: reshapeImages(images, product.title),
|
||||||
variants: removeEdgesAndNodes(variants)
|
variants: removeEdgesAndNodes(variants)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user