-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix Popover Nested Scrolling #365
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e2f742
Create stories for testing the combination of Popover and nested scro…
4e7b05a
Activate event bubbling for scroll events
34a0b24
Update changelog
17ff02c
Hide popover if the trigger element scrolls out of view
f8ac73a
Appease linter
8447d00
Remove extra story
30562f6
Simplify hiding logic
39bcdc5
Add comment explaining event listener work
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 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 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 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 |
---|---|---|
|
@@ -44,6 +44,8 @@ export interface PopoverDialogProps extends StandardFunctionProps { | |
caretSize: number | ||
/** Adds reasonable styles to popover dialog contents so you do not have to */ | ||
enableDefaultStyles: boolean | ||
/** Allows the popover to dismiss itself when the trigger is no longer in view */ | ||
onHide: () => void | ||
} | ||
|
||
export type PopoverDialogRef = HTMLDivElement | ||
|
@@ -55,6 +57,7 @@ export const PopoverDialog = forwardRef<PopoverDialogRef, PopoverDialogProps>( | |
type, | ||
style, | ||
color, | ||
onHide, | ||
testID, | ||
contents, | ||
position, | ||
|
@@ -106,12 +109,27 @@ export const PopoverDialog = forwardRef<PopoverDialogRef, PopoverDialogProps>( | |
'cf-popover--contents__default-styles': enableDefaultStyles, | ||
}) | ||
|
||
const hidePopoverWhenOutOfView = ( | ||
entries: IntersectionObserverEntry[] | ||
): void => { | ||
if (!!entries.length && entries[0].isIntersecting === false) { | ||
onHide() | ||
} | ||
} | ||
|
||
const observer = new IntersectionObserver(hidePopoverWhenOutOfView) | ||
|
||
useLayoutEffect((): (() => void) => { | ||
handleUpdateStyles() | ||
window.addEventListener('scroll', handleUpdateStyles) | ||
observer.observe(triggerRef.current) | ||
// The third argument in addEventListener is "false" by default and controls bubbling | ||
// scroll events do not bubble by default so setting this to "true" | ||
// allows the listener to pick up scroll events from nested scrollable elements | ||
window.addEventListener('scroll', handleUpdateStyles, true) | ||
window.addEventListener('resize', handleUpdateStyles) | ||
|
||
return (): void => { | ||
observer.disconnect() | ||
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. What does this do? 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. Might not be necessary, but I threw it in there so the observer stops observing as a sort of "cleanup" |
||
window.removeEventListener('scroll', handleUpdateStyles) | ||
window.removeEventListener('resize', handleUpdateStyles) | ||
} | ||
|
This file contains 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
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.
What does the
true
do here?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.
Took me a whole day to figure out, but
scroll
events do not bubble by default. Adding this third argument in forces them to bubbleThere 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.
Can you add a comment explaining that?