-
Notifications
You must be signed in to change notification settings - Fork 381
feat(Toolbar): dynamic sticky #12375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| 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<any>; | ||
| }): 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<HTMLDivElement>(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; | ||
|
kmcfaul marked this conversation as resolved.
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The 🐛 Proposed fix- const numbers = showEvenOnly ? array.filter((number) => number % 2 === 0) : array;
+ const filteredByEven = showEvenOnly ? array.filter((number) => number % 2 === 0) : array;
+ const numbers = searchValue
+ ? filteredByEven.filter((number) => `item ${number}`.includes(searchValue))
+ : filteredByEven;Also applies to: 51-56, 69-71 🤖 Prompt for AI Agents |
||
|
|
||
| return ( | ||
| <div id="dynamic-sticky-scroll-parent" ref={scrollParentRef} style={{ overflowY: 'scroll', height: '200px' }}> | ||
| <Toolbar id="toolbar-sticky" inset={{ default: 'insetNone' }} isStickyBase isStickyStuck={isStickyStuck}> | ||
| <ToolbarContent> | ||
| <ToolbarItem> | ||
| <SearchInput | ||
| aria-label="Sticky example search input" | ||
| value={searchValue} | ||
| onChange={(_event, value) => setSearchValue(value)} | ||
| onClear={() => setSearchValue('')} | ||
| /> | ||
| </ToolbarItem> | ||
| <ToolbarItem alignSelf="center"> | ||
| <Checkbox | ||
| label="Show only even number items" | ||
| isChecked={showEvenOnly} | ||
| onChange={(_event, checked) => setShowEvenOnly(checked)} | ||
| id="showOnlyEvenCheckbox" | ||
| /> | ||
| </ToolbarItem> | ||
| </ToolbarContent> | ||
| </Toolbar> | ||
| <ul> | ||
| {numbers.map((number) => ( | ||
| <li key={number}>{`item ${number}`}</li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.