-
Notifications
You must be signed in to change notification settings - Fork 31
fix(PopoverContainer): Give popover better scroll awareness #3138
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
19a45dc
fix(PopoverContainer): Give popover better scroll awareness
dreamwasp 893c7c6
Merge branch 'main' into cass-gm-1236
dreamwasp a12f630
nicer example
dreamwasp bf5a0fd
Merge branch 'main' into cass-gm-1236
dreamwasp 7df3952
merge dev
dreamwasp e28f95c
Merge branch 'main' into cass-gm-1236
dreamwasp 6b48e64
remove example
dreamwasp 8c5a4ef
update react-focus-on
dreamwasp 5b50a83
tentative fix, need to do more testing
dreamwasp ce4ca53
need to optimize
dreamwasp d2b1526
keep this for now
dreamwasp 8c99806
ready to test with LXStudio
dreamwasp 0b95b2d
clean up PopoverContainer
dreamwasp 271f4ba
need
dreamwasp 3ca6377
Merge branch 'main' into cass-gm-1236
dreamwasp 1560f76
swap to better prop name and start testing modals
dreamwasp 525d16d
Merge branch 'cass-gm-1236' of github.com:Codecademy/gamut into cass-…
dreamwasp e3595d7
test overlay changes
dreamwasp 2b98bce
fix tests (need to audit)
dreamwasp 330ea6b
swap to better data-floating naming
dreamwasp 961baa0
better comments
dreamwasp acba37f
fix docs
dreamwasp 6a56b37
test scroll
dreamwasp aa655e5
Revert "test scroll"
dreamwasp 15f2c46
test and look over updates
dreamwasp 656aef9
Revert "test and look over updates"
dreamwasp 2ff7c8e
test scrollingparents
dreamwasp 1e1acb3
lint fix
dreamwasp 15765f1
Popover scrolling parents test
dreamwasp be75610
move things around and share multi-level scrolling
dreamwasp fb4a130
remove commented out code
dreamwasp 57a03dd
Merge branch 'main' into cass-gm-1236
dreamwasp 5734e50
nicer example
dreamwasp 90a1a08
fancier example
dreamwasp d1ca909
tooltip floating
dreamwasp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { findAllAdditionalScrollingParents, findResizingParent } from './utils'; | ||
|
||
export const useScrollingParentsEffect = ( | ||
targetRef: React.RefObject< | ||
Pick<HTMLDivElement, 'getBoundingClientRect' | 'contains'> | ||
>, | ||
setTargetRect: (rect: DOMRect | undefined) => void | ||
) => { | ||
useEffect(() => { | ||
if (!targetRef.current) { | ||
return; | ||
} | ||
|
||
const target = targetRef.current as unknown as HTMLElement; | ||
const scrollingParents = findAllAdditionalScrollingParents(target); | ||
|
||
const updatePosition = () => { | ||
setTargetRect(targetRef?.current?.getBoundingClientRect()); | ||
}; | ||
|
||
// For immediate updates during scroll | ||
const immediateUpdate = () => { | ||
updatePosition(); | ||
}; | ||
|
||
const cleanup: (() => void)[] = []; | ||
|
||
// Add listeners to all scrolling parents (window scroll handled by useWindowScroll) | ||
scrollingParents.forEach((parent) => { | ||
if (parent.addEventListener) { | ||
// Use immediate update for smoother experience | ||
parent.addEventListener('scroll', immediateUpdate, { passive: true }); | ||
cleanup.push(() => | ||
parent.removeEventListener('scroll', immediateUpdate) | ||
); | ||
} | ||
}); | ||
|
||
return () => { | ||
cleanup.forEach((fn) => fn()); | ||
}; | ||
}, [targetRef, setTargetRect]); | ||
}; | ||
|
||
export const useResizingParentEffect = ( | ||
targetRef: React.RefObject< | ||
Pick<HTMLDivElement, 'getBoundingClientRect' | 'contains'> | ||
>, | ||
setTargetRect: (rect: DOMRect | undefined) => void | ||
) => { | ||
useEffect(() => { | ||
// handles movement of target within a clipped container e.g. Drawer | ||
if (!targetRef.current || typeof ResizeObserver === 'undefined') { | ||
return; | ||
} | ||
const resizingParent = findResizingParent( | ||
targetRef.current as unknown as HTMLElement | ||
); | ||
if (!resizingParent?.addEventListener) { | ||
return; | ||
} | ||
const handler = () => { | ||
setTargetRect(targetRef?.current?.getBoundingClientRect()); | ||
}; | ||
const ro = new ResizeObserver(handler); | ||
ro.observe(resizingParent); | ||
return () => ro.unobserve(resizingParent); | ||
}, [targetRef, setTargetRect]); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something to include here is to check against the PopoverContainer's
getBoundingClientRect
and compare against the parents rect's top/bottom/left/right and if the popover goes out of bounds, then also close the popover?Would probably need to check with design and can be ticketed as a fast follow