Skip to content

Commit bc75326

Browse files
ljbc1994Louie Colgan
andauthored
feat: add helpful error messages to IntersectionObserve component (#34)
* feat: add helpful error messages to `IntersectionObserve` component * fix: remove test for checking error messages Co-authored-by: Louie Colgan <[email protected]>
1 parent de338ad commit bc75326

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<!-- Versioning properties -->
3535
<PropertyGroup>
36-
<VersionPrefix Condition=" '$(VersionPrefix)'=='' ">3.0.0</VersionPrefix>
36+
<VersionPrefix Condition=" '$(VersionPrefix)'=='' ">3.0.1</VersionPrefix>
3737
<VersionSuffix Condition=" '$(VersionSuffix)'=='' ">dev</VersionSuffix>
3838
</PropertyGroup>
3939

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ public class IntersectionObserverEntry
291291
}
292292
```
293293

294+
## Additional Information
295+
296+
### Upgrading to `2.0.1`+
297+
298+
In versions prior to `2.0.1`, the `IntersectionObserve` component didn't require a reference to the node as it was wrapped in an element that was automatically observed. This was changed to ensure the consumer provides the reference to prevent any potential layout issues and make it explicit what element should be observed.
299+
300+
Therefore, before `2.0.1`, if the consumer had an element with `display: none;` within the `IntersectionObserve` component, this would have worked. However, as we're now observing the element provided as opposed to a wrapped element, this will no longer work. To resolve this, you can wrap the observed element in a div and observe the container div instead of the observed element.
301+
294302
## Feature Requests
295303
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!
296304

src/Ljbc1994.Blazor.IntersectionObserver/IntersectionObserve.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Ljbc1994.Blazor.IntersectionObserver.Components
1010
{
1111
public class IntersectionObserve : ComponentBase, IAsyncDisposable
1212
{
13+
private const string NO_ELEMENT_MESSAGE = "The element reference to observe is required, for example: @ref=\"Context.Ref.Current\" must be provided on the element";
14+
1315
[Inject] private IIntersectionObserverService ObserverService { get; set; }
1416

1517
[Parameter] public RenderFragment<IntersectionObserverContext> ChildContent { get; set; }
@@ -40,12 +42,14 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4042

4143
private async Task InitialiseObserver()
4244
{
43-
if (this.IntersectionObserverContext?.Ref?.Current == null)
45+
var elementReference = this.IntersectionObserverContext?.Ref?.Current;
46+
47+
if (elementReference == null || Equals(elementReference, default(ElementReference)))
4448
{
45-
throw new Exception("You need to provide the element to observe, for example: @ref=\"Context.Ref.Current\"");
49+
throw new Exception(NO_ELEMENT_MESSAGE);
4650
}
4751

48-
this.Observer = await this.ObserverService.Observe(this.IntersectionObserverContext.Ref.Current, this.OnIntersectUpdate, this.Options);
52+
this.Observer = await this.ObserverService.Observe(elementReference.Value, this.OnIntersectUpdate, this.Options);
4953
}
5054

5155
private async void OnIntersectUpdate(IList<IntersectionObserverEntry> entries)
@@ -80,6 +84,10 @@ public async ValueTask DisposeAsync()
8084

8185
protected override void BuildRenderTree(RenderTreeBuilder builder)
8286
{
87+
if (this.ChildContent == null)
88+
{
89+
throw new Exception($"No element found to observe. {NO_ELEMENT_MESSAGE}");
90+
}
8391
this.ChildContent(this.IntersectionObserverContext)(builder);
8492
}
8593
}

src/Ljbc1994.Blazor.IntersectionObserver/Ljbc1994.Blazor.IntersectionObserver.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
<Title>Blazor Intersection Observer</Title>
1717
<Description>Intersection Observer API for Blazor applications</Description>
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
19-
<Version>3.0.0</Version>
19+
<Version>3.0.1</Version>
2020
<Product>BlazorIntersectionObserver</Product>
2121
<PackageReadmeFile>README.md</PackageReadmeFile>
2222
<PackageReleaseNotes>
23+
3.0.1
24+
- Add helpful error messages if the consumer fails to provide an element to observe or does not provide
25+
any child content.
26+
2327
3.0.0
2428
- *BREAKING CHANGE* Namespace has been changed to `Ljbc1994.Blazor.IntersectionObserver` to avoid
2529
namespace conflicts with Blazor libraries.

0 commit comments

Comments
 (0)