|
1 | | -import React, { useState, useEffect, useRef, useContext } from 'react' |
2 | | -import PropTypes from 'prop-types' |
3 | | -import clsx from 'clsx' |
4 | | -import { Toolbar } from '@material-ui/core' |
| 1 | +import React, { useContext } from 'react' |
5 | 2 | import { makeStyles } from '@material-ui/core/styles' |
6 | | -import { useAmp } from 'next/amp' |
| 3 | +import { AppBar as MUIAppBar, Container, Toolbar, useScrollTrigger, Slide } from '@material-ui/core' |
| 4 | +import PropTypes from 'prop-types' |
7 | 5 | import PWAContext from './PWAContext' |
8 | 6 |
|
9 | | -export const styles = theme => ({ |
| 7 | +const useStyles = makeStyles(theme => ({ |
10 | 8 | /** |
11 | | - * Styles applied to the root element when user is not `offline`. |
| 9 | + * Styles applied to the root element. |
12 | 10 | */ |
13 | 11 | root: { |
14 | | - height: 64, |
15 | | - boxSizing: 'content-box', |
16 | | - position: 'relative', |
| 12 | + boxSizing: 'border-box', |
| 13 | + backgroundColor: theme.palette.background.default, |
| 14 | + boxShadow: 'none', |
| 15 | + borderBottom: `1px solid ${theme.palette.divider}`, |
17 | 16 | zIndex: theme.zIndex.modal + 10, |
| 17 | + height: theme.headerHeight, |
| 18 | + display: 'flex', |
| 19 | + flexDirection: 'column', |
| 20 | + alignItems: 'stretch', |
18 | 21 | }, |
19 | | - |
20 | 22 | /** |
21 | | - * Styles applied to the root element when Amp is used. |
| 23 | + * Styles applied to the spacer that fills the height behind the floating toolbar. |
22 | 24 | */ |
23 | | - withAmp: { |
24 | | - zIndex: theme.zIndex.amp.modal + 1, |
| 25 | + spacer: { |
| 26 | + boxSizing: 'border-box', |
| 27 | + height: theme.headerHeight, |
25 | 28 | }, |
26 | | - |
27 | | - /** |
28 | | - * Styles applied to the offline warning element. |
29 | | - */ |
30 | | - offline: { |
31 | | - textAlign: 'center', |
32 | | - backgroundColor: '#f34c4c', |
33 | | - color: 'white', |
34 | | - }, |
35 | | - |
36 | 29 | /** |
37 | 30 | * Styles applied to the `Toolbar` element. |
38 | 31 | */ |
39 | 32 | toolbar: { |
40 | | - height: '64px', |
41 | | - maxWidth: theme.maxWidth, |
| 33 | + justifyContent: 'space-between', |
| 34 | + alignItems: 'center', |
42 | 35 | flex: 1, |
43 | 36 | }, |
44 | | - |
45 | | - /** |
46 | | - * Styles applied to the element wrapped around the `Toolbar`. |
47 | | - */ |
48 | | - wrap: { |
49 | | - borderBottom: `1px solid ${theme.palette.divider}`, |
50 | | - position: 'absolute', |
51 | | - top: 0, |
52 | | - left: 0, |
53 | | - right: 0, |
54 | | - backgroundColor: theme.palette.background.paper, |
55 | | - zIndex: theme.zIndex.modal + 10, |
56 | | - display: 'flex', |
57 | | - justifyContent: 'center', |
58 | | - }, |
59 | | - |
60 | 37 | /** |
61 | | - * Styles applied to the `Toolbar` wrapper when the `AppBar` is unstuck. |
62 | | - */ |
63 | | - unstuck: { |
64 | | - transform: 'translateY(-100%)', |
65 | | - }, |
66 | | - |
67 | | - /** |
68 | | - * Styles applied to the `Toolbar` wrapper element when the user has scrolled and the `AppBar` |
69 | | - * will animate back into place. |
70 | | - */ |
71 | | - animate: { |
72 | | - transition: 'transform .15s ease-in', |
73 | | - }, |
74 | | - |
75 | | - /** |
76 | | - * Styles applied to the `Toolbar` wrapper element the user has scrolled and the `AppBar` is hidden. |
77 | | - */ |
78 | | - hidden: { |
79 | | - position: 'fixed', |
80 | | - zIndex: theme.zIndex.modal + 10, |
81 | | - boxShadow: theme.shadows[2], |
82 | | - top: 0, |
83 | | - left: 0, |
84 | | - right: 0, |
85 | | - }, |
86 | | - |
87 | | - /** |
88 | | - * Styles applied to the `Toolbar` wrapper element when [`fixed`](#prop-fixed) is `true`. |
| 38 | + * Styles applied to the offline warning element. |
89 | 39 | */ |
90 | | - fixed: { |
91 | | - position: 'fixed', |
| 40 | + offline: { |
| 41 | + textAlign: 'center', |
| 42 | + backgroundColor: '#f34c4c', |
| 43 | + zIndex: 999999, |
| 44 | + width: '100vw', |
| 45 | + color: 'white', |
92 | 46 | }, |
93 | | -}) |
94 | | - |
95 | | -const useStyles = makeStyles(styles, { name: 'RSFAppBar' }) |
| 47 | +})) |
96 | 48 |
|
97 | | -export default function AppBar({ classes, children, fixed, offlineWarning }) { |
| 49 | +export default function AppBar({ children, style, fixed, offlineWarning, classes }) { |
| 50 | + const trigger = useScrollTrigger() |
98 | 51 | classes = useStyles({ classes }) |
99 | 52 |
|
100 | | - const [state, applyState] = useState({ |
101 | | - stuck: false, |
102 | | - hidden: false, |
103 | | - animate: false, |
104 | | - }) |
105 | | - const { stuck, hidden, animate } = state |
106 | | - const setState = newState => applyState({ ...state, ...newState }) |
107 | | - const lastScrollY = useRef() |
108 | | - const unstickAt = useRef() |
109 | | - const stickAt = useRef() |
110 | 53 | const { offline } = useContext(PWAContext) |
111 | | - const items = React.Children.toArray(children) |
112 | | - |
113 | | - const onScroll = () => { |
114 | | - const height = 64, |
115 | | - { scrollY } = window, |
116 | | - unstickBufferZone = 30 |
117 | 54 |
|
118 | | - if (scrollY === 0) { |
119 | | - setState({ hidden: false, stuck: false, animate: false }) |
120 | | - } else if (scrollY > height && !hidden) { |
121 | | - setState({ hidden: true }) |
122 | | - } |
123 | | - |
124 | | - if (scrollY < stickAt.current && !stuck) { |
125 | | - stickAt.current = null |
126 | | - unstickAt.current = scrollY + unstickBufferZone |
127 | | - setState({ stuck: true }) |
128 | | - } else if (scrollY > unstickAt.current && stuck) { |
129 | | - unstickAt.current = null |
130 | | - stickAt.current = scrollY - unstickBufferZone |
131 | | - setState({ stuck: false }) |
132 | | - } |
133 | | - |
134 | | - if (lastScrollY.current > scrollY && stuck) { |
135 | | - unstickAt.current = scrollY + unstickBufferZone |
136 | | - } |
137 | | - |
138 | | - if (lastScrollY.current < scrollY && !stuck) { |
139 | | - stickAt.current = scrollY - unstickBufferZone |
140 | | - } |
| 55 | + let appBar = ( |
| 56 | + <MUIAppBar |
| 57 | + className={classes.root} |
| 58 | + style={{ |
| 59 | + ...style, |
| 60 | + }} |
| 61 | + > |
| 62 | + <Toolbar disableGutters className={classes.toolbar}> |
| 63 | + {children} |
| 64 | + </Toolbar> |
| 65 | + </MUIAppBar> |
| 66 | + ) |
141 | 67 |
|
142 | | - lastScrollY.current = scrollY |
| 68 | + if (!fixed) { |
| 69 | + appBar = ( |
| 70 | + <Slide in={!trigger}> |
| 71 | + {appBar} |
| 72 | + </Slide> |
| 73 | + ) |
143 | 74 | } |
144 | 75 |
|
145 | | - useEffect(() => { |
146 | | - if (!fixed) { |
147 | | - window.addEventListener('scroll', onScroll, { passive: true }) |
148 | | - return () => window.removeEventListener('scroll', onScroll, { passive: true }) |
149 | | - } |
150 | | - }, [state]) |
151 | | - |
152 | | - useEffect(() => { |
153 | | - if (hidden && !animate) { |
154 | | - setTimeout(() => setState({ animate: true }), 100) |
155 | | - } |
156 | | - }, [hidden]) |
157 | | - |
158 | 76 | return ( |
159 | | - <div> |
| 77 | + <> |
| 78 | + <div className={classes.spacer} /> |
160 | 79 | {offline && <div className={classes.offline}>{offlineWarning}</div>} |
161 | | - <div className={clsx({ [classes.root]: true, [classes.withAmp]: useAmp() })}> |
162 | | - <div |
163 | | - className={clsx({ |
164 | | - [classes.wrap]: true, |
165 | | - [classes.fixed]: fixed, |
166 | | - [classes.hidden]: hidden, |
167 | | - [classes.unstuck]: hidden && !stuck, |
168 | | - [classes.animate]: animate && window.scrollY > 0, |
169 | | - })} |
170 | | - > |
171 | | - <Toolbar disableGutters classes={{ root: classes.toolbar }}> |
172 | | - {items} |
173 | | - </Toolbar> |
174 | | - </div> |
175 | | - </div> |
176 | | - </div> |
| 80 | + {appBar} |
| 81 | + </> |
177 | 82 | ) |
178 | 83 | } |
179 | 84 |
|
|
0 commit comments