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

Refactor the way scroll is saved and restored to avoid premature restoration #47

Merged

Conversation

Intrepidd
Copy link
Contributor

I realised #40 had a side effect where scroll would jump to 0 before each visit.

This seems to be because we listen on turbo:click to set the scroll position to 0 and we restore it in a turbo:before-visit event.

This looked a bit weird to me, I tried coming up with my own implementation that worked for me in my use cases, but this may be naive and I might be missing some crucial context.

Basically, I chose to store the scroll position only just before a reload is requested, and to restore and reset after a load.
This ensures we always save the position at the right time, negating the need for debounced scroll position etc.

@mikker
Copy link
Contributor

mikker commented Nov 20, 2023

Sounds reasonable and very nice to remove the flaky debounce stuff 👏

@elik-ru
Copy link

elik-ru commented Nov 22, 2023

I would delete stored position right after it was restored.
This will ensure it won't try scroll on next visits.

@Intrepidd
Copy link
Contributor Author

I would delete stored position right after it was restored.

This is what I'm doing here, isn't it ?

@Intrepidd
Copy link
Contributor Author

Or do you mean actually destroying it rather than setting it to zero ?

@elik-ru
Copy link

elik-ru commented Nov 22, 2023

Or do you mean actually destroying it rather than setting it to zero ?

Yes, I mean completely destroy it.

When restoring:

  1. if there is no previously stored position - don't touch scroll at all.
  2. Otherwise scroll to stored position
  3. Destroy stored value.

@kirillplatonov
Copy link
Owner

Looks like a nice simplification! And I agree with @elik-ru suggestion, skipping scroll restoration when there's no stored position is better.

@Intrepidd
Copy link
Contributor Author

Makes a lot of sense! Just pushed a commit in this direction.

Intrepidd and others added 2 commits November 28, 2023 10:15
Co-authored-by: Kirill Platonov <kirill@platmart.io>
Co-authored-by: Kirill Platonov <kirill@platmart.io>
@kirillplatonov
Copy link
Owner

Thanks @Intrepidd 👏

@kirillplatonov kirillplatonov merged commit 3983133 into kirillplatonov:main Nov 29, 2023
@Intrepidd
Copy link
Contributor Author

@kirillplatonov any way we can get a release pushed ? Thanks !

@kirillplatonov
Copy link
Owner

Published v1.3.1

@mikker
Copy link
Contributor

mikker commented Jan 4, 2024

This isn't working for me anymore. Anyone else having the same experience?

@Intrepidd
Copy link
Contributor Author

This isn't working for me anymore. Anyone else having the same experience?

What are you experiencing ? No issues on my end and no more intempestive scroll reset.

@mikker
Copy link
Contributor

mikker commented Jan 11, 2024

It doesn't seem to save the scroll position when scrolling. If I set it manually, it does restore correctly though.
Edit Huh, now it's working. Will investigate a bit.

@mikker
Copy link
Contributor

mikker commented Jan 11, 2024

I see the problem now - it's because Tailwind builds a new build when I save the view. So it goes

  1. Save index.html.slim
  2. Livereload sees the change, saves scroll, fires a reload
  3. Page loads but doesn't make it to the scroll in time for…
  4. Tailwind builds application.css
  5. Livereload sees the change, fires a reload
  6. Page loads but scroll wasn't saved

Not sure how to fix this yet. Any thoughts?

@Intrepidd
Copy link
Contributor Author

Ah yes ! Actually I have this in my config since I started using this gem :

# Only rely on the builds folder for reloading to avoid double reloads
config.hotwire_livereload.listen_paths = [Rails.root.join("app/assets/builds")]

I rely on only one the builds folder, which is updated when my view changes, through tailwind :)

@elik-ru
Copy link

elik-ru commented Jan 11, 2024

I see the problem now - it's because Tailwind builds a new build when I save the view. So it goes

  1. Save index.html.slim
  2. Tailwind builds application.css

Not sure how to fix this yet. Any thoughts?
I would say that changing just view should not rebuild css. Probably something can be adjusted here?

@Intrepidd
Copy link
Contributor Author

I would say that changing just view should not rebuild css. Probably something can be adjusted here?

This is the way tailwind works, it listens to changes in your HTML / rb files to detect tailwind classes used so it can generate its CSS with only the classes you use.

@mikker
Copy link
Contributor

mikker commented Jan 11, 2024

Only listening to Tailwind, of course! That'll fix it. Might be a helpful thing to add as a note to the Readme?

@kirillplatonov
Copy link
Owner

Ah yes ! Actually I have this in my config since I started using this gem :

# Only rely on the builds folder for reloading to avoid double reloads
config.hotwire_livereload.listen_paths = [Rails.root.join("app/assets/builds")]

I rely on only one the builds folder, which is updated when my view changes, through tailwind :)

This looks like a great solution for Tailwind users! Worth mentioning in the Readme.

@kirillplatonov
Copy link
Owner

Fully overriding default listen paths is probably not necessary. In #52 I added ability to skip only few default folders that are conflicting with Tailwind:

  # ...
  config.hotwire_livereload.skip_listen_paths << Rails.root.join("app/helpers")
  config.hotwire_livereload.skip_listen_paths << Rails.root.join("app/javascript")
  config.hotwire_livereload.skip_listen_paths << Rails.root.join("app/views")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants