Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AV-75840 top nav highlight #178

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/css/header.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
}

.navbar-new-top {
padding: 0.5rem 0;
}

Expand Down Expand Up @@ -82,7 +85,8 @@
}

.navbar-new-bottom .nav-item {
margin: 0 var(--base-space);
margin: 0 var(--base-extra-small-space);
padding: var(--base-extra-small-space) var(--base-space);
}

.navbar-nav .nav-link {
Expand Down Expand Up @@ -213,6 +217,10 @@
left: -0.75em;
}

.nav-item-selected {
background-color: var(--color-brand-red);
}

@media screen and (min-width: 1024px) {
.navbar-start > a.navbar-item:hover,
.navbar-start > .navbar-item:hover > .navbar-link {
Expand Down Expand Up @@ -267,7 +275,7 @@
}

@media screen and (max-width: 992px) {
.navbar {
.navbar-new-top {
padding: 0.8rem 0;
}

Expand All @@ -291,12 +299,6 @@
width: 100%;
}

.navbar-new-bottom .nav-item {
padding: 0 15px;
width: 100%;
margin: 10px 0;
}

.search {
margin: 10px 0;
width: 100%;
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/includes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = (haystack, needle) => ~(haystack || '').indexOf(needle)
module.exports = (haystack, needle) => {
return ~(haystack || '').indexOf(needle)
}
23 changes: 23 additions & 0 deletions src/helpers/nav-group-selected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

module.exports = module.exports = (navGroup, {
data: {
root: { page, site },
},
}) => {
if (navGroup === 'home') {
// FAKE value to signal that we instead check all the navgroups
const navGroups = site.keys.navGroups
// NB this relies on variable stored in site.keys, so must be called
// *after* nav-groups has already prepared the data
const possible = navGroups.filter((it) => selected(it, page))
return possible.length === 1 && possible[0].title === 'Home'
} else {
return selected(navGroup, page)
}
}

function selected (navGroup, page) {
return (navGroup.url === page?.url) ||
(navGroup.components?.includes(page.component?.name))
}
44 changes: 29 additions & 15 deletions src/helpers/nav-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ module.exports = ({
},
}) => {
let navGroups = site.keys.navGroups

if (!navGroups) return []
if (navGroups._compiled) return navGroups

const components = site.components
const componentNames = Object.keys(components)
const claimed = []
navGroups = JSON.parse(navGroups).reduce((accum, navGroup) => {
const componentNamesInGroup = (navGroup.components || []).reduce((matched, componentName) => {
if (~componentName.indexOf('*')) {
const rx = new RegExp(`^${componentName.replace(/[*]/g, '.*?')}$`)
return matched.concat(componentNames.filter((candidate) => rx.test(candidate)))
} else if (componentName in components) {
return matched.concat(componentName)
}
return matched
}, [])

navGroups = JSON.parse(navGroups).map((navGroup) => {
const componentNamesInGroup =
(navGroup.components || []).flatMap(
(componentName) => componentNames.filter(globbify(componentName)))

claimed.push(...componentNamesInGroup)
return accum.concat(compileNavGroup(navGroup, componentNamesInGroup, contentCatalog, components))
}, [])
const orphaned = componentNames.filter((it) => claimed.indexOf(it) < 0)

return compileNavGroup(
navGroup,
componentNamesInGroup,
contentCatalog,
components)
})

const orphaned = componentNames.filter((it) => !claimed.includes(it))

if (orphaned.length) {
const homeIdx = orphaned.indexOf('home')
if (~homeIdx) {
Expand All @@ -41,18 +46,22 @@ module.exports = ({
: navGroups.push(compileNavGroup({ title: 'General' }, orphaned, contentCatalog, components))
}
}

navGroups._compiled = true
return (site.keys.navGroups = navGroups)

site.keys.navGroups = navGroups
return navGroups
}

function compileNavGroup (navGroup, componentNamesInGroup, contentCatalog, components) {
navGroup.components = componentNamesInGroup
let startPage = navGroup.startPage
if (startPage) {
startPage = contentCatalog.resolvePage(startPage)
if (startPage) navGroup.url = startPage.pub.url
delete navGroup.startPage
}

navGroup.components = componentNamesInGroup
if (componentNamesInGroup.length) {
navGroup.latestVersions = componentNamesInGroup.reduce((latestVersionMap, it) => {
latestVersionMap[it] = components[it].latest.version
Expand All @@ -61,3 +70,8 @@ function compileNavGroup (navGroup, componentNamesInGroup, contentCatalog, compo
}
return navGroup
}

function globbify (componentName) {
const rx = new RegExp(`^${componentName.replace(/[*]/g, '.*?')}$`)
return (it) => rx.test(it)
}
15 changes: 10 additions & 5 deletions src/partials/header-content.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
</a>
</li>
<li>
<a class="navbar-brand cb-documentation" href="{{#with (and site.url site.homeUrl)}}{{{@root.site.url}}}{{{this}}}{{else}}{{{siteRootPath}}}{{/with}}">
<a class="navbar-brand cb-documentation"
href="{{#if (and site.url site.homeUrl)}}{{{site.url}}}{{{site.homeUrl}}}{{else}}{{{siteRootPath}}}{{/if}}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious if this still works as expected with replacing the #with helper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! So with is basically an if but with scoping.

So {{{@root.site.url}}}{{{this}}}
relied on that behaviour + and's behaviour (or returning the second truthy value on success) which honestly is a bit "cute" to understand.

This refactor just makes the logic a bit more explicit.

<img src="{{{uiRootPath}}}/img/cb-documentation.svg" alt="Couchbase Documentation" class="cb-docs" />
<img src="{{{uiRootPath}}}/img/cb-docs-hover.svg" alt="Couchbase Documentation" class="hide cb-hover-docs" />
</a>
Expand All @@ -33,16 +34,19 @@

<div class="navbar-collapse collapse" id="navbar2">
<ul class="navbar-nav w-100 justify-content-start">
<li class="nav-item">
<a href="{{#with (and site.url site.homeUrl)}}{{@root.site.url}}{{this}}{{else}}{{siteRootPath}}{{/with}}" class="nav-link">
{{#with (nav-groups)}}

<li class="nav-item {{#if (nav-group-selected "home")}}nav-item-selected{{/if}}"">
<a href="{{#if (and @root.site.url @root.site.homeUrl)}}{{@root.site.url}}{{@root.site.homeUrl}}{{else}}{{siteRootPath}}{{/if}}" class="nav-link">
<i class="fas fa-home"></i>
</a>
</li>
{{#each (nav-groups)}}
{{#each this}}
{{#if ./url}}
<li class="nav-item">
<li class="nav-item {{#if (nav-group-selected this)}}nav-item-selected{{/if}}">
<a class="nav-link" href="{{relativize ./url}}">
{{{./title}}}

{{#if (eq ./title 'Tutorials')}}
<span class="arrow">
<i class="fas fa-arrow-right"></i>
Expand All @@ -52,6 +56,7 @@
</li>
{{/if}}
{{/each}}
{{/with}}
</ul>
</div>
<div class="primary-action">
Expand Down