diff --git a/src/Components/Web/src/Virtualization/Virtualize.cs b/src/Components/Web/src/Virtualization/Virtualize.cs
index ec39b20818..3a99973768 100644
--- a/src/Components/Web/src/Virtualization/Virtualize.cs
+++ b/src/Components/Web/src/Virtualization/Virtualize.cs
@@ -253,6 +253,15 @@ namespace Microsoft.AspNetCore.Components.Web.Virtualization
{
CalcualteItemDistribution(spacerSize, spacerSeparation, containerSize, out var itemsBefore, out var visibleItemCapacity);
+ // Since we know the before spacer is now visible, we absolutely have to slide the window up
+ // by at least one element. If we're not doing that, the previous item size info we had must
+ // have been wrong, so just move along by one in that case to trigger an update and apply the
+ // new size info.
+ if (itemsBefore == _itemsBefore && itemsBefore > 0)
+ {
+ itemsBefore--;
+ }
+
UpdateItemDistribution(itemsBefore, visibleItemCapacity);
}
@@ -262,6 +271,15 @@ namespace Microsoft.AspNetCore.Components.Web.Virtualization
var itemsBefore = Math.Max(0, _itemCount - itemsAfter - visibleItemCapacity);
+ // Since we know the after spacer is now visible, we absolutely have to slide the window down
+ // by at least one element. If we're not doing that, the previous item size info we had must
+ // have been wrong, so just move along by one in that case to trigger an update and apply the
+ // new size info.
+ if (itemsBefore == _itemsBefore && itemsBefore < _itemCount - visibleItemCapacity)
+ {
+ itemsBefore++;
+ }
+
UpdateItemDistribution(itemsBefore, visibleItemCapacity);
}
diff --git a/src/Components/test/E2ETest/Tests/VirtualizationTest.cs b/src/Components/test/E2ETest/Tests/VirtualizationTest.cs
index f047bbb002..1bbf45d903 100644
--- a/src/Components/test/E2ETest/Tests/VirtualizationTest.cs
+++ b/src/Components/test/E2ETest/Tests/VirtualizationTest.cs
@@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
+using System.Threading.Tasks;
using BasicTestApp;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
@@ -211,6 +212,33 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
Browser.NotEqual(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
}
+ [Fact]
+ public async Task ToleratesIncorrectItemSize()
+ {
+ Browser.MountTestComponent
+ Slightly incorrect item size:
+
Viewport as root: