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

Cannot scroll on swap with server-sent events #784

Open
ocharles opened this issue Jan 27, 2022 · 5 comments
Open

Cannot scroll on swap with server-sent events #784

ocharles opened this issue Jan 27, 2022 · 5 comments

Comments

@ocharles
Copy link

From what I can tell, it's not possible to add any scrolling on swap with server-sent events. I'd like to be able to say hx-swap="beforeend scroll:bottom" and have it just work, but looking at the source code:

        function processSSESwap(elt, sseEventName) {
            var sseSourceElt = getClosestMatch(elt, hasEventSource);
            if (sseSourceElt) {
                var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
                var sseListener = function (event) {
                    if (maybeCloseSSESource(sseSourceElt)) {
                        sseEventSource.removeEventListener(sseEventName, sseListener);
                        return;
                    }

                    ///////////////////////////
                    // TODO: merge this code with AJAX and WebSockets code in the future.

                    var response = event.data;
                    withExtensions(elt, function(extension){
                        response = extension.transformResponse(response, null, elt);
                    });

                    var swapSpec = getSwapSpecification(elt)
                    var target = getTarget(elt)
                    var settleInfo = makeSettleInfo(elt);

                    selectAndSwap(swapSpec.swapStyle, elt, target, response, settleInfo)
                    settleImmediately(settleInfo.tasks)
                    triggerEvent(elt, "htmx:sseMessage", event)
                };

                getInternalData(elt).sseListener = sseListener;
                sseEventSource.addEventListener(sseEventName, sseListener);
            } else {
                triggerErrorEvent(elt, "htmx:noSSESourceError");
            }
        }

I notice that while the swapSpec is parsed correctly, we only ever use swapSpec.swapStyle - completely ignoring swapSpec.scroll.

@ocharles
Copy link
Author

ocharles commented Jan 27, 2022

I think I can probably hack around this for now using the (undocumented) htmx:sseMessage event and adding some JavaScript to scroll to the bottom.

Edit: My workaround is:

<div @htmx:sse-message.camel="$el.scrollIntoView(false)">

@benpate
Copy link
Collaborator

benpate commented May 14, 2022

Yes. Agreed. Without duplicating a whole lot of code, this would require that html publish a workable swap() method for external scripts to use. So this depends on #255

@wimdeblauwe
Copy link
Contributor

Another workaround that does not require AlpineJS is this:

<div id="progress-log"
             class="mt-4 p-2 h-96 font-mono bg-gray-700 text-gray-100 overflow-y-auto"
             hx-swap="beforeend"
             sse-swap="message"
             hx-on::after-settle="this.scrollTo(0, this.scrollHeight);">

@danawoodman
Copy link

hate to plus one.... but plus one

cbiffle added a commit to cbiffle/htmx that referenced this issue Feb 16, 2024
Was pulling my hair out trying to get this working before I realized
that it's a known issue.
@aral
Copy link
Contributor

aral commented Feb 22, 2024

This also affects the WebSocket extension (basically anything that depends on hx-swap-oob)

#2308

And it also affects use of the idiomorph extension with WebSockets:

bigskysoftware/idiomorph#40

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

No branches or pull requests

5 participants