Skip to content
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 8 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

### 1.0.3 (Unreleased)
### 1.0.4 (Unreleased)

- [#365](https://github.com/influxdata/clockface/pull/365): Fix attachment of `Popover` when trigger element is nested in a scrolling element

### 1.0.3

- [#360](https://github.com/influxdata/clockface/pull/360): Add gradients in the green-yellow & yellow-red spectrums to the `Gradients` enum
- [#356](https://github.com/influxdata/clockface/pull/356): Fix bug causing `FormValidationElement` to render in error state before the user has interacted with anything
Expand Down
1 change: 1 addition & 0 deletions src/Components/Popover/Base/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export const PopoverRoot = forwardRef<PopoverRef, PopoverProps>(
position={position}
contents={contents(handleHideDialog)}
testID={testID}
onHide={handleHideDialog}
color={color}
style={style}
type={type}
Expand Down
20 changes: 19 additions & 1 deletion src/Components/Popover/Base/PopoverDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,6 +57,7 @@ export const PopoverDialog = forwardRef<PopoverDialogRef, PopoverDialogProps>(
type,
style,
color,
onHide,
testID,
contents,
position,
Expand Down Expand Up @@ -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)
Copy link

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?

Copy link
Contributor Author

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 bubble

Copy link

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?

window.addEventListener('resize', handleUpdateStyles)

return (): void => {
observer.disconnect()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
Expand Down
88 changes: 88 additions & 0 deletions src/Components/Popover/Documentation/Popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
QuestionMarkTooltipRef,
} from '../Composed/QuestionMarkTooltip'
import {SquareButton} from '../../Button/Composed/SquareButton'
import {DapperScrollbars} from '../../DapperScrollbars/DapperScrollbars'

// Types
import {
Expand All @@ -47,6 +48,10 @@ const composedPopoverStories = storiesOf('Atomic|Popover/Composed', module)
.addDecorator(withKnobs)
.addDecorator(jsxDecorator)

const testPopoverStories = storiesOf('Atomic|Popover/Tests', module)
.addDecorator(withKnobs)
.addDecorator(jsxDecorator)

const exampleStyle = {
width: '250px',
height: '200px',
Expand Down Expand Up @@ -277,3 +282,86 @@ composedPopoverStories.add(
},
}
)

testPopoverStories.add('Popover Trigger within a DapperScrollbars', () => {
const triggerRef = createRef<HTMLDivElement>()

return (
<div className="story--example">
<Popover.Popover
triggerRef={triggerRef}
contents={() => (
<>
<div>
I'm just a simple popover looking for my
<br />
place in this <strong>vast and beautiful</strong> world.
<br />
Will you help me?
</div>
</>
)}
showEvent={PopoverInteraction.Click}
hideEvent={PopoverInteraction.Click}
position={PopoverPosition.Below}
color={ComponentColor.Success}
type={PopoverType.Outline}
/>
<DapperScrollbars
style={{width: '100%', height: '100%', position: 'absolute'}}
>
<div style={{padding: '30px'}}>
<p>
Lorem ipsum dolor amet mixtape authentic lomo stumptown messenger
bag hexagon artisan, beard enamel pin biodiesel keytar tumeric raw
denim iceland selfies. Blue bottle banh mi hot chicken plaid irony
raclette yr skateboard messenger bag four dollar toast food truck
palo santo tote bag. Hella hell of trust fund celiac, shaman
whatever intelligentsia prism sriracha man braid fanny pack cardigan
af semiotics. Retro freegan locavore, blue bottle hell of tofu
austin direct trade williamsburg actually tattooed forage occupy
plaid kitsch. Microdosing 3 wolf moon four loko, shabby chic
sriracha echo park hell of small batch franzen chia tumeric. Hashtag
authentic echo park, cornhole flannel sartorial cloud bread
taxidermy.
</p>
<p>
Poke tbh synth, everyday carry hella small batch XOXO salvia
literally 3 wolf moon pork belly. Blog marfa migas iPhone, crucifix
salvia tousled. Actually hella polaroid kitsch waistcoat tote bag
forage palo santo single-origin coffee cornhole umami fingerstache.
Heirloom gastropub twee pickled tumblr snackwave portland leggings
raclette shabby chic kitsch knausgaard. Lumbersexual palo santo woke
forage narwhal meh letterpress. Gochujang YOLO cardigan, hexagon
hell of kogi succulents squid brooklyn.
</p>
<p>
Poke tbh synth, everyday carry hella small batch XOXO salvia
literally 3 wolf moon pork belly. Blog marfa migas iPhone, crucifix
salvia tousled. Actually hella polaroid kitsch waistcoat tote bag
forage palo santo single-origin coffee cornhole umami fingerstache.
Heirloom gastropub twee pickled tumblr snackwave portland leggings
raclette shabby chic kitsch knausgaard. Lumbersexual palo santo woke
forage narwhal meh letterpress. Gochujang YOLO cardigan, hexagon
hell of kogi succulents squid brooklyn.
</p>
<div
className="mockComponent mockButton"
ref={triggerRef}
style={{margin: '30px 0'}}
>
Click Me
</div>
<p>
Ennui truffaut artisan drinking vinegar distillery tumeric roof
party kickstarter. Leggings vice try-hard cardigan crucifix pork
belly poutine. Yuccie 8-bit leggings, kombucha gluten-free
post-ironic knausgaard snackwave craft beer. Tumblr shabby chic deep
v, irony slow-carb celiac skateboard kale chips succulents ennui
franzen.
</p>
</div>
</DapperScrollbars>
</div>
)
})