Skip to content

Commit 0f7a5ff

Browse files
Merge pull request #29 from commitd/cf-layout-plugin
Use Gatsby Layout Plugin to maintain sidebar position
2 parents ef94217 + e66d825 commit 0f7a5ff

File tree

9 files changed

+71
-61
lines changed

9 files changed

+71
-61
lines changed

theme/gatsby-config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ module.exports = ({
3939
`gatsby-plugin-typescript`,
4040
`gatsby-plugin-material-ui`,
4141
`gatsby-plugin-react-helmet`,
42+
{
43+
resolve: `gatsby-plugin-layout`,
44+
options: {
45+
component: require.resolve(`./src/layout/index.tsx`)
46+
}
47+
},
4248
{
4349
resolve: `gatsby-source-filesystem`,
4450
options: {

theme/gatsby-node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ exports.createPages = async (
131131
path: item.url,
132132
component: path.resolve(`${__dirname}/src/layout/docs.tsx`),
133133
context: {
134+
layout: 'docs',
135+
current: item,
134136
id: item.id,
135137
previous,
136138
next
@@ -142,6 +144,7 @@ exports.createPages = async (
142144
path: '/print',
143145
component: path.resolve(`${__dirname}/src/layout/print.tsx`),
144146
context: {
147+
layout: 'print',
145148
data
146149
}
147150
})

theme/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@material-ui/icons": "^4.5.1",
3737
"@mdx-js/mdx": "^1.5.1",
3838
"@mdx-js/react": "^1.5.1",
39+
"gatsby-plugin-layout": "^1.3.0",
3940
"gatsby-plugin-material-ui": "^2.1.6",
4041
"gatsby-plugin-mdx": "^1.0.55",
4142
"gatsby-plugin-react-helmet": "^3.1.13",

theme/src/components/Layout.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import React, { useState, ReactNode, Dispatch } from 'react'
1+
import { Box, CodeStyle, Container, ThemeProvider } from '@committed/components'
2+
import { Content, Header as LayoutHeader, Nav, Root } from '@committed/layout'
3+
import { navigate } from 'gatsby'
4+
import React, { Dispatch, ReactNode, useState } from 'react'
5+
import { useHotkeys } from 'react-hotkeys-hook'
26
import 'typeface-dosis'
37
import 'typeface-lato'
4-
import { Header } from './Header'
5-
import { Footer } from './Footer'
6-
import { Sidebar } from './Sidebar'
78
import { PageContext } from '../types'
9+
import { Footer } from './Footer'
10+
import { Header } from './Header'
811
import { PreviousNext } from './PreviousNext'
912
import { SEO, SEOProps } from './SEO'
10-
import { navigate } from 'gatsby'
11-
import { useHotkeys } from 'react-hotkeys-hook'
12-
import { Root, Header as LayoutHeader, Nav, Content } from '@committed/layout'
13-
import { ThemeProvider, CodeStyle, Container, Box } from '@committed/components'
13+
import { Sidebar } from './Sidebar'
1414

1515
export interface LayoutProps extends SEOProps {
1616
id: string
@@ -33,7 +33,6 @@ export const Layout = ({
3333
pageContext = { id: 'default' },
3434
children,
3535
location = {},
36-
title,
3736
...props
3837
}: LayoutProps) => {
3938
const [collapsed, setCollapsed] = useState((location && location.state) || {})
@@ -68,7 +67,9 @@ export const Layout = ({
6867
}
6968
}}
7069
>
71-
<SEO title={title} {...props} />
70+
{pageContext.current && pageContext.current.title && (
71+
<SEO title={pageContext.current.title} {...props} />
72+
)}
7273
<Root
7374
style={{ minHeight: '100vh' }}
7475
config={{

theme/src/components/TreeNode.tsx

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import React from 'react'
2-
import { Item, Info } from '../types'
3-
import { firstUrl } from '../util/tree'
41
import {
5-
useTheme,
6-
Theme,
7-
List,
2+
Collapse,
83
Icons,
4+
List,
95
ListItem,
106
ListItemText,
11-
Collapse
7+
Theme,
8+
useTheme
129
} from '@committed/components'
10+
import React from 'react'
11+
import { Info, Item } from '../types'
12+
import { firstUrl } from '../util/tree'
1313

1414
export interface TreeNodeProps extends Item {
1515
isActive: (id: string) => boolean
@@ -21,30 +21,6 @@ export interface TreeNodeProps extends Item {
2121
id: string
2222
}
2323

24-
class Scroller extends React.Component<{ active: boolean }> {
25-
private ref = React.createRef<HTMLDivElement>()
26-
27-
scroll = () => {
28-
const { active } = this.props
29-
30-
if (active) {
31-
this.ref.current.scrollIntoView({ block: 'center', inline: 'nearest' })
32-
}
33-
}
34-
35-
componentDidMount() {
36-
this.scroll()
37-
}
38-
39-
componentDidUpdate() {
40-
this.scroll()
41-
}
42-
43-
render() {
44-
return <div ref={this.ref}>{this.props.children}</div>
45-
}
46-
}
47-
4824
export const TreeNode = React.memo(
4925
({
5026
id,
@@ -65,7 +41,7 @@ export const TreeNode = React.memo(
6541
const title = info ? info.title : label
6642
const active = isActive(id)
6743
return (
68-
<Scroller active={active}>
44+
<>
6945
<ListItem
7046
style={{
7147
paddingLeft: theme.spacing(2) * (level + 1)
@@ -111,7 +87,7 @@ export const TreeNode = React.memo(
11187
</List>
11288
</Collapse>
11389
) : null}
114-
</Scroller>
90+
</>
11591
)
11692
}
11793
)

theme/src/layout/docs.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
1-
import React from 'react'
21
import { graphql } from 'gatsby'
3-
import { Layout } from '../components/Layout'
2+
import React from 'react'
43
import { Markdown } from '../components/Markdown'
54
import '../style/mermaid.css'
65

7-
export default ({ pageContext, location, data }) => {
6+
export default ({ data }) => {
87
const {
9-
docs: { id, body, metaDescription, metaTitle, tableOfContents }
8+
docs: { body, tableOfContents }
109
} = data
11-
return (
12-
<Layout
13-
id={id}
14-
pageContext={pageContext}
15-
location={location}
16-
title={metaTitle}
17-
description={metaDescription}
18-
>
19-
<Markdown>{body}</Markdown>
20-
</Layout>
21-
)
10+
return <Markdown>{body}</Markdown>
2211
}
2312

2413
export const pageQuery = graphql`
2514
query DocQuery($id: String!) {
2615
docs(id: { eq: $id }) {
27-
id
28-
metaDescription
29-
metaTitle
3016
tableOfContents
3117
body
3218
}

theme/src/layout/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
import { Layout } from '../components/Layout'
3+
4+
export default props => {
5+
const { children, pageContext, location } = props
6+
7+
// Pass through when in print mode
8+
if (pageContext.layout === 'print') {
9+
return <>{children}</>
10+
} else {
11+
return (
12+
<Layout id={pageContext.id} pageContext={pageContext} location={location}>
13+
{children}
14+
</Layout>
15+
)
16+
}
17+
}

theme/src/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ export interface PageContext {
5757
id: string
5858
previous?: Info
5959
next?: Info
60+
current?: Info
6061
}

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,13 @@
786786
dependencies:
787787
regenerator-runtime "^0.13.2"
788788

789+
"@babel/runtime@^7.9.2":
790+
version "7.9.6"
791+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.9.6.tgz#a9102eb5cadedf3f31d08a9ecf294af7827ea29f"
792+
integrity sha512-64AF1xY3OAkFHqOb9s4jpgk1Mm5vDZ4L3acHvAml+53nO1XbXLuDodsVpO4OIUsmemlUHMxNdYMNJmsvOwLrvQ==
793+
dependencies:
794+
regenerator-runtime "^0.13.4"
795+
789796
"@babel/template@^7.6.0", "@babel/template@^7.7.0":
790797
version "7.7.0"
791798
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.7.0.tgz#4fadc1b8e734d97f56de39c77de76f2562e597d0"
@@ -5960,6 +5967,13 @@ gatsby-page-utils@^0.0.28:
59605967
micromatch "^3.1.10"
59615968
slash "^3.0.0"
59625969

5970+
gatsby-plugin-layout@^1.3.0:
5971+
version "1.3.0"
5972+
resolved "https://registry.yarnpkg.com/gatsby-plugin-layout/-/gatsby-plugin-layout-1.3.0.tgz#5ed5574cb08b36735a74073629f9f51c95463716"
5973+
integrity sha512-S8fYUIUZ8sluA0oSmqXoVkMPgT+iINHS5xYxtmvqlWnvOa1We4KbEV+9UBN8WEL+RtMCFuxJj2fVuLspqax67g==
5974+
dependencies:
5975+
"@babel/runtime" "^7.9.2"
5976+
59635977
gatsby-plugin-manifest@^2.2.26:
59645978
version "2.2.26"
59655979
resolved "https://registry.yarnpkg.com/gatsby-plugin-manifest/-/gatsby-plugin-manifest-2.2.26.tgz#040a7dc2ab54e64cc97481adfb498e5dc8caade4"
@@ -11301,6 +11315,11 @@ regenerator-runtime@^0.13.2, regenerator-runtime@^0.13.3:
1130111315
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5"
1130211316
integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==
1130311317

11318+
regenerator-runtime@^0.13.4:
11319+
version "0.13.5"
11320+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.5.tgz#d878a1d094b4306d10b9096484b33ebd55e26697"
11321+
integrity sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA==
11322+
1130411323
regenerator-transform@^0.14.0:
1130511324
version "0.14.1"
1130611325
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.1.tgz#3b2fce4e1ab7732c08f665dfdb314749c7ddd2fb"

0 commit comments

Comments
 (0)