Skip to content

Commit febbfa0

Browse files
author
Louie Colgan
authored
feat(readme): updated readme with api details
# What's the change? - Cleaned up readme with accurate examples - Added observer entry class for easier API reference
1 parent 6cefc53 commit febbfa0

1 file changed

Lines changed: 47 additions & 14 deletions

File tree

README.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,25 @@ Install `BlazorIntersectionObserver` through NuGet.
2323

2424
Now you'll need to add the service to the service configuration.
2525

26-
```cs
27-
using Blazor.IntersectionObserver;
26+
#### WebAssembly (Program.cs)
2827

29-
public class Startup
28+
```cs
29+
public class Program
3030
{
31+
public static async Task Main(string[] args)
32+
{
33+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
34+
builder.Services.AddIntersectionObserver();
35+
}
36+
}
37+
```
38+
39+
*OR*
40+
41+
#### Server (Startup.cs)
42+
43+
```cs
44+
public class Startup {
3145
public void ConfigureServices(IServiceCollection services)
3246
{
3347
services.AddIntersectionObserver();
@@ -43,11 +57,11 @@ object which contains the observer entry! Easy!
4357
#### Component setup
4458

4559
```razor
46-
@using Blazor.IntersectionObserver.Components
60+
@using Blazor.IntersectionObserver
4761
4862
<IntersectionObserve>
4963
<div>
50-
Hey... look I'm @(context?.IsIntersecting ? "in view": "out of view")
64+
Hey... look I'm @(context != null && context.IsIntersecting ? "in view": "out of view")
5165
</div>
5266
</IntersectionObserve>
5367
```
@@ -65,14 +79,14 @@ To directly use the service, you just need to inject it and observe the element(
6579
<img ref="ImageElement" src="@(IsIntersecting ? "https://www.placecage.com/g/500/500" : "")"/>
6680
6781
@functions {
68-
public ElementRef ImageElement { get; set; }
82+
public ElementReference ImageElement { get; set; }
6983
public bool IsIntersecting { get; set; }
7084
7185
protected override async Task OnAfterRenderAsync(bool firstRender)
7286
{
7387
if (firstRender)
7488
{
75-
SetupObserver();
89+
await SetupObserver();
7690
}
7791
}
7892
@@ -116,7 +130,7 @@ This a shorthand way of observing an element by providing:
116130
This returns an `IntersectionObserver` instance, allowing you to `disconnect` the observer or `unobserve` an element. Or if you wish, observe additional elements.
117131

118132
```cs
119-
var observer = ObserverService.Observe(ElementRef, (entries) => {
133+
var observer = await ObserverService.Observe(ElementRef, (entries) => {
120134
IsIntersecting = entries.FirstOrDefault().IsIntersecting;
121135
StateHasChanged();
122136
}, options);
@@ -132,13 +146,13 @@ The `Create` method follows the same approach as the Intersection Observer API,
132146
This returns an `IntersectionObserver` instance, allowing you to `Observe` elements. This also provides the ability to `disconnect` or `unobserve` the element.
133147

134148
```cs
135-
var observer = ObserverService.Create((entries) => {
149+
var observer = await ObserverService.Create((entries) => {
136150
IsIntersecting = entries.FirstOrDefault().IsIntersecting;
137151
StateHasChanged();
138152
}, options);
139153

140-
observer.Observe(FirstImage);
141-
observer.Unobserve(FirstImage);
154+
await observer.Observe(FirstImage);
155+
await observer.Unobserve(FirstImage);
142156
```
143157

144158
### `IntersectionObserver` Methods
@@ -148,14 +162,14 @@ observer.Unobserve(FirstImage);
148162
To observe an element, provide the element reference to the `IntersectionObserver` instance by calling `Observe`.
149163

150164
```cs
151-
observer.Observe(ElementRef);
165+
observer.Observe(ElementReference);
152166
```
153167

154168
#### `Unobserve`
155169
To unobserve an element, provide the element reference to the `IntersectionObserver` instance by calling `Unobserve`.
156170

157171
```cs
158-
observer.Unobserve(ElementRef);
172+
observer.Unobserve(ElementReference);
159173
```
160174

161175
#### `Disconnect`
@@ -200,7 +214,7 @@ Rather than directly interfacing with the service, you can use this convenience
200214
201215
<IntersectionObserve>
202216
<div>
203-
Hey... look I'm @(context?.IsIntersecting ? "intersecting!": "not intersecting!")
217+
Hey... look I'm @(context != null && context.IsIntersecting ? "intersecting!": "not intersecting!")
204218
</div>
205219
</IntersectionObserve>
206220
@@ -216,6 +230,25 @@ Rather than directly interfacing with the service, you can use this convenience
216230
- `Style` (`string`) - The style for the element.
217231
- `Class` (`string`) - The class for the element.
218232

233+
#### Context
234+
The context is the `IntersectionObserverEntry` object, with the following signature:
235+
236+
```cs
237+
public class IntersectionObserverEntry
238+
{
239+
public bool IsIntersecting { get; set; }
240+
241+
public double IntersectionRatio { get; set; }
242+
243+
public DOMRectReadOnly BoundingClientRect { get; set; }
244+
245+
public DOMRectReadOnly RootBounds { get; set; }
246+
247+
public bool IsVisible { get; set; }
248+
249+
public double Time { get; set; }
250+
}
251+
```
219252

220253
## Implementation Detail
221254
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`.

0 commit comments

Comments
 (0)