Skip to content

Commit e9ca60d

Browse files
authored
Merge pull request #23 from ljbc1994/feat/2.0.0
feat(2.0.0): refactored to be in line with the api
2 parents 9f884fc + 550fe8f commit e9ca60d

23 files changed

Lines changed: 556 additions & 616 deletions

LICENCE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(The MIT License)
22

3-
Copyright (c) 2019 Louie Colgan
3+
Copyright (c) 2021 Louie Colgan
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the

README.md

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ object which contains the observer entry! Easy!
6060
@using Blazor.IntersectionObserver
6161
6262
<IntersectionObserve>
63-
<div>
64-
Hey... look I'm @(context != null && context.IsIntersecting ? "in view": "out of view")
63+
<div @ref="context.Ref.Current">
64+
Hey... I'm @(context.IsIntersecting ? "in view": "out of view")
6565
</div>
6666
</IntersectionObserve>
6767
```
@@ -179,14 +179,45 @@ To disconnect the observer, call `Disconnect` on the `IntersectionObserver` inst
179179
observer.Disconnect();
180180
```
181181

182+
This will remove all the observed elements from the observer, i.e.
183+
184+
```razor
185+
@using Blazor.IntersectionObserver
186+
@implements IAsyncDisposable
187+
@inject IIntersectionObserverService ObserverService
188+
189+
<div @ref="ImageRef"></div>
190+
191+
@functions {
192+
private IntersectionObserver Observer;
193+
@* Code... *@
194+
195+
public async ValueTask DisconnectAll()
196+
{
197+
if (this.Observer != null)
198+
{
199+
await this.Observer.Disconnect();
200+
}
201+
}
202+
}
203+
204+
```
205+
206+
#### `Dispose`
207+
To remove the observer, call `Dispose` on the `IntersectionObserver` instance.
208+
209+
```cs
210+
observer.Dispose();
211+
```
212+
182213
This is a useful method to clean up observers when components are disposed of, i.e.
183214

184215
```razor
185216
@using Blazor.IntersectionObserver
186217
@implements IAsyncDisposable
187218
@inject IIntersectionObserverService ObserverService
188219
189-
<div @ref="NicolasCageRef"></div>
220+
<div @ref="ImageRef"></div>
190221
191222
@functions {
192223
private IntersectionObserver Observer;
@@ -196,7 +227,7 @@ This is a useful method to clean up observers when components are disposed of, i
196227
{
197228
if (this.Observer != null)
198229
{
199-
await this.Observer.Disconnect();
230+
await this.Observer.Dispose();
200231
}
201232
}
202233
}
@@ -209,12 +240,14 @@ This is a useful method to clean up observers when components are disposed of, i
209240

210241
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`!
211242

243+
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.
244+
212245
```razor
213246
@* Injecting service... *@
214247
215248
<IntersectionObserve>
216-
<div>
217-
Hey... look I'm @(context != null && context.IsIntersecting ? "intersecting!": "not intersecting!")
249+
<div @ref="context.Ref.Current">
250+
Hey... look I'm @(context.IsIntersecting ? "intersecting!": "not intersecting!")
218251
</div>
219252
</IntersectionObserve>
220253
@@ -227,13 +260,18 @@ Rather than directly interfacing with the service, you can use this convenience
227260
- `IsIntersecting` (`bool`) - Whether the element is intersecting - used for two-way binding.
228261
- `Options` (`IntersectionObserverOptions`) - The options for the observer.
229262
- `Once` (`bool`) - Only observe once for an intersection, then the instance disposes of itself.
230-
- `Style` (`string`) - The style for the element.
231-
- `Class` (`string`) - The class for the element.
232263

233264
#### Context
234-
The context is the `IntersectionObserverEntry` object, with the following signature:
265+
The context is the `IntersectionObserverContext` object, with the following signature:
235266

236267
```cs
268+
public class IntersectionObserverContext
269+
{
270+
public IntersectionObserverEntry Entry { get; set; }
271+
public ForwardReference Ref { get; set; } = new ForwardReference();
272+
public bool IsIntersecting => this.Entry?.IsIntersecting ?? false;
273+
}
274+
237275
public class IntersectionObserverEntry
238276
{
239277
public bool IsIntersecting { get; set; }
@@ -250,9 +288,6 @@ public class IntersectionObserverEntry
250288
}
251289
```
252290

253-
## Implementation Detail
254-
To avoid creating an unnecessary number of observers for every element being observed, if a `Blazor Observer` shares exactly the same options as another, they will both use the same `IntersectionObserver` instance in JS. As each `Blazor Observer` has a unique id and callback, the elements that are being observed will still be passed to their respective `Blazor Observer`.
255-
256291
## Feature Requests
257292
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!
258293

azure-pipelines.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pool:
88

99
variables:
1010
buildConfiguration: 'Release'
11+
variables:
12+
Major: '2'
13+
Minor: '0'
14+
Patch: '0'
1115

1216
steps:
1317
- task: UseDotNet@2
@@ -53,8 +57,8 @@ steps:
5357
packagesToPack: 'src/Blazor.IntersectionObserver/*.csproj'
5458
configuration: '$(buildConfiguration)'
5559
versioningScheme: byPrereleaseNumber
56-
majorVersion: '0'
57-
minorVersion: '1'
58-
patchVersion: '0'
60+
majorVersion: '$(Major)'
61+
minorVersion: '$(Minor)'
62+
patchVersion: '$(Patch)'
5963

6064

samples/Blazor.IntersectionObserver.Client/Pages/Index.razor

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
</div>
1616

1717
<div class="window">
18-
<IntersectionObserve Options="@ObserverOptions" Class="box" Style="@BoxStyle" OnChange="@OnIntersectingChanged">
19-
<div class="vertical">
20-
Welcome to <strong>The Box!</strong>
18+
<IntersectionObserve Options="@ObserverOptions" OnChange="@OnIntersectingChanged">
19+
<div class="box" style="@BoxStyle" @ref="context.Ref.Current">
20+
<div class="vertical">
21+
Welcome to <strong>The Box!</strong>
22+
</div>
2123
</div>
2224
</IntersectionObserve>
2325
</div>

samples/Blazor.IntersectionObserver.Client/Pages/LazyImages.razor

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
<div class="card-columns">
2020
@foreach (var image in Images)
2121
{
22-
<div class="card">
23-
<IntersectionObserve Class="image-container" Once=@true>
24-
@{var isIntersecting = (context != null && context.IsIntersecting);}
25-
<div class="image-loader @(isIntersecting ? "image-loader--hide" : "image-loader--show")">Loading...</div>
26-
<img class="image @(isIntersecting ? "image--show" : "image--hide")" src="@(isIntersecting ? image: "")" />
22+
<div class="card" @key="image">
23+
<IntersectionObserve Once=@true>
24+
<div class="image-container" @ref="context.Ref.Current">
25+
@{var isIntersecting = context.IsIntersecting;}
26+
<div class="image-loader @(isIntersecting ? "image-loader--hide" : "image-loader--show")">Loading...</div>
27+
<img class="image @(isIntersecting ? "image--show" : "image--hide")" src="@(isIntersecting ? image: "")" />
28+
</div>
2729
</IntersectionObserve>
2830
<div class="card-body">
2931
<h5 class="card-title">Card title</h5>
@@ -47,7 +49,7 @@
4749
{
4850
var height = RandomNumber(250, 650);
4951
var width = RandomNumber(250, 650);
50-
return $"https://www.placehold.it/{width}x{height}";
52+
return $"https://picsum.photos/{width}/{height}?id=${x}";
5153
})
5254
.ToList();
5355
}

samples/Blazor.IntersectionObserver.Server/Pages/Index.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
</div>
1616

1717
<div class="window">
18-
<IntersectionObserve Options="@ObserverOptions" Class="box" Style="@BoxStyle" OnChange="@OnIntersectingChanged">
19-
<div class="vertical">
18+
<IntersectionObserve Options="@ObserverOptions" OnChange="@OnIntersectingChanged">
19+
<div class="box" style="@BoxStyle" @ref="context.Ref.Current">
2020
Welcome to <strong>The Box!</strong>
2121
</div>
2222
</IntersectionObserve>

samples/Blazor.IntersectionObserver.Server/Pages/LazyImages.razor

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
<div class="card-columns">
2020
@foreach (var image in Images)
2121
{
22-
<div class="card">
23-
<IntersectionObserve Class="image-container" Once=@true>
24-
@{var isIntersecting = (context != null && context.IsIntersecting);}
25-
<div class="image-loader @(isIntersecting ? "image-loader--hide" : "image-loader--show")">Loading...</div>
26-
<img class="image @(isIntersecting ? "image--show" : "image--hide")" src="@(isIntersecting ? image: "")" />
22+
<div class="card" @key="image">
23+
<IntersectionObserve Once=@true>
24+
<div class="image-container" @ref="context.Ref.Current">
25+
@{var isIntersecting = context.IsIntersecting;}
26+
<div class="image-loader @(isIntersecting ? "image-loader--hide" : "image-loader--show")">Loading...</div>
27+
<img class="image @(isIntersecting ? "image--show" : "image--hide")" src="@(isIntersecting ? image: "")" />
28+
</div>
2729
</IntersectionObserve>
2830
<div class="card-body">
2931
<h5 class="card-title">Card title</h5>
@@ -47,7 +49,7 @@
4749
{
4850
var height = RandomNumber(250, 650);
4951
var width = RandomNumber(250, 650);
50-
return $"https://www.placehold.it/{width}x{height}";
52+
return $"https://picsum.photos/{width}/{height}?id={x}";
5153
})
5254
.ToList();
5355
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Components;
2+
3+
namespace Blazor.IntersectionObserver.API
4+
{
5+
public class ForwardReference
6+
{
7+
private ElementReference current;
8+
9+
public ElementReference Current
10+
{
11+
get => this.current;
12+
set => this.Set(value);
13+
}
14+
15+
public void Set(ElementReference value)
16+
{
17+
this.current = value;
18+
}
19+
}
20+
}

src/Blazor.IntersectionObserver/Blazor.IntersectionObserver.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
<Title>Blazor Intersection Observer</Title>
1717
<Description>Intersection Observer API for Blazor applications</Description>
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
19-
<Version>1.1.0</Version>
19+
<Version>2.0.0</Version>
2020
<Product>BlazorIntersectionObserver</Product>
21-
<PackageReleaseNotes>06/12/2020
21+
<PackageReleaseNotes>
22+
19/05/2021
23+
- *BREAKING CHANGE* The IntersectionObserve component now requires a reference to the node it's observing.
24+
- The imported observer script is now minified.
25+
26+
06/12/2020
2227
- Updated project to use dotnet 5</PackageReleaseNotes>
2328
<PackageLicenseFile>LICENCE.txt</PackageLicenseFile>
29+
<Copyright>Copyright © 2021 - Louie Colgan</Copyright>
2430
</PropertyGroup>
2531

2632
<ItemGroup>

src/Blazor.IntersectionObserver/Configuration/Constants.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ namespace Blazor.IntersectionObserver.Configuration
22
{
33
internal static class Constants
44
{
5-
public static string CREATE = $"create";
5+
public static string CREATE = "create";
66

7-
public static string OBSERVE = $"observe";
7+
public static string OBSERVE = "observe";
88

9-
public static string UNOBSERVE = $"unobserve";
9+
public static string UNOBSERVE = "unobserve";
1010

11-
public static string DISCONNECT = $"disconnect";
11+
public static string DISCONNECT = "disconnect";
1212

13-
public static string OBSERVE_ELEMENT = $"observeElement";
13+
public static string OBSERVE_ELEMENT = "observeElement";
14+
15+
public static string REMOVE = "remove";
1416
}
1517
}

0 commit comments

Comments
 (0)