-
Notifications
You must be signed in to change notification settings - Fork 645
Add stickyTop prop to PageLayout.Pane #2232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
515992a
3d1f53b
bc54d11
d06c1a7
2d429d1
3919fdd
e4060b0
c433888
800b3ec
d949608
319f756
753a6f1
0adf6b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': minor | ||
| --- | ||
|
|
||
| Add a `stickyTop` prop, the height of a sticky header, to the `PageLayout.Pane` to push the pane down for the sticky header when necessary. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,36 @@ See [storybook](https://primer.style/react/storybook?path=/story/layout-pagelayo | |
| </Box> | ||
| ``` | ||
|
|
||
| ### With a stickyTop sticky pane | ||
|
|
||
| ```jsx live | ||
| <Box sx={{height: 320, overflowY: 'auto', border: '1px solid', borderColor: 'border.default'}}> | ||
| <Box | ||
| height="64px" | ||
| width="100%" | ||
| borderBottom="1px solid" | ||
| borderColor="border.default" | ||
| sx={{position: 'sticky', top: '0', backgroundColor: 'canvas.subtle'}} | ||
| display="flex" | ||
| alignItems="center" | ||
| justifyContent="center" | ||
| > | ||
| <Heading>Sticky top content</Heading> | ||
| </Box> | ||
broccolinisoup marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <PageLayout> | ||
| <PageLayout.Content> | ||
| <Placeholder label="Content" height={320} /> | ||
| </PageLayout.Content> | ||
| <PageLayout.Pane position="start" stickyTop={64} sticky> | ||
| <Placeholder label="Pane" height={120} /> | ||
| </PageLayout.Pane> | ||
| <PageLayout.Footer> | ||
| <Placeholder label="Footer" height={64} /> | ||
| </PageLayout.Footer> | ||
| </PageLayout> | ||
| </Box> | ||
| ``` | ||
|
|
||
| ## Props | ||
|
|
||
| ### PageLayout | ||
|
|
@@ -337,6 +367,12 @@ See [storybook](https://primer.style/react/storybook?path=/story/layout-pagelayo | |
| defaultValue="false" | ||
| description="Whether the pane should stick to the top of the screen while the content scrolls." | ||
| /> | ||
| <PropsTableRow | ||
| name="stickyTop" | ||
| type="number | string" | ||
| defaultValue="0" | ||
| description="Use stickyTop to push the sticky pane down to make room for a sticky header if necessary" | ||
|
||
| /> | ||
| <PropsTableRow | ||
| name="padding" | ||
| type={`| 'none' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -659,4 +659,74 @@ NestedScrollContainer.argTypes = { | |
| } | ||
| } | ||
|
|
||
| export const CustomStickyHeader: Story = args => ( | ||
| // a box to create a sticky top element that will be on the consumer side and outside of the PageLayout component | ||
| <Box> | ||
| <Box | ||
| sx={{ | ||
| position: 'sticky', | ||
| top: 0, | ||
| height: args.stickyTop, | ||
| display: 'grid', | ||
| placeItems: 'center', | ||
| backgroundColor: 'canvas.subtle', | ||
| borderBottom: '1px solid', | ||
| borderColor: 'border.default' | ||
| }} | ||
| > | ||
| Custom sticky header | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah this is very explanatory. Thanks for updating it! |
||
| </Box> | ||
| <PageLayout rowGap="none" columnGap="none" padding="none" containerWidth="full"> | ||
| <PageLayout.Content padding="normal" width="large"> | ||
| <Box sx={{display: 'grid', gap: 3}}> | ||
| {Array.from({length: args.numParagraphsInContent}).map((_, i) => ( | ||
| <Box key={i} as="p" sx={{margin: 0}}> | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at enim id lorem tempus egestas a non ipsum. | ||
| Maecenas imperdiet ante quam, at varius lorem molestie vel. Sed at eros consequat, varius tellus et, | ||
| auctor felis. Donec pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec sit amet massa | ||
| purus. Nunc sem lectus, bibendum a sapien nec, tristique tempus felis. Ut porttitor auctor tellus in | ||
| imperdiet. Ut blandit tincidunt augue, quis fringilla nunc tincidunt sed. Vestibulum auctor euismod nisi. | ||
| Nullam tincidunt est in mi tincidunt dictum. Sed consectetur aliquet velit ut ornare. | ||
| </Box> | ||
| ))} | ||
| </Box> | ||
| </PageLayout.Content> | ||
| <PageLayout.Pane position="start" padding="normal" divider="line" sticky stickyTop={args.stickyTop}> | ||
| <Box sx={{display: 'grid', gap: 3}}> | ||
| {Array.from({length: args.numParagraphsInPane}).map((_, i) => ( | ||
| <Box key={i} as="p" sx={{margin: 0}}> | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at enim id lorem tempus egestas a non ipsum. | ||
| Maecenas imperdiet ante quam, at varius lorem molestie vel. Sed at eros consequat, varius tellus et, | ||
| auctor felis. Donec pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec sit amet massa | ||
| purus. | ||
| </Box> | ||
| ))} | ||
| </Box> | ||
| </PageLayout.Pane> | ||
| <PageLayout.Footer padding="normal" divider="line"> | ||
| <Placeholder label="Footer" height={64} /> | ||
| </PageLayout.Footer> | ||
| </PageLayout> | ||
| </Box> | ||
| ) | ||
|
|
||
| CustomStickyHeader.argTypes = { | ||
| sticky: { | ||
| type: 'boolean', | ||
| defaultValue: true | ||
| }, | ||
| stickyTop: { | ||
| type: 'string', | ||
| defaultValue: '8rem' | ||
| }, | ||
| numParagraphsInPane: { | ||
| type: 'number', | ||
| defaultValue: 10 | ||
| }, | ||
| numParagraphsInContent: { | ||
| type: 'number', | ||
| defaultValue: 30 | ||
| } | ||
| } | ||
|
|
||
| export default meta | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,12 @@ import {useInView} from 'react-intersection-observer' | |
| * Calculates the height of the sticky pane such that it always | ||
| * fits into the viewport even when the header or footer are visible. | ||
| */ | ||
| // TODO: Handle sticky header | ||
| export function useStickyPaneHeight() { | ||
| const rootRef = React.useRef<HTMLDivElement>(null) | ||
|
|
||
| // Default the height to the viewport height | ||
| const [height, setHeight] = React.useState('100vh') | ||
| const [stickyTop, setStickyTop] = React.useState<number | string>(0) | ||
|
|
||
| // Create intersection observers to track the top and bottom of the content region | ||
| const [contentTopRef, contentTopInView, contentTopEntry] = useInView() | ||
|
|
@@ -44,11 +44,13 @@ export function useStickyPaneHeight() { | |
| // We need to account for this when calculating the offset. | ||
| const overflowScroll = Math.max(window.scrollY + window.innerHeight - document.body.scrollHeight, 0) | ||
|
|
||
| calculatedHeight = `calc(100vh - ${topOffset + bottomOffset - overflowScroll}px)` | ||
| const stickyTopWithUnits = typeof stickyTop === 'number' ? `${stickyTop}px` : stickyTop | ||
|
|
||
| calculatedHeight = `calc(100vh - (max(${topOffset}px, ${stickyTopWithUnits}) + ${bottomOffset}px - ${overflowScroll}px))` | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love that! 🙌🏼 |
||
| } | ||
|
|
||
| setHeight(calculatedHeight) | ||
| }, [contentTopEntry, contentBottomEntry]) | ||
| }, [contentTopEntry, contentBottomEntry, stickyTop]) | ||
|
|
||
| // We only want to add scroll and resize listeners if the pane is sticky. | ||
| // Since hooks can't be called conditionally, we need to use state to track | ||
|
|
@@ -88,10 +90,19 @@ export function useStickyPaneHeight() { | |
| } | ||
| }, [isEnabled, contentTopInView, contentBottomInView, calculateHeight]) | ||
|
|
||
| function enableStickyPane(top: string | number) { | ||
| setIsEnabled(true) | ||
| setStickyTop(top) | ||
| } | ||
|
|
||
| function disableStickyPane() { | ||
| setIsEnabled(false) | ||
| } | ||
|
|
||
| return { | ||
| rootRef, | ||
| enableStickyPane: () => setIsEnabled(true), | ||
| disableStickyPane: () => setIsEnabled(false), | ||
| enableStickyPane, | ||
| disableStickyPane, | ||
| contentTopRef, | ||
| contentBottomRef, | ||
| stickyPaneHeight: height | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.