forked from aravindballa/gatsby-theme-andy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrainNoteContainer.js
174 lines (164 loc) Β· 4.81 KB
/
BrainNoteContainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/** @jsx jsx */
import React from 'react';
import {
useStackedPagesProvider,
StackedPagesProvider,
PageIndexProvider,
} from 'react-stacked-pages-hook';
import { Helmet } from 'react-helmet';
import { Styled, jsx, Flex, Box } from 'theme-ui';
import useWindowWidth from '../utils/useWindowWidth';
import Header from './Header';
import BrainNote from './BrainNote';
import { LinkToStacked } from './CustomLinkToStacked';
const NOTE_WIDTH = 576; // w-xl
const StackedPageWrapper = ({ i, ...rest }) => (
<PageIndexProvider value={i}>
<NoteWrapper {...rest} i={i} />
</PageIndexProvider>
);
// A wrapper component to render the content of a page when stacked
const NoteWrapper = ({ children, slug, title, overlay, obstructed, highlighted, i }) => {
return (
<>
<Flex
bg={highlighted ? 'accent' : 'background'}
px={3}
className="note-container"
sx={{
flexDirection: 'column',
flexShrink: 0,
overflowY: 'auto',
position: [null, null, 'sticky'], // here
maxWidth: ['100%', '100%', '100vw'],
boxShadow: overlay ? `0 0 8px rgba(0, 0, 0, 0.125)` : '',
width: ['100%', '100%', NOTE_WIDTH],
left: 40 * i,
right: -585,
}}
>
<Box
sx={{
display: ['none', 'none', 'block'],
transition: 'opacity',
transitionDuration: 100,
opacity: obstructed ? 1 : 0,
}}
>
<Box
sx={{
position: 'absolute',
zIndex: 10,
transform: 'rotate(90deg)',
transformOrigin: 'left',
}}
pb={2}
>
<LinkToStacked
to={slug}
sx={{ fontWeight: 'bold', textDecoration: 'none', color: 'text' }}
>
{title || slug}
</LinkToStacked>
</Box>
</Box>
<Flex
sx={{
flexDirection: 'column',
minHeight: '100%',
transition: 'opacity',
transitionDuration: 100,
opacity: obstructed ? 0 : 1,
}}
>
{children}
</Flex>
</Flex>
</>
);
};
const BrainNotesContainer = ({ slug, note, location, siteMetadata }) => {
const [width] = useWindowWidth();
// process data from gatsby pageQuery API
const processPageQuery = React.useCallback((x) => x.brainNote, []);
const [state, scrollContainer] = useStackedPagesProvider({
firstPage: { slug, data: { brainNote: note } },
location,
processPageQuery,
pageWidth: NOTE_WIDTH,
});
const { stackedPages, stackedPageStates } = state;
let pages = stackedPages;
let indexToShow;
if (width < 768) {
const activeSlug = Object.keys(state.stackedPageStates).find(
(slug) => state.stackedPageStates[slug].active
);
indexToShow = state.stackedPages.findIndex((page) => page.slug === activeSlug);
if (indexToShow === -1) {
indexToShow = state.stackedPages.length - 1;
}
pages = [state.stackedPages[indexToShow]];
}
return (
<Flex
sx={{
flexDirection: 'column',
height: '100vh',
minHeight: '100vh',
}}
>
<Helmet>
<meta charSet="utf-8" />
<title>
{note.title} - {siteMetadata.title}
</title>
</Helmet>
<Header siteMetadata={siteMetadata} />
<Flex
ref={scrollContainer}
sx={{
flex: 1,
flexGrow: 1,
overflowX: [null, null, 'auto'],
overflowY: 'hidden',
}}
>
<Flex
className="note-columns-container"
sx={{
minWidth: 'unset',
flexGrow: 1,
transition: [null, null, 'width'],
transitionDuration: 100,
width: ['100%', '100%', NOTE_WIDTH * (pages.length + 1)],
}}
>
<StackedPagesProvider value={state}>
{/* Render the stacked pages */}
{pages.map((page, i) => (
<StackedPageWrapper
i={i}
key={page.slug}
slug={page.slug}
title={page.data.title}
overlay={stackedPageStates[page.slug] && stackedPageStates[page.slug].overlay}
obstructed={
indexToShow !== undefined
? false
: stackedPageStates[page.slug] && stackedPageStates[page.slug].obstructed
}
highlighted={
stackedPageStates[page.slug] && stackedPageStates[page.slug].highlighted
}
>
<BrainNote note={page.data} />
</StackedPageWrapper>
))}
</StackedPagesProvider>
</Flex>
</Flex>
</Flex>
);
};
export default BrainNotesContainer;