Placeholder
Generates an inline SVG placeholder graphic for content that isn't ready yet, or swaps in a real image once it is.
Overview
<Placeholder> renders an inline <svg> sized to width/height, filled with a context color
and a {width}x{height} label — useful for mocking up layouts before real images exist. Pass
src and it renders a real <img> instead (or any component passed via component), so the same
component can point at a project's own default image, a placehold.co URL, or any other source
once one is available — no need to swap components as content comes online.
widthandheightsize the graphic and (unless overridden) generate its default label text.colorsets the generated graphic's background/foreground to one of the context colors. Has no effect whensrcis set.textandtitleoverride or hide (false) the generated graphic's label and accessible title. Have no effect whensrcis set.srcswitches to rendering a real image, forwarding any other nativeimgprops (alt,loading,srcSet, …) alongside it. Only applies whensrcis set:fluid(responsive,image fluid),thumbnail(image thumbnail),rounded, andalign(start/center/end).componentswaps the<img>rendered insrcmode for any other element or component — see Using a custom image component.
Examples
Basic usage
With no other props, <Placeholder> renders a generated graphic sized to width/height.
import { Placeholder } from '@chassis-ui/react'
export const Example = () => {
return <Placeholder width={320} height={160} />
}Color
Pass color to set the generated graphic's background and foreground to one of the context colors.
import { Flex, Placeholder } from '@chassis-ui/react'
export const Example = () => {
return (
<Flex gap={'sm'} wrap="wrap">
<Placeholder color="default" width={140} height={100} />
<Placeholder color="alternate" width={140} height={100} />
<Placeholder color="primary" width={140} height={100} />
<Placeholder color="secondary" width={140} height={100} />
<Placeholder color="neutral" width={140} height={100} />
<Placeholder color="success" width={140} height={100} />
<Placeholder color="danger" width={140} height={100} />
<Placeholder color="warning" width={140} height={100} />
<Placeholder color="info" width={140} height={100} />
</Flex>
)
}Custom text and title
text and title default to {width}x{height} and "Placeholder" respectively. Pass a string
to override either, or false to hide it.
import { Flex, Placeholder } from '@chassis-ui/react'
export const Example = () => {
return (
<Flex gap="md" wrap="wrap">
<Placeholder width={200} height={120} title="Cover" text="No cover yet" />
<Placeholder width={200} height={120} title={false} text="Coming soon" />
<Placeholder width={200} height={120} text={false} />
</Flex>
)
}Using a real image
Pass src (and alt) to render an actual image instead of the generated graphic — handy for
swapping a mockup for a placehold.co URL, or a project's own default image, without changing
components.
import { Placeholder } from '@chassis-ui/react'
export const Example = () => {
return (
<Placeholder
src="https://placehold.co/320x160"
alt="A placeholder image served from placehold.co"
width={320}
height={160}
/>
)
}Styling a real image
fluid, thumbnail, rounded, and align only apply in src mode, mapping to chassis-css's
.image.fluid, .image.thumbnail, .rounded, and float/centering utilities respectively.
import { Flex, Placeholder } from '@chassis-ui/react'
export const Example = () => {
return (
<Flex gap="md" wrap="wrap" align="center">
<Placeholder src="https://placehold.co/160x100" alt="" fluid />
<Placeholder src="https://placehold.co/160x100" alt="" thumbnail />
<Placeholder src="https://placehold.co/160x100" alt="" rounded />
</Flex>
)
}Using a custom image component
<Placeholder> renders a plain <img> in src mode by default, but its component prop accepts
any element or component — including a framework's own Image (Next.js, Gatsby, Remix, etc.).
Whatever is passed is fully type-checked at the call site, so a component's own props (fill,
priority, loader, and so on) are available and validated alongside src/alt:
import Image from 'next/image'
<Placeholder component={Image} src="/hero.jpg" alt="" fill priority />From a React Server Component, where a component reference can't be passed as a prop, render the
image as the child with asChild. The child carries its own src:
import Image from 'next/image'
<Placeholder asChild fluid rounded>
<Image src="/hero.jpg" width={800} height={400} alt="" />
</Placeholder>API
Placeholder
| Prop | Type | Default |
|---|---|---|
align? | PlaceholderAlign | — |
Set the horizontal alignment. Only applies when src is set. | ||
asChild? | boolean | — |
Render the single child element in place of this component's own element, with this
component's classes, props and ref merged onto it: <Button asChild><Link href="/login">Log
in</Link></Button>. The alternative to component for React Server Components — an element
can be passed from the server to this package's client components, a component reference
(component={Link}) can't. | ||
className? | string | — |
| A string of all className you want applied to the component. | ||
color? | ContextColor | — |
Sets the color of the generated placeholder graphic to one of Chassis context colors. Has no
effect when src is set. | ||
component? | ElementType | — |
Component used for the root node when src is set. Either a string to use an HTML element
or a component — e.g. a framework's own Image component. Its own props (src, fill,
priority, etc.) are type-checked at the call site once passed here. With asChild, the
child element is the image and carries its own src. | ||
fluid? | boolean | — |
Make the image responsive, so it never grows larger than its parent. Only applies when src
is set. | ||
height? | string | number | — |
| Placeholder height. | ||
ref? | any | — |
rounded? | boolean | — |
Give the image a rounded border-radius. Only applies when src is set. | ||
src? | string | — |
Renders a real image instead of the generated placeholder graphic, e.g. a default asset or a
placehold.co URL. | ||
text? | string | false | — |
Text shown in the generated placeholder graphic. Defaults to {width}x{height}. Pass false
to hide it. Has no effect when src is set. | ||
thumbnail? | boolean | — |
Give the image a thumbnail appearance (padding, background, border, box-shadow). Only
applies when src is set. | ||
title? | string | false | 'Placeholder' |
Accessible title for the generated placeholder graphic, rendered as an SVG <title>. Pass
false to hide it. Has no effect when src is set. | ||
variant? | "solid" | "smooth" | — |
Sets the variant of the generated placeholder graphic to one of Chassis context styles. Has no
effect when src is set. | ||
width? | string | number | — |
| Placeholder width. | ||