Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Opening Lightbox always scrolls to top of page #60

Closed
jerryslaw opened this issue Jul 7, 2017 · 3 comments
Closed

Opening Lightbox always scrolls to top of page #60

jerryslaw opened this issue Jul 7, 2017 · 3 comments

Comments

@jerryslaw
Copy link

jerryslaw 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
Copy link

zquancai 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
Copy link

Thanks @zquancai shouldReturnFocusAfterClose did the trick :)

@bhushan-patil-official
Copy link

bhushan-patil-official commented Nov 8, 2022

Thanks @zquancai it worked for me too. I was using inside headless ui thought headless ui's tab has the issue.

@fritz-c fritz-c closed this as completed Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants