-
Notifications
You must be signed in to change notification settings - Fork 1
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(web-react): Get initial scroll from offsetY #1203
Conversation
* useEffect runs enableScroll function by default * in that time the document.body.style.top is not set * so the function fallsback to 0 even if the window is scrolled * do not work particulary with #anchors
✅ Deploy Preview for spirit-design-system-storybook ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
✅ Deploy Preview for spirit-design-system-demo canceled.
|
✅ Deploy Preview for spirit-design-system-validations canceled.
|
✅ Deploy Preview for spirit-design-system-react ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
@@ -17,15 +17,15 @@ const enableScroll = (offsetY: number) => { | |||
body.style.paddingRight = ''; | |||
body.style.top = ''; | |||
body.classList.remove(CLASSNAME_SCROLLING_DISABLED); | |||
window.scrollTo(0, -offsetY); | |||
window.scrollTo(0, offsetY); |
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.
@crishpeen Let's discuss why there is here a negative value and what it caused. When used on Jobs.cz the Modal is rendered but not open. The useEffect
hook is called causing the scroll to zero even if there is an #
anchor in the URL and window.offsetY
set to 5878
. This scroll must be saved and used.
Same if the Modal is open -> closed -> open: the page must stay on the same offset.
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.
I am not saying my code is flawless, probably the opposite, but with your updates our demo scrolls to the top on modal close. So I think it is still broken.
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.
I am not saying that this is the fix. This modification works on Jobs.cz side but I did not test it on our demo. Just saved the work to consult with you.
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.
Can you please explain the negative value for offset?
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.
Now when I look into it, it looks like it shouldn't be negative.
You want to reset the position fixed (removing the class) and the body.top and then you want to scroll to the spot where you were before. So I think it shouldn't be negative.
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.
I have created and example with an anchor. Now I think I know why the value is negative.
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.
- Revert this after PR is ready.
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.
- Revert this after PR is ready
@@ -25,7 +25,7 @@ export const useScrollControl = (ref: MutableRefObject<HTMLDialogElement | null> | |||
if (isOpen) { | |||
disableScroll(); | |||
} else if (ref.current && !ref.current.open) { | |||
const offsetY = parseFloat(document.body.style.top || '0'); | |||
const offsetY = parseFloat(document.body.style.top || '0') || window.scrollY; |
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.
I am afraid that this solution is still not what we need in Jobs. 😞
We need to prevent the page from bouncing once the modal is closed. But we have to consider a scenario where the modal has not been opened yet – that case falls in to this branch of the condition. That's why when it comes to the native scroll to the anchor in Jobs, we face the problem that only on Firefox we scroll to the top of the page, because Chrome scrolls to the anchor after the execution of this script.
A suggestion for modification is as follows:
const offsetY = parseFloat(document.body.style.top || '0') || window.scrollY; | |
const enableScroll = (offsetY: number) => { | |
… | |
window.scrollTo(0, offsetY); | |
}; | |
const offsetY = window.scrollY ? window.scrollY : Math.max(-1 * parseFloat(document.body.style.top) || 0, 0); |
- When we open the modal, setting
position: fixed;
tobody
resetswindow.scrollY
to0
. Hence, we need to ask for the value stored intop
, which has saved the size of the page's offset scroll. This value corresponds to a downward shift of the page, so it is negative and we need to convert it to positive by multiplying by -1 to get they
page coordinate where we want to scroll. - If the modal is not open, the
top
property is not defined, so we need to look atwindow.scrollY
if there was a scroll on the page to prevent an unwanted scrolling toy: 0
. Math.max
is used to avoid the value of the variable-0
.
FYI: The code suggestion has been tested on Jobs and it looks like the scrolling and open/close modal works as it should. 🎉
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.
@crishpeen @literat I know that this solution is not ideal, but I have no idea how else to fix different native implementation of the anchor scroll. What do you think? Can we apply this suggestion or do you see any side effects or improvement ideas?
Closing in favor of #1230 |
Description
Additional context
Issue reference
Before submitting the PR, please make sure you do the following
fixes #123
).