@ogygia/image
Art direction
One image can have other sets of files picked by a media query, called twins. A dark version follows the visitor's theme, a different crop shows on small screens, and a GIF shows a still frame to visitors who ask for less motion, all with no JavaScript.
On this page
Sometimes one file isn’t right for every visitor. A chart drawn on white glares in dark mode. A wide hero turns into a thin strip on a phone. A looping GIF bothers someone whose system asks for less motion. Each of these is the same image with another set of files, shown under a media condition.
ogygia calls that other set a twin. Each twin becomes a <source media> row in the <picture>, before the format rows, and the browser shows the first row that matches. Each twin row carries its own width and height, so the page keeps the right box at every media. No script runs.
<script lang="ts">
import { Img } from '@ogygia/image';
const chart = import.meta.og.image('./chart.png', {
dark: './chart-dark.png'
});
</script>
<Img of={chart} width={720} alt="Visits per month" />Row order
The browser takes the first <source> whose media matches, so the order of the rows is the whole rule. They always go in this order:
- The still of a moving image, at
(prefers-reduced-motion: reduce). - Each art twin: the page’s first (from
<Img art>or the preset), then the source’s own (from the macro’sart). - The dark file, at
(prefers-color-scheme: dark). - The source’s own formats.
An art twin that crops a source with a dark file comes first as the dark file at <media> and (prefers-color-scheme: dark), then as the light one at <media>. So a phone in dark mode gets the dark crop.
<picture>
<source
media="(max-width: 600px) and (prefers-color-scheme: dark)"
type="image/avif"
…
/>
<source
media="(max-width: 600px) and (prefers-color-scheme: dark)"
type="image/webp"
…
/>
<source media="(max-width: 600px)" type="image/avif" … />
<source media="(max-width: 600px)" type="image/webp" … />
<source
media="(prefers-color-scheme: dark)"
type="image/avif"
…
/>
<source
media="(prefers-color-scheme: dark)"
type="image/webp"
…
/>
<source type="image/avif" … />
<img … />
</picture>Dark mode
Pass the dark file to the macro as dark, a path relative to the calling file like the image’s own. It’s a source of its own, read at build. Its placeholder is painted only with image({ theme }): without it, the <img> paints the light file’s placeholder, so a visitor in dark mode sees a light blur until the dark file arrives (#2290). Pass placeholder: false to a dark-twinned source if that flash matters.
The dark file must have the same shape as the light one, or the build fails with image_option_invalid: a different shape would move the page when the theme flips. A dark file must be a local file (a URL fails the same way), and a glob takes none.
Without more, the visitor’s system setting picks.
Follow your app’s theme switch
An app with its own theme switch (a data-theme attribute, a class) tells the plugin how its themes are written, as two CSS selectors that match when each theme is chosen.
A <source media> can’t read an attribute, so with theme set, an image with a dark file renders two <picture>s, data-og-theme="light" and data-og-theme="dark", and the plugin’s stylesheet shows one. The dark one shows when the dark selector matches, or when the system prefers dark and the light selector doesn’t; the light one otherwise. The stylesheet is a CSS file, so a strict CSP allows it, and an app without theme gets an empty one.
Both images are lazy, and a lazy image that’s hidden is never fetched, so only the shown picture downloads. A priority image can’t be lazy, so both pictures load for every visitor: the hidden one is only hidden, not skipped. The preloads carry each theme’s media, so the system theme’s file starts first. Leave priority off a dark-twinned image unless the double download is acceptable. The id prop goes on the light picture’s <img>.
Each selector is a plain CSS selector with no {, }, ;, < or >; anything else fails with image_option_invalid.
This site is set up that way. The demo is one <Img> of a photo with a dark file. The buttons set the framed page’s data-theme, and the other picture shows, with no script in the page. site follows this site’s theme switch, and your system when the switch is on auto.
export const visits = import.meta.og.image(
'./charts/visits.png',
{
dark: './charts/visits-dark.png'
}
);import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { image } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
image({
theme: {
dark: ':root[data-theme="dark"]',
light: ':root[data-theme="light"]'
}
}),
sveltekit()
]
});Small screens
Art direction is a page decision, so it lives on presets and props. An art twin is a Twin: a media condition plus either another crop of the same source (aspect, position, width) or another source (of). Another source keeps its own shape unless the twin says an aspect.
A preset’s art applies to every image shown with it. <Img art> replaces the preset’s for that image. getImage(of, { art }) lays out the same rows.
A preset’s twin can only crop (media plus aspect, position, width). Another source (of) goes on <Img art> or getImage(): in a preset it fails the build with image_option_unknown, even though the preset’s type lets you write it.
media is one condition in parentheses, like (max-width: 600px): no comma and no media type, so it can be joined with the dark condition. Anything else fails the build in a preset (image_option_invalid) and the render on a prop (image_prop_invalid). A twin’s own source can’t have twins.
Ship a designer’s second file with the source
When a second file always goes with the image, whatever page shows it, give it to the macro as art: an object of media condition to path. Each file is read at build like the image itself, and keeps its own shape and box.
The source’s art twins come after the page’s, so a preset or <Img art> with the same media wins.
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { image } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
image({
presets: {
hero: {
layout: 'fullWidth',
aspect: '16/9',
art: [
{
media: '(max-width: 600px)',
aspect: '4/5',
position: 'attention'
}
]
}
}
}),
sveltekit()
]
});<script lang="ts">
import { Img, type Twin } from '@ogygia/image';
const hero = import.meta.og.image('../lib/images/hero.jpg');
const tall = import.meta.og.image('../lib/images/hero-tall.jpg');
const phone: Twin[] = [{ media: '(max-width: 600px)', of: tall }];
</script>
<!-- the preset's crop for phones -->
<Img of={hero} preset="hero" alt="The harbor at dusk" />
<!-- a designer's own file for phones, in place of the preset's crop -->
<Img of={hero} preset="hero" art={phone} alt="The harbor at dusk" />export const hero = import.meta.og.image('./images/hero.jpg', {
art: { '(max-width: 600px)': './images/hero-tall.jpg' }
});Reduced motion
A moving image gets a still twin by itself: its first frame, in the image’s formats (AVIF and WebP by default), shown at (prefers-reduced-motion: reduce). A visitor whose system asks for less motion sees the still, with no script. The still’s file name has -s after its size.
A moving image is any source sharp reads with more than one frame: an animated GIF or WebP, and also a multi-page TIFF, such as a scan, which is then shown as animated WebP too (#2290). Export a scan’s first page as its own file to show it as a still image.
The still follows a system setting; it doesn’t put a pause control on the page. If a long or looping animation needs one (WCAG’s success criterion 2.2.2, Pause, Stop, Hide), that’s still yours to add.
The moving image itself is offered as animated WebP only, whatever the formats say. Every frame is kept.
- sharp makes the still on any page.
- Cloudflare makes it with
anim=false. - Vercel’s optimizer can’t keep one frame. With
vercel()alone, no moving image offers a still, and the build says so withimage_still_left_out. That holds on prerendered pages too, and the warning’s advice to prerender doesn’t bring it back (#2290). Vercel can’t resize a moving image either, so it fails withimage_encoder_cannoton a page rendered on request, and in dev (why dev differs). Listsharp()aftervercel()and the endpoint makes them (Vercel).
descriptor.animated is true for a moving source, so a component can pick a narrower preset for it. In markdown, image({ markdown: { animated } }) does that for you (Moving images in markdown).
<script lang="ts">
import { Img, type ImageDescriptor } from '@ogygia/image';
let { of, alt }: { of: ImageDescriptor; alt: string } =
$props();
</script>
<Img {of} preset={of.animated ? 'motion' : 'prose'} {alt} />Preloading
prioritywrites one preload per row set, each with the media it’s shown at plus the negation of every row before it, so each visitor preloads only the file the picture will pick.warmmakes every twin’s files too (Make a live page’s files at build).- A list kept apart from the markup is a
Twin[](import type { Twin } from '@ogygia/image').