Skip to content

Commit 11413df

Browse files
committed
improve on scrolling function used
1 parent d6ff7d5 commit 11413df

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/web/Jordnaer/Extensions/JsRuntimeExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ public static async Task GoBackAsync(this IJSRuntime jsRuntime)
1010

1111
public static async Task NavigateTo(this IJSRuntime jsRuntime, string newUrl)
1212
=> await jsRuntime.InvokeVoidAsyncWithErrorHandling("utilities.updatePathAndQueryString", newUrl);
13-
13+
public static async ValueTask<bool> ScrollToBottomOfElement(this IJSRuntime jsRuntime, string selector) => await jsRuntime.InvokeVoidAsyncWithErrorHandling(
14+
"scrollFunctions.scrollToBottomOfElement",
15+
selector);
16+
public static async Task<bool> HideElement(this IJSRuntime jsRuntime, string selector)
17+
=> await jsRuntime.InvokeVoidAsyncWithErrorHandling(
18+
"utilities.hideElement",
19+
selector);
1420
public static async ValueTask<GeoLocation?> GetGeolocation(this IJSRuntime jsRuntime)
1521
{
1622
var (success, geoLocation) = await jsRuntime.InvokeAsyncWithErrorHandling<GeoLocation>("utilities.getGeolocation");

src/web/Jordnaer/Features/Chat/ChatMessageList.razor

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
<MudText Typo="Typo.h5">@ActiveChat.GetDisplayName(CurrentUser.Id!)</MudText>
3434
</MudAppBar>
3535
}
36-
37-
<MudList Class="chat-message-list" Padding="false" ReadOnly Dense T="RenderFragment">
36+
37+
@*The elements below cannot be readonly/disabled, as it disabled the tooltips *@
38+
<MudList Class="chat-message-list" Padding="false" Dense T="RenderFragment">
3839
<MudSpacer />
3940
<MudVirtualize Items="ActiveChat?.Messages" Context="message" OverscanCount="8" ItemSize="80">
4041

@@ -96,8 +97,8 @@
9697
AdornmentIcon="@Icons.Material.Filled.Send"
9798
AdornmentColor="@(string.IsNullOrWhiteSpace(_messageInput.Value) ? Color.Default : Color.Primary)"
9899
OnAdornmentClick="SendMessage"
99-
Class="mt-3"
100-
Lines="3"
100+
Class="mt-3 p-1"
101+
Lines="2"
101102
IconSize="Size.Large"
102103
Style="@($"background: {JordnaerTheme.CustomTheme.PaletteLight.BackgroundGray}")" />
103104
</MudList>
@@ -162,7 +163,7 @@
162163

163164
protected override async Task OnAfterRenderAsync(bool firstRender)
164165
{
165-
await ScrollToBottom();
166+
await JsRuntime.ScrollToBottomOfElement(".chat-message-window");
166167
}
167168

168169
private void MarkMessageIfSuccessfullySentByCurrentUser()
@@ -182,8 +183,6 @@
182183
LastMessageWasSentSuccessfullyByCurrentUser = lastMessage.SenderId == CurrentUser.Id;
183184
}
184185

185-
private async ValueTask ScrollToBottom() => await JsRuntime.InvokeVoidAsyncWithErrorHandling("scrollFunctions.scrollToBottomOfElement", ".chat-message-window");
186-
187186
private async Task BackToList()
188187
{
189188
ActiveChat = null;
@@ -222,7 +221,7 @@
222221
}
223222

224223
await _messageInput.FocusAsync();
225-
await ScrollToBottom();
224+
await JsRuntime.ScrollToBottomOfElement(".chat-message-window");
226225
}
227226

228227
// TODO: Add MudScrollToTop that shows when user is scrolled up in the chat window

src/web/Jordnaer/wwwroot/js/scroll.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ window.scrollFunctions = {
2121
scrollToBottomOfElement: function (selector) {
2222
const element = document.querySelector(selector);
2323

24-
if (!element) return;
24+
if (!element) {
25+
return;
26+
}
2527

26-
element.scrollTop = element.scrollHeight;
28+
// Ensure the element is fully rendered before scrolling
29+
setTimeout(function () {
30+
element.scrollTo({
31+
top: element.scrollHeight,
32+
left: 0,
33+
behavior: 'instant' // 'auto', 'instant' or 'smooth' (default is 'auto')
34+
});
35+
}, 50); // Adjust the delay as needed
2736
}
2837
};

0 commit comments

Comments
 (0)