diff --git a/src/Elastic.ApiExplorer/Landing/_Partials/_ApiIndexGroup.cshtml b/src/Elastic.ApiExplorer/Landing/_Partials/_ApiIndexGroup.cshtml index 85118eb323..7f6e326fae 100644 --- a/src/Elastic.ApiExplorer/Landing/_Partials/_ApiIndexGroup.cshtml +++ b/src/Elastic.ApiExplorer/Landing/_Partials/_ApiIndexGroup.cshtml @@ -26,7 +26,7 @@ @if (row.Endpoint is { Rows.Count: > 0 } endpoint) { - @(await RenderPartialAsync<_HttpMethodBadge, string>(endpoint.Rows[0].Method)) + @(await RenderPartialAsync<_HttpMethodBadge, IReadOnlyList>(endpoint.Rows[0].Methods)) @row.Title @endpoint.Rows[0].Route diff --git a/src/Elastic.ApiExplorer/Operations/OperationEndpoint.cs b/src/Elastic.ApiExplorer/Operations/OperationEndpoint.cs index 87a05a3583..72a292ef72 100644 --- a/src/Elastic.ApiExplorer/Operations/OperationEndpoint.cs +++ b/src/Elastic.ApiExplorer/Operations/OperationEndpoint.cs @@ -21,7 +21,11 @@ public sealed record EndpointSegment(string Text, bool Optional) } /// One distinct route of an operation, with its dominant method and the methods that behave the same. -public sealed record EndpointRow(string Route, string Method, IReadOnlyList AlsoMethods, IReadOnlyList Segments); +public sealed record EndpointRow(string Route, string Method, IReadOnlyList AlsoMethods, IReadOnlyList Segments) +{ + /// Every method of the route, the dominant one first: what a method chip lists. + public IReadOnlyList Methods => [Method, .. AlsoMethods]; +} /// /// Every route and method of one operation merged into display rows: longest route first, the dominant method per @@ -44,6 +48,17 @@ public sealed partial record OperationEndpoint( public const string SpaceParameterDescription = "An identifier for the space. If `space_id` is not provided in the URL, the default space is used. " + SpacesNote; + /// Every method of every route: the leading route's dominant method first, the rest by rank. + public IReadOnlyList Methods => + [ + Rows[0].Method, + .. Rows + .SelectMany(static r => r.Methods) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Where(m => !string.Equals(m, Rows[0].Method, StringComparison.OrdinalIgnoreCase)) + .OrderBy(Rank) + ]; + private static readonly string[] MethodRank = ["post", "put", "patch", "get", "delete", "head", "options", "trace"]; /// diff --git a/src/Elastic.ApiExplorer/Operations/OperationNavigationItem.cs b/src/Elastic.ApiExplorer/Operations/OperationNavigationItem.cs index b9239c9a50..1c132e715d 100644 --- a/src/Elastic.ApiExplorer/Operations/OperationNavigationItem.cs +++ b/src/Elastic.ApiExplorer/Operations/OperationNavigationItem.cs @@ -19,12 +19,8 @@ public record ApiOperation( string Route, IOpenApiPathItem Path, string ApiName -) : IApiModel, IApiOperationNavigationModel +) : IApiModel { - // The page header leads with the dominant method, which can come from the description's method listing - // rather than the spec; the sidebar badge must agree with it. - string IApiOperationNavigationModel.HttpMethod => OperationEndpoint.Build(this, Operation.Description, []).Rows[0].Method; - /// /// The one deprecation fact the page title badge, the overview rows, the sidebar row and the search index all read. /// Collapsed siblings always agree on it, because operations that differ in lifecycle are never collapsed onto one page. @@ -86,7 +82,7 @@ public class OperationNavigationItem( apiOperation, root, parent -), IEndpointOrOperationNavigationItem +), IEndpointOrOperationNavigationItem, IApiOperationNavigationItem { /// Other operations of the same API collapsed onto this page (other methods and routes). public IReadOnlyList Siblings { get; init; } = []; @@ -96,4 +92,9 @@ public class OperationNavigationItem( /// The routes and methods this page lists, its own and its '. public OperationEndpoint Endpoint => field ??= OperationEndpoint.Build(Model, Model.Operation.Description, Siblings); + + // The sidebar chip must list what the page header lists, including methods that come from the description's method listing or from collapsed siblings. + IReadOnlyList IApiOperationNavigationItem.HttpMethods => Endpoint.Methods; + + bool IApiOperationNavigationItem.Deprecated => Model.Deprecated; } diff --git a/src/Elastic.ApiExplorer/Operations/_Partials/_EndpointPath.cshtml b/src/Elastic.ApiExplorer/Operations/_Partials/_EndpointPath.cshtml index f1f5735075..a517881b0c 100644 --- a/src/Elastic.ApiExplorer/Operations/_Partials/_EndpointPath.cshtml +++ b/src/Elastic.ApiExplorer/Operations/_Partials/_EndpointPath.cshtml @@ -1,16 +1,11 @@ @using Elastic.ApiExplorer.Operations @inherits RazorSlice -@* One merged route: dominant method badge, "also GET" for methods that behave the same, optional segments dashed. *@ +@* One merged route: one chip listing every method that behaves the same, the dominant one first, optional segments dashed. *@ @{ var row = Model.Row; } - @(await RenderPartialAsync<_HttpMethodBadge, string>(row.Method)) - @if (row.AlsoMethods.Count > 0) - { - var also = string.Join(", ", row.AlsoMethods.Select(static m => m.ToUpperInvariant())); - also @also - } + @(await RenderPartialAsync<_HttpMethodBadge, IReadOnlyList>(row.Methods)) @* Segments on the brace lines: Razor would otherwise emit the indentation and line breaks as text, which inline layout shows as a space after each optional segment. *@ @if (row.Segments.Count == 0){@("/")}@foreach (var segment in row.Segments) { diff --git a/src/Elastic.ApiExplorer/_Partials/_ApiCodeSample.cshtml b/src/Elastic.ApiExplorer/_Partials/_ApiCodeSample.cshtml index 33bde0678e..f0b55961c3 100644 --- a/src/Elastic.ApiExplorer/_Partials/_ApiCodeSample.cshtml +++ b/src/Elastic.ApiExplorer/_Partials/_ApiCodeSample.cshtml @@ -4,7 +4,7 @@
@if (!string.IsNullOrEmpty(Model.HttpMethod)) { - @(await RenderPartialAsync<_HttpMethodBadge, string>(Model.HttpMethod)) + @(await RenderPartialAsync<_HttpMethodChip, string>(Model.HttpMethod)) } @if (!string.IsNullOrEmpty(Model.Route)) { diff --git a/src/Elastic.ApiExplorer/_Partials/_HttpMethodBadge.cshtml b/src/Elastic.ApiExplorer/_Partials/_HttpMethodBadge.cshtml index f510094767..a29253e36f 100644 --- a/src/Elastic.ApiExplorer/_Partials/_HttpMethodBadge.cshtml +++ b/src/Elastic.ApiExplorer/_Partials/_HttpMethodBadge.cshtml @@ -1,15 +1,18 @@ -@inherits RazorSlice -@* Labeled HTTP method chip — Figma APIs node 10067:6424. Icons match the Nav V2 glyphs. *@ - - +} diff --git a/src/Elastic.ApiExplorer/_Partials/_HttpMethodChip.cshtml b/src/Elastic.ApiExplorer/_Partials/_HttpMethodChip.cshtml new file mode 100644 index 0000000000..cec8bdf857 --- /dev/null +++ b/src/Elastic.ApiExplorer/_Partials/_HttpMethodChip.cshtml @@ -0,0 +1,14 @@ +@inherits RazorSlice + + + @Model.ToUpperInvariant() + diff --git a/src/Elastic.Documentation.Navigation/IApiOperationNavigationItem.cs b/src/Elastic.Documentation.Navigation/IApiOperationNavigationItem.cs new file mode 100644 index 0000000000..746bd7ea10 --- /dev/null +++ b/src/Elastic.Documentation.Navigation/IApiOperationNavigationItem.cs @@ -0,0 +1,18 @@ +// Licensed to Elasticsearch B.V under one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information + +namespace Elastic.Documentation.Navigation; + +/// +/// An API operation leaf that exposes its HTTP methods and deprecation for sidebar chrome. It is the page's +/// navigation item rather than its model, because the methods of operations collapsed onto the page live there. +/// +public interface IApiOperationNavigationItem : INavigationItem +{ + /// Lowercase HTTP method names (e.g. post, get), the page's dominant method first. + IReadOnlyList HttpMethods { get; } + + /// True when the spec marks the operation deprecated. + bool Deprecated { get; } +} diff --git a/src/Elastic.Documentation.Navigation/IApiOperationNavigationModel.cs b/src/Elastic.Documentation.Navigation/IApiOperationNavigationModel.cs deleted file mode 100644 index 439e812162..0000000000 --- a/src/Elastic.Documentation.Navigation/IApiOperationNavigationModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information - -namespace Elastic.Documentation.Navigation; - -/// -/// Navigation model for an API operation leaf that exposes its HTTP method and deprecation for sidebar chrome. -/// -public interface IApiOperationNavigationModel : INavigationModel -{ - /// Lowercase HTTP method name (e.g. get, post). - string HttpMethod { get; } - - /// True when the spec marks the operation deprecated. - bool Deprecated { get; } -} diff --git a/src/Elastic.Documentation.Site/Assets/api-docs.css b/src/Elastic.Documentation.Site/Assets/api-docs.css index 3391426b3c..67e0d3dcef 100644 --- a/src/Elastic.Documentation.Site/Assets/api-docs.css +++ b/src/Elastic.Documentation.Site/Assets/api-docs.css @@ -102,10 +102,6 @@ padding-top: 2px; } -.api-url-row > .api-url .api-url-also-method { - margin-top: 4px; -} - /* The first route is the one to read; the alternates below it are compact and muted. */ .api-page-intro .api-url-listing > li:first-child .api-url { padding: 4px 12px 4px 4px; @@ -131,12 +127,6 @@ font-size: 11px; } -.api-page-intro .api-url-listing > li + li .api-url-also-method { - margin-top: 3px; - font-size: 10px; - line-height: 14px; -} - .api-page-intro .api-url-listing > li + li .api-url .api-url-path { padding-top: 1px; } @@ -650,6 +640,11 @@ font-size: 11px; } +/* A joined chip is as wide as its methods; each part must not also claim a whole chip width. */ +.api-index-item .api-method-group .api-method { + min-width: 0; +} + .api-index-item-title { min-width: 0; overflow-wrap: anywhere; @@ -875,12 +870,128 @@ display: none; } +/* + * An operation that accepts several methods: one pill, the dominant method on the left. The chips join on a slanted + * edge, each keeps its outline on its outer edges, and the pill owns the corner radius. + */ +.api-method-group, +.nav-v2-api-method-group { + display: inline-flex; + flex-shrink: 0; + align-items: flex-start; + align-self: flex-start; +} + +.api-method-group { + border-radius: 8px; +} + +.nav-v2-api-method-group { + border-radius: 4px; +} + +.api-method-group > .api-method, +.nav-v2-api-method-group > .nav-v2-api-method { + --method-slant: 0.5em; + border-width: 1px 0; + border-radius: 0; +} + +.api-method-group > .api-method:first-child, +.nav-v2-api-method-group > .nav-v2-api-method:first-child { + border-left-width: 1px; + border-top-left-radius: inherit; + border-bottom-left-radius: inherit; +} + +.api-method-group > .api-method:last-child, +.nav-v2-api-method-group > .nav-v2-api-method:last-child { + border-right-width: 1px; + border-top-right-radius: inherit; + border-bottom-right-radius: inherit; +} + +.api-method-group > .api-method:not(:last-child), +.nav-v2-api-method-group > .nav-v2-api-method:not(:last-child) { + clip-path: polygon( + 0 0, + 100% 0, + calc(100% - var(--method-slant)) 100%, + 0 100% + ); +} + +.api-method-group > .api-method:not(:first-child), +.nav-v2-api-method-group > .nav-v2-api-method:not(:first-child) { + margin-left: calc(2px - var(--method-slant)); + clip-path: polygon(var(--method-slant) 0, 100% 0, 100% 100%, 0 100%); +} + +/* Room either side of the seam, so the labels do not crowd the slant. */ +.api-method-group > .api-method:not(:last-child) > :last-child { + margin-right: 0.35em; +} + +.api-method-group > .api-method:not(:first-child) > :first-child { + margin-left: 0.35em; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:not(:last-child) { + padding-right: calc(4px + 0.35em); +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:not(:first-child) { + padding-left: calc(4px + 0.35em); +} + +/* The dominant method keeps its colour; the others recede to the old "also" look: no fill, dashed grey outline, muted text. */ +.api-method-group > .api-method:not(:first-child), +#pages-nav + a.sidebar-link + .nav-v2-api-method-group + > .nav-v2-api-method:not(:first-child) { + border-style: dashed; + border-color: var(--color-grey-40, #98a2b3); + background-color: transparent; + color: var(--color-grey-100); +} + +.api-method-group > .api-method:not(:first-child) { + font-weight: 400; +} + +#pages-nav .nav-v2-api-method-group { + margin-top: 2px; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method { + margin-top: 0; + border-width: 1px 0; + border-radius: 0; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:first-child { + border-left-width: 1px; + border-top-left-radius: inherit; + border-bottom-left-radius: inherit; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:last-child { + border-right-width: 1px; + border-top-right-radius: inherit; + border-bottom-right-radius: inherit; +} + /* Legacy rows reserve 32px on the right; a row with a chip needs less. */ -#pages-nav li:has(> a.sidebar-link > .nav-v2-api-method) { +#pages-nav + li:has( + > a.sidebar-link > :is(.nav-v2-api-method, .nav-v2-api-method-group) + ) { padding-right: 8px; } -#pages-nav a.sidebar-link:has(> .nav-v2-api-method) { +#pages-nav + a.sidebar-link:has(> :is(.nav-v2-api-method, .nav-v2-api-method-group)) { display: flex; align-items: flex-start; justify-content: space-between; @@ -3473,21 +3584,6 @@ html:has(.api-page-shell) { } } -/* Merged endpoint rows: dominant method, "also GET", optional segments dashed. */ -.api-url-also-method { - flex-shrink: 0; - padding: 0 6px; - border: 1px dashed var(--color-grey-40, #98a2b3); - border-radius: 999px; - color: #516381; - font-family: var(--font-body, Inter, ui-sans-serif, system-ui, sans-serif); - font-size: 11px; - font-weight: 500; - line-height: 16px; - white-space: nowrap; - cursor: help; -} - .api-url-path { min-width: 0; } diff --git a/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.test.tsx b/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.test.tsx index 93da8e98ae..81c823cb64 100644 --- a/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.test.tsx +++ b/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.test.tsx @@ -46,3 +46,28 @@ describe('RouteLine highlighting', () => { expect(marks('/_search', ['_sea', 'search'])).toEqual(['_search']) }) }) + +describe('RouteLine methods', () => { + const chips = (methods: string[]) => { + const { container } = renderWithProviders( + + ) + return { + labels: [...container.querySelectorAll('.api-method-label')].map( + (l) => l.textContent + ), + joined: container.querySelector('.api-method-group') !== null, + } + } + + it('shows one method as a lone chip', () => { + expect(chips(['GET'])).toEqual({ labels: ['GET'], joined: false }) + }) + + it('joins every method into one chip, the dominant method first', () => { + expect(chips(['POST', 'GET'])).toEqual({ + labels: ['POST', 'GET'], + joined: true, + }) + }) +}) diff --git a/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.tsx b/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.tsx index 06a56db3e3..d020c4319f 100644 --- a/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.tsx +++ b/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/RouteLine.tsx @@ -71,12 +71,27 @@ const markRanges = ( return nodes } -/** The route a query named: the method the page leads with and the path. */ +const MethodChip = ({ method }: { method: string }) => ( + + {method} + +) + +/** One chip per method, joined into one when the route takes several, the method the page leads with first. */ +const MethodChips = ({ methods }: { methods: string[] }) => + methods.length === 1 ? ( + + ) : ( + + {methods.map((method) => ( + + ))} + + ) + +/** The route a query named: every method it takes and the path. */ export const RouteLine = ({ - route: { - path, - methods: [method], - }, + route: { path, methods }, tokens, }: { route: ApiRoute @@ -110,9 +125,12 @@ export const RouteLine = ({ .api-method { height: calc(${euiTheme.size.base} + ${euiTheme.size.xxs}); padding: 0 ${euiTheme.size.xs}; - border-radius: ${euiTheme.size.xs}; font-size: 10px; } + > .api-method, + .api-method-group { + border-radius: ${euiTheme.size.xs}; + } code { display: flex; min-width: 0; @@ -139,9 +157,7 @@ export const RouteLine = ({ } `} > - - {method} - + {spans}
) diff --git a/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/SearchResultsList.test.tsx b/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/SearchResultsList.test.tsx index 579a416a24..27391d6b10 100644 --- a/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/SearchResultsList.test.tsx +++ b/src/Elastic.Documentation.Site/Assets/web-components/NavigationSearch/SearchResultsList.test.tsx @@ -191,8 +191,7 @@ describe('property rows', () => { }) const lines = container.querySelectorAll('.route-line') expect(lines).toHaveLength(1) - expect(lines[0]).toHaveTextContent('POST') - expect(lines[0]).not.toHaveTextContent('GET') + expect(lines[0]).toHaveTextContent('POSTGET') expect(lines[0]).toHaveTextContent('/{index}/_search') expect(lines[0].querySelector('mark')).toHaveTextContent('search') expect(screen.queryByText('Returns search hits')).toBeNull() @@ -211,7 +210,7 @@ describe('property rows', () => { groups: groupSearchResults([withRoutes]), }) expect(container.querySelector('.route-line')).toHaveTextContent( - 'POST/_search' + 'POSTGET/_search' ) }) diff --git a/src/Elastic.Documentation.Site/Components/Navigation/NavSide/NavSideExamples.cs b/src/Elastic.Documentation.Site/Components/Navigation/NavSide/NavSideExamples.cs index 531c8515bb..50b6424fc3 100644 --- a/src/Elastic.Documentation.Site/Components/Navigation/NavSide/NavSideExamples.cs +++ b/src/Elastic.Documentation.Site/Components/Navigation/NavSide/NavSideExamples.cs @@ -9,7 +9,7 @@ namespace Elastic.Documentation.Site.Components.Navigation.NavSide; public static class NavSideExamples { - private static NavigationRenderNode Leaf(string title, string url, string? badge = null, string? method = null) => + private static NavigationRenderNode Leaf(string title, string url, string? badge = null, params string[] methods) => new() { Kind = NavigationRenderNodeKind.Leaf, @@ -17,7 +17,7 @@ private static NavigationRenderNode Leaf(string title, string url, string? badge NavigationTitle = title, Url = url, Badge = badge, - HttpMethod = method + HttpMethods = methods }; private static NavigationRenderNode Group(string id, string title, string url, params NavigationRenderNode[] children) => @@ -50,10 +50,10 @@ private static NavigationRenderNode Group(string id, string title, string url, p "sg-nav-2", "REST API", "#", - Leaf("Search documents", "#", method: "post"), - Leaf("Get document", "#", method: "get"), - Leaf("Get document source", "#", method: "get") with { Deprecated = true }, - Leaf("Delete document", "#", method: "delete") + Leaf("Search documents", "#", methods: ["post", "get"]), + Leaf("Get document", "#", methods: "get"), + Leaf("Get document source", "#", methods: "get") with { Deprecated = true }, + Leaf("Delete document", "#", methods: "delete") ), Leaf("Query DSL", "#", badge: "ns"), new() { Kind = NavigationRenderNodeKind.Island, IsTopLevel = false, NavigationTitle = "Clients", Url = "#" } diff --git a/src/Elastic.Documentation.Site/Components/Navigation/NavTree/NavTreeModel.cs b/src/Elastic.Documentation.Site/Components/Navigation/NavTree/NavTreeModel.cs index 5b5973d8de..f1a8c6bf62 100644 --- a/src/Elastic.Documentation.Site/Components/Navigation/NavTree/NavTreeModel.cs +++ b/src/Elastic.Documentation.Site/Components/Navigation/NavTree/NavTreeModel.cs @@ -14,9 +14,15 @@ public sealed record NavTreeModel /// The tree's content hash. Pages that share a tree share this id. public required string Id { get; init; } - public static bool TryParseMethod(string? method, out HttpVerb parsed) + /// The methods the badge can show, in the order given; a method with no badge, such as OPTIONS, is skipped. + public static IReadOnlyList ParseMethods(IEnumerable methods) { - parsed = HttpVerb.Get; - return method is not null && Enum.TryParse(method, ignoreCase: true, out parsed); + var parsed = new List(); + foreach (var method in methods) + { + if (Enum.TryParse(method, ignoreCase: true, out var verb)) + parsed.Add(verb); + } + return parsed; } } diff --git a/src/Elastic.Documentation.Site/Components/Navigation/NavTree/_NavTreeLabel.cshtml b/src/Elastic.Documentation.Site/Components/Navigation/NavTree/_NavTreeLabel.cshtml index f305349b87..d21b416350 100644 --- a/src/Elastic.Documentation.Site/Components/Navigation/NavTree/_NavTreeLabel.cshtml +++ b/src/Elastic.Documentation.Site/Components/Navigation/NavTree/_NavTreeLabel.cshtml @@ -9,7 +9,19 @@ { @await RenderPartialAsync(_Badge.Create(new BadgeModel { Text = badge, Tone = Tone.Info })) } -@if (NavTreeModel.TryParseMethod(Model.HttpMethod, out var method)) +@if (NavTreeModel.ParseMethods(Model.HttpMethods) is { Count: > 0 } methods) { - @await RenderPartialAsync(_MethodBadge.Create(new MethodBadgeModel { Method = method, Small = true })) + if (methods.Count == 1) + { + @await RenderPartialAsync(_MethodBadge.Create(new MethodBadgeModel { Method = methods[0], Small = true })) + } + else + { + + @foreach (var method in methods) + { + @await RenderPartialAsync(_MethodBadge.Create(new MethodBadgeModel { Method = method, Small = true })) + } + + } } diff --git a/src/Elastic.Documentation.Site/Components/Pages/ApiReference/api-reference.css b/src/Elastic.Documentation.Site/Components/Pages/ApiReference/api-reference.css index 9258eb521c..2cabd6d953 100644 --- a/src/Elastic.Documentation.Site/Components/Pages/ApiReference/api-reference.css +++ b/src/Elastic.Documentation.Site/Components/Pages/ApiReference/api-reference.css @@ -118,11 +118,6 @@ font-size: 0.6875rem; } -.api-page-intro .api-url-listing > li + li .api-url-also-method { - font-size: 0.625rem; - line-height: 0.875rem; -} - .api-page-intro .api-security-badges { display: flex; flex-wrap: wrap; @@ -659,6 +654,11 @@ font-size: 0.6875rem; } +/* A joined chip is as wide as its methods; each part must not also claim a whole chip width. */ +.api-index-item .api-method-group .api-method { + min-width: 0; +} + .api-index-item-title { min-width: 0; overflow-wrap: anywhere; @@ -877,12 +877,128 @@ color: var(--color-text-muted); } +/* + * An operation that accepts several methods: one pill, the dominant method on the left. The chips join on a slanted + * edge, each keeps its outline on its outer edges, and the pill owns the corner radius. + */ +.api-method-group, +.nav-v2-api-method-group { + display: inline-flex; + flex-shrink: 0; + align-items: flex-start; + align-self: flex-start; +} + +.api-method-group { + border-radius: 8px; +} + +.nav-v2-api-method-group { + border-radius: 4px; +} + +.api-method-group > .api-method, +.nav-v2-api-method-group > .nav-v2-api-method { + --method-slant: 0.5em; + border-width: 1px 0; + border-radius: 0; +} + +.api-method-group > .api-method:first-child, +.nav-v2-api-method-group > .nav-v2-api-method:first-child { + border-left-width: 1px; + border-top-left-radius: inherit; + border-bottom-left-radius: inherit; +} + +.api-method-group > .api-method:last-child, +.nav-v2-api-method-group > .nav-v2-api-method:last-child { + border-right-width: 1px; + border-top-right-radius: inherit; + border-bottom-right-radius: inherit; +} + +.api-method-group > .api-method:not(:last-child), +.nav-v2-api-method-group > .nav-v2-api-method:not(:last-child) { + clip-path: polygon( + 0 0, + 100% 0, + calc(100% - var(--method-slant)) 100%, + 0 100% + ); +} + +.api-method-group > .api-method:not(:first-child), +.nav-v2-api-method-group > .nav-v2-api-method:not(:first-child) { + margin-left: calc(2px - var(--method-slant)); + clip-path: polygon(var(--method-slant) 0, 100% 0, 100% 100%, 0 100%); +} + +/* Room either side of the seam, so the labels do not crowd the slant. */ +.api-method-group > .api-method:not(:last-child) > :last-child { + margin-right: 0.35em; +} + +.api-method-group > .api-method:not(:first-child) > :first-child { + margin-left: 0.35em; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:not(:last-child) { + padding-right: calc(4px + 0.35em); +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:not(:first-child) { + padding-left: calc(4px + 0.35em); +} + +/* The dominant method keeps its colour; the others recede to the old "also" look: no fill, dashed grey outline, muted text. */ +.api-method-group > .api-method:not(:first-child), +#pages-nav + a.sidebar-link + .nav-v2-api-method-group + > .nav-v2-api-method:not(:first-child) { + border-style: dashed; + border-color: var(--color-border-control); + background-color: transparent; + color: var(--color-text-muted); +} + +.api-method-group > .api-method:not(:first-child) { + font-weight: 400; +} + +#pages-nav .nav-v2-api-method-group { + margin-top: 2px; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method { + margin-top: 0; + border-width: 1px 0; + border-radius: 0; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:first-child { + border-left-width: 1px; + border-top-left-radius: inherit; + border-bottom-left-radius: inherit; +} + +#pages-nav .nav-v2-api-method-group > .nav-v2-api-method:last-child { + border-right-width: 1px; + border-top-right-radius: inherit; + border-bottom-right-radius: inherit; +} + /* Legacy rows reserve 32px on the right; a row with a chip needs less. */ -#pages-nav li:has(> a.sidebar-link > .nav-v2-api-method) { +#pages-nav + li:has( + > a.sidebar-link > :is(.nav-v2-api-method, .nav-v2-api-method-group) + ) { padding-right: var(--space-2); } -#pages-nav a.sidebar-link:has(> .nav-v2-api-method) { +#pages-nav + a.sidebar-link:has(> :is(.nav-v2-api-method, .nav-v2-api-method-group)) { display: flex; align-items: flex-start; justify-content: space-between; @@ -3629,21 +3745,6 @@ html:has(.api-page-shell) { } } -/* Merged endpoint rows: dominant method, "also GET", optional segments dashed. */ -.api-url-also-method { - flex-shrink: 0; - padding: 0 0.375rem; - border: 1px dashed var(--color-border-control); - border-radius: 62.4375rem; - color: var(--color-text-muted); - font-family: var(--font-body, Inter, ui-sans-serif, system-ui, sans-serif); - font-size: 0.6875rem; - font-weight: 500; - line-height: 1rem; - white-space: nowrap; - cursor: help; -} - .api-url-path { min-width: 0; } diff --git a/src/Elastic.Documentation.Site/Components/Primitives/Method/method.css b/src/Elastic.Documentation.Site/Components/Primitives/Method/method.css index f85e0de262..2acd88ddb6 100644 --- a/src/Elastic.Documentation.Site/Components/Primitives/Method/method.css +++ b/src/Elastic.Documentation.Site/Components/Primitives/Method/method.css @@ -12,3 +12,49 @@ padding: 0 var(--space-1); font-size: 0.625rem; } + +/* An operation that accepts several methods: one pill, the dominant method on the left. The badges join on a slanted edge and each keeps its outline on its outer edges. */ +.method-group { + display: inline-flex; + align-items: center; + border-radius: var(--radius-sm); +} + +.method-group > .method { + --method-slant: 0.5em; + border-width: var(--border-width) 0; + border-radius: 0; +} + +.method-group > .method:first-child { + border-left-width: var(--border-width); + border-top-left-radius: var(--radius-sm); + border-bottom-left-radius: var(--radius-sm); +} + +.method-group > .method:last-child { + border-right-width: var(--border-width); + border-top-right-radius: var(--radius-sm); + border-bottom-right-radius: var(--radius-sm); +} + +.method-group > .method:not(:last-child) { + padding-right: calc(var(--space-1) + 0.35em); + clip-path: polygon( + 0 0, + 100% 0, + calc(100% - var(--method-slant)) 100%, + 0 100% + ); +} + +.method-group > .method:not(:first-child) { + margin-left: calc(2px - var(--method-slant)); + padding-left: calc(var(--space-1) + 0.35em); + border-left-width: 0; + border-style: dashed; + border-color: var(--color-border); + background: transparent; + clip-path: polygon(var(--method-slant) 0, 100% 0, 100% 100%, 0 100%); + color: var(--color-text-muted); +} diff --git a/src/Elastic.Documentation.Site/Navigation/NavigationRenderModel.cs b/src/Elastic.Documentation.Site/Navigation/NavigationRenderModel.cs index d1073fe01e..39390f6866 100644 --- a/src/Elastic.Documentation.Site/Navigation/NavigationRenderModel.cs +++ b/src/Elastic.Documentation.Site/Navigation/NavigationRenderModel.cs @@ -30,8 +30,8 @@ public sealed record NavigationRenderNode public required string Url { get; init; } /// Badge parsed from a [ns]/[cmd]/[alias] title prefix; doubles as its CSS class suffix. public string? Badge { get; init; } - /// Lowercase HTTP method for API operation leaves; drives the sidebar method glyph. - public string? HttpMethod { get; init; } + /// Lowercase HTTP methods for API operation leaves, the dominant one first; drives the sidebar method chip. + public IReadOnlyList HttpMethods { get; init; } = []; /// True for an API operation leaf the spec marks deprecated. public bool Deprecated { get; init; } /// Only projected for nodes, where it drives the expand/collapse checkbox and its persisted state. @@ -397,14 +397,14 @@ private static NavigationRenderNode CreateNode(INodeNavigationItem { Model: IApiOperationNavigationModel op } ? op : null; + var operation = item as IApiOperationNavigationItem; return new NavigationRenderNode { Kind = NavigationRenderNodeKind.Leaf, IsTopLevel = isTopLevel, NavigationTitle = navigationTitle, Badge = badge, - HttpMethod = operation?.HttpMethod, + HttpMethods = operation?.HttpMethods ?? [], Deprecated = operation?.Deprecated ?? false, Url = item.Url }; @@ -465,7 +465,8 @@ private static void AppendNode(IncrementalHash hash, NavigationRenderNode node) AppendInt(hash, node.IsTopLevel ? 1 : 0); Append(hash, node.NavigationTitle); Append(hash, node.Badge ?? string.Empty); - Append(hash, node.HttpMethod ?? string.Empty); + foreach (var method in node.HttpMethods) + Append(hash, method); Append(hash, node.Url); Append(hash, node.Id ?? string.Empty); AppendInt(hash, node.ShowToggle ? 1 : 0); diff --git a/src/Elastic.Documentation.Site/Navigation/_NavHttpMethodGlyph.cshtml b/src/Elastic.Documentation.Site/Navigation/_NavHttpMethodGlyph.cshtml index 100d242f7e..7b03debd2d 100644 --- a/src/Elastic.Documentation.Site/Navigation/_NavHttpMethodGlyph.cshtml +++ b/src/Elastic.Documentation.Site/Navigation/_NavHttpMethodGlyph.cshtml @@ -1,5 +1,9 @@ @inherits RazorSlice -@if (Model.HttpMethod is { } httpMethod) +@if (Model.HttpMethods is [var only]) { -@httpMethod.ToUpperInvariant() +@only.ToUpperInvariant() +} +else if (Model.HttpMethods.Count > 1) +{ +@foreach (var httpMethod in Model.HttpMethods){@httpMethod.ToUpperInvariant()} } diff --git a/tests/Elastic.ApiExplorer.Tests/ApiPagesNavRenderingTests.cs b/tests/Elastic.ApiExplorer.Tests/ApiPagesNavRenderingTests.cs index dcbc761c1c..a8bf5aaa1a 100644 --- a/tests/Elastic.ApiExplorer.Tests/ApiPagesNavRenderingTests.cs +++ b/tests/Elastic.ApiExplorer.Tests/ApiPagesNavRenderingTests.cs @@ -14,6 +14,7 @@ using Elastic.Documentation.Site; using Elastic.Documentation.Site.FileProviders; using Elastic.Documentation.Site.Navigation; +using Nullean.Snapshots; using Nullean.Snapshots.Html; using RazorSlices; @@ -206,7 +207,7 @@ public async Task Render_OperationRow_ShowsVerbTextAfterOperationName(string met IsTopLevel = false, NavigationTitle = title, Url = "/api/doc/elasticsearch/operation/example", - HttpMethod = method + HttpMethods = [method] } ]).RenderAsync(cancellationToken: TestContext.Current!.Execution.CancellationToken); @@ -231,7 +232,7 @@ public async Task Render_DeprecatedOperationRow_MarksTheLinkAndNamesItForScreenR IsTopLevel = false, NavigationTitle = "Get a document source", Url = "/api/doc/elasticsearch/operation/operation-get-source", - HttpMethod = "get", + HttpMethods = ["get"], Deprecated = true } ]; @@ -351,4 +352,31 @@ private static int CountOccurrences(string html, string value) return count; } + + [Test] + public async Task Render_MultiMethodOperationRow_JoinsEveryMethodIntoOneChipDominantFirst() + { + var html = await _TocTreeNav.Create([ + new NavigationRenderNode + { + Kind = NavigationRenderNodeKind.Leaf, + IsTopLevel = false, + NavigationTitle = "Search", + Url = "/api/doc/elasticsearch/operation/search", + HttpMethods = ["post", "get"] + } + ]).RenderAsync(cancellationToken: TestContext.Current!.Execution.CancellationToken); + + OperationLink(html) + .Should() + .MatchInlineSnapshot( + """ +
+ Search + POSTGET + + + """ + ); + } } diff --git a/tests/Elastic.ApiExplorer.Tests/HttpMethodBadgeRenderingTests.cs b/tests/Elastic.ApiExplorer.Tests/HttpMethodBadgeRenderingTests.cs new file mode 100644 index 0000000000..41f50c8045 --- /dev/null +++ b/tests/Elastic.ApiExplorer.Tests/HttpMethodBadgeRenderingTests.cs @@ -0,0 +1,50 @@ +// Licensed to Elasticsearch B.V under one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information + +using AwesomeAssertions; +using Elastic.ApiExplorer._Partials; +using Nullean.Snapshots; +using RazorSlices; + +namespace Elastic.ApiExplorer.Tests; + +public class HttpMethodBadgeRenderingTests +{ + private static ValueTask RenderAsync(params string[] methods) => + _HttpMethodBadge.Create(methods).RenderAsync(cancellationToken: TestContext.Current!.Execution.CancellationToken); + + [Test] + public async Task Render_OneMethod_IsALoneChip() + { + var html = await RenderAsync("get"); + + html.Should().NotContain("api-method-group"); + html.Should().Contain("api-method api-method-get"); + } + + [Test] + public async Task Render_SeveralMethods_JoinsOneChipPerMethodDominantFirstUnderOneTooltip() + { + var html = await RenderAsync("post", "get"); + + html.Should().MatchInlineSnapshot( + """ + + + + POST + + + + GET + + + """ + ); + } +} diff --git a/tests/Elastic.ApiExplorer.Tests/OpenApiGeneratorMarkdownEmissionTests.cs b/tests/Elastic.ApiExplorer.Tests/OpenApiGeneratorMarkdownEmissionTests.cs index d9274d3f1c..37728d5599 100644 --- a/tests/Elastic.ApiExplorer.Tests/OpenApiGeneratorMarkdownEmissionTests.cs +++ b/tests/Elastic.ApiExplorer.Tests/OpenApiGeneratorMarkdownEmissionTests.cs @@ -188,7 +188,7 @@ public async Task Generate_CollapsedOperations_RedirectFormerUrls() var file = context.WriteFileSystem.File; var canonical = await file.ReadAllTextAsync(Path.Join(operationDir, "operation-search", "index.html")); var former = await file.ReadAllTextAsync(Path.Join(operationDir, "operation-search-1", "index.html")); - canonical.Should().Contain("api-url-also-method"); + canonical.Should().Contain("class=\"api-method-group\""); former.Should().Contain( "window.location.replace(\"/api/doc/elasticsearch/operation/operation-search\" + window.location.search + window.location.hash)" ); diff --git a/tests/Elastic.ApiExplorer.Tests/OperationDeprecatedProjectionTests.cs b/tests/Elastic.ApiExplorer.Tests/OperationDeprecatedProjectionTests.cs index 60236d8a3d..e331eb7691 100644 --- a/tests/Elastic.ApiExplorer.Tests/OperationDeprecatedProjectionTests.cs +++ b/tests/Elastic.ApiExplorer.Tests/OperationDeprecatedProjectionTests.cs @@ -51,7 +51,7 @@ public void Create_DeprecatedOperation_ProjectsDeprecatedOntoTheLeaf() var deprecated = leaves.Single(n => n.Url == Operation("docs-get-source").Url); deprecated.Deprecated.Should().BeTrue(); - deprecated.HttpMethod.Should().Be("get"); + deprecated.HttpMethods.Should().Equal("get"); leaves.Single(n => n.Url == Operation("docs-get").Url).Deprecated.Should().BeFalse(); } diff --git a/tests/Elastic.ApiExplorer.Tests/OperationEndpointTests.cs b/tests/Elastic.ApiExplorer.Tests/OperationEndpointTests.cs index 3b8906305f..6390896712 100644 --- a/tests/Elastic.ApiExplorer.Tests/OperationEndpointTests.cs +++ b/tests/Elastic.ApiExplorer.Tests/OperationEndpointTests.cs @@ -7,7 +7,6 @@ using Elastic.ApiExplorer.Infrastructure; using Elastic.ApiExplorer.Model; using Elastic.ApiExplorer.Operations; -using Elastic.Documentation.Navigation; using Microsoft.OpenApi; namespace Elastic.ApiExplorer.Tests; @@ -15,17 +14,26 @@ namespace Elastic.ApiExplorer.Tests; public class OperationEndpointTests { [Test] - public void HttpMethod_ListingAddsDominantMethod_UsesDominant() + public void Methods_ListingAddsDominantMethod_LeadsWithDominant() { var withListing = Op(HttpMethod.Get, "/{index}/_mvt/{field}"); - withListing = withListing with - { - Operation = new OpenApiOperation { Description = Bump(("POST", "/{index}/_mvt/{field}"), ("GET", "/{index}/_mvt/{field}")) } - }; + var listing = Bump(("POST", "/{index}/_mvt/{field}"), ("GET", "/{index}/_mvt/{field}")); var withoutListing = Op(HttpMethod.Get, "/_cluster/health"); - ((IApiOperationNavigationModel)withListing).HttpMethod.Should().Be("post"); - ((IApiOperationNavigationModel)withoutListing).HttpMethod.Should().Be("get"); + OperationEndpoint.Build(withListing, listing, []).Methods.Should().Equal("post", "get"); + OperationEndpoint.Build(withoutListing, null, []).Methods.Should().Equal("get"); + } + + [Test] + public void Methods_RowsWithDifferentMethods_LeadsWithTheMainRowsDominantMethodThenByRank() + { + var endpoint = OperationEndpoint.FromVariants( + [new EndpointVariant("get", "/{index}/_x/{id}"), new EndpointVariant("put", "/_x"), new EndpointVariant("post", "/_x")], + "/{index}/_x/{id}" + ); + + endpoint.Rows[0].Methods.Should().Equal("get"); + endpoint.Methods.Should().Equal("get", "post", "put"); } private static ApiOperation Op(HttpMethod method, string route, string? operationId = null, params string[] queryParameters) => diff --git a/tests/Navigation.Tests/.snapshots/DesignSystem/NavSideRenderingTests/IslandSidebar_ShowsBackLinksAsPillsAndTheSectionTitleAsAHeading.verified.html b/tests/Navigation.Tests/.snapshots/DesignSystem/NavSideRenderingTests/IslandSidebar_ShowsBackLinksAsPillsAndTheSectionTitleAsAHeading.verified.html index 033e244a4e..7a4536366b 100644 --- a/tests/Navigation.Tests/.snapshots/DesignSystem/NavSideRenderingTests/IslandSidebar_ShowsBackLinksAsPillsAndTheSectionTitleAsAHeading.verified.html +++ b/tests/Navigation.Tests/.snapshots/DesignSystem/NavSideRenderingTests/IslandSidebar_ShowsBackLinksAsPillsAndTheSectionTitleAsAHeading.verified.html @@ -61,7 +61,10 @@