Debian Codegraph @

node/@patternfly/react-core/src/components/Page/examples/PageDynamicStickySection.tsx

raw
1 import { useLayoutEffect, useState, useRef } from 'react';
2 import {
3 Page,
4 Masthead,
5 MastheadMain,
6 MastheadBrand,
7 MastheadLogo,
8 MastheadContent,
9 PageSection,
10 Toolbar,
11 ToolbarContent,
12 ToolbarItem,
13 Breadcrumb,
14 BreadcrumbItem,
15 Content
16 } from '@patternfly/react-core';
17
18 const useIsStuckFromScrollParent = ({
19 shouldTrack,
20 scrollParentRef
21 }: {
22 shouldTrack: boolean;
23 scrollParentRef: React.RefObject<any>;
24 }): boolean => {
25 const [isStuck, setIsStuck] = useState(false);
26
27 useLayoutEffect(() => {
28 if (!shouldTrack) {
29 setIsStuck(false);
30 return;
31 }
32
33 const scrollElement = scrollParentRef.current;
34 if (!scrollElement) {
35 setIsStuck(false);
36 return;
37 }
38
39 const syncFromScroll = () => {
40 setIsStuck(scrollElement.scrollTop > 0);
41 };
42 syncFromScroll();
43 scrollElement.addEventListener('scroll', syncFromScroll, { passive: true });
44 return () => scrollElement.removeEventListener('scroll', syncFromScroll);
45 }, [shouldTrack, scrollParentRef]);
46
47 return isStuck;
48 };
49
50 export const PageDynamicStickySection: React.FunctionComponent = () => {
51 const scrollParentRef = useRef<HTMLDivElement>(null);
52 const isStickyStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef });
53
54 const headerToolbar = (
55 <Toolbar id="dynamic-sticky-toolbar">
56 <ToolbarContent>
57 <ToolbarItem>header-tools</ToolbarItem>
58 </ToolbarContent>
59 </Toolbar>
60 );
61
62 const masthead = (
63 <Masthead>
64 <MastheadMain>
65 <MastheadBrand>
66 <MastheadLogo href="https://patternfly.org" target="_blank">
67 Logo
68 </MastheadLogo>
69 </MastheadBrand>
70 </MastheadMain>
71 <MastheadContent>{headerToolbar}</MastheadContent>
72 </Masthead>
73 );
74
75 return (
76 <Page masthead={masthead}>
77 <div ref={scrollParentRef} style={{ overflowY: 'auto', height: '100%' }}>
78 <PageSection type="breadcrumb" stickyBase="top" isStickyStuck={isStickyStuck}>
79 <Breadcrumb>
80 <BreadcrumbItem>Section home</BreadcrumbItem>
81 <BreadcrumbItem to="#">Section title</BreadcrumbItem>
82 <BreadcrumbItem to="#" isActive>
83 Section landing
84 </BreadcrumbItem>
85 </Breadcrumb>
86 </PageSection>
87 <PageSection>
88 <Content>
89 <h1>Main title</h1>
90 <p>
91 Scroll the container to see the breadcrumb section above dynamically apply its stuck styling. The section
92 uses <code>stickyBase=&quot;top&quot;</code> to remain fixed at the top of the scroll parent, and{' '}
93 <code>isStickyStuck</code> is toggled via a scroll event listener to apply visual styling when the section
94 is no longer at the top edge.
95 </p>
96 </Content>
97 </PageSection>
98 {Array.from({ length: 30 }, (_, i) => (
99 <PageSection key={i} variant={i % 2 === 0 ? 'default' : 'secondary'}>
100 <Content>
101 <p>{`Section ${i + 1} content`}</p>
102 </Content>
103 </PageSection>
104 ))}
105 </div>
106 </Page>
107 );
108 };