import { useLayoutEffect, useState, useRef } from 'react'; import { Toolbar, ToolbarItem, ToolbarContent, SearchInput, Checkbox } from '@patternfly/react-core'; const useIsStuckFromScrollParent = ({ shouldTrack, scrollParentRef }: { /** Indicates whether to track the scroll top position of the scroll parent element */ shouldTrack: boolean; /** Reference to the scroll parent element */ 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 ToolbarDynamicSticky = () => { const scrollParentRef = useRef(null); const isStickyStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef }); const [showEvenOnly, setShowEvenOnly] = useState(true); const [searchValue, setSearchValue] = useState(''); const array = Array.from(Array(30), (_, x) => x); // create array of numbers from 1-30 for demo purposes const numbers = showEvenOnly ? array.filter((number) => number % 2 === 0) : array; return (
setSearchValue(value)} onClear={() => setSearchValue('')} /> setShowEvenOnly(checked)} id="showOnlyEvenCheckbox" />
    {numbers.map((number) => (
  • {`item ${number}`}
  • ))}
); };