BlazorPro.BlazorSize 6.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package BlazorPro.BlazorSize --version 6.1.2                
NuGet\Install-Package BlazorPro.BlazorSize -Version 6.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazorPro.BlazorSize" Version="6.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlazorPro.BlazorSize --version 6.1.2                
#r "nuget: BlazorPro.BlazorSize, 6.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BlazorPro.BlazorSize as a Cake Addin
#addin nuget:?package=BlazorPro.BlazorSize&version=6.1.2

// Install BlazorPro.BlazorSize as a Cake Tool
#tool nuget:?package=BlazorPro.BlazorSize&version=6.1.2                

BlazorSize is a JavaScript interop library for Blazor that is used to detect the Browser's current size, change in size, and test media queries.

BlazorSize was designed to allow Razor Components to implement adaptive rendering. Handle mobile/desktop rendering changes at runtime with ease and complement your apps CSS media queries with programmatic work flows.

Add the NuGet package

Package Manager

Install-Package BlazorPro.BlazorSize 

CLI

dotnet add package BlazorPro.BlazorSize 

.csproj

<PackageReference Include="BlazorPro.BlazorSize" Version="version-number" />

Import the namespace

Add a reference to the namespace in your _Imports.razor or at the top of a page.

@using BlazorPro.BlazorSize

Configure DI

In startup.cs register ResizeListener with the applications service collection.

services.AddMediaQueryService();

Add the MediaQueryList

Add a MediaQueryList to your application's main layout or root. The MediaQueryList is responsible for communicating with all MediaQuery components in your app. The MediaQueryList component will consolidate redundant media queries and manage resources so that unused event listeners are disposed of properly.

<MediaQueryList>
    <div class="sidebar">
        <NavMenu />
    </div>

    <div class="main">
        <div class="top-row px-4">
            <a href="http://blazor.net" target="_blank" class="ml-md-auto">About</a>
        </div>

        <div class="content px-4">
            @Body
        </div>
    </div>
</MediaQueryList>

Add a MediaQuery and bind

Using the @bind-Matches directive we can easily bind to a browser's media query and respond to it.

<details> <summary >See Example</summary>

@if (IsSmall)
{
    <WeatherCards Data="forecasts"></WeatherCards>
}
else
{
    <WeatherGrid Data="forecasts"></WeatherGrid>
}

@if (IsMedium)
{
    <span>Medium</span>
}

<MediaQuery Media="@Breakpoints.OnlyMedium" @bind-Matches="IsMedium" />
<MediaQuery Media="@Breakpoints.SmallDown" @bind-Matches="IsSmall" />

@code {
    WeatherForecast[] forecasts;

    bool IsMedium = false;
    bool IsSmall = false;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
    }
}

</details>

No-Code Templates

The MediaQuery component can also use templates instead of the @bind directive. Templates are useful for swapping out UI bits when the screen size changes.

<details> <summary >See Example</summary>


<MediaQuery Media="@Breakpoints.SmallDown">
    <Matched>
        <WeatherCards Data="forecasts"></WeatherCards>
    </Matched>
    <Unmatched>
        <WeatherGrid Data="forecasts"></WeatherGrid>
    </Unmatched>
</MediaQuery>

</details>

Helpers

Common media queries are already included as helpers to keep you out of the Bootstrap docs. Stay in your code longer and write cleaner statements too!

<details> <summary >See Example</summary>


/// @media(min-width: 576px) { ... }
/// Small devices (landscape phones, 576px and up)
IsSmallUpMedia = await listener.MatchMedia(Breakpoints.SmallUp);

/// @media(min-width: 768px) { ... }
/// Medium devices (tablets, 768px and up)
IsMediumUpMedia = await listener.MatchMedia(Breakpoints.MediumUp);

// Large devices (desktops, 992px and up)
/// @media(min-width: 992px) { ... }
IsLargeUpMedia = await listener.MatchMedia(Breakpoints.LargeUp);

/// Extra large devices (large desktops, 1200px and up)
/// @media(min-width: 1200px) { ... }
IsXLargeUpMedia = await listener.MatchMedia(Breakpoints.XLargeUp);

/// Extra small devices (portrait phones, less than 576px)
/// @media(max-width: 575.98px) { ... }
IsXSmallDown = await listener.MatchMedia(Breakpoints.XSmallDown);

/// Small devices (landscape phones, less than 768px)
/// @media(max-width: 767.98px) { ... }
IsSmallDown = = await listener.MatchMedia(Breakpoints.SmallDown);

/// Medium devices (tablets, less than 992px)
/// @media(max-width: 991.98px) { ... }
IsMediumDown = = await listener.MatchMedia(Breakpoints.MediumDown);

/// Large devices (desktops, less than 1200px)
/// @media(max-width: 1199.98px) { ... }
LargeDown = = await listener.MatchMedia(Breakpoints.LargeDown);

/// Small devices (landscape phones, 576px and up)
/// @media(min-width: 576px) and(max-width: 767.98px) { ... }
IsSmallOnly = = await listener.MatchMedia(Breakpoints.OnlySmall);

/// Medium devices (tablets, 768px and up)
/// @media(min-width: 768px) and(max-width: 991.98px) { ... }
IsMediumOnly = = await listener.MatchMedia(Breakpoints.OnlyMedium);

/// Large devices (desktops, 992px and up)
/// @media(min-width: 992px) and(max-width: 1199.98px) { ... }
IsOnlyLarge = = await listener.MatchMedia(Breakpoints.OnlyLarge);

/// <summary>
/// Combines two media queries with the `and` keyword.
/// Values must include parenthesis.
/// Ex: (min-width: 992px) and (max-width: 1199.98px)
Breakpoints.Between(string min, string max)

Example:
string BetweenMediumAndLargeOnly => Breakpoints.Between(Breakpoints.MediumUp, Breakpoints.LargeDown);
// out: "(min-width: 768px) and (max-width: 1199.98px)"

IsBetweenMediumAndLargeOnly = await listener.MatchMedia(BetweenMediumAndLargeOnly);

</details>

Listening for the Resize event

The ResizeListener is a service that allows your application to listen for the browser's resize event. The ResizeListener is a throttled to improve performance and can be adjusted thru configuration. If you only need to respond the user's device or screen size the MediaQueryList & MediaQuery components provide a more performant experience.

Configure DI

In startup.cs register ResizeListener with the applications service collection.

services.AddScoped<ResizeListener>();
//services.AddResizeListener(options =>
//                            {
//                                options.ReportRate = 300;
//                                options.EnableLogging = true;
//                                options.SuppressInitEvent = true;
//                            });

Usage

This example shows how to get the browsers width/height and check for media query matches. Depending on the matched media query the view can toggle between two components WeatherGrid or WeatherCards.

<details> <summary >See Example</summary>

@inject IResizeListener listener
@implements IDisposable
@page "/fetchdata"

@using BlazorSize.Example.Data
@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates adaptive rendering of a Blazor UI.</p>

<h3>Height: @browser.Height</h3>
<h3>Width: @browser.Width</h3>
<h3>MQ: @IsSmallMedia</h3>

@if (IsSmallMedia)
{
	
    <WeatherCards Data="forecasts"></WeatherCards>
}
else
{
	
    <WeatherGrid Data="forecasts"></WeatherGrid>
}

@code {
    WeatherForecast[] forecasts;

	// We can also capture the browser's width / height if needed. We hold the value here.
    BrowserWindowSize browser = new BrowserWindowSize();

    bool IsSmallMedia = false;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }

    protected override void OnAfterRender(bool firstRender)
    {

        if (firstRender)
        {
			// Subscribe to the OnResized event. This will do work when the browser is resized.
            listener.OnResized += WindowResized;
        }
    }

    void IDisposable.Dispose()
    {
		// Always use IDisposable in your component to unsubscribe from the event.
		// Be a good citizen and leave things how you found them. 
		// This way event handlers aren't called when nobody is listening.
        listener.OnResized -= WindowResized;
    }

	// This method will be called when the window resizes.
	// It is ONLY called when the user stops dragging the window's edge. (It is already throttled to protect your app from perf. nightmares)
    async void WindowResized(object _, BrowserWindowSize window)
    {
		// Get the browsers's width / height
        browser = window;

		// Check a media query to see if it was matched. We can do this at any time, but it's best to check on each resize
        IsSmallMedia = await listener.MatchMedia(Breakpoints.SmallDown);

		// We're outside of the component's lifecycle, be sure to let it know it has to re-render.
        StateHasChanged();
    }

}

</details>

Reference the JavaScript interop

.NET 3.x is not supported by BlazorSize 5.0 or higher

This is only needed for .NET 3.2 or below

Add the JavaScript interop to your application's index.html or _hosts.cshtml

    
    <script src="_content/BlazorPro.BlazorSize/blazorSize.min.js"></script>
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages (10)

Showing the top 5 NuGet packages that depend on BlazorPro.BlazorSize:

Package Downloads
DataJuggler.BlazorGallery

A project template for creating a Blazor Image Gallery project that uses SQL Server for a backend. You will need SQL Server or SQL Server Express to run this project. See the Read Me for more info. Watch this video to see a demo https://youtu.be/yQz1dqYiy2g.

Egea.Extensions.Razor

Package Description

Blazm.Components

A collection of Blazor components

BlazorPro.BlazorSize.UnitTesting

A testing library to provide helper extensions for BlazorSize.

Jakar.Extensions.Blazor

Extensions to aid in development.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on BlazorPro.BlazorSize:

Repository Stars
telerik/blazor-ui
A collection of examples related to Telerik UI for Blazor Components: https://www.telerik.com/blazor-ui
Version Downloads Last updated
9.0.0 415 11/15/2024
8.0.0 82,714 4/22/2024
8.0.0-preview 131 4/15/2024
6.2.2 127,747 8/30/2023
6.2.1 95,779 4/25/2023
6.2.0 46,050 1/6/2023
6.1.2 146,713 2/12/2022
6.1.1 8,253 1/28/2022
6.1.0 23,584 12/1/2021
6.0.0 32,908 11/10/2021
6.0.0-pre2 2,606 10/5/2021
6.0.0-pre 270 8/23/2021
5.0.0 61,443 5/27/2021
5.0.0-pre 1,136 5/25/2021
3.2.1 19,629 3/9/2021
3.2.0 2,310 3/5/2021
3.1.0 62,531 11/30/2020
3.0.0 1,779 11/23/2020
2.3.0 45,052 8/11/2020
2.0.2 21,945 4/21/2020
1.2.2 10,300 1/26/2020
1.2.1 1,126 1/23/2020
1.2.0 1,281 1/22/2020

6.1.2 Fixed casing on JS modules, fixes deployment issues on Linux. (regression)
6.1.1 Replaced deprecated `addListener` method. Removed console log messages, again.
6.1.0 Removed dependency for bUnit.Core. Breaking changes may occur in test projects. Removed console log messages.
6.0.0 Fixed possible null issues. .NET 6 support. Moved APIs for bUnit to BlazorPro.BlazorSize.UnitTesting.
5.0.0 Added support for bUnit. Removed support for .NET 3.x
4.0.0 no release, aligning with .NET 5.0. Next version will no longer support .NET 3.x
3.2.0
Added service interfaces, IMediaQueryService, IResizeListener to support unit testing scenarios
Added end-to-end testing on source for QA
3.1.0
Added .NET 5 module loading for BlazorSize Resize Listener.
Fixed casing on JS modules, fixes deployment issues on Linux.
3.0.0
Added .NET 5 module loading for BlazorSize Media Queries.
2.3.0
Fixed additional regressions from 2.2.0, update recommended.
2.2.0
Fixed possible regressions, update recommended.
Updated Blazor dependencies.
2.1.1
Fixed MSBuild including *.json files breaking NuGet pkg.
2.0.0
Added MediaQueryList & MediaQuery components.
     1.2.2
     Fixes initalization issues with Client-Side Blazor
     1.2.0
     Full-release