-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[59769] When adding new relations, auto-scroll to show the newly added relation #17355
[59769] When adding new relations, auto-scroll to show the newly added relation #17355
Conversation
8f2522a
to
6c71aed
Compare
34e143b
to
8775574
Compare
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.
Please have a look at my remarks.
frontend/src/stimulus/controllers/dynamic/work-packages/relations-tab/scroll.controller.ts
Outdated
Show resolved
Hide resolved
app/components/work_package_relations_tab/index_component.html.erb
Outdated
Show resolved
Hide resolved
|
||
connect() { | ||
// Wait to ensure DOM is fully loaded | ||
setTimeout(() => { |
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.
setTimeout
does not automatically ensure you that the DOM is fully loaded afaik, there might still be some delays that could prevent that could from being executed.
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 left some comments below
} | ||
|
||
waitForRenderAndAct() { | ||
const element = document.querySelector('[data-scroll-to="true"]'); |
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'd recommend to use a stimulus target instead of a normal data attribute. The value is crutial for this controller to work, so it should be an explicit target imho.
if (element) { | ||
element.scrollIntoView({ behavior: 'smooth', block: 'center' }); | ||
} else { | ||
requestAnimationFrame(this.waitForRenderAndAct.bind(this)); |
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.
This creates like an infinte loop when there is no scroll target (e.g. on a fresh page reload). The code is just executed endlessly.
Further, requestAnimationFrame
is just meant for animations and will execute the code 60 times per second (assuming 60hertz screen), because that makes sense for animations. But you don't have an animation here.
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.
@bsatarnejad I tried looking into it as you requested. The best I could do is by using a MutationObserver instead of requestAnimationFrame
so that it tries to scroll only when things change. Code is on branch 59769-when-adding-new-relations-auto-scroll-to-show-the-newly-added-relation-wip-scbl
.
It can probably be optimized further by limiting where it's listening, instead of listening to the whole body.
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.
Hi @cbliard , @HDinger
@ulferts and I investigated this issue more. The problem arises because a new row is added to the DOM, but it takes additional time to fully render and become visible in the view. As a result, we can't scroll to it immediately and need to use a timeout to give the Turbo.renderStream method enough time to render the new row.
Jonas encountered a similar issue in the activities tab: https://github.com/opf/openproject/blob/dev/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts#L213-L224
Ticket
https://community.openproject.org/projects/stream-planning-and-reporting/work_packages/59769/activity
Screen recording
Screen.Recording.2024-12-04.at.16.44.53.mov
What are you trying to accomplish?
Scroll to the currently created relation in relations tab.
What approach did you choose and why?
After creating a relation, pass the relation-wp-id to the component for initializing it.
Merge checklist