Skip to content

Ema scrolls to the anchor in every DOM patch #88

@lua-vr

Description

@lua-vr

This is just for keeping track of the discussion in #86 (comment). (if the URL being displayed has an anchor, every time the page is patched the client would scroll to the anchor again).

Proposed fix (to the current, non-multisite ema) is:

 src/Ema/Server.hs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/Ema/Server.hs b/src/Ema/Server.hs
index cb98105..882f188 100644
--- a/src/Ema/Server.hs
+++ b/src/Ema/Server.hs
@@ -278,6 +278,8 @@ wsClientShim =
         function init(reconnecting) {
           // The route current DOM is displaying
           let routeVisible = document.location.pathname;
+          // If client should scroll to anchor on next DOM patch
+          let pendingHashChange = false;
 
           const verb = reconnecting ? "Reopening" : "Opening";
           console.log(`ema: $${verb} conn $${wsUrl} ...`);
@@ -300,6 +302,7 @@ wsClientShim =
           function switchRoute(path, hash = "") {
              console.log(`ema: → Switching to $${path + hash}`);
              window.history.pushState({}, "", path + hash);
+             if (hash) { pendingHashChange = true };
              sendObservePath(path);
           }
 
@@ -358,11 +361,12 @@ wsClientShim =
                 window.scrollTo({ top: 0});
                 routeVisible = document.location.pathname;
               }
-              if (window.location.hash) {
+              if (pendingHashChange && window.location.hash) {
                 var el = document.querySelector(window.location.hash);
                 if (el !== null) {
                   el.scrollIntoView({ behavior: 'smooth' });
                 }
+                pendingHashChange = false;
               }
               watchCurrentRoute();
             };
-- 
2.35.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions