Pictures that open full size in a viewer over the page: screenshots, a photo gallery, product shots. Each LightboxItem is a link to its image, drawn as a small picture; a click opens the viewer on that image, with previous and next buttons, a close button and the caption.
Pictures that open full size
Use a Lightbox for pictures people need to see at full size, such as screenshots or product photos. Put a LightboxItem for each image inside it, laid out as you like, and name the viewer with Label. Counter shows the open image's place, and Caption puts text under it.
Static
Code
@* Screenshots of four pages; a click opens one full size, and the dark shots show in dark mode. *@
<Lightbox Label="Page screenshots" Counter>
<Grid Columns="new() { Base = 2, Md = 4 }" Gap="3">
foreach (var (name, alt) in _pages)
{
<LightboxItem Src="($"img/blocks/{name}.webp")" DarkSrc="($"img/blocks/{name}-dark.webp")" Alt="alt" Caption="alt" />
}
</Grid>
</Lightbox>
{
private readonly (string Name, string Alt)[] _pages =
[
("pricing", "The pricing page, three plans side by side"),
("features", "The features page, a grid of six features"),
("signup", "The sign-up page, a form beside its pitch"),
("faq", "The questions page, an accordion of answers"),
];
}Add this example to a page with the izzy CLI:
izzy add component Lightbox --at <file>:<line>Each item is a link to its image, so people can open it in a new tab or copy its address, as with any link.
To show a different image in dark mode, such as a screenshot taken in dark mode, set DarkSrc. For a smaller file in the link than in the viewer, set Thumbnail.
Thumbnails and dots
To add a small picture of each image under the open one, set Nav to Thumbnails; Dots adds a dot for each instead. To wrap from the last image to the first, set Loop.
Static
Code
@* A small picture of each page under the open one, and the last wraps to the first. *@
<Lightbox Label="Page screenshots" Nav="LightboxNav.Thumbnails" Loop>
<Grid Columns="new() { Base = 3, Md = 6 }" Gap="3">
foreach (var (name, alt) in _pages)
{
<LightboxItem Src="($"img/blocks/{name}.webp")" DarkSrc="($"img/blocks/{name}-dark.webp")" Alt="alt" />
}
</Grid>
</Lightbox>
{
private readonly (string Name, string Alt)[] _pages =
[
("hero", "The home page's opening section"),
("features", "The features page"),
("pricing", "The pricing page"),
("articles", "The articles page"),
("contact", "The contact page"),
("legal", "The terms page"),
];
}The next image shows at once. To fade it in, set Transition to Fade.
The controls fade after 3 seconds without a pointer move or a key. HideControlsAfter changes that, and TimeSpan.Zero keeps them.
Opening it from C#
To open the viewer from C#, and to know which image is open, bind @bind-Index. It's null while the viewer is closed.
Always Interactive
Code
@* A button opens the viewer on the pricing page, and the text follows what's open. *@
<Stack Gap="3">
<Row Gap="3" Align="Align.Center">
<Button Variant="ButtonVariant.Outline" OnClick="() => _open = 1">Open the pricing page</Button>
<TextBlock Color="Color.Muted">(_open is { } i ? $"Showing {i + 1} of 3" : "Closed")</TextBlock>
</Row>
<Lightbox Label="Page screenshots" @bind-Index="_open" Counter>
<Grid Columns="3" Gap="3">
<LightboxItem Src="img/blocks/features.webp" DarkSrc="img/blocks/features-dark.webp" Alt="The features page" />
<LightboxItem Src="img/blocks/pricing.webp" DarkSrc="img/blocks/pricing-dark.webp" Alt="The pricing page" />
<LightboxItem Src="img/blocks/faq.webp" DarkSrc="img/blocks/faq-dark.webp" Alt="The questions page" />
</Grid>
</Lightbox>
</Stack>
{
private int? _open;
}Details
Lay the items out as you like inside it (a Grid, a Row). Counter shows "2 of 5"; Nav adds a dot or a small picture for each item under the image. DarkSrc on an item swaps in another image where the color scheme is dark. @bind-Index says which item is open, and opens or closes the viewer from C#, on an interactive page. The next image shows at once; Transition Fade fades it in. The controls fade when nothing moves for HideControlsAfter, and come back with the pointer or a key. An image is shown at its own size, or scaled down to fit.
Parts
Written inside Lightbox. The API page lists their parameters.
LightboxItem: one picture of a Lightbox: a link to the image, drawn as a small picture of it (or as its content).
Mode support
| Feature | Static | Interactive |
|---|---|---|
| Links to each image, drawn as small pictures you lay out | Yes | Yes |
| A viewer over the page with previous and next, a counter, captions and a close button | Yes | Yes |
| Dots or thumbnails to move between the images | Yes | Yes |
| Another image where the color scheme is dark | Yes | Yes |
| The open image bound to C# | No | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, each link opens its image on its own, and there's no viewer.
Accessibility
- Each item is a link named by its image's Alt, which is required (or by its own content).
- The viewer is a modal dialog named by
Label, which is required. Focus moves to its close button, stays inside while it's open, and returns to the link that opened it. - The slides are a carousel: each is a group named "3 of 5", and the one a person moves to is announced. The image's Alt and its caption are read with it.
- The controls stay visible while keyboard focus is on one.
Keyboard navigation
In the viewer
| Press this key | To do this |
|---|---|
| Left or Right | Go to the previous or next image (reversed on right-to-left pages) |
| Home or End | Go to the first or last image |
| Escape | Close the viewer |
Parameters
| Prop | Type | Default |
|---|---|---|
LabelWhat the pictures are, such as "Product photos": the viewer's accessible name. | string | Required |
@bind-IndexThe item open in the viewer, from 0; null when the viewer is closed. | int? | |
CounterShows the open item's place, such as "2 of 5". | bool | false |
NavAdds a dot or a small picture for each item under the image. | LightboxNav | LightboxNav.None |
ArrowsShows the previous and next buttons. | bool | true |
TransitionHow one image gives way to the next: LightboxTransition.None, the default, shows it at once; LightboxTransition.Fade fades it in. | LightboxTransition | LightboxTransition.None |
LoopWraps from the last image to the first, and back. | bool | false |
CloseOnBackdropCloses the viewer on a click outside the image and its controls. | bool | true |
HideControlsAfterHow long without a pointer move or a key before the viewer's controls fade. | TimeSpan | TimeSpan.FromMilliseconds(3000) |
ChildContentThe LightboxItems, laid out as you like. | RenderFragment? |
Plus 27 surface props, 17 item props and 4 common props.
Full APISee also
- LightboxItem: one picture of a Lightbox: a link to the image, drawn as a small picture of it (or as its content).
- Carousel: shows slides in a row that swipes and scrolls a slide at a time.
- Image: loads an image lazily and keeps its space.
- Dialog: opens a native modal dialog from a trigger, a link or C#.