Blazor Lightbox

 


I came across an article about HTML lightboxes.  I did a quick search and I could not find a lightbox control for Blazor.  So I decided to create one.  As it turns out, the Blazor part was educational.

For those not familiar with what a Lightbox control is.  It is a control that displays images and videos by filling the screen and dimming out the rest of the web page.

HTML and CSS

Doing a quick search I found a lightbox on Codepen source.  The HTML consists of a couple of anchor tags.

    <!-- thumbnail image wrapped in a link -->
    <a href="#img1">
      <img src="https://picsum.photos/seed/9/500/300">
    </a>


    <!-- lightbox container hidden with CSS -->
    <a href="#" class="lightbox" id="img1">
      <span style="background-image: url('https://picsum.photos/seed/9/900/450')"></span>
    </a>


The CSS consists of basic UI styling such as centering.

If you notice, for the larger content, the image is used as the background image of the anchor tag.

OnClick Events

This is the Blazor coding part.  When a user clicks on the thumbnail we need to hide the thumbnail and show the larger content while greying out the background web page.  We already have the CSS to grey out the background, we need 2 new styles:
1. Turn the thumbnail off
    .smallbox {
        display: none;
    }

2. Turn the thumbnail on
    .lbon {
        display: block;
    }

With the style defined, we need to add and remove the styles on the thumbnail click
    
    private void ShowLightBox()
    {
        onStyle = "lbon";
        smallboxShow = "smallbox";
        StateHasChanged();
    }

Then add the event to the anchor tag:

    <a class="@smallboxShow" @onclick="ShowLightBox">
        <img src="https://picsum.photos/seed/5/500/300">
    </a>

We then need to reverse the displays when the user clicks anywhere within the large content:

    private void HideLightBox()
    {
        onStyle = "";
        smallboxShow = "";
        StateHasChanged();
    }

Add it to the large content tag:

        <a class="lightbox @onStyle" id="img1" @onclick="HideLightBox">
  <span style="background-image: url('https://picsum.photos/seed/5/900/450')"></span>
        </a>

Make it a Component

I wanted to take the control a step further so I created a component for it.  I needed 2 parameters:

    [Parameter]
    public string ThumbnailSource { get; set; }

    [Parameter]
    public string LargeImageSource { get; set; }

These are to let the user define what images to use for the thumbnail and the larger image.  We set the values in the OnInitializedAsync method:

    protected override async Task OnInitializedAsync()
    {
        largeImage = $"{LargeImageSource}900/450";
        smallImage = $"{ThumbnailSource}500/300";
        await Task.CompletedTask;
    }

We do this so so can control the size of each image.

The user adds these parameters when calling the component:

        <LightboxComponent 
            ThumbnailSource="https://picsum.photos/seed/5/" 
            LargeImageSource="https://picsum.photos/seed/5/">
        </LightboxComponent>

So this gives us a nice lightbox component.  But a real lightbox control handles video as the larger content.

Adding Video Support

It took a couple of steps to add the video support:
1. Add a new video source parameter

    [Parameter]
    public string VideoSource { get; set; }

2. Add the video player HTML
3. Add an additional show video style
4. Add a check to see if a video source was passed in as a parameter, if not default to the image, else use the video

    @if (string.IsNullOrEmpty(VideoSource))
    {
        <a class="lightbox @onStyle" id="img1" @onclick="HideLightBox">
            <span style="background-image: url(@largeImage)"></span>
        </a>
    }
    else
    {
        <div class="lightbox @videoStyle" @onclick="HideLightBox">
            <iframe src="@VideoSource" width="900" height="450" frameborder="0" autoplay style="margin-left:25%;margin-right:25%;margin-top:25%"></iframe>
        </div>
    }

This specific HTML element supports streaming video such as Vimeo or YouTube.  If you have the media source on your site, you can use the normal video HTML tag:

    <video width="320" height="240" controls>
       <source src="movie.mp4" type="video/mp4">
    </video>


Summary

We got to do a couple of cool Blazor tricks in this project:
1. Swap style classes
2. Create a component with parameters
3. Play a video

We ended up with a nice component we can add to our library.  A good next step would be to support linked images in your lightbox.  It will allow the user to step through a list of content.




Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component