Skip to content

Commit fb385b6

Browse files
authored
PageLayout: Update position responsive prop API (#2199)
* Update position prop API * Update snapshots * Update docs * Create fair-tips-travel.md
1 parent e0113df commit fb385b6

File tree

4 files changed

+117
-68
lines changed

4 files changed

+117
-68
lines changed

.changeset/fair-tips-travel.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
* Updated the `position` prop in `PageLayout.Pane` to use the new responsive prop API introduced in https://github.com/primer/react/pull/2174.
6+
* Deprecated the `positionWhenNarrow` prop in favor of the new responsive prop API
7+
8+
**Before**
9+
10+
```
11+
position="start"
12+
positionWhenNarrow="end"
13+
```
14+
15+
**After**
16+
17+
```
18+
position={{regular: 'start', narrow: 'end'}}
19+
```

docs/content/PageLayout.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,28 @@ See [storybook](https://primer.style/react/storybook?path=/story/layout-pagelayo
244244
<PropsTableRow
245245
name="position"
246246
type={`| 'start'
247-
| 'end'`}
247+
| 'end'
248+
| {
249+
narrow?:
250+
| 'start'
251+
| 'end'
252+
regular?:
253+
| 'start'
254+
| 'end'
255+
wide?:
256+
| 'start'
257+
| 'end'
258+
}`}
248259
defaultValue="'start'"
249260
/>
250261
<PropsTableRow
251262
name="positionWhenNarrow"
263+
deprecated
252264
type={`| 'inherit'
253265
| 'start'
254266
| 'end'`}
255267
defaultValue="'inherit'"
268+
description="Use the position prop with a responsive value instead."
256269
/>
257270
<PropsTableRow
258271
name="width"

src/PageLayout/PageLayout.tsx

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,21 @@ Content.displayName = 'PageLayout.Content'
279279
// PageLayout.Pane
280280

281281
export type PageLayoutPaneProps = {
282-
position?: keyof typeof panePositions
282+
position?: keyof typeof panePositions | ResponsiveValue<keyof typeof panePositions>
283+
/**
284+
* @deprecated Use the `position` prop with a responsive value instead.
285+
*
286+
* Before:
287+
* ```
288+
* position="start"
289+
* positionWhenNarrow="end"
290+
* ```
291+
*
292+
* After:
293+
* ```
294+
* position={{regular: 'start', narrow: 'end'}}
295+
* ```
296+
*/
283297
positionWhenNarrow?: 'inherit' | keyof typeof panePositions
284298
width?: keyof typeof paneWidths
285299
divider?: 'none' | 'line' | ResponsiveValue<'none' | 'line', 'none' | 'line' | 'filled'>
@@ -322,6 +336,14 @@ const Pane: React.FC<React.PropsWithChildren<PageLayoutPaneProps>> = ({
322336
children,
323337
sx = {}
324338
}) => {
339+
// Combine position and positionWhenNarrow for backwards compatibility
340+
const positionProp =
341+
!isResponsiveValue(position) && positionWhenNarrow !== 'inherit'
342+
? {regular: position, narrow: positionWhenNarrow}
343+
: position
344+
345+
const responsivePosition = useResponsiveValue(positionProp, 'end')
346+
325347
// Combine divider and dividerWhenNarrow for backwards compatibility
326348
const dividerProp =
327349
!isResponsiveValue(divider) && dividerWhenNarrow !== 'inherit'
@@ -331,26 +353,25 @@ const Pane: React.FC<React.PropsWithChildren<PageLayoutPaneProps>> = ({
331353
const dividerVariant = useResponsiveValue(dividerProp, 'none')
332354
const isHidden = useResponsiveValue(hidden, false)
333355
const {rowGap, columnGap} = React.useContext(PageLayoutContext)
334-
const computedPositionWhenNarrow = positionWhenNarrow === 'inherit' ? position : positionWhenNarrow
335356
return (
336357
<Box
337358
as="aside"
338359
// eslint-disable-next-line @typescript-eslint/no-explicit-any
339360
sx={(theme: any) =>
340361
merge<BetterSystemStyleObject>(
341362
{
342-
order: panePositions[computedPositionWhenNarrow],
363+
order: panePositions[responsivePosition],
343364
display: isHidden ? 'none' : 'flex',
344-
flexDirection: computedPositionWhenNarrow === 'end' ? 'column' : 'column-reverse',
365+
flexDirection: responsivePosition === 'end' ? 'column' : 'column-reverse',
345366
width: '100%',
346367
marginX: 0,
347-
[computedPositionWhenNarrow === 'end' ? 'marginTop' : 'marginBottom']: SPACING_MAP[rowGap],
368+
[responsivePosition === 'end' ? 'marginTop' : 'marginBottom']: SPACING_MAP[rowGap],
348369
[`@media screen and (min-width: ${theme.breakpoints[1]})`]: {
349370
width: 'auto',
350-
[position === 'end' ? 'marginLeft' : 'marginRight']: SPACING_MAP[columnGap],
371+
[responsivePosition === 'end' ? 'marginLeft' : 'marginRight']: SPACING_MAP[columnGap],
351372
marginY: `0 !important`,
352-
flexDirection: position === 'end' ? 'row' : 'row-reverse',
353-
order: panePositions[position]
373+
flexDirection: responsivePosition === 'end' ? 'row' : 'row-reverse',
374+
order: panePositions[responsivePosition]
354375
}
355376
},
356377
sx
@@ -360,15 +381,11 @@ const Pane: React.FC<React.PropsWithChildren<PageLayoutPaneProps>> = ({
360381
{/* Show a horizontal divider when viewport is narrow. Otherwise, show a vertical divider. */}
361382
<HorizontalDivider
362383
variant={{narrow: dividerVariant, regular: 'none'}}
363-
sx={{
364-
[computedPositionWhenNarrow === 'end' ? 'marginBottom' : 'marginTop']: SPACING_MAP[rowGap]
365-
}}
384+
sx={{[responsivePosition === 'end' ? 'marginBottom' : 'marginTop']: SPACING_MAP[rowGap]}}
366385
/>
367386
<VerticalDivider
368387
variant={{narrow: 'none', regular: dividerVariant}}
369-
sx={{
370-
[position === 'end' ? 'marginRight' : 'marginLeft']: SPACING_MAP[columnGap]
371-
}}
388+
sx={{[responsivePosition === 'end' ? 'marginRight' : 'marginLeft']: SPACING_MAP[columnGap]}}
372389
/>
373390

374391
<Box sx={{width: paneWidths[width]}}>{children}</Box>

src/PageLayout/__snapshots__/PageLayout.test.tsx.snap

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -487,48 +487,48 @@ exports[`PageLayout renders pane in different position when narrow 1`] = `
487487
margin-right: auto;
488488
}
489489
490-
.c10 {
490+
.c6 {
491+
-webkit-order: 3;
492+
-ms-flex-order: 3;
493+
order: 3;
494+
display: -webkit-box;
495+
display: -webkit-flex;
496+
display: -ms-flexbox;
497+
display: flex;
498+
-webkit-flex-direction: column;
499+
-ms-flex-direction: column;
500+
flex-direction: column;
501+
width: 100%;
502+
margin-left: 0;
503+
margin-right: 0;
504+
margin-top: 16px;
505+
}
506+
507+
.c7 {
491508
margin-left: -16px;
492509
margin-right: -16px;
493510
display: none;
494511
margin-bottom: 16px;
495512
}
496513
497-
.c7 {
514+
.c8 {
498515
height: 100%;
499516
display: none;
500517
margin-right: 16px;
501518
}
502519
503-
.c8 {
520+
.c9 {
504521
width: 100%;
505522
}
506523
507-
.c9 {
524+
.c10 {
508525
-webkit-order: 4;
509526
-ms-flex-order: 4;
510527
order: 4;
511528
width: 100%;
512529
margin-top: 16px;
513530
}
514531
515-
.c6 {
516-
-webkit-order: 1;
517-
-ms-flex-order: 1;
518-
order: 1;
519-
display: -webkit-box;
520-
display: -webkit-flex;
521-
display: -ms-flexbox;
522-
display: flex;
523-
-webkit-flex-direction: column-reverse;
524-
-ms-flex-direction: column-reverse;
525-
flex-direction: column-reverse;
526-
width: 100%;
527-
margin-left: 0;
528-
margin-right: 0;
529-
margin-bottom: 16px;
530-
}
531-
532532
@media screen and (min-width:1012px) {
533533
.c0 {
534534
padding: 24px;
@@ -557,65 +557,65 @@ exports[`PageLayout renders pane in different position when narrow 1`] = `
557557
}
558558
559559
@media screen and (min-width:768px) {
560-
.c10 {
560+
.c6 {
561+
width: auto;
562+
margin-left: 16px;
563+
margin-top: 0 !important;
564+
margin-bottom: 0 !important;
565+
-webkit-flex-direction: row;
566+
-ms-flex-direction: row;
567+
flex-direction: row;
568+
-webkit-order: 3;
569+
-ms-flex-order: 3;
570+
order: 3;
571+
}
572+
}
573+
574+
@media screen and (min-width:1012px) {
575+
.c6 {
576+
margin-top: 24px;
577+
}
578+
}
579+
580+
@media screen and (min-width:768px) {
581+
.c7 {
561582
margin-left: 0 !important;
562583
margin-right: 0 !important;
563584
}
564585
}
565586
566587
@media screen and (min-width:1012px) {
567-
.c10 {
588+
.c7 {
568589
margin-left: -24px;
569590
margin-right: -24px;
570591
margin-bottom: 24px;
571592
}
572593
}
573594
574595
@media screen and (min-width:1012px) {
575-
.c7 {
596+
.c8 {
576597
margin-right: 24px;
577598
}
578599
}
579600
580601
@media screen and (min-width:768px) {
581-
.c8 {
602+
.c9 {
582603
width: 256px;
583604
}
584605
}
585606
586607
@media screen and (min-width:1012px) {
587-
.c8 {
608+
.c9 {
588609
width: 296px;
589610
}
590611
}
591612
592613
@media screen and (min-width:1012px) {
593-
.c9 {
614+
.c10 {
594615
margin-top: 24px;
595616
}
596617
}
597618
598-
@media screen and (min-width:768px) {
599-
.c6 {
600-
width: auto;
601-
margin-left: 16px;
602-
margin-top: 0 !important;
603-
margin-bottom: 0 !important;
604-
-webkit-flex-direction: row;
605-
-ms-flex-direction: row;
606-
flex-direction: row;
607-
-webkit-order: 3;
608-
-ms-flex-order: 3;
609-
order: 3;
610-
}
611-
}
612-
613-
@media screen and (min-width:1012px) {
614-
.c6 {
615-
margin-bottom: 24px;
616-
}
617-
}
618-
619619
<div>
620620
<div
621621
class="c0"
@@ -643,23 +643,23 @@ exports[`PageLayout renders pane in different position when narrow 1`] = `
643643
<aside
644644
class="c6"
645645
>
646-
<div
647-
class="c3"
648-
/>
649646
<div
650647
class="c7"
651648
/>
652649
<div
653650
class="c8"
651+
/>
652+
<div
653+
class="c9"
654654
>
655655
Pane
656656
</div>
657657
</aside>
658658
<footer
659-
class="c9"
659+
class="c10"
660660
>
661661
<div
662-
class="c10"
662+
class="c7"
663663
/>
664664
Footer
665665
</footer>

0 commit comments

Comments
 (0)