Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/content/PageLayout.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +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" defaultValue="0" description="The height of the sticky top element" />
<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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add into the description that if it is number, we expect px and if it is string value with units?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good idea!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! 800b3ec let me know if you have a better wording :)

/>
<PropsTableRow
name="padding"
type={`| 'none'
Expand Down
4 changes: 2 additions & 2 deletions src/PageLayout/PageLayout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,8 @@ CustomStickyHeader.argTypes = {
defaultValue: true
},
stickyTop: {
type: 'number',
defaultValue: 100
type: 'string',
defaultValue: '8rem'
},
numParagraphsInPane: {
type: 'number',
Expand Down
4 changes: 2 additions & 2 deletions src/PageLayout/PageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const PageLayoutContext = React.createContext<{
padding: keyof typeof SPACING_MAP
rowGap: keyof typeof SPACING_MAP
columnGap: keyof typeof SPACING_MAP
enableStickyPane?: (stickyTopHeight: number) => void
enableStickyPane?: (top: number | string) => void
disableStickyPane?: () => void
contentTopRef?: (node?: Element | null | undefined) => void
contentBottomRef?: (node?: Element | null | undefined) => void
Expand Down Expand Up @@ -362,7 +362,7 @@ export type PageLayoutPaneProps = {
*/
dividerWhenNarrow?: 'inherit' | 'none' | 'line' | 'filled'
sticky?: boolean
stickyTop?: number // the height of the sticky top element
stickyTop?: string | number
hidden?: boolean | ResponsiveValue<boolean>
} & SxProp

Expand Down
18 changes: 10 additions & 8 deletions src/PageLayout/useStickyPaneHeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +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')
// stickyTop state
const [stickyTopHeight, setStickyTopHeight] = React.useState(0)
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()
Expand Down Expand Up @@ -46,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 - ${Math.max(topOffset, stickyTopHeight) + bottomOffset - overflowScroll}px)`
const stickyTopWithUnits = typeof stickyTop === 'number' ? `${stickyTop}px` : stickyTop

calculatedHeight = `calc(100vh - (max(${topOffset}px, ${stickyTopWithUnits}) + ${bottomOffset}px - ${overflowScroll}px))`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love that! 🙌🏼

}

setHeight(calculatedHeight)
}, [contentTopEntry, contentBottomEntry, stickyTopHeight])
}, [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
Expand Down Expand Up @@ -90,12 +90,14 @@ export function useStickyPaneHeight() {
}
}, [isEnabled, contentTopInView, contentBottomInView, calculateHeight])

const enableStickyPane = (stickyTop: number) => {
function enableStickyPane(top: string | number) {
setIsEnabled(true)
setStickyTopHeight(stickyTop)
setStickyTop(top)
}

const disableStickyPane = () => setIsEnabled(false)
function disableStickyPane() {
setIsEnabled(false)
}

return {
rootRef,
Expand Down