Skip to content

Commit 6067e42

Browse files
authored
Merge pull request #178 from couchbase/hakim-top-nav-highlight
AV-75840 top nav highlight
2 parents 6f8e169 + e61865a commit 6067e42

File tree

5 files changed

+75
-29
lines changed

5 files changed

+75
-29
lines changed

src/css/header.css

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
flex-wrap: wrap;
4343
align-items: center;
4444
justify-content: space-between;
45+
}
46+
47+
.navbar-new-top {
4548
padding: 0.5rem 0;
4649
}
4750

@@ -82,7 +85,8 @@
8285
}
8386

8487
.navbar-new-bottom .nav-item {
85-
margin: 0 var(--base-space);
88+
margin: 0 var(--base-extra-small-space);
89+
padding: var(--base-extra-small-space) var(--base-space);
8690
}
8791

8892
.navbar-nav .nav-link {
@@ -213,6 +217,10 @@
213217
left: -0.75em;
214218
}
215219

220+
.nav-item-selected {
221+
background-color: var(--color-brand-red);
222+
}
223+
216224
@media screen and (min-width: 1024px) {
217225
.navbar-start > a.navbar-item:hover,
218226
.navbar-start > .navbar-item:hover > .navbar-link {
@@ -267,7 +275,7 @@
267275
}
268276

269277
@media screen and (max-width: 992px) {
270-
.navbar {
278+
.navbar-new-top {
271279
padding: 0.8rem 0;
272280
}
273281

@@ -291,12 +299,6 @@
291299
width: 100%;
292300
}
293301

294-
.navbar-new-bottom .nav-item {
295-
padding: 0 15px;
296-
width: 100%;
297-
margin: 10px 0;
298-
}
299-
300302
.search {
301303
margin: 10px 0;
302304
width: 100%;

src/helpers/includes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
'use strict'
22

3-
module.exports = (haystack, needle) => ~(haystack || '').indexOf(needle)
3+
module.exports = (haystack, needle) => {
4+
return ~(haystack || '').indexOf(needle)
5+
}

src/helpers/nav-group-selected.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
module.exports = module.exports = (navGroup, {
4+
data: {
5+
root: { page, site },
6+
},
7+
}) => {
8+
if (navGroup === 'home') {
9+
// FAKE value to signal that we instead check all the navgroups
10+
const navGroups = site.keys.navGroups
11+
// NB this relies on variable stored in site.keys, so must be called
12+
// *after* nav-groups has already prepared the data
13+
const possible = navGroups.filter((it) => selected(it, page))
14+
return possible.length === 1 && possible[0].title === 'Home'
15+
} else {
16+
return selected(navGroup, page)
17+
}
18+
}
19+
20+
function selected (navGroup, page) {
21+
return (navGroup.url === page?.url) ||
22+
(navGroup.components?.includes(page.component?.name))
23+
}

src/helpers/nav-groups.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@ module.exports = ({
66
},
77
}) => {
88
let navGroups = site.keys.navGroups
9+
910
if (!navGroups) return []
1011
if (navGroups._compiled) return navGroups
12+
1113
const components = site.components
1214
const componentNames = Object.keys(components)
1315
const claimed = []
14-
navGroups = JSON.parse(navGroups).reduce((accum, navGroup) => {
15-
const componentNamesInGroup = (navGroup.components || []).reduce((matched, componentName) => {
16-
if (~componentName.indexOf('*')) {
17-
const rx = new RegExp(`^${componentName.replace(/[*]/g, '.*?')}$`)
18-
return matched.concat(componentNames.filter((candidate) => rx.test(candidate)))
19-
} else if (componentName in components) {
20-
return matched.concat(componentName)
21-
}
22-
return matched
23-
}, [])
16+
17+
navGroups = JSON.parse(navGroups).map((navGroup) => {
18+
const componentNamesInGroup =
19+
(navGroup.components || []).flatMap(
20+
(componentName) => componentNames.filter(globbify(componentName)))
21+
2422
claimed.push(...componentNamesInGroup)
25-
return accum.concat(compileNavGroup(navGroup, componentNamesInGroup, contentCatalog, components))
26-
}, [])
27-
const orphaned = componentNames.filter((it) => claimed.indexOf(it) < 0)
23+
24+
return compileNavGroup(
25+
navGroup,
26+
componentNamesInGroup,
27+
contentCatalog,
28+
components)
29+
})
30+
31+
const orphaned = componentNames.filter((it) => !claimed.includes(it))
32+
2833
if (orphaned.length) {
2934
const homeIdx = orphaned.indexOf('home')
3035
if (~homeIdx) {
@@ -41,18 +46,22 @@ module.exports = ({
4146
: navGroups.push(compileNavGroup({ title: 'General' }, orphaned, contentCatalog, components))
4247
}
4348
}
49+
4450
navGroups._compiled = true
45-
return (site.keys.navGroups = navGroups)
51+
52+
site.keys.navGroups = navGroups
53+
return navGroups
4654
}
4755

4856
function compileNavGroup (navGroup, componentNamesInGroup, contentCatalog, components) {
49-
navGroup.components = componentNamesInGroup
5057
let startPage = navGroup.startPage
5158
if (startPage) {
5259
startPage = contentCatalog.resolvePage(startPage)
5360
if (startPage) navGroup.url = startPage.pub.url
5461
delete navGroup.startPage
5562
}
63+
64+
navGroup.components = componentNamesInGroup
5665
if (componentNamesInGroup.length) {
5766
navGroup.latestVersions = componentNamesInGroup.reduce((latestVersionMap, it) => {
5867
latestVersionMap[it] = components[it].latest.version
@@ -61,3 +70,8 @@ function compileNavGroup (navGroup, componentNamesInGroup, contentCatalog, compo
6170
}
6271
return navGroup
6372
}
73+
74+
function globbify (componentName) {
75+
const rx = new RegExp(`^${componentName.replace(/[*]/g, '.*?')}$`)
76+
return (it) => rx.test(it)
77+
}

src/partials/header-content.hbs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
</a>
1010
</li>
1111
<li>
12-
<a class="navbar-brand cb-documentation" href="{{#with (and site.url site.homeUrl)}}{{{@root.site.url}}}{{{this}}}{{else}}{{{siteRootPath}}}{{/with}}">
12+
<a class="navbar-brand cb-documentation"
13+
href="{{#if (and site.url site.homeUrl)}}{{{site.url}}}{{{site.homeUrl}}}{{else}}{{{siteRootPath}}}{{/if}}">
1314
<img src="{{{uiRootPath}}}/img/cb-documentation.svg" alt="Couchbase Documentation" class="cb-docs" />
1415
<img src="{{{uiRootPath}}}/img/cb-docs-hover.svg" alt="Couchbase Documentation" class="hide cb-hover-docs" />
1516
</a>
@@ -33,16 +34,19 @@
3334

3435
<div class="navbar-collapse collapse" id="navbar2">
3536
<ul class="navbar-nav w-100 justify-content-start">
36-
<li class="nav-item">
37-
<a href="{{#with (and site.url site.homeUrl)}}{{@root.site.url}}{{this}}{{else}}{{siteRootPath}}{{/with}}" class="nav-link">
37+
{{#with (nav-groups)}}
38+
39+
<li class="nav-item {{#if (nav-group-selected "home")}}nav-item-selected{{/if}}"">
40+
<a href="{{#if (and @root.site.url @root.site.homeUrl)}}{{@root.site.url}}{{@root.site.homeUrl}}{{else}}{{siteRootPath}}{{/if}}" class="nav-link">
3841
<i class="fas fa-home"></i>
3942
</a>
4043
</li>
41-
{{#each (nav-groups)}}
44+
{{#each this}}
4245
{{#if ./url}}
43-
<li class="nav-item">
46+
<li class="nav-item {{#if (nav-group-selected this)}}nav-item-selected{{/if}}">
4447
<a class="nav-link" href="{{relativize ./url}}">
4548
{{{./title}}}
49+
4650
{{#if (eq ./title 'Tutorials')}}
4751
<span class="arrow">
4852
<i class="fas fa-arrow-right"></i>
@@ -52,6 +56,7 @@
5256
</li>
5357
{{/if}}
5458
{{/each}}
59+
{{/with}}
5560
</ul>
5661
</div>
5762
<div class="primary-action">

0 commit comments

Comments
 (0)