Skip to content

Commit 7e76c79

Browse files
author
cvluca
committed
update sidebar
1 parent 4b357eb commit 7e76c79

File tree

10 files changed

+56
-27
lines changed

10 files changed

+56
-27
lines changed

gatsby-browser.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
*/
66

77
// You can delete this file if you're not using it
8+
import wrapWithProvider from "./wrap-with-provider"
9+
export const wrapRootElement = wrapWithProvider

gatsby-ssr.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
*/
66

77
// You can delete this file if you're not using it
8+
import wrapWithProvider from "./wrap-with-provider"
9+
export const wrapRootElement = wrapWithProvider

src/components/Container/Container.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Component } from 'react';
22
import { connect } from "react-redux";
33
import { getSidebarDockedState, getHeaderHeightState } from '../../store/selectors';
4-
import { pullRight } from '../ResponsiveSidebar/sidebar-config';
54

65
class Container extends Component {
76
render() {
@@ -11,8 +10,8 @@ class Container extends Component {
1110
style={{
1211
position: "absolute",
1312
top: headerHeight + 30,
14-
left: (sidebarDocked && !pullRight) ? "20%" : 0,
15-
right: (sidebarDocked && pullRight) ? "20%" : 0,
13+
left: (sidebarDocked) ? "20%" : 0,
14+
right: 0,
1615
bottom: 0,
1716
}}
1817
>

src/components/Layout/layout.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import React from 'react'
22
import PropTypes from 'prop-types'
33
import Helmet from 'react-helmet'
44
import { StaticQuery, graphql } from 'gatsby'
5-
import { Provider } from 'react-redux'
6-
7-
import store from '../../store'
85
import Header from '../Header/header'
96
import './layout.css'
107
import ResponsiveSidebar from '../ResponsiveSidebar';
@@ -22,7 +19,6 @@ const Layout = ({ children }) => (
2219
}
2320
`}
2421
render={data => (
25-
<Provider store={store}>
2622
<>
2723
<Helmet
2824
title={data.site.siteMetadata.title}
@@ -38,8 +34,7 @@ const Layout = ({ children }) => (
3834
<Container>
3935
{children}
4036
</Container>
41-
</>
42-
</Provider>
37+
</>
4338
)}
4439
/>
4540
)

src/components/ResponsiveSidebar/ResponsiveSideBar.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getSidebarState, getHeaderHeightState } from "../../store/selectors";
44
import { connect } from "react-redux";
55
import { onSetSidebarOpen, onSetSidebarDocked } from "../../actions/sidebar";
66
import MediaQuery from "react-responsive";
7-
import { maxWidth, pullRight, styles } from './sidebar-config';
7+
import { maxWidth, styles } from './sidebar-config';
88
import SidebarContent from "./SidebarContent";
99

1010
class ResponsiveSidebar extends Component {
@@ -33,7 +33,6 @@ class ResponsiveSidebar extends Component {
3333
open={sidebarOpen}
3434
docked={!matches}
3535
onSetOpen={this.onSetSidebarOpen}
36-
pullRight={pullRight}
3736
shadow={false}
3837
>
3938
<b></b>

src/components/ResponsiveSidebar/SidebarContent.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const convertToTree = (data) => {
2323

2424
const constructTree = (list) => {
2525
let tree = []
26+
let dir = []
2627
list.forEach(item => {
2728
if (item.parents === [] || item.parents === null) tree.push(item)
2829
else {
@@ -31,18 +32,20 @@ const constructTree = (list) => {
3132
if (subtree
3233
.filter(node => node.title === item.parents[i] && node.children)
3334
.length === 0) {
34-
subtree.push({
35+
const newNode = {
3536
key: "tree/" + item.parents[i],
3637
title: item.parents[i],
3738
children: []
38-
})
39+
}
40+
subtree.push(newNode)
41+
dir.push(newNode)
3942
}
4043
subtree = subtree.find(node => node.title === item.parents[i] && node.children).children
4144
}
4245
subtree.push(item)
4346
}
4447
})
45-
return tree
48+
return [tree, dir]
4649
}
4750

4851
class SidebarContent extends Component {
@@ -51,6 +54,7 @@ class SidebarContent extends Component {
5154
}
5255

5356
render() {
57+
const { expandedKeys } = this.props.sidebar
5458
return (
5559
<StaticQuery
5660
query={graphql`
@@ -72,7 +76,7 @@ class SidebarContent extends Component {
7276
}
7377
`}
7478
render={data => {
75-
const tree = convertToTree(data)
79+
const [tree, dir] = convertToTree(data)
7680
const loop = data => data.map((item) => {
7781
if (item.children) {
7882
return (
@@ -87,11 +91,17 @@ class SidebarContent extends Component {
8791
</Menu.Item>
8892
)
8993
})
94+
const selectedKeys = data.allMarkdownRemark.edges
95+
.filter(item => window.location.pathname === item.node.fields.slug ||
96+
window.location.pathname+'/' === item.node.fields.slug)
97+
.length > 0 ? [expandedKeys] : []
98+
const defaultOpenKeys = dir.map(item => item.key)
9099
return (
91100
<div>
92101
<Menu
93102
mode="inline"
94-
defaultOpenKeys={tree.map(node => node.key)}
103+
defaultOpenKeys={defaultOpenKeys}
104+
selectedKeys={selectedKeys}
95105
>
96106
{loop(tree)}
97107
</Menu>

src/components/ResponsiveSidebar/sidebar-config.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
export const pullRight = true;
21
export const maxWidth = 800;
32

43
export const styles =
54
{
65
root: {
76
position: "fixed",
87
top: 100,
9-
left: pullRight ? "80%" : 10,
10-
right: !pullRight ? "80%" : 10,
8+
left: 10,
9+
right: "80%",
1110
bottom: 0,
1211
overflow: "hidden"
1312
},
1413
sidebar: {
1514
zIndex: 2,
1615
position: "absolute",
17-
left: pullRight ? 10 : null,
18-
right: !pullRight ? 10 : null,
16+
left: null,
17+
right: 10,
1918
top: 0,
2019
bottom: 0,
2120
transition: "transform .3s ease-out",

src/store/index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { createStore } from "redux";
1+
import { createStore as reduxCreateStore } from "redux"
22
import rootReducer from "../reducers";
33
// import thunk from "redux-thunk";
44

55
// const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
6-
export default createStore(
6+
// export default createStore(
7+
// rootReducer,
8+
// composeEnhancers(applyMiddleware(thunk))
9+
// );
10+
const createStore = () => reduxCreateStore(
711
rootReducer,
812
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
9-
// composeEnhancers(applyMiddleware(thunk))
10-
);
13+
)
14+
export default createStore

src/templates/postTemplate.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import React from "react"
22
import { graphql } from "gatsby"
33
import Layout from "../components/Layout";
4+
import { connect } from 'react-redux'
5+
import { onSidebarContentExpand } from '../actions/sidebar'
46
import "katex/dist/katex.min.css"
57

6-
export default function Template({
8+
function Template({
79
data, // this prop will be injected by the GraphQL query below.
10+
onSidebarContentExpand
811
}) {
912
const { markdownRemark } = data // data.markdownRemark holds our post data
10-
const { frontmatter, html } = markdownRemark
13+
const { frontmatter, html, id } = markdownRemark
14+
onSidebarContentExpand(id)
1115
return (
1216
<Layout>
1317
<div className="blog-post-container">
@@ -24,12 +28,19 @@ export default function Template({
2428
)
2529
}
2630

31+
const mapDispatchToProps = {
32+
onSidebarContentExpand
33+
}
34+
35+
export default connect(()=>({}), mapDispatchToProps) (Template)
36+
2737
export const pageQuery = graphql`
2838
query($path: String!) {
2939
markdownRemark(fields: { slug: { eq: $path} }) {
3040
fields {
3141
slug
3242
}
43+
id
3344
html
3445
frontmatter {
3546
date(formatString: "MMMM DD, YYYY")

wrap-with-provider.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react"
2+
import { Provider } from "react-redux"
3+
import createStore from './src/store'
4+
5+
const store = createStore()
6+
7+
// eslint-disable-next-line react/display-name,react/prop-types
8+
export default ({ element }) => <Provider store={store}>{element}</Provider>

0 commit comments

Comments
 (0)