Skip to content
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

fix(Popup): execute onClose when Popup closes on scroll #2182

Merged
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
8 changes: 5 additions & 3 deletions src/modules/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,19 @@ export default class Popup extends Component {
return portalProps
}

hideOnScroll = () => {
hideOnScroll = (e) => {
this.setState({ closed: true })

eventStack.unsub('scroll', this.hideOnScroll, { target: window })
setTimeout(() => this.setState({ closed: false }), 50)

this.handleClose(e)
Copy link
Member

Choose a reason for hiding this comment

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

@mkarajohn Did you investigate what closed value in state actually does? Why it becomes false after 50ms? This looks confusing to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I I saw it reopens the modal, even though I do not understand the reason as to why it does so.

From my understanding of the docs, and from the visual feedback, when the modal closes on scroll, it closes for good.

Could you please explain to me?

Copy link
Member

Choose a reason for hiding this comment

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

Seems it is only used in one place, render. If true, the trigger is rendered directly, else, the Portal is rendered with the trigger set:

if (closed) return trigger

// ..snip

return (
  <Portal...

I can't see why a timeout is added here?

Also, I've been racking my brain as to why we have an error logged regarding setState being called on an unmounted component, and I believe this is the issue :) There is no cancelling of this timeout when the Popup unmounts, therefore, setState can still be called 50ms after unmount.

This looks like it might be a hack to "reset" the portal. By rendering the trigger only, there is no Portal so it appears to have closed. Then, by nearly immediately (50ms?) rending the Portal again this ensures the Portal behavior is added to the trigger again. Future click/hover/etc will re-open the Portal.

I would have to investigate a solution to have a solid answer, but my suggestion would be to explore moving Popup to an AutoControlledComponent. Then, in hideOnScroll, we likely need to this.trySetState({ open: false }) without a timeout. This way, we are attempting to immediately close the Popup on scroll.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@levithomason

Also, I've been racking my brain as to why we have an error logged regarding setState being called on an unmounted component, and I believe this is the issue :) There is no cancelling of this timeout when the Popup unmounts, therefore, setState can still be called 50ms after unmount.

This was supposed to be closed here. It seems like it's been undone or something.

So, what about the initial concern that this PR addresses? The fact that the onClose is not called when the Popup closes because of scrolling?

Copy link
Member

Choose a reason for hiding this comment

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

So, what about the initial concern that this PR addresses? The fact that the onClose is not called when the Popup closes because of scrolling?

Yes, any state change due to user interaction should fire callbacks. This would include scroll. We only skip callbacks when state is changed through props, such as toggling the open prop.

}

handleClose = (e) => {
debug('handleClose()')
const { onClose } = this.props
if (onClose) onClose(e, this.props)

_.invoke(this.props, 'onClose', e, this.props)
}

handleOpen = (e) => {
Expand Down
17 changes: 12 additions & 5 deletions test/specs/modules/Popup/Popup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,21 @@ describe('Popup', () => {
wrapper.find('button').simulate('click')
assertInBody('.ui.popup.visible')

document.body.scrollTop = 100
domEvent.scroll(window)
assertInBody('.ui.popup.visible', false)
})

const evt = document.createEvent('CustomEvent')
evt.initCustomEvent('scroll', false, false, null)
it('is called with (e, props) when scroll', () => {
const onClose = sandbox.spy()
const trigger = <button>foo</button>

window.dispatchEvent(evt)
wrapperMount(<Popup content='foo' hideOnScroll onClose={onClose} trigger={trigger} />)
.find('button')
.simulate('click')

assertInBody('.ui.popup.visible', false)
domEvent.scroll(window)
onClose.should.have.been.calledOnce()
onClose.should.have.been.calledWithMatch({}, { content: 'foo', onClose, trigger })
})
})

Expand Down