Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ export const Next: React.FC<
</MediaGalleryPrimitive.Next>
);

export const Thumbnails: React.FC<
React.ComponentProps<typeof MediaGalleryPrimitive.Thumbnails>
> = ({ children, ...props }) => (
<MediaGalleryPrimitive.Thumbnails {...props}>
{children}
</MediaGalleryPrimitive.Thumbnails>
);

export const ThumbnailRepeater: React.FC<
React.ComponentProps<typeof MediaGalleryPrimitive.ThumbnailRepeater>
> = ({ children, ...props }) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ export default function ProductDetails({
</div>

{/* Thumbnail Images */}
<StyledMediaGallery.Thumbnails>
<StyledMediaGallery.ThumbnailRepeater>
<StyledMediaGallery.ThumbnailItem />
</StyledMediaGallery.ThumbnailRepeater>
</StyledMediaGallery.Thumbnails>
<StyledMediaGallery.ThumbnailRepeater>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<StyledMediaGallery.Thumbnails> is still needed, I don't care that it has no headless logic but having the emptyState prop which is null by default and it does the logic of hasThumbnails ? {children} : {emptyState}

<StyledMediaGallery.ThumbnailItem />
</StyledMediaGallery.ThumbnailRepeater>
</div>

{/* Product Info */}
Expand Down
41 changes: 11 additions & 30 deletions packages/headless-components/media/src/react/MediaGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ import {
Previous as CorePrevious,
Viewport as CoreViewport,
Indicator as CoreIndicator,
ThumbnailList as CoreThumbnailList,
ThumbnailItem as CoreThumbnailItem,
} from './core/MediaGallery.js';
import React, { createContext, useContext } from 'react';
import type { MediaItem } from '../services/media-gallery-service.js';
import { MediaGalleryServiceDefinition } from '../services/media-gallery-service.js';
import type { MediaGalleryServiceConfig } from '../services/media-gallery-service.js';
import { Slot } from '@radix-ui/react-slot';
import { WixMediaImage } from './WixMediaImage.js';
import { useService } from '@wix/services-manager-react';
import { ServiceAPI } from '@wix/services-definitions';

// Components that render actual DOM elements get test IDs on their rendered elements
// Components that only provide context/logic don't introduce new DOM elements
Expand Down Expand Up @@ -345,7 +346,6 @@ export const Indicator = React.forwardRef<HTMLDivElement, IndicatorProps>(
},
);

const ThumbnailsContext = createContext<{ items: MediaItem[] } | null>(null);
const ThumbnailItemContext = createContext<{ index: number } | null>(null);

/**
Expand All @@ -356,30 +356,6 @@ export interface ThumbnailsProps {
children: React.ReactNode;
}

/**
* Thumbnails container component that provides thumbnail context to its children.
* Only renders when there are multiple media items to display.
*
* @component
* @example
* ```tsx
* <MediaGallery.Thumbnails>
* <MediaGallery.ThumbnailRepeater>
* <MediaGallery.ThumbnailItem />
* </MediaGallery.ThumbnailRepeater>
* </MediaGallery.Thumbnails>
* ```
*/
export const Thumbnails = ({ children }: ThumbnailsProps) => (
<CoreThumbnailList>
{({ items }) => (
<ThumbnailsContext.Provider value={{ items: items as MediaItem[] }}>
{children}
</ThumbnailsContext.Provider>
)}
</CoreThumbnailList>
);

/**
* Props for the ThumbnailRepeater component
*/
Expand All @@ -402,11 +378,16 @@ export interface ThumbnailRepeaterProps {
* ```
*/
export const ThumbnailRepeater = ({ children }: ThumbnailRepeaterProps) => {
const ctx = useContext(ThumbnailsContext);
if (!ctx || !ctx.items || ctx.items.length <= 1) return null;
const mediaService = useService(MediaGalleryServiceDefinition) as ServiceAPI<
typeof MediaGalleryServiceDefinition
>;

const mediaToDisplay = mediaService.mediaToDisplay.get();

if (!mediaToDisplay || mediaToDisplay.length <= 1) return null;
return (
<>
{ctx.items.map((_, i) => (
{mediaToDisplay.map((_, i) => (
<ThumbnailItemContext.Provider key={i} value={{ index: i }}>
{children}
</ThumbnailItemContext.Provider>
Expand Down