Skip to content

Commit 3ec0c9d

Browse files
broccolinisoupcolebemissiddharthkp
authored
Rename stickyTop->offsetHeader and improve docs (#2262)
* rename stickyTop->offsetTop and improve docs * add live to jsx * add changeset * Some formatting * stickyTop prop name update goes as minor as no usage * rename offsetTop->offsetHeader * update stickyTop arg name to offsetHeader * add to content to trigger new scnapshot generate from chromatic Co-authored-by: Cole Bemis <colebemis@github.com> Co-authored-by: Siddharth Kshetrapal <siddharthkp@github.com>
1 parent cc88235 commit 3ec0c9d

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed

.changeset/rich-singers-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
Rename `stickyTop` prop name for the PageLayout to `offsetHeader` and improve docs

docs/content/PageLayout.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ See [storybook](https://primer.style/react/storybook?path=/story/layout-pagelayo
170170

171171
### With a custom sticky header
172172

173+
Add `offsetHeader` prop to specify the height of the custom sticky header along with `sticky` prop
174+
173175
```jsx live
174176
<Box sx={{height: 320, overflowY: 'auto', border: '1px solid', borderColor: 'border.default'}}>
175177
<Box
@@ -190,7 +192,7 @@ See [storybook](https://primer.style/react/storybook?path=/story/layout-pagelayo
190192
<PageLayout.Content>
191193
<Placeholder label="Content" height={320} />
192194
</PageLayout.Content>
193-
<PageLayout.Pane position="start" stickyTop={64} sticky>
195+
<PageLayout.Pane position="start" sticky offsetHeader={64}>
194196
<Placeholder label="Pane" height={120} />
195197
</PageLayout.Pane>
196198
<PageLayout.Footer>
@@ -469,10 +471,10 @@ On macOS, you can open the VoiceOver rotor by pressing `VO-U`. You can navigate
469471
description="Whether the pane should stick to the top of the screen while the content scrolls."
470472
/>
471473
<PropsTableRow
472-
name="stickyTop"
474+
name="offsetHeader"
473475
type="number | string"
474476
defaultValue="0"
475-
description="Use stickyTop to push the sticky pane down to make room for a sticky header if necessary. Use the type `string` to specify the height with a unit i.e. 5rem; otherwise the type `number` will be taken as px."
477+
description="Use offsetHeader along with the sticky prop to push the sticky pane down to make room for a sticky header if necessary. Use the type `string` to specify the height with a unit i.e. 5rem; otherwise the type `number` will be taken as px."
476478
/>
477479
<PropsTableRow
478480
name="padding"

docs/content/SplitPageLayout.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ If you need a more flexible layout component, consider using the [PageLayout](/P
253253
<SplitPageLayout.Content>
254254
<Placeholder label="Content" height={320} />
255255
</SplitPageLayout.Content>
256-
<SplitPageLayout.Pane stickyTop={64}>
256+
<SplitPageLayout.Pane sticky offsetHeader={64}>
257257
<Placeholder label="Pane" height={120} />
258258
</SplitPageLayout.Pane>
259259
<SplitPageLayout.Footer>
@@ -383,10 +383,10 @@ If you need a more flexible layout component, consider using the [PageLayout](/P
383383
description="Whether the pane should stick to the top of the screen while the content scrolls."
384384
/>
385385
<PropsTableRow
386-
name="stickyTop"
386+
name="offsetHeader"
387387
type="number | string"
388388
defaultValue="0"
389-
description="Use stickyTop to push the sticky pane down to make room for a sticky header if necessary. Use the type `string` to specify the height with a unit i.e. 5rem; otherwise the type `number` will be taken as px."
389+
description="Use offsetHeader along with the sticky prop to push the sticky pane down to make room for a sticky header if necessary. Use the type `string` to specify the height with a unit i.e. 5rem; otherwise the type `number` will be taken as px."
390390
/>
391391
<PropsTableRow
392392
name="padding"

src/PageLayout/PageLayout.stories.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ export const CustomStickyHeader: Story = args => (
606606
sx={{
607607
position: 'sticky',
608608
top: 0,
609-
height: args.stickyTop,
609+
height: args.offsetHeader,
610610
display: 'grid',
611611
placeItems: 'center',
612612
backgroundColor: 'canvas.subtle',
@@ -624,20 +624,20 @@ export const CustomStickyHeader: Story = args => (
624624
return (
625625
<Box key={i} as="p" sx={{margin: 0}}>
626626
<span data-testid={testId}>
627-
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at enim id lorem tempus egestas a non
628-
ipsum. Maecenas imperdiet ante quam, at varius lorem molestie vel. Sed at eros consequat, varius
629-
tellus et, auctor felis. Donec pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec
630-
sit amet massa purus. Nunc sem lectus, bibendum a sapien nec, tristique tempus felis. Ut porttitor
631-
auctor tellus in imperdiet. Ut blandit tincidunt augue, quis fringilla nunc tincidunt sed. Vestibulum
632-
auctor euismod nisi. Nullam tincidunt est in mi tincidunt dictum. Sed consectetur aliquet velit ut
633-
ornare.
627+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vitae orci et magna consectetur
628+
ullamcorper eget ac purus. Nam at enim id lorem tempus egestas a non ipsum. Maecenas imperdiet ante
629+
quam, at varius lorem molestie vel. Sed at eros consequat, varius tellus et, auctor felis. Donec
630+
pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec sit amet massa purus. Nunc sem
631+
lectus, bibendum a sapien nec, tristique tempus felis. Ut porttitor auctor tellus in imperdiet. Ut
632+
blandit tincidunt augue, quis fringilla nunc tincidunt sed. Vestibulum auctor euismod nisi. Nullam
633+
tincidunt est in mi tincidunt dictum. Sed consectetur aliquet velit ut ornare.
634634
</span>
635635
</Box>
636636
)
637637
})}
638638
</Box>
639639
</PageLayout.Content>
640-
<PageLayout.Pane position="start" padding="normal" divider="line" sticky stickyTop={args.stickyTop}>
640+
<PageLayout.Pane position="start" padding="normal" divider="line" sticky offsetHeader={args.offsetHeader}>
641641
<Box sx={{display: 'grid', gap: 3}}>
642642
{Array.from({length: args.numParagraphsInPane}).map((_, i) => {
643643
const testId = `paragraph${i}`
@@ -666,7 +666,7 @@ CustomStickyHeader.argTypes = {
666666
type: 'boolean',
667667
defaultValue: true
668668
},
669-
stickyTop: {
669+
offsetHeader: {
670670
type: 'string',
671671
defaultValue: '8rem'
672672
},

src/PageLayout/PageLayout.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export type PageLayoutPaneProps = {
388388
*/
389389
dividerWhenNarrow?: 'inherit' | 'none' | 'line' | 'filled'
390390
sticky?: boolean
391-
stickyTop?: string | number
391+
offsetHeader?: string | number
392392
hidden?: boolean | ResponsiveValue<boolean>
393393
} & SxProp
394394

@@ -411,7 +411,7 @@ const Pane: React.FC<React.PropsWithChildren<PageLayoutPaneProps>> = ({
411411
divider: responsiveDivider = 'none',
412412
dividerWhenNarrow = 'inherit',
413413
sticky = false,
414-
stickyTop = 0,
414+
offsetHeader = 0,
415415
hidden: responsiveHidden = false,
416416
children,
417417
sx = {}
@@ -438,11 +438,11 @@ const Pane: React.FC<React.PropsWithChildren<PageLayoutPaneProps>> = ({
438438

439439
React.useEffect(() => {
440440
if (sticky) {
441-
enableStickyPane?.(stickyTop)
441+
enableStickyPane?.(offsetHeader)
442442
} else {
443443
disableStickyPane?.()
444444
}
445-
}, [sticky, enableStickyPane, disableStickyPane, stickyTop])
445+
}, [sticky, enableStickyPane, disableStickyPane, offsetHeader])
446446

447447
return (
448448
<Box
@@ -466,9 +466,9 @@ const Pane: React.FC<React.PropsWithChildren<PageLayoutPaneProps>> = ({
466466
...(sticky
467467
? {
468468
position: 'sticky',
469-
// If stickyTop has value, it will stick the pane to the position where the sticky top ends
470-
// else top will be 0 as the default value of stickyTop
471-
top: typeof stickyTop === 'number' ? `${stickyTop}px` : stickyTop,
469+
// If offsetHeader has value, it will stick the pane to the position where the sticky top ends
470+
// else top will be 0 as the default value of offsetHeader
471+
top: typeof offsetHeader === 'number' ? `${offsetHeader}px` : offsetHeader,
472472
overflow: 'hidden',
473473
maxHeight: 'var(--sticky-pane-height)'
474474
}

src/PageLayout/interaction.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ CustomStickyHeader.argTypes = {
395395
type: 'boolean',
396396
defaultValue: true
397397
},
398-
stickyTop: {
398+
offsetHeader: {
399399
type: 'string',
400400
defaultValue: '8rem'
401401
},

src/PageLayout/useStickyPaneHeight.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function useStickyPaneHeight() {
1010

1111
// Default the height to the viewport height
1212
const [height, setHeight] = React.useState('100vh')
13-
const [stickyTop, setStickyTop] = React.useState<number | string>(0)
13+
const [offsetHeader, setOffsetHeader] = React.useState<number | string>(0)
1414

1515
// Create intersection observers to track the top and bottom of the content region
1616
const [contentTopRef, contentTopInView, contentTopEntry] = useInView()
@@ -29,15 +29,16 @@ export function useStickyPaneHeight() {
2929
const topRect = contentTopEntry?.target.getBoundingClientRect()
3030
const bottomRect = contentBottomEntry?.target.getBoundingClientRect()
3131

32-
const stickyTopWithUnits = typeof stickyTop === 'number' ? `${stickyTop}px` : stickyTop
32+
// Custom sticky header's height with units
33+
const offsetHeaderWithUnits = typeof offsetHeader === 'number' ? `${offsetHeader}px` : offsetHeader
3334

3435
if (scrollContainer) {
3536
const scrollRect = scrollContainer.getBoundingClientRect()
3637

3738
const topOffset = topRect ? Math.max(topRect.top - scrollRect.top, 0) : 0
3839
const bottomOffset = bottomRect ? Math.max(scrollRect.bottom - bottomRect.bottom, 0) : 0
3940

40-
calculatedHeight = `calc(${scrollRect.height}px - (max(${topOffset}px, ${stickyTopWithUnits}) + ${bottomOffset}px))`
41+
calculatedHeight = `calc(${scrollRect.height}px - (max(${topOffset}px, ${offsetHeaderWithUnits}) + ${bottomOffset}px))`
4142
} else {
4243
const topOffset = topRect ? Math.max(topRect.top, 0) : 0
4344
const bottomOffset = bottomRect ? Math.max(window.innerHeight - bottomRect.bottom, 0) : 0
@@ -46,11 +47,11 @@ export function useStickyPaneHeight() {
4647
// We need to account for this when calculating the offset.
4748
const overflowScroll = Math.max(window.scrollY + window.innerHeight - document.body.scrollHeight, 0)
4849

49-
calculatedHeight = `calc(100vh - (max(${topOffset}px, ${stickyTopWithUnits}) + ${bottomOffset}px - ${overflowScroll}px))`
50+
calculatedHeight = `calc(100vh - (max(${topOffset}px, ${offsetHeaderWithUnits}) + ${bottomOffset}px - ${overflowScroll}px))`
5051
}
5152

5253
setHeight(calculatedHeight)
53-
}, [contentTopEntry, contentBottomEntry, stickyTop])
54+
}, [contentTopEntry, contentBottomEntry, offsetHeader])
5455

5556
// We only want to add scroll and resize listeners if the pane is sticky.
5657
// Since hooks can't be called conditionally, we need to use state to track
@@ -92,7 +93,7 @@ export function useStickyPaneHeight() {
9293

9394
function enableStickyPane(top: string | number) {
9495
setIsEnabled(true)
95-
setStickyTop(top)
96+
setOffsetHeader(top)
9697
}
9798

9899
function disableStickyPane() {

0 commit comments

Comments
 (0)