Skip to content

Commit c7be45e

Browse files
authored
Move new version dropdown to sidebar and add missing behaviour from the old dropdown (#1389)
* Move new version dropdown to sidebar and add missing behaviour from the old dropdown * Add comment * Add comment
1 parent a0d3562 commit c7be45e

File tree

12 files changed

+173
-62
lines changed

12 files changed

+173
-62
lines changed

src/Elastic.ApiExplorer/Endpoints/EndpointView.cshtml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
Next = null,
1313
NavigationHtml = Model.NavigationHtml,
1414
UrlPathPrefix = null,
15-
VersionDropdown = null,
16-
CurrentVersion = null,
1715
AllowIndexing = false,
1816
CanonicalBaseUrl = null,
1917
GoogleTagManager = new GoogleTagManagerConfiguration(),

src/Elastic.ApiExplorer/Landing/LandingView.cshtml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
Next = null,
1313
NavigationHtml = Model.NavigationHtml,
1414
UrlPathPrefix = null,
15-
VersionDropdown = null,
16-
CurrentVersion = "9.0+",
1715
AllowIndexing = false,
1816
CanonicalBaseUrl = null,
1917
GoogleTagManager = new GoogleTagManagerConfiguration(),

src/Elastic.ApiExplorer/Operations/OperationView.cshtml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
Next = null,
1313
NavigationHtml = Model.NavigationHtml,
1414
UrlPathPrefix = null,
15-
VersionDropdown = null,
16-
CurrentVersion = null,
1715
AllowIndexing = false,
1816
CanonicalBaseUrl = null,
1917
GoogleTagManager = new GoogleTagManagerConfiguration(),

src/Elastic.Documentation.Site/Assets/web-components/VersionDropdown.tsx

Lines changed: 132 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'strict'
2+
13
import {
24
EuiButton,
35
EuiContextMenu,
@@ -6,7 +8,12 @@ import {
68
EuiIcon,
79
EuiPopover,
810
EuiText,
11+
EuiPanel,
12+
EuiLink,
13+
useEuiOverflowScroll,
914
useGeneratedHtmlId,
15+
useEuiTheme,
16+
useEuiFontSize,
1017
} from '@elastic/eui'
1118
import { icon as EuiIconVisualizeApp } from '@elastic/eui/es/components/icon/assets/app_visualize'
1219
import { icon as EuiIconArrowDown } from '@elastic/eui/es/components/icon/assets/arrow_down'
@@ -50,12 +57,18 @@ type VersionDropdownItem = {
5057
}
5158

5259
type VersionDropdownProps = {
53-
currentVersion: string
54-
items: VersionDropdownItem[]
60+
currentVersion?: string
61+
allVersionsUrl?: string
62+
items?: VersionDropdownItem[]
5563
}
5664

57-
const VersionDropdown = ({ currentVersion, items }: VersionDropdownProps) => {
65+
const VersionDropdown = ({
66+
allVersionsUrl,
67+
currentVersion,
68+
items,
69+
}: VersionDropdownProps) => {
5870
const [isPopoverOpen, setPopover] = useState(false)
71+
const { euiTheme } = useEuiTheme()
5972

6073
const contextMenuPopoverId = useGeneratedHtmlId({
6174
prefix: 'contextMenuPopover',
@@ -84,60 +97,112 @@ const VersionDropdown = ({ currentVersion, items }: VersionDropdownProps) => {
8497
const convertToPanels = (
8598
items: VersionDropdownItem[]
8699
): EuiContextMenuPanelDescriptor[] => {
87-
return items.flatMap((item, index) => {
88-
if (item.children == null) {
89-
return []
90-
} else {
91-
return {
92-
id: index + 1,
93-
title: item.name,
94-
initialFocusedItemIndex: 0,
95-
width: WIDTH,
96-
disabled: item.disabled,
97-
size: 's',
98-
items: item.children ? convertItems(item.children) : [],
99-
}
100-
}
101-
})
100+
return items == null
101+
? []
102+
: items.flatMap((item, index) => {
103+
if (item.children == null) {
104+
return []
105+
} else {
106+
return {
107+
id: index + 1,
108+
title: item.name,
109+
initialFocusedItemIndex: 0,
110+
width: WIDTH,
111+
disabled: item.disabled,
112+
size: 's',
113+
items: item.children
114+
? convertItems(item.children)
115+
: [],
116+
}
117+
}
118+
})
102119
}
103120

104121
const WIDTH = 175
105122

106-
const topLevelItems = items.map((item, index) => {
107-
return {
108-
name: item.name,
109-
panel: item.children?.length ? index + 1 : undefined,
110-
href: item.href,
111-
disabled: item.disabled,
112-
}
113-
})
123+
const topLevelItems = () =>
124+
items.map((item, index) => {
125+
return {
126+
name: item.name,
127+
panel: item.children?.length ? index + 1 : undefined,
128+
href: item.href,
129+
disabled: item.disabled,
130+
}
131+
})
114132

115-
const subpanels = convertToPanels(items)
133+
const subpanels = () => convertToPanels(items)
116134

117-
const panels: EuiContextMenuPanelDescriptor[] = [
135+
const panels = (): EuiContextMenuPanelDescriptor[] => [
118136
{
119137
id: 0,
120138
title: (
121139
<EuiFlexGroup gutterSize="s" alignItems="center">
122140
<EuiFlexItem grow={0}>
123-
<EuiIcon type="check" />
141+
<EuiIcon type="check" size="s" />
124142
</EuiFlexItem>
125143
<EuiFlexItem grow={1}>
126-
Current ({currentVersion})
144+
<EuiText size="s">{currentVersion}</EuiText>
127145
</EuiFlexItem>
128146
</EuiFlexGroup>
129147
),
130148
width: WIDTH,
131149
size: 's',
132150
items: [
133-
...topLevelItems,
134-
{
135-
name: 'All versions',
136-
href: 'https://elastic.co',
137-
},
151+
...(items == null
152+
? [
153+
{
154+
renderItem: () => (
155+
<EuiPanel paddingSize="s" hasShadow={false}>
156+
<EuiText size="xs" color="subdued">
157+
There are no other versions available
158+
for this page.
159+
</EuiText>
160+
</EuiPanel>
161+
),
162+
},
163+
]
164+
: topLevelItems()),
165+
...(items?.length === 0
166+
? [
167+
{
168+
renderItem: () => (
169+
<EuiPanel paddingSize="s" hasShadow={false}>
170+
<EuiText size="xs" color="subdued">
171+
This page was fully migrated to the
172+
current version.
173+
</EuiText>
174+
</EuiPanel>
175+
),
176+
},
177+
]
178+
: []),
179+
...(allVersionsUrl != null
180+
? [
181+
{
182+
renderItem: () => (
183+
<EuiPanel
184+
css={css`
185+
border-top: 1px solid
186+
${euiTheme.border.color};
187+
padding: ${euiTheme.size.s};
188+
`}
189+
>
190+
<EuiLink
191+
href="/docs/versions"
192+
color="text"
193+
>
194+
<EuiText size="s">
195+
View all versions
196+
</EuiText>
197+
</EuiLink>
198+
</EuiPanel>
199+
),
200+
},
201+
]
202+
: []),
138203
],
139204
},
140-
...subpanels,
205+
...(items != null ? subpanels() : []),
141206
]
142207

143208
const button = (
@@ -150,13 +215,12 @@ const VersionDropdown = ({ currentVersion, items }: VersionDropdownProps) => {
150215
style={{ borderRadius: 9999 }}
151216
>
152217
<EuiText
153-
size="xs"
154218
css={css`
155-
font-weight: 600;
156-
font-size: 0.875rem;
219+
font-weight: ${euiTheme.font.weight.bold};
220+
font-size: ${useEuiFontSize('xs').fontSize};
157221
`}
158222
>
159-
Current ({currentVersion})
223+
Current version ({currentVersion})
160224
</EuiText>
161225
</EuiButton>
162226
)
@@ -168,10 +232,35 @@ const VersionDropdown = ({ currentVersion, items }: VersionDropdownProps) => {
168232
isOpen={isPopoverOpen}
169233
closePopover={closePopover}
170234
panelPaddingSize="none"
171-
anchorPosition="downLeft"
235+
anchorPosition="downRight"
172236
repositionOnScroll={true}
173237
>
174-
<EuiContextMenu initialPanelId={0} size="s" panels={panels} />
238+
<EuiContextMenu
239+
initialPanelId={0}
240+
size="s"
241+
panels={panels()}
242+
css={css`
243+
max-height: 70vh;
244+
// This is needed because the CSS reset we are using
245+
// is probably not fully compatible with the EUI
246+
button {
247+
cursor: pointer;
248+
&:disabled {
249+
cursor: default;
250+
}
251+
}
252+
.euiContextMenuPanel__title {
253+
position: sticky;
254+
top: 0;
255+
// !important because clicking on the title
256+
// makes the background transparent
257+
// and you unexpectedly see the items behind it.
258+
background-color: ${euiTheme.colors
259+
.backgroundBasePlain} !important;
260+
}
261+
${useEuiOverflowScroll('y')}
262+
`}
263+
/>
175264
</EuiPopover>
176265
)
177266
}
@@ -182,6 +271,7 @@ customElements.define(
182271
props: {
183272
items: 'json',
184273
currentVersion: 'string',
274+
allVersionsUrl: 'string',
185275
},
186276
})
187277
)

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313
</label>
1414
}
1515
<a href="@Model.Link("/")" class="text-lg leading-[1em] hover:text-blue-elastic active:text-blue-elastic-100">Docs</a>
16-
17-
@if (Model.Features.IsVersionDropdownEnabled && Model.VersionDropdown is not null)
18-
{
19-
<div id="version-dropdown">
20-
<version-dropdown current-version='@(Model.CurrentVersion)' items='@(new HtmlString(Model.VersionDropdown))' />
21-
</div>
22-
}
2316
</div>
2417
<ul class="flex gap-6">
2518
<li class="text-nowrap hover:text-blue-elastic active:text-blue-elastic-100">

src/Elastic.Documentation.Site/_ViewModels.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public class GlobalLayoutViewModel
2525
public required INavigationItem? Previous { get; init; }
2626
public required INavigationItem? Next { get; init; }
2727

28-
public required string? VersionDropdown { get; init; }
29-
public required string? CurrentVersion { get; init; }
30-
3128
public required string NavigationHtml { get; init; }
3229
public required string? UrlPathPrefix { get; init; }
3330
public required Uri? CanonicalBaseUrl { get; init; }

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ private async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument
8585
.Distinct()
8686
.ToHashSet();
8787

88+
string? allVersionsUrl = null;
89+
90+
if (PositionalNavigation.MarkdownNavigationLookup.TryGetValue("docs-content://versions.md", out var item))
91+
allVersionsUrl = item.Url;
92+
8893
var slice = Index.Create(new IndexViewModel
8994
{
9095
SiteName = siteName,
@@ -111,6 +116,7 @@ private async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument
111116
StaticFileContentHashProvider = StaticFileContentHashProvider,
112117
ReportIssueUrl = reportUrl,
113118
CurrentVersion = legacyPages?.Count > 0 ? legacyPages.ElementAt(0).Version : "9.0+",
119+
AllVersionsUrl = allVersionsUrl,
114120
LegacyPages = legacyPages?.Skip(1).ToArray(),
115121
VersionDropdownItems = VersionDrownDownItemViewModel.FromLegacyPageMappings(legacyPages?.Skip(1).ToArray()),
116122
Products = allProducts

src/Elastic.Markdown/Slices/Index.cshtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
ReportIssueUrl = Model.ReportIssueUrl,
3232
LegacyPages = Model.LegacyPages,
3333
CurrentVersion = Model.CurrentVersion,
34-
VersionDropdown = JsonSerializer.Serialize(Model.VersionDropdownItems,
34+
AllVersionsUrl = Model.AllVersionsUrl,
35+
VersionDropdownSerializedModel = JsonSerializer.Serialize(Model.VersionDropdownItems,
3536
ViewModelSerializerContext.Default.VersionDrownDownItemViewModelArray),
3637
};
3738
protected override Task ExecuteSectionAsync(string name)

src/Elastic.Markdown/Slices/IndexViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class IndexViewModel
3434

3535
public required string NavigationHtml { get; init; }
3636
public required string CurrentVersion { get; init; }
37+
38+
public required string? AllVersionsUrl { get; init; }
3739
public required LegacyPageMapping[]? LegacyPages { get; init; }
3840
public required VersionDrownDownItemViewModel[]? VersionDropdownItems { get; init; }
3941
public required string? UrlPathPrefix { get; init; }

src/Elastic.Markdown/Slices/Layout/_LandingPage.cshtml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
Upgrade versions
2020
</a>
2121
</div>
22+
@if (Model.AllVersionsUrl is not null)
23+
{
24+
<div class="mt-6">
25+
<a href="@Model.AllVersionsUrl" class="link">
26+
View previous docs versions
27+
<svg class="link-arrow"
28+
xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
29+
<path stroke-linecap="round" stroke-linejoin="round" d="M17.25 8.25 21 12m0 0-3.75 3.75M21 12H3"/>
30+
</svg>
31+
</a>
32+
</div>
33+
}
2234
</div>
2335
<div class="flex justify-center items-center">
2436
<img src="@Model.Static("web-documentation-hero-illustration.svg")" alt="" width="400">

0 commit comments

Comments
 (0)