1
1
import React from 'react'
2
2
import { Box } from '..'
3
- import { isResponsiveValue , useResponsiveValue , ResponsiveValue } from '../hooks/useResponsiveValue'
3
+ import { useResponsiveValue , ResponsiveValue } from '../hooks/useResponsiveValue'
4
4
import { SxProp , merge , BetterSystemStyleObject } from '../sx'
5
5
import Heading from '../Heading'
6
6
import { ArrowLeftIcon } from '@primer/octicons-react'
7
7
import Link from '../Link'
8
8
import { ForwardRefComponent as PolymorphicForwardRefComponent } from '../utils/polymorphic'
9
+ import { displayResponsively } from './manageResponsiveVisibility'
9
10
const REGION_ORDER = {
10
11
ContextArea : 0 ,
11
12
TitleArea : 1 ,
@@ -53,28 +54,6 @@ const Root: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({children, sx
53
54
)
54
55
}
55
56
56
- function displayResponsively < T , F > ( value : T , fallback : F ) : BetterSystemStyleObject {
57
- if ( isResponsiveValue ( value ) ) {
58
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
- const responsiveValue = value as Extract < T , ResponsiveValue < any > >
60
-
61
- return {
62
- [ `@media screen and (max-width: 768px)` ] : {
63
- display : 'narrow' in responsiveValue ? ( responsiveValue . narrow ? 'none' : 'flex' ) : fallback ? 'none' : 'flex' ,
64
- } ,
65
- [ `@media screen and (min-width: 768px)` ] : {
66
- display :
67
- 'regular' in responsiveValue ? ( responsiveValue . regular ? 'none' : 'flex' ) : fallback ? 'none' : 'flex' ,
68
- } ,
69
- [ `@media screen and (min-width: 1440px)` ] : {
70
- display : 'wide' in responsiveValue ? ( responsiveValue . wide ? 'none' : 'flex' ) : fallback ? 'none' : 'flex' ,
71
- } ,
72
- }
73
- } else {
74
- return { display : fallback ? 'none' : 'flex' }
75
- }
76
- }
77
-
78
57
// PageHeader.ContextArea : Only visible on narrow viewports by default to provide user context of where they are at their journey. `visible` prop available
79
58
// to manage their custom visibility but consumers should be careful if they choose to hide this on narrow viewports.
80
59
// PageHeader.ContextArea Sub Components: PageHeader.ParentLink, PageHeader.ContextBar, PageHeader.ContextAreaActions
@@ -89,7 +68,7 @@ const ContextArea: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({
89
68
alignItems : 'center' ,
90
69
gap : '0.5rem' ,
91
70
order : REGION_ORDER . ContextArea ,
92
- ...displayResponsively ( hidden , false ) ,
71
+ ...displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
93
72
}
94
73
return < Box sx = { merge < BetterSystemStyleObject > ( contentNavStyles , sx ) } > { children } </ Box >
95
74
}
@@ -111,7 +90,6 @@ const ParentLink = React.forwardRef<HTMLAnchorElement, ParentLinkProps>(
111
90
} ,
112
91
ref ,
113
92
) => {
114
- const isHidden = useResponsiveValue ( hidden , false )
115
93
return (
116
94
< >
117
95
< Link
@@ -121,9 +99,9 @@ const ParentLink = React.forwardRef<HTMLAnchorElement, ParentLinkProps>(
121
99
muted
122
100
sx = { merge < BetterSystemStyleObject > (
123
101
{
124
- display : isHidden ? 'none' : 'flex' ,
125
102
alignItems : 'center' ,
126
103
gap : '0.5rem' ,
104
+ ...displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
127
105
} ,
128
106
sx ,
129
107
) }
@@ -146,8 +124,11 @@ const ContextBar: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({
146
124
sx = { } ,
147
125
hidden = hiddenOnRegularAndWide ,
148
126
} ) => {
149
- const isHidden = useResponsiveValue ( hidden , false )
150
- return < Box sx = { merge < BetterSystemStyleObject > ( { display : isHidden ? 'none' : 'flex' } , sx ) } > { children } </ Box >
127
+ return (
128
+ < Box sx = { merge < BetterSystemStyleObject > ( displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) , sx ) } >
129
+ { children }
130
+ </ Box >
131
+ )
151
132
}
152
133
153
134
// ContextAreaActions
@@ -157,17 +138,16 @@ const ContextAreaActions: React.FC<React.PropsWithChildren<PageHeaderProps>> = (
157
138
sx = { } ,
158
139
hidden = hiddenOnRegularAndWide ,
159
140
} ) => {
160
- const isHidden = useResponsiveValue ( hidden , false )
161
141
return (
162
142
< Box
163
143
sx = { merge < BetterSystemStyleObject > (
164
144
{
165
- display : isHidden ? 'none' : 'flex' ,
166
145
flexDirection : 'row' ,
167
146
alignItems : 'center' ,
168
147
gap : '0.5rem' ,
169
148
flexGrow : '1' ,
170
149
justifyContent : 'right' ,
150
+ ...displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
171
151
} ,
172
152
sx ,
173
153
) }
@@ -202,14 +182,18 @@ const TitleArea: React.FC<React.PropsWithChildren<TitleAreaProps>> = ({
202
182
hidden = false ,
203
183
variant = 'medium' ,
204
184
} ) => {
205
- const isHidden = useResponsiveValue ( hidden , false )
206
185
const currentVariant = useResponsiveValue ( variant , 'medium' )
207
186
const height = currentVariant === 'large' ? LARGE_TITLE_HEIGHT : MEDIUM_TITLE_HEIGHT
208
187
return (
209
188
< TitleAreaContext . Provider value = { { titleVariant : currentVariant , titleAreaHeight : height } } >
210
189
< Box
211
190
sx = { merge < BetterSystemStyleObject > (
212
- { gap : '0.5rem' , display : isHidden ? 'none' : 'flex' , flexDirection : 'row' , alignItems : 'flex-start' } ,
191
+ {
192
+ gap : '0.5rem' ,
193
+ ...displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
194
+ flexDirection : 'row' ,
195
+ alignItems : 'flex-start' ,
196
+ } ,
213
197
sx ,
214
198
) }
215
199
>
@@ -224,13 +208,16 @@ const LeadingAction: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({
224
208
sx = { } ,
225
209
hidden = hiddenOnNarrow ,
226
210
} ) => {
227
- const isHidden = useResponsiveValue ( hidden , false )
228
211
const { titleAreaHeight} = React . useContext ( TitleAreaContext )
229
212
230
213
return (
231
214
< Box
232
215
sx = { merge < BetterSystemStyleObject > (
233
- { display : isHidden ? 'none' : 'flex' , alignItems : 'center' , height : titleAreaHeight } ,
216
+ {
217
+ ...displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
218
+ alignItems : 'center' ,
219
+ height : titleAreaHeight ,
220
+ } ,
234
221
sx ,
235
222
) }
236
223
>
@@ -240,13 +227,12 @@ const LeadingAction: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({
240
227
}
241
228
242
229
const LeadingVisual : React . FC < React . PropsWithChildren < PageHeaderProps > > = ( { children, sx = { } , hidden = false } ) => {
243
- const isHidden = useResponsiveValue ( hidden , false )
244
230
const { titleAreaHeight} = React . useContext ( TitleAreaContext )
245
231
return (
246
232
< Box
247
233
sx = { merge < BetterSystemStyleObject > (
248
234
{
249
- display : isHidden ? 'none' : 'flex' ,
235
+ ... displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
250
236
alignItems : 'center' ,
251
237
height : titleAreaHeight ,
252
238
} ,
@@ -264,7 +250,6 @@ export type TitleProps = {
264
250
} & PageHeaderProps
265
251
266
252
const Title : React . FC < React . PropsWithChildren < TitleProps > > = ( { children, sx = { } , hidden = false , as = 'h3' } ) => {
267
- const isHidden = useResponsiveValue ( hidden , false )
268
253
const { titleVariant} = React . useContext ( TitleAreaContext )
269
254
return (
270
255
< Heading
@@ -287,7 +272,7 @@ const Title: React.FC<React.PropsWithChildren<TitleProps>> = ({children, sx = {}
287
272
medium : '600' ,
288
273
subtitle : '400' ,
289
274
} [ titleVariant ] ,
290
- display : isHidden ? 'none' : 'flex' ,
275
+ ... displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
291
276
} ,
292
277
sx ,
293
278
) }
@@ -297,14 +282,13 @@ const Title: React.FC<React.PropsWithChildren<TitleProps>> = ({children, sx = {}
297
282
)
298
283
}
299
284
const TrailingVisual : React . FC < React . PropsWithChildren < PageHeaderProps > > = ( { children, sx = { } , hidden = false } ) => {
300
- const isHidden = useResponsiveValue ( hidden , false )
301
285
const { titleAreaHeight} = React . useContext ( TitleAreaContext )
302
286
303
287
return (
304
288
< Box
305
289
sx = { merge < BetterSystemStyleObject > (
306
290
{
307
- display : isHidden ? 'none' : 'flex' ,
291
+ ... displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
308
292
alignItems : 'center' ,
309
293
height : titleAreaHeight ,
310
294
} ,
@@ -321,13 +305,16 @@ const TrailingAction: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({
321
305
sx = { } ,
322
306
hidden = hiddenOnNarrow ,
323
307
} ) => {
324
- const isHidden = useResponsiveValue ( hidden , false )
325
308
const { titleAreaHeight} = React . useContext ( TitleAreaContext )
326
309
327
310
return (
328
311
< Box
329
312
sx = { merge < BetterSystemStyleObject > (
330
- { display : isHidden ? 'none' : 'flex' , alignItems : 'center' , height : titleAreaHeight } ,
313
+ {
314
+ ...displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
315
+ alignItems : 'center' ,
316
+ height : titleAreaHeight ,
317
+ } ,
331
318
sx ,
332
319
) }
333
320
>
@@ -337,13 +324,12 @@ const TrailingAction: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({
337
324
}
338
325
339
326
const Actions : React . FC < React . PropsWithChildren < PageHeaderProps > > = ( { children, sx = { } , hidden = false } ) => {
340
- const isHidden = useResponsiveValue ( hidden , false )
341
327
const { titleAreaHeight} = React . useContext ( TitleAreaContext )
342
328
return (
343
329
< Box
344
330
sx = { merge < BetterSystemStyleObject > (
345
331
{
346
- display : isHidden ? 'none' : 'flex' ,
332
+ ... displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
347
333
flexDirection : 'row' ,
348
334
gap : '0.5rem' ,
349
335
flexGrow : '1' ,
@@ -361,12 +347,11 @@ const Actions: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({children,
361
347
362
348
// PageHeader.Description: The description area of the header. Visible on all viewports
363
349
const Description : React . FC < React . PropsWithChildren < PageHeaderProps > > = ( { children, sx = { } , hidden = false } ) => {
364
- const isHidden = useResponsiveValue ( hidden , true )
365
350
return (
366
351
< Box
367
352
sx = { merge < BetterSystemStyleObject > (
368
353
{
369
- display : isHidden ? 'none' : 'flex' ,
354
+ ... displayResponsively ( hidden , false , 'display' , 'none' , 'flex' ) ,
370
355
flexDirection : 'row' ,
371
356
alignItems : 'center' ,
372
357
gap : '0.5rem' ,
@@ -381,16 +366,8 @@ const Description: React.FC<React.PropsWithChildren<PageHeaderProps>> = ({childr
381
366
382
367
// PageHeader.Navigation: The local navigation area of the header. Visible on all viewports
383
368
const Navigation : React . FC < React . PropsWithChildren < PageHeaderProps > > = ( { children, sx = { } , hidden = false } ) => {
384
- const isHidden = useResponsiveValue ( hidden , false )
385
369
return (
386
- < Box
387
- sx = { merge < BetterSystemStyleObject > (
388
- {
389
- display : isHidden ? 'none' : 'block' ,
390
- } ,
391
- sx ,
392
- ) }
393
- >
370
+ < Box sx = { merge < BetterSystemStyleObject > ( displayResponsively ( hidden , false , 'display' , 'none' , 'block' ) , sx ) } >
394
371
{ children }
395
372
</ Box >
396
373
)
0 commit comments