This repository was archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPage.js
203 lines (185 loc) · 6.19 KB
/
Page.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { cloneElement, useRef, useState, useEffect, useCallback } from 'react'
import cn from 'classnames'
import PropTypes from 'prop-types'
import { Helmet } from 'react-helmet'
// eslint-disable-next-line no-restricted-imports -- TODO: migrate to @react-spring/web
import { Spring } from 'react-spring/renderprops'
import calcScrollbarWidth from 'scrollbar-width'
import SearchBar from 'components/search-bar/ConnectedSearchBar'
import styles from './Page.module.css'
const messages = {
dotAudius: '• Audius',
audius: 'Audius'
}
const HEADER_MARGIN_PX = 32
// Pixels on the right side of the header to account for potential scrollbars
const MIN_GUTTER_WIDTH = 20
// Responsible for positioning the header
const HeaderContainer = ({ header, containerRef, showSearch }) => {
// Need to offset the header on the right side
// the width of the scrollbar.
const [scrollBarWidth, setScrollbarWidth] = useState(0)
const refreshScrollWidth = useCallback(() => {
const width = calcScrollbarWidth(true)
// For some odd reason, narrow windows ONLY in Firefox
// return 0 width for the scroll bars.
setScrollbarWidth(width > 0 ? width : MIN_GUTTER_WIDTH)
}, [])
useEffect(() => {
refreshScrollWidth()
}, [refreshScrollWidth])
// Only Safari & Chrome support the CSS
// frosted glasss effect.
const [isChromeOrSafari, setIsChromeOrSafari] = useState(false)
useEffect(() => {
const chromeOrSafari = () => {
const userAgent = navigator.userAgent.toLowerCase()
return (
userAgent.indexOf('chrome') > -1 || userAgent.indexOf('safari') > -1
)
}
setIsChromeOrSafari(chromeOrSafari)
}, [])
const headerContainerRef = useRef()
return (
<div
className={styles.headerContainer}
ref={containerRef}
style={{
right: `${scrollBarWidth}px`
}}
>
<div
ref={headerContainerRef}
className={styles.frosted}
style={{
// Need to set a different gradient for
// browsers that don't support the
// backdrop-filter frosted glass effect.
paddingLeft: `${scrollBarWidth}px`,
background: isChromeOrSafari
? 'linear-gradient(180deg, var(--page-header-gradient-1) 0%, var(--page-header-gradient-1) 20%, var(--page-header-gradient-2) 65%)'
: 'linear-gradient(180deg, var(--page-header-gradient-1) 0%, var(--page-header-gradient-1) 40%, var(--page-header-gradient-2-alt) 85%)'
}}
>
{cloneElement(header, {
isChromeOrSafari,
scrollBarWidth,
headerContainerRef,
topLeftElement: showSearch ? <SearchBar /> : null
})}
</div>
{/* We attach the box shadow as a separate element to
avoid overlapping the scroll bar.
*/}
<div className={styles.headerBoxShadow} />
</div>
)
}
export const Page = (props) => {
const [headerHeight, setHeaderHeight] = useState(0)
const calculateHeaderHeight = (element) => {
if (element) {
setHeaderHeight(element.offsetHeight)
}
}
return (
<Spring
from={{ opacity: 0.2 }}
to={{ opacity: 1 }}
config={{ duration: props.fadeDuration }}
>
{(animProps) => (
<div
ref={props.containerRef}
style={animProps}
className={cn(styles.pageContainer, {
[props.containerClassName]: !!props.containerClassName
})}
>
<Helmet encodeSpecialCharacters={false}>
{props.title ? (
<title>{`${props.title} ${messages.dotAudius}`}</title>
) : (
<title>{messages.audius}</title>
)}
{props.description ? (
<meta name='description' content={props.description} />
) : null}
{/* TODO: re-enable once we fix redirects and casing of canonicalUrls */}
{/* {props.canonicalUrl && (
<link rel='canonical' href={props.canonicalUrl} />
)} */}
{props.structuredData && (
<script type='application/ld+json'>
{JSON.stringify(props.structuredData)}
</script>
)}
</Helmet>
{props.header && (
<HeaderContainer
header={props.header}
showSearch={props.showSearch}
containerRef={calculateHeaderHeight}
/>
)}
<div
className={cn({
[styles.inset]: props.variant === 'inset',
[styles.flush]: props.variant === 'flush',
[styles.medium]: props.size === 'medium',
[styles.large]: props.size === 'large',
[props.containerClassName]: !!props.containerClassName
})}
style={
props.variant === 'inset'
? { paddingTop: `${headerHeight + HEADER_MARGIN_PX}px` }
: null
}
>
{/* Set an id so that nested components can mount in relation to page if needed, e.g. fixed menu popups. */}
<div
id='page'
className={cn(styles.pageContent, {
[props.contentClassName]: !!props.contentClassName
})}
>
{props.children}
</div>
</div>
{props.scrollableSearch && (
<div className={styles.searchWrapper}>
<SearchBar />
</div>
)}
</div>
)}
</Spring>
)
}
Page.propTypes = {
title: PropTypes.string,
description: PropTypes.string,
canonicalUrl: PropTypes.string,
structuredData: PropTypes.object,
variant: PropTypes.oneOf(['inset', 'flush']),
size: PropTypes.oneOf(['medium', 'large']),
containerRef: PropTypes.node,
contentClassName: PropTypes.string,
containerClassName: PropTypes.string,
fadeDuration: PropTypes.number,
header: PropTypes.node,
// There are some pages which don't have a fixed header but still display
// a search bar that scrolls with the page.
scrollableSearch: PropTypes.bool,
children: PropTypes.node,
showSearch: PropTypes.bool
}
Page.defaultProps = {
variant: 'inset',
size: 'medium',
fadeDuration: 200,
scrollableSearch: false,
showSearch: true
}
export default Page