| 1 |
import { useEffect, useState } from 'react'; |
| 2 |
import { |
| 3 |
PageSection, |
| 4 |
JumpLinks, |
| 5 |
JumpLinksItem, |
| 6 |
JumpLinksList, |
| 7 |
Sidebar, |
| 8 |
SidebarContent, |
| 9 |
SidebarPanel, |
| 10 |
Switch, |
| 11 |
Title, |
| 12 |
Content, |
| 13 |
getResizeObserver |
| 14 |
} from '@patternfly/react-core'; |
| 15 |
import { DashboardWrapper } from '@patternfly/react-core/dist/js/demos/DashboardWrapper'; |
| 16 |
import mastheadStyles from '@patternfly/react-styles/css/components/Masthead/masthead'; |
| 17 |
|
| 18 |
export const JumpLinksScrollspy: React.FunctionComponent = () => { |
| 19 |
const headings = [1, 2, 3, 4, 5]; |
| 20 |
|
| 21 |
const [isVertical, setIsVertical] = useState(true); |
| 22 |
const [offsetHeight, setOffsetHeight] = useState(10); |
| 23 |
const offsetForPadding = 10; |
| 24 |
|
| 25 |
// Update offset based on the masthead and jump links nav heights. |
| 26 |
useEffect(() => { |
| 27 |
const masthead = document.getElementsByClassName(mastheadStyles.masthead)[0]; |
| 28 |
|
| 29 |
if (isVertical) { |
| 30 |
setOffsetHeight(masthead.offsetHeight + offsetForPadding); |
| 31 |
} else { |
| 32 |
// Append jump links nav height to the masthead height when value exists. |
| 33 |
const jumpLinksHeaderHeight = document.getElementsByClassName('pf-m-sticky')[0].offsetHeight; |
| 34 |
jumpLinksHeaderHeight && setOffsetHeight(masthead.offsetHeight + jumpLinksHeaderHeight + offsetForPadding); |
| 35 |
} |
| 36 |
|
| 37 |
const observer = getResizeObserver( |
| 38 |
document.getElementsByClassName(mastheadStyles.masthead)[0], |
| 39 |
() => { |
| 40 |
if (isVertical) { |
| 41 |
setOffsetHeight(masthead.offsetHeight + offsetForPadding); |
| 42 |
} else { |
| 43 |
// Append jump links nav height to the masthead height when value exists. |
| 44 |
const jumpLinksHeaderHeight = document.getElementsByClassName('pf-m-sticky')[0].offsetHeight; |
| 45 |
jumpLinksHeaderHeight && setOffsetHeight(masthead.offsetHeight + jumpLinksHeaderHeight + offsetForPadding); |
| 46 |
} |
| 47 |
}, |
| 48 |
true |
| 49 |
); |
| 50 |
|
| 51 |
return () => observer(); |
| 52 |
}, [isVertical]); |
| 53 |
|
| 54 |
return ( |
| 55 |
<DashboardWrapper breadcrumb={null} mainContainerId="scrollable-element"> |
| 56 |
<PageSection aria-labelledby="main-title"> |
| 57 |
<Title headingLevel="h1" size="2xl" id="main-title"> |
| 58 |
Main title |
| 59 |
</Title> |
| 60 |
<Switch |
| 61 |
id="is-vertical" |
| 62 |
label="Vertical jump links" |
| 63 |
isChecked={isVertical} |
| 64 |
onChange={(_event, check) => setIsVertical(check)} |
| 65 |
/> |
| 66 |
</PageSection> |
| 67 |
<PageSection padding={{ default: 'noPadding' }}> |
| 68 |
<Sidebar hasGutter orientation={!isVertical && 'stack'}> |
| 69 |
<SidebarPanel variant="sticky"> |
| 70 |
<PageSection aria-label="Jump links navigation"> |
| 71 |
<JumpLinks |
| 72 |
isVertical={isVertical} |
| 73 |
isCentered={!isVertical} |
| 74 |
label="Jump to section" |
| 75 |
scrollableSelector=".pf-v6-c-page__main-container" |
| 76 |
offset={offsetHeight} |
| 77 |
expandable={{ default: isVertical ? 'expandable' : 'nonExpandable', md: 'nonExpandable' }} |
| 78 |
isExpanded |
| 79 |
> |
| 80 |
{headings.map((i) => ( |
| 81 |
<JumpLinksItem key={i} href={`#heading-${i}`}> |
| 82 |
{`Heading ${i}`} |
| 83 |
<JumpLinksList></JumpLinksList> |
| 84 |
</JumpLinksItem> |
| 85 |
))} |
| 86 |
</JumpLinks> |
| 87 |
</PageSection> |
| 88 |
</SidebarPanel> |
| 89 |
<SidebarContent hasNoBackground> |
| 90 |
<PageSection aria-label="Main content"> |
| 91 |
<Content> |
| 92 |
{headings.map((i) => ( |
| 93 |
<div key={i} style={{ maxWidth: '800px', marginBottom: '32px' }}> |
| 94 |
<h2 id={`heading-${i}`} tabIndex={-1}> |
| 95 |
{`Heading ${i}`} |
| 96 |
</h2> |
| 97 |
<p> |
| 98 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut |
| 99 |
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris |
| 100 |
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit |
| 101 |
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt |
| 102 |
in culpa qui officia deserunt mollit anim id est laborum. |
| 103 |
</p> |
| 104 |
<br /> |
| 105 |
<p> |
| 106 |
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum |
| 107 |
deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non |
| 108 |
provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum |
| 109 |
fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta |
| 110 |
nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, |
| 111 |
omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis |
| 112 |
debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non |
| 113 |
recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus |
| 114 |
maiores alias consequatur aut perferendis doloribus asperiores repellat. |
| 115 |
</p> |
| 116 |
</div> |
| 117 |
))} |
| 118 |
</Content> |
| 119 |
</PageSection> |
| 120 |
</SidebarContent> |
| 121 |
</Sidebar> |
| 122 |
</PageSection> |
| 123 |
</DashboardWrapper> |
| 124 |
); |
| 125 |
}; |