Media Grid
A product-media uploader: an empty DropZone that becomes a grid once images are added, with the first image spanning a 2x2 hero cell and a hover overlay to delete.
Preview
Code
Example.jsx
import { useCallback, useState } from 'react';
import { BlockStack, Button, Card, DropZone, Icon, Image, Text } from '@shopify/polaris';
import { DeleteIcon, PlusIcon } from '@shopify/polaris-icons';
const MEDIA_GRID_STYLES = `
.MediaGrid-Grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: var(--p-space-150);
width: 100%;
margin: 0;
padding: 0;
}
.MediaGrid-Item {
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 1px solid rgba(204, 204, 204, 1);
border-radius: var(--p-border-radius-200);
aspect-ratio: 1;
}
.MediaGrid-Overlay {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: #00000080;
border-radius: var(--p-border-radius-200);
opacity: 0;
transition: opacity var(--p-motion-duration-150);
}
.MediaGrid-Overlay:hover {
opacity: 1;
}
.MediaGrid-Delete {
position: absolute;
top: 5px;
right: 5px;
cursor: pointer;
}
.MediaGrid-Image {
object-fit: cover;
border-radius: var(--p-border-radius-200);
aspect-ratio: 1;
}
.MediaGrid-Add {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
aspect-ratio: 1;
cursor: pointer;
background: var(--p-color-bg-surface-secondary);
border-color: var(--p-color-border-tertiary);
border-style: dashed;
border-width: var(--p-border-width-0165);
border-radius: var(--p-border-radius-200);
}
.MediaGrid-Add:hover {
background: var(--p-color-bg-surface-secondary-hover);
}
`;
const createMediaId = () => Math.random().toString(20).slice(3);
export const Example = () => {
const [images, setImages] = useState([]);
const handleDrop = useCallback((_droppedFiles, acceptedFiles) => {
setImages((prev) => [
...prev,
...acceptedFiles.map((file) => ({ file, id: createMediaId() })),
]);
}, []);
return (
<Card>
<style>{MEDIA_GRID_STYLES}</style>
<BlockStack gap="400">
<Text variant="headingMd" as="h3">
Media
</Text>
{images.length ? (
<div className="MediaGrid-Grid">
{images.map((image, index) => (
<div
key={image.id}
className="MediaGrid-Item"
style={index === 0 ? { gridColumn: '1 / span 2', gridRow: '1 / span 2' } : undefined}
>
<div className="MediaGrid-Overlay">
<div className="MediaGrid-Delete">
<Button
size="micro"
icon={DeleteIcon}
tone="critical"
accessibilityLabel="Remove image"
onClick={() =>
setImages((prev) => prev.filter((_, position) => position !== index))
}
/>
</div>
</div>
<Image
source={URL.createObjectURL(image.file)}
width="100%"
height="100%"
alt="Product media"
className="MediaGrid-Image"
/>
</div>
))}
<DropZone outline={false} onDrop={handleDrop} accept="image/*" type="image">
<div className="MediaGrid-Add">
<Icon source={PlusIcon} />
</div>
</DropZone>
</div>
) : (
<DropZone accept="image/*" type="image" onDrop={handleDrop}>
<DropZone.FileUpload />
</DropZone>
)}
</BlockStack>
</Card>
);
};