Skip to content

Commit 9779ca1

Browse files
authored
feat(blazorui): add store scroll feature to BitAppShell #10233 (#10234)
1 parent af8bb32 commit 9779ca1

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/BlazorUI/Bit.BlazorUI.Extras/Components/AppShell/BitAppShell.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace BitBlazorUI {
22
export class AppShell {
3+
private static STORE_KEY = 'bit-appshell-scrolls';
4+
35
public static PreScroll: number = 0;
46

57
private static _currentUrl: string;
@@ -9,9 +11,10 @@ namespace BitBlazorUI {
911
public static initScroll(container: HTMLElement, url: string) {
1012
AppShell._container = container;
1113
AppShell._currentUrl = url;
12-
AppShell._scrolls[url] = AppShell.PreScroll;
13-
if (AppShell.PreScroll > 0) {
14-
AppShell._container.scrollTo({ top: AppShell.PreScroll, behavior: 'instant' });
14+
AppShell._scrolls = JSON.parse(sessionStorage.getItem(AppShell.STORE_KEY) || '{}');
15+
AppShell.storeScroll(url, AppShell.PreScroll > 0 ? AppShell.PreScroll : AppShell._scrolls[url]);
16+
if (AppShell._scrolls[url]! > 0) {
17+
AppShell._container.scrollTo({ top: AppShell._scrolls[url], behavior: 'instant' });
1518
}
1619
AppShell.addScroll();
1720
}
@@ -22,7 +25,7 @@ namespace BitBlazorUI {
2225

2326
public static afterRenderScroll(url: string) {
2427
AppShell._currentUrl = url;
25-
AppShell._scrolls[url] = AppShell._scrolls[url] || 0;
28+
AppShell.storeScroll(url, AppShell._scrolls[url]);
2629
AppShell._container?.scrollTo({ top: AppShell._scrolls[url], behavior: 'instant' });
2730
AppShell.addScroll();
2831
}
@@ -40,7 +43,12 @@ namespace BitBlazorUI {
4043
}
4144

4245
private static onScroll() {
43-
AppShell._scrolls[AppShell._currentUrl] = AppShell._container?.scrollTop;
46+
AppShell.storeScroll(AppShell._currentUrl, AppShell._container?.scrollTop);
47+
}
48+
49+
private static storeScroll(url: string, value: number | undefined) {
50+
AppShell._scrolls[url] = value || 0;
51+
window.sessionStorage.setItem(AppShell.STORE_KEY, JSON.stringify(AppShell._scrolls));
4452
}
4553
}
4654
}

src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/OverviewPage.razor

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@page "/overview"
2+
@page "/components"
23

34
<PageOutlet Url="overview"
45
Title="Overview"

src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Shared/MainLayout.razor.NavItems.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public partial class MainLayout
55

66
private readonly List<BitNavItem> _navItems =
77
[
8-
new() { Text = "Overview", Url = "/overview" },
8+
new() { Text = "Overview", Url = "/overview", AdditionalUrls = ["/components"] },
99
new() { Text = "Getting started", Url = "/getting-started" },
1010
new()
1111
{

0 commit comments

Comments
 (0)