BlazorIntersectionObserver 2.0.3
See the version list below for details.
dotnet add package BlazorIntersectionObserver --version 2.0.3
NuGet\Install-Package BlazorIntersectionObserver -Version 2.0.3
<PackageReference Include="BlazorIntersectionObserver" Version="2.0.3" />
paket add BlazorIntersectionObserver --version 2.0.3
#r "nuget: BlazorIntersectionObserver, 2.0.3"
// Install BlazorIntersectionObserver as a Cake Addin #addin nuget:?package=BlazorIntersectionObserver&version=2.0.3 // Install BlazorIntersectionObserver as a Cake Tool #tool nuget:?package=BlazorIntersectionObserver&version=2.0.3
A comprehensive wrapper around the Intersection Observer API, giving you all the goodness of observing intersections in a performant way.
This is a wrapper around the Intersection Observer API so that you can use it in Blazor
for .NET 5. It has the same API structure with convenience methods and components for a better dev experience. It works with both Blazor WebAssembly and Blazor Server.
Get Started
1. Installation
Install BlazorIntersectionObserver
through NuGet.
> dotnet add package BlazorIntersectionObserver
2. Register the service
Now you'll need to add the service to the service configuration.
WebAssembly (Program.cs)
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddIntersectionObserver();
}
}
OR
Server (Startup.cs)
public class Startup {
public void ConfigureServices(IServiceCollection services)
{
services.AddIntersectionObserver();
}
}
3. Use it!
For the quickest setup, use the IntersectionObserve
component. This provides an implicit context
object which contains the observer entry! Easy!
Component setup
@using Blazor.IntersectionObserver
<IntersectionObserve>
<div @ref="context.Ref.Current">
Hey... I'm @(context.IsIntersecting ? "in view": "out of view")
</div>
</IntersectionObserve>
OR
Service setup
To directly use the service, you just need to inject it and observe the element(s).
@using Blazor.IntersectionObserver
@inject IIntersectionObserverService ObserverService
<img @ref="ImageElement" src="@(IsIntersecting ? "https://www.placecage.com/g/500/500" : "")"/>
@functions {
public ElementReference ImageElement { get; set; }
public bool IsIntersecting { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await SetupObserver();
}
}
public async void SetupObserver()
{
await ObserverService.Observe(ImageElement, (entries) =>
{
var entry = entries.FirstOrDefault();
IsIntersecting = entry.IsIntersecting;
StateHasChanged();
});
}
}
Documentation and Usage
Options
You can pass through options
to the ObserverService
methods, these are the same as the Intersection Observer API options.
Example
var options = new IntersectionObserverOptions {
Root = BodyRef,
Threshold = new List<double> { 0.25, 0.5, 1 },
RootMargin = "10px 10px 10px 10px"
};
Service Methods
Observe
This a shorthand way of observing an element by providing:
- The element you want to observe.
- The callback to trigger on an intersection update.
- The intersection observer options.
This returns an IntersectionObserver
instance, allowing you to disconnect
the observer or unobserve
an element. Or if you wish, observe additional elements.
var observer = await ObserverService.Observe(ElementRef, (entries) => {
IsIntersecting = entries.FirstOrDefault().IsIntersecting;
StateHasChanged();
}, options);
Create
The Create
method follows the same approach as the Intersection Observer API, you create the observer and then pass elements you wish to observe by calling the Observe
method on the observer instance. To create the observer, provide the following:
- The callback to trigger on an intersection update.
- The intersection observer options.
This returns an IntersectionObserver
instance, allowing you to Observe
elements. This also provides the ability to disconnect
or unobserve
the element.
var observer = await ObserverService.Create((entries) => {
IsIntersecting = entries.FirstOrDefault().IsIntersecting;
StateHasChanged();
}, options);
await observer.Observe(FirstImage);
await observer.Unobserve(FirstImage);
IntersectionObserver
Methods
Observe
To observe an element, provide the element reference to the IntersectionObserver
instance by calling Observe
.
observer.Observe(ElementReference);
Unobserve
To unobserve an element, provide the element reference to the IntersectionObserver
instance by calling Unobserve
.
observer.Unobserve(ElementReference);
Disconnect
To disconnect the observer, call Disconnect
on the IntersectionObserver
instance.
observer.Disconnect();
This will remove all the observed elements from the observer, i.e.
@using Blazor.IntersectionObserver
@implements IAsyncDisposable
@inject IIntersectionObserverService ObserverService
<div @ref="ImageRef"></div>
@functions {
private IntersectionObserver Observer;
@* Code... *@
public async ValueTask DisconnectAll()
{
if (this.Observer != null)
{
await this.Observer.Disconnect();
}
}
}
Dispose
To remove the observer, call Dispose
on the IntersectionObserver
instance.
observer.Dispose();
This is a useful method to clean up observers when components are disposed of, i.e.
@using Blazor.IntersectionObserver
@implements IAsyncDisposable
@inject IIntersectionObserverService ObserverService
<div @ref="ImageRef"></div>
@functions {
private IntersectionObserver Observer;
@* Code... *@
public async ValueTask DisposeAsync()
{
if (this.Observer != null)
{
await this.Observer.Dispose();
}
}
}
Component
<IntersectionObserve>
Rather than directly interfacing with the service, you can use this convenience component for quick and easy observing. You can access the observer entry through the implicit @context
!
You need to make sure to provide the reference of the element you want to observe, this is done by passing the element reference to the context reference.
@* Injecting service... *@
<IntersectionObserve>
<div @ref="context.Ref.Current">
Hey... look I'm @(context.IsIntersecting ? "intersecting!": "not intersecting!")
</div>
</IntersectionObserve>
@* Component code... *@
Props
OnChange
(EventCallback<IntersectionObserverEntry>
) - When the intersection observer has a entry update.IsIntersecting
(bool
) - Whether the element is intersecting - used for two-way binding.Options
(IntersectionObserverOptions
) - The options for the observer.Once
(bool
) - Only observe once for an intersection, then the instance disposes of itself.
Context
The context is the IntersectionObserverContext
object, with the following signature:
public class IntersectionObserverContext
{
public IntersectionObserverEntry Entry { get; set; }
public ForwardReference Ref { get; set; } = new ForwardReference();
public bool IsIntersecting => this.Entry?.IsIntersecting ?? false;
}
public class IntersectionObserverEntry
{
public bool IsIntersecting { get; set; }
public double IntersectionRatio { get; set; }
public DOMRectReadOnly BoundingClientRect { get; set; }
public DOMRectReadOnly RootBounds { get; set; }
public bool IsVisible { get; set; }
public double Time { get; set; }
}
Feature Requests
There's so much that IntersectionObserver
can do, so if you have any requests or you want better documentation and examples, feel free to make a pull request or create an issue!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
-
net5.0
- Microsoft.AspNetCore.Components.Web (>= 5.0.0)
- Microsoft.JSInterop (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
19/05/2021
- *BREAKING CHANGE* The IntersectionObserve component now requires a reference to the node it's observing.
- The imported observer script is now minified.
06/12/2020
- Updated project to use dotnet 5