Skip to content

Commit 4509194

Browse files
authored
Simplify navigation and remove lazy loading (#2530)
* Simplify navigation and remove lazy loading * Remove more related to lazy load nav
1 parent 66be3e3 commit 4509194

24 files changed

Lines changed: 51 additions & 141 deletions

File tree

config/assembler.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ environments:
2828
google_tag_manager:
2929
enabled: false
3030
feature_flags:
31-
LAZY_LOAD_NAVIGATION: true
3231
SEARCH_OR_ASK_AI: true
3332
dev:
3433
uri: http://localhost:4000

src/Elastic.ApiExplorer/ApiViewModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public GlobalLayoutViewModel CreateGlobalLayoutModel() =>
3737
Previous = null,
3838
Next = null,
3939
NavigationHtml = NavigationHtml,
40-
NavigationFileName = string.Empty,
4140
UrlPathPrefix = BuildContext.UrlPathPrefix,
4241
AllowIndexing = BuildContext.AllowIndexing,
4342
CanonicalBaseUrl = BuildContext.CanonicalBaseUrl,

src/Elastic.ApiExplorer/OpenApiGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private async Task<IFileInfo> Render<T>(string prefix, INavigationItem current,
298298
if (!outputFile.Directory!.Exists)
299299
outputFile.Directory.Create();
300300

301-
var navigationRenderResult = await navigationRenderer.RenderNavigation(current.NavigationRoot, current, INavigationHtmlWriter.AllLevels, ctx);
301+
var navigationRenderResult = await navigationRenderer.RenderNavigation(current.NavigationRoot, current, ctx);
302302
renderContext = renderContext with
303303
{
304304
CurrentNavigation = current,

src/Elastic.Documentation.Configuration/Builder/FeatureFlags.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ public bool PrimaryNavEnabled
2020
set => _featureFlags["primary-nav"] = value;
2121
}
2222

23-
public bool LazyLoadNavigation
24-
{
25-
get => IsEnabled("lazy-load-navigation");
26-
set => _featureFlags["lazy-load-navigation"] = value;
27-
}
28-
2923
public bool DisableGitHubEditLink
3024
{
3125
get => IsEnabled("disable-github-edit-link");

src/Elastic.Documentation.Site/Assets/main.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import('./web-components/AppliesToPopover')
3737
import('./web-components/FullPageSearch/FullPageSearchComponent')
3838

3939
const { getOS } = new UAParser()
40-
const isLazyLoadNavigationEnabled =
41-
$('meta[property="docs:feature:lazy-load-navigation"]')?.content === 'true'
4240

4341
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4442
type HtmxEvent = any
@@ -96,23 +94,14 @@ document.addEventListener('DOMContentLoaded', function () {
9694
initMath()
9795
})
9896

99-
document.addEventListener('htmx:load', function (event: HtmxEvent) {
97+
document.addEventListener('htmx:load', function () {
10098
initTocNav()
10199
initHighlight()
102100
initCopyButton()
103101
initTabs()
104102
initAppliesSwitch()
105103
initMath()
106-
107-
// We do this so that the navigation is not initialized twice
108-
// When lazy load is enabled, only initialize when nav-tree loads
109-
// Defer with requestAnimationFrame to ensure DOM is fully rendered
110-
// The throttle with trailing: true will ensure only the last call executes
111-
if (!isLazyLoadNavigationEnabled || event.detail.elt.id === 'nav-tree') {
112-
requestAnimationFrame(() => {
113-
initNav()
114-
})
115-
}
104+
initNav()
116105
initSmoothScroll()
117106
openDetailsWithAnchor()
118107
initImageCarousel()

src/Elastic.Documentation.Site/Assets/pages-nav.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,21 @@ export const scrollCurrentNaviItemIntoView = throttle(
5555
{ leading: false, trailing: true }
5656
)
5757

58-
function setDropdown(dropdown: HTMLElement) {
59-
if (dropdown) {
60-
const anchors = $$('a', dropdown)
61-
anchors.forEach((a) => {
62-
a.addEventListener('mousedown', (e) => {
63-
e.preventDefault()
64-
})
65-
a.addEventListener('mouseup', () => {
66-
if (document.activeElement instanceof HTMLElement) {
67-
document.activeElement.blur()
68-
}
69-
})
70-
})
71-
}
58+
/**
59+
* Prevents focus-based dropdowns from closing before link navigation completes.
60+
* Without this, clicking a link inside the dropdown would transfer focus away,
61+
* causing the dropdown to close via CSS :focus-within before navigation happens.
62+
*/
63+
function preventFocusLossOnLinkClick(anchor: HTMLAnchorElement) {
64+
anchor.addEventListener('mousedown', (e) => {
65+
e.preventDefault()
66+
})
67+
// Close dropdown after click completes
68+
anchor.addEventListener('mouseup', () => {
69+
if (document.activeElement instanceof HTMLElement) {
70+
document.activeElement.blur()
71+
}
72+
})
7273
}
7374

7475
export function initNav() {
@@ -77,13 +78,9 @@ export function initNav() {
7778
return
7879
}
7980

80-
const pagesDropdown = $('#pages-dropdown')
81-
if (pagesDropdown) {
82-
setDropdown(pagesDropdown)
83-
}
84-
const pageVersionDropdown = $('#page-version-dropdown')
85-
if (pageVersionDropdown) {
86-
setDropdown(pageVersionDropdown)
81+
const dropdownActiveAnchor = $('#pages-dropdown a.pages-dropdown_active')
82+
if (dropdownActiveAnchor) {
83+
preventFocusLossOnLinkClick(dropdownActiveAnchor)
8784
}
8885

8986
// Remove current class from all nav items before marking new ones

src/Elastic.Documentation.Site/Layout/_Head.cshtml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,3 @@
3535
{
3636
<meta property="og:url" content="@Model.CanonicalUrl" />
3737
}
38-
<meta property="docs:feature:lazy-load-navigation" content="@Model.Features.LazyLoadNavigation" />

src/Elastic.Documentation.Site/Layout/_PagesNav.cshtml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
@inherits RazorSlice<Elastic.Documentation.Site.GlobalLayoutViewModel>
22
<aside class="sidebar font-sans bg-white fixed md:sticky shadow-2xl md:shadow-none left-[100%] group-has-[#pages-nav-hamburger:checked]/body:left-0 bottom-0 md:left-auto pl-6 md:pl-2 top-[calc(var(--offset-top)+1px)] w-[80%] md:w-auto shrink-0 border-r-1 border-r-grey-20 z-40 md:z-auto">
3-
4-
@if (Model.Features.LazyLoadNavigation && !string.IsNullOrEmpty(Model.NavigationFileName))
5-
{
6-
<div class="hidden" hx-get="@(Model.Link(Model.NavigationFileName))" hx-trigger="load" hx-params="nav" hx-push-url="false" hx-swap="innerHTML" hx-target="#pages-nav"></div>
7-
}
83
<nav
94
id="pages-nav"
105
class="sidebar-nav h-full">

src/Elastic.Documentation.Site/Navigation/INavigationHtmlWriter.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ namespace Elastic.Documentation.Site.Navigation;
99

1010
public interface INavigationHtmlWriter
1111
{
12-
const int AllLevels = -1;
13-
1412
Task<NavigationRenderResult> RenderNavigation(
1513
IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation,
1614
INavigationItem currentNavigationItem,
17-
int maxLevel,
1815
Cancel ctx = default
1916
);
2017

src/Elastic.Documentation.Site/Navigation/IsolatedBuildNavigationHtmlWriter.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ namespace Elastic.Documentation.Site.Navigation;
1212
public class IsolatedBuildNavigationHtmlWriter(BuildContext context, IRootNavigationItem<INavigationModel, INavigationItem> siteRoot)
1313
: INavigationHtmlWriter
1414
{
15-
private readonly ConcurrentDictionary<(string, int), string> _renderedNavigationCache = [];
15+
private readonly ConcurrentDictionary<string, string> _renderedNavigationCache = [];
1616

1717
public async Task<NavigationRenderResult> RenderNavigation(
18-
IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, INavigationItem currentNavigationItem, int maxLevel, Cancel ctx = default
18+
IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, INavigationItem currentNavigationItem, Cancel ctx = default
1919
)
2020
{
2121
var navigation = context.Configuration.Features.PrimaryNavEnabled || currentRootNavigation.IsUsingNavigationDropdown
2222
? currentRootNavigation
2323
: siteRoot;
24-
var id = ShortId.Create($"{(navigation.Id, maxLevel).GetHashCode()}");
25-
if (_renderedNavigationCache.TryGetValue((navigation.Id, maxLevel), out var value))
24+
var id = ShortId.Create($"{navigation.Id.GetHashCode()}");
25+
if (_renderedNavigationCache.TryGetValue(navigation.Id, out var value))
2626
{
2727
return new NavigationRenderResult
2828
{
2929
Html = value,
3030
Id = id
3131
};
3232
}
33-
var model = CreateNavigationModel(navigation, maxLevel);
33+
var model = CreateNavigationModel(navigation);
3434
value = await ((INavigationHtmlWriter)this).Render(model, ctx);
35-
_renderedNavigationCache[(navigation.Id, maxLevel)] = value;
35+
_renderedNavigationCache[navigation.Id] = value;
3636
return new NavigationRenderResult
3737
{
3838
Html = value,
3939
Id = id
4040
};
4141
}
4242

43-
private NavigationViewModel CreateNavigationModel(IRootNavigationItem<INavigationModel, INavigationItem> navigation, int maxLevel) =>
43+
private NavigationViewModel CreateNavigationModel(IRootNavigationItem<INavigationModel, INavigationItem> navigation) =>
4444
new()
4545
{
4646
Title = navigation.NavigationTitle,
@@ -49,7 +49,6 @@ private NavigationViewModel CreateNavigationModel(IRootNavigationItem<INavigatio
4949
IsPrimaryNavEnabled = context.Configuration.Features.PrimaryNavEnabled,
5050
IsUsingNavigationDropdown = context.Configuration.Features.PrimaryNavEnabled || navigation.IsUsingNavigationDropdown,
5151
IsGlobalAssemblyBuild = false,
52-
TopLevelItems = siteRoot.NavigationItems.OfType<INodeNavigationItem<INavigationModel, INavigationItem>>().ToList(),
53-
MaxLevel = maxLevel
52+
TopLevelItems = siteRoot.NavigationItems.OfType<INodeNavigationItem<INavigationModel, INavigationItem>>().ToList()
5453
};
5554
}

0 commit comments

Comments
 (0)