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

Opening Lightbox always scrolls to top of page #60

Open
jerryslaw opened this issue Jul 7, 2017 · 2 comments

Comments

@jerryslaw
Copy link

commented Jul 7, 2017

I'm using masonry to show images on my page. Every click on each image opens Lightbox. When I have many images, I have to scroll page down. Then, when I open Lightbox, it scrolls (moves) to the top of page. IMO it's not desired behavior. I think it is caused by hardcoded position of react-modal component (position: absolute, top: 0, etc). Maybe Lightbox should take current position to show modal, or maybe simply have position: fixed?

@zquancai

This comment has been minimized.

Copy link

commented Apr 20, 2018

I also encountered this problem, it is not a bug.
It also should not be Lightbox's problem.
The solution is to set the container tabIndex=1, like this:

import Lightbox from 'react-image-lightbox';
// ... other
<div tabIndex="1" onClick={() => { this.isOpen = true }} >
  <img
    alt="fail"
    src="https://avatars2.githubusercontent.com/u/8609783?s=88&v=4"
  />
  <Lightbox
    mainSrc="https://avatars2.githubusercontent.com/u/8609783?s=88&v=4"
    onCloseRequest={() => this.setState({ isOpen: false })}
  />
</div>

But eslint maybe does not pass.
or set the react-modal props like this:

import Lightbox from 'react-image-lightbox';
// ... other

<div tabIndex="1" onClick={() => { this.isOpen = true }} >
  <img
    alt="fail"
    src="https://avatars2.githubusercontent.com/u/8609783?s=88&v=4"
  />
  <Lightbox
    reactModalProps={{ shouldReturnFocusAfterClose: false }}
    mainSrc="https://avatars2.githubusercontent.com/u/8609783?s=88&v=4"
    onCloseRequest={() => this.setState({ isOpen: false })}
  />
</div>

The following 2 pieces of code can help you:
1、shouldReturnFocusAfterClose
2、focusLaterElements

@andresgutgon

This comment has been minimized.

Copy link

commented Nov 19, 2018

Thanks @zquancai shouldReturnFocusAfterClose did the trick :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.