Skip to content
This repository was archived by the owner on Nov 21, 2025. It is now read-only.

Commit cc77280

Browse files
authored
feat(gatsby-theme-docz): sidebar scroll to selected item in menu (#1228)
1 parent f0eee70 commit cc77280

3 files changed

Lines changed: 43 additions & 12 deletions

File tree

core/gatsby-theme-docz/src/components/NavGroup/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import * as styles from './styles'
77
import { NavLink } from '../NavLink'
88
import { ChevronDown } from '../Icons'
99

10-
export const NavGroup = ({ item }) => {
11-
const current = useCurrentDoc()
10+
export const NavGroup = ({ item, sidebarRef }) => {
11+
const currentDoc = useCurrentDoc()
12+
const currentDocRef = React.useRef()
1213
const { name, menu } = item
1314
const [subheadingsVisible, setShowsubheadings] = React.useState(
14-
current.menu === name
15+
currentDoc.menu === name
1516
)
1617
const toggleSubheadings = () => setShowsubheadings(!subheadingsVisible)
17-
18+
React.useEffect(() => {
19+
if (sidebarRef.current && currentDocRef.current) {
20+
sidebarRef.current.scrollTo(0, currentDocRef.current.offsetTop)
21+
}
22+
}, [])
1823
return (
1924
<div sx={styles.wrapper} data-testid="nav-group">
2025
<div sx={styles.title} onClick={toggleSubheadings}>
@@ -25,6 +30,13 @@ export const NavGroup = ({ item }) => {
2530
{menu &&
2631
subheadingsVisible &&
2732
menu.map(menu => {
33+
if (currentDoc.route === menu.route) {
34+
return (
35+
<NavLink key={menu.id} item={menu} ref={currentDocRef}>
36+
{menu.name}
37+
</NavLink>
38+
)
39+
}
2840
return (
2941
<NavLink key={menu.id} item={menu}>
3042
{menu.name}

core/gatsby-theme-docz/src/components/NavLink/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const getCurrentHash = () => {
2020
return window.location ? window.location.hash : ''
2121
}
2222

23-
export const NavLink = ({ item, ...props }) => {
23+
export const NavLink = React.forwardRef(({ item, ...props }, ref) => {
2424
const docs = useDocs()
2525
const to = item.route
2626
const headings = docs && getHeadings(to, docs)
@@ -30,7 +30,13 @@ export const NavLink = ({ item, ...props }) => {
3030
const currentHash = getCurrentHash()
3131
return (
3232
<React.Fragment>
33-
<Link {...props} to={to} sx={styles.link} activeClassName="active" />
33+
<Link
34+
{...props}
35+
to={to}
36+
sx={styles.link}
37+
activeClassName="active"
38+
ref={ref}
39+
/>
3440
{showHeadings &&
3541
headings.map(heading => (
3642
<Link
@@ -44,4 +50,4 @@ export const NavLink = ({ item, ...props }) => {
4450
))}
4551
</React.Fragment>
4652
)
47-
}
53+
})

core/gatsby-theme-docz/src/components/Sidebar/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/** @jsx jsx */
2-
import React, { useState } from 'react'
2+
import React, { useState, useRef, useEffect } from 'react'
33
import { Global } from '@emotion/core'
44
import { jsx, Box } from 'theme-ui'
5-
import { useMenus } from 'docz'
5+
import { useMenus, useCurrentDoc } from 'docz'
66

77
import * as styles from './styles'
88
import { NavSearch } from '../NavSearch'
@@ -12,11 +12,16 @@ import { NavGroup } from '../NavGroup'
1212
export const Sidebar = React.forwardRef((props, ref) => {
1313
const [query, setQuery] = useState('')
1414
const menus = useMenus({ query })
15-
15+
const currentDoc = useCurrentDoc()
16+
const currentDocRef = useRef()
1617
const handleChange = ev => {
1718
setQuery(ev.target.value)
1819
}
19-
20+
useEffect(() => {
21+
if (ref.current && currentDocRef.current) {
22+
ref.current.scrollTo(0, currentDocRef.current.offsetTop)
23+
}
24+
}, [])
2025
return (
2126
<>
2227
<Box onClick={props.onClick} sx={styles.overlay(props)}>
@@ -30,7 +35,15 @@ export const Sidebar = React.forwardRef((props, ref) => {
3035
/>
3136
{menus &&
3237
menus.map(menu => {
33-
if (!menu.route) return <NavGroup key={menu.id} item={menu} />
38+
if (!menu.route)
39+
return <NavGroup key={menu.id} item={menu} sidebarRef={ref} />
40+
if (menu.route === currentDoc.route) {
41+
return (
42+
<NavLink key={menu.id} item={menu} ref={currentDocRef}>
43+
{menu.name}
44+
</NavLink>
45+
)
46+
}
3447
return (
3548
<NavLink key={menu.id} item={menu}>
3649
{menu.name}

0 commit comments

Comments
 (0)