import { useLayoutEffect, useState, useRef } from 'react'; import { Page, Masthead, MastheadMain, MastheadBrand, MastheadLogo, MastheadContent, PageSection, Toolbar, ToolbarContent, ToolbarItem, Breadcrumb, BreadcrumbItem, Content } from '@patternfly/react-core'; const useIsStuckFromScrollParent = ({ shouldTrack, scrollParentRef }: { shouldTrack: boolean; scrollParentRef: React.RefObject; }): boolean => { const [isStuck, setIsStuck] = useState(false); useLayoutEffect(() => { if (!shouldTrack) { setIsStuck(false); return; } const scrollElement = scrollParentRef.current; if (!scrollElement) { setIsStuck(false); return; } const syncFromScroll = () => { setIsStuck(scrollElement.scrollTop > 0); }; syncFromScroll(); scrollElement.addEventListener('scroll', syncFromScroll, { passive: true }); return () => scrollElement.removeEventListener('scroll', syncFromScroll); }, [shouldTrack, scrollParentRef]); return isStuck; }; export const PageDynamicStickySection: React.FunctionComponent = () => { const scrollParentRef = useRef(null); const isStickyStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef }); const headerToolbar = ( header-tools ); const masthead = ( Logo {headerToolbar} ); return (
Section home Section title Section landing

Main title

Scroll the container to see the breadcrumb section above dynamically apply its stuck styling. The section uses stickyBase="top" to remain fixed at the top of the scroll parent, and{' '} isStickyStuck is toggled via a scroll event listener to apply visual styling when the section is no longer at the top edge.

{Array.from({ length: 30 }, (_, i) => (

{`Section ${i + 1} content`}

))}
); };