elm / virtual-dom

The foundation of HTML and SVG in Elm.
https://package.elm-lang.org/packages/elm/virtual-dom/latest
BSD 3-Clause "New" or "Revised" License
209 stars 80 forks source link

Html.Keyed.node does not preserve scroll positions across children #183

Open pravdomil opened 1 year ago

pravdomil commented 1 year ago

The __3_REORDER patch removes nodes from DOM and that causes scroll positions to reset.

I think that it can use only insertBefore function to preserve node state.

SSCCE

module Main exposing (..)

import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Keyed

main : Program () Bool ()
main =
    Browser.sandbox
        { init = False
        , view = view
        , update = always not
        }

view : Bool -> Html ()
view model =
    div []
        [ Html.Keyed.node "div"
            []
            ([ content "A"
             , content "B"
             ]
                |> (\x ->
                        if model then
                            List.reverse x

                        else
                            x
                   )
            )
        , button [ onClick () ] [ text "Swap" ]
        ]

content a =
    ( a
    , div
        [ id a
        , style "overflow" "auto"
        , style "height" "100px"
        , style "width" "100px"
        , style "padding" "10px"
        , style "border" "1px solid blue"
        ]
        [ text (String.repeat 5000 (a ++ " "))
        ]
    )

related to #178