Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

Lightbox

Since 0.1.0-alpha.24
Opens pictures full size in a viewer over the page.

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

Preview

The pricing page, three plans side by sideThe pricing page, three plans side by sideThe features page, a grid of six featuresThe features page, a grid of six featuresThe sign-up page, a form beside its pitchThe sign-up page, a form beside its pitchThe questions page, an accordion of answersThe questions page, an accordion of answers

Code

Usage.razor
@* 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>

@code {
    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:

bash
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

Preview

The home page's opening sectionThe home page's opening sectionThe features pageThe features pageThe pricing pageThe pricing pageThe articles pageThe articles pageThe contact pageThe contact pageThe terms pageThe terms page

Code

Thumbnails.razor
@* 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>

@code {
    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

Preview

Closed

The features pageThe features page The pricing pageThe pricing page The questions pageThe questions page

Code

Bound.razor
@* 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>

@code {
    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

What works in each mode, Lightbox
FeatureStaticInteractive
Links to each image, drawn as small pictures you lay outYesYes
A viewer over the page with previous and next, a counter, captions and a close buttonYesYes
Dots or thumbnails to move between the imagesYesYes
Another image where the color scheme is darkYesYes
The open image bound to C#NoYes

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

Lightbox keys, In the viewer
Press this keyTo do this
Left or RightGo to the previous or next image (reversed on right-to-left pages)
Home or EndGo to the first or last image
EscapeClose the viewer

Parameters

Lightbox parameters
PropTypeDefault
LabelWhat the pictures are, such as "Product photos": the viewer's accessible name.stringRequired
@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".boolfalse
NavAdds a dot or a small picture for each item under the image.LightboxNavLightboxNav.None
ArrowsShows the previous and next buttons.booltrue
TransitionHow one image gives way to the next: LightboxTransition.None, the default, shows it at once; LightboxTransition.Fade fades it in.LightboxTransitionLightboxTransition.None
LoopWraps from the last image to the first, and back.boolfalse
CloseOnBackdropCloses the viewer on a click outside the image and its controls.booltrue
HideControlsAfterHow long without a pointer move or a key before the viewer's controls fade.TimeSpanTimeSpan.FromMilliseconds(3000)
ChildContentThe LightboxItems, laid out as you like.RenderFragment?

See 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#.

Connection lost

Rejoining the server…