Media Gallery
One to ten images in a mosaic, laid out by how many there are.
One to ten images in a mosaic, laid out by how many there are. Compose it from the exported registry parts so local content and behaviour stay in the application while the visual contract stays token-driven.
import {
MediaGallery,
MediaGalleryItem,
} from "@/registry/bases/newt/ui/media-gallery"
/* Drawn rather than fetched, so the demo holds up offline. */
function tile(from: string, to: string): string {
return (
"data:image/svg+xml;utf8," +
encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="${from}"/><stop offset="1" stop-color="${to}"/>
</linearGradient></defs>
<rect width="200" height="200" fill="url(#g)"/>
</svg>`
)
)
}
export default function MediaGalleryDemo() {
return (
<MediaGallery className="h-[220px]">
<MediaGalleryItem
src={tile("#5865f2", "#1e1f22")}
alt="The yard at noon"
/>
<MediaGalleryItem src={tile("#23a55a", "#1e1f22")} alt="The roof line" />
<MediaGalleryItem src={tile("#eb459e", "#1e1f22")} alt="The skyline" />
</MediaGallery>
)
}<script setup lang="ts">
import {
MediaGallery,
MediaGalleryItem,
} from "~~/registry/bases/newt/ui/media-gallery"
/* Drawn rather than fetched, so the demo holds up offline. */
function tile(from: string, to: string): string {
return (
"data:image/svg+xml;utf8," +
encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="${from}"/><stop offset="1" stop-color="${to}"/>
</linearGradient></defs>
<rect width="200" height="200" fill="url(#g)"/>
</svg>`
)
)
}
</script>
<template>
<MediaGallery class="h-[220px]" :count="3">
<MediaGalleryItem
:src="tile('#5865f2', '#1e1f22')"
alt="The yard at noon"
/>
<MediaGalleryItem :src="tile('#23a55a', '#1e1f22')" alt="The roof line" />
<MediaGalleryItem :src="tile('#eb459e', '#1e1f22')" alt="The skyline" />
</MediaGallery>
</template>Installation
npx newtui@latest add media-gallerynpx shadcn@latest add @newtui/media-gallerynpx shadcn-vue@latest add @newtui/media-gallerypnpm dlx newtui@latest add media-gallerypnpm dlx shadcn@latest add @newtui/media-gallerypnpm dlx shadcn-vue@latest add @newtui/media-galleryyarn dlx newtui@latest add media-galleryyarn dlx shadcn@latest add @newtui/media-galleryyarn dlx shadcn-vue@latest add @newtui/media-gallerybunx newtui@latest add media-gallerybunx shadcn@latest add @newtui/media-gallerybunx shadcn-vue@latest add @newtui/media-gallerydeno run -A npm:newtui@latest add media-gallerydeno run -A npm:shadcn@latest add @newtui/media-gallerydeno run -A npm:shadcn-vue@latest add @newtui/media-galleryvlx newtui@latest add media-galleryvlx shadcn@latest add @newtui/media-galleryvlx shadcn-vue@latest add @newtui/media-galleryvp dlx newtui@latest add media-galleryvp dlx shadcn@latest add @newtui/media-galleryvp dlx shadcn-vue@latest add @newtui/media-gallerynubx newtui@latest add media-gallerynubx shadcn@latest add @newtui/media-gallerynubx shadcn-vue@latest add @newtui/media-gallerynlx newtui@latest add media-gallerynlx shadcn@latest add @newtui/media-gallerynlx shadcn-vue@latest add @newtui/media-galleryupx newtui@latest add media-galleryupx shadcn@latest add @newtui/media-galleryupx shadcn-vue@latest add @newtui/media-gallery- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Copy the source into your project
import * as React from "react"
import { cn } from "@/lib/utils"
/*
* One to ten images in a mosaic. The layout is decided by how many there are,
* not by what each one is: two side by side, three as one large and two
* stacked, more than that in a grid. The count drives it because that is the
* only thing the gallery knows before the images load.
*/
function layoutFor(count: number): string {
if (count <= 1) return "grid-cols-1"
if (count === 2) return "grid-cols-2"
/* Three: the first takes the full height beside a stacked pair. */
if (count === 3) return "grid-cols-2 grid-rows-2 [&>*:first-child]:row-span-2"
if (count === 4) return "grid-cols-2"
return "grid-cols-3"
}
export interface MediaGalleryProps extends React.HTMLAttributes<HTMLDivElement> {
/**
* How many items the grid holds. Counted from the children when it is not
* given, which is what a static list wants.
*/
count?: number
}
const MediaGallery = React.forwardRef<HTMLDivElement, MediaGalleryProps>(
({ className, count, children, ...props }, ref) => (
<div
ref={ref}
className={cn(
"grid max-w-[550px] auto-rows-[1fr] gap-1 overflow-hidden rounded-[var(--newt-radius-lg)]",
layoutFor(count ?? React.Children.count(children)),
className
)}
{...props}
>
{children}
</div>
)
)
MediaGallery.displayName = "MediaGallery"
/*
* The props of the element or component `as` names, so a custom image
* component's own props type-check.
*/
export type MediaGalleryItemProps<E extends React.ElementType = "img"> = {
/**
* The image element or component to render. Defaults to `img`; pass your
* framework's image component for optimised sources. It receives `src`,
* `alt`, `className` and the spoiler attributes and handlers.
*/
as?: E
/** Blurs the image until it is clicked. */
spoiler?: boolean
/** Accessible name of a hidden spoiler, read in place of the image. */
spoilerLabel?: string
} & Omit<React.ComponentPropsWithRef<E>, "as" | "spoiler">
type MediaGalleryItemComponent = {
<E extends React.ElementType = "img">(
props: MediaGalleryItemProps<E>
): React.ReactNode
displayName?: string
}
/*
* SAFETY: `forwardRef` drops the generic signature. The render hands every
* prop it does not set itself to `as` unchanged, so the props of `as` are
* the props the part receives.
*/
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- see SAFETY
const MediaGalleryItem = React.forwardRef<
HTMLImageElement,
MediaGalleryItemProps
>(
(
{
as: Tag = "img",
className,
spoiler = false,
spoilerLabel = "Spoiler, click to reveal",
alt = "",
...props
},
ref
) => {
const [revealed, setRevealed] = React.useState(false)
const hidden = spoiler && !revealed
return (
<Tag
ref={ref}
alt={alt}
data-spoiler={hidden || undefined}
role={hidden ? "button" : undefined}
tabIndex={hidden ? 0 : undefined}
aria-label={hidden ? spoilerLabel : undefined}
onClick={() => setRevealed(true)}
onKeyDown={(event: React.KeyboardEvent<HTMLImageElement>) => {
if (!hidden) return
if (event.key === "Enter" || event.key === " ") {
event.preventDefault()
setRevealed(true)
}
}}
/*
* `object-cover` on a stretched cell: the mosaic keeps its shape whatever
* aspect ratio arrives, which is the point of a mosaic.
*/
className={cn(
"h-full w-full object-cover",
hidden && "cursor-pointer blur-lg",
className
)}
{...props}
/>
)
}
) as MediaGalleryItemComponent
MediaGalleryItem.displayName = "MediaGalleryItem"
export { MediaGallery, MediaGalleryItem }<script setup lang="ts">
import { computed, useSlots, type HTMLAttributes, type VNode } from "vue"
import { cn } from "@/lib/utils"
import { mediaGalleryLayout } from "."
const props = defineProps<{
class?: HTMLAttributes["class"]
/**
* How many items the grid holds. Counted from the slot when it is not
* given, which is what a static list wants.
*/
count?: number
}>()
const slots = useSlots()
const items = computed(() => {
if (props.count !== undefined) return props.count
const nodes: VNode[] = slots.default?.() ?? []
/* A `v-for` arrives as one fragment, so its children are the real count. */
return nodes.reduce(
(total, node) =>
total + (Array.isArray(node.children) ? node.children.length : 1),
0
)
})
</script>
<template>
<div
:class="
cn(
'grid max-w-[550px] auto-rows-[1fr] gap-1 overflow-hidden rounded-[var(--newt-radius-lg)]',
mediaGalleryLayout(items),
props.class
)
"
>
<slot />
</div>
</template><script setup lang="ts">
import { ref, type Component, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = withDefaults(
defineProps<{
class?: HTMLAttributes["class"]
/**
* The image element or component to render. Defaults to `img`; pass
* `NuxtImg` or another image component for optimised sources. It
* receives `src`, `alt`, `class` and the spoiler attributes and handlers.
*/
as?: string | Component
src: string
alt?: string
/** Blurs the image until it is clicked. */
spoiler?: boolean
/** Accessible name of a hidden spoiler, read in place of the image. */
spoilerLabel?: string
}>(),
{
as: "img",
alt: "",
spoiler: false,
spoilerLabel: "Spoiler, click to reveal",
}
)
const revealed = ref(false)
function reveal() {
revealed.value = true
}
function onKeydown(event: KeyboardEvent) {
if (revealed.value || !props.spoiler) return
if (event.key === "Enter" || event.key === " ") {
event.preventDefault()
reveal()
}
}
</script>
<template>
<component
:is="props.as"
:src="props.src"
:alt="props.alt"
:data-spoiler="props.spoiler && !revealed ? true : undefined"
:role="props.spoiler && !revealed ? 'button' : undefined"
:tabindex="props.spoiler && !revealed ? 0 : undefined"
:aria-label="props.spoiler && !revealed ? props.spoilerLabel : undefined"
:class="
cn(
'h-full w-full object-cover',
props.spoiler && !revealed && 'cursor-pointer blur-lg',
props.class
)
"
@click="reveal"
@keydown="onKeydown"
/>
</template>export { default as MediaGallery } from "./MediaGallery.vue"
export { default as MediaGalleryItem } from "./MediaGalleryItem.vue"
/*
* One to ten images in a mosaic. The layout is decided by how many there are,
* not by what each one is: two side by side, three as one large and two
* stacked, more than that in a grid. The count drives it because that is the
* only thing the gallery knows before the images load.
*/
export function mediaGalleryLayout(count: number): string {
if (count <= 1) return "grid-cols-1"
if (count === 2) return "grid-cols-2"
/* Three: the first takes the full height beside a stacked pair. */
if (count === 3) return "grid-cols-2 grid-rows-2 [&>*:first-child]:row-span-2"
if (count === 4) return "grid-cols-2"
return "grid-cols-3"
}Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Media Gallery to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Media Gallery to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { MediaGallery } from "@/components/ui/media-gallery"
export function Example() {
return <MediaGallery />
}<script setup lang="ts">
import { MediaGallery, MediaGalleryItem } from "@/components/ui/media-gallery"
</script>
<template>
<MediaGallery />
</template>API reference
MediaGallery
children / default slot?React.ReactNode / Vue slot
Content rendered inside the component.
React.ReactNode / Vue slotclassName / class?string
Additional classes merged with the component defaults.
stringMediaGalleryItem
as?React.ElementType (React) / string | Component (Vue)
The image element or component to render, such as `NuxtImg`. It gets `src`, `alt`, the classes and the spoiler attributes and handlers, so the spoiler still works.
React.ElementType (React) / string | Component (Vue)"img"spoiler?boolean
Blurs the image until it is clicked or activated with `Enter` or `Space`.
booleanfalsespoilerLabel?string
Accessible name of a hidden spoiler, read in place of the image.
string"Spoiler, click to reveal"Accessibility
Preserve the native semantics of the rendered elements and pass through ARIA attributes when the surrounding context needs a label or description. Any interactive child needs a visible focus indicator, and meaning must not rely on colour alone.
A spoilered item takes its name from spoilerLabel. Translating labels lists every English default.
Tokens
Media Gallery reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer.
--newt-radius-lg