Blazor.Extensions.ScreenObserver 1.0.2

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Blazor.Extensions.ScreenObserver --version 1.0.2
                    
NuGet\Install-Package Blazor.Extensions.ScreenObserver -Version 1.0.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="Blazor.Extensions.ScreenObserver" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Blazor.Extensions.ScreenObserver" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Blazor.Extensions.ScreenObserver" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Blazor.Extensions.ScreenObserver --version 1.0.2
                    
#r "nuget: Blazor.Extensions.ScreenObserver, 1.0.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.
#:package Blazor.Extensions.ScreenObserver@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Blazor.Extensions.ScreenObserver&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Blazor.Extensions.ScreenObserver&version=1.0.2
                    
Install as a Cake Tool

Blazor.Extensions.ScreenObserver

An open-source Blazor library that efficiently tracks viewport and DOM element dimensions in real-time using JSInterop. Simplifies creating adaptive layouts by providing a clean C# API to monitor both screen size changes and element dimension variations for responsive UIs.

Installation

dotnet add package Blazor.Extensions.ScreenObserver

Setup

Register the service in your Program.cs file:

using Blazor.Extentions.ScreenObserver
//...
builder.Services.AddScreenObserver();

Add the JavaScript file to your project. You can do this by adding the following line to your _Host.cshtml, App.razor or index.html file:

<body>
    @* ... *@
    <script src="_content/Blazor.Extensions.ScreenObserver/screenObserver.js"></script>
</body>

Usage

@implements IAsyncDisposable
@using Blazor.Extentions.ScreenObserver
@inject IBrowserScreenService ScreenService


<div id="parentElement" style="max-width:800px;width:100%;padding:16px;background-color:red;">
    <div id="childElement" style="width:50%;padding:16px;background-color:blue;"></div>
</div>

<ul>
    <li>Screen Width: @(_screenWidth)</li>
    <li>Parent Width: @(_parentWidth)</li>
    <li>Child Width: @(_childWidth)</li>
</ul>


@code {
    int _screenWidth;
    int _parentWidth;
    int _childWidth;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            // Subscribe to screen size changes
            _screenWidth = await ScreenService.ObserveScreenSize(OnScreenChanged);
            _parentWidth = await ScreenService.ObserveElement("parentElement", OnParentElementChanged);
            _childWidth = await ScreenService.ObserveElement("childElement", OnChildElementChanged);

            StateHasChanged();
        }
    }

    void OnScreenChanged(int width)
    {
        _screenWidth = width;
        StateHasChanged();
    }

    void OnParentElementChanged(int width)
    {
        _parentWidth = width;
        StateHasChanged();
    }

    void OnChildElementChanged(int width)
    {
        _childWidth = width;
        StateHasChanged();
    }

    public async ValueTask DisposeAsync()
    {
        // Unsubscribe when component is disposed
        await ScreenService.StopObservingScreen(OnScreenChanged);
        await ScreenService.StopObservingElement("parentElement", OnParentElementChanged);
        await ScreenService.StopObservingElement("childElement", OnChildElementChanged);

        // Or..
        // Dispose all
        await ScreenService.DisposeAsync();
    }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated