This little program might help illustrate what I’m trying to do:
module Main exposing (…)
import Browser
import Browser.Dom exposing (Element, Error)
import Html exposing (br, button, div, text)
import Html.Attributes exposing (id, style)
import Html.Events exposing (onClick)
import Task
– MAIN
main : Program () MyApp Msg
main =
Browser.element
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
– MODEL
type alias MyApp =
{ names : String, width1 : Float, width2 : Float, width3 : Float }
init : () → ( MyApp, Cmd Msg )
init _ =
( { names = “short text”, width1 = 0, width2 = 0, width3 = 0 }
, Browser.Dom.getElement “stop” |> Task.attempt GotElement
)
– UPDATE
type Msg
= SetText String
| GotElement (Result Error Element)
update msg model =
case msg of
SetText n →
( { model | names = n }
, Browser.Dom.getElement “stop” |> Task.attempt GotElement
)
GotElement (Err err) ->
( model, Cmd.none )
GotElement (Ok element) ->
( { model
| width1 = element.scene.width
, width2 = element.element.width
, width3 = element.viewport.width
}
, Cmd.none
)
– VIEW
view s =
div
[ id “stop”
, style “width” “100px”
, style “overflow” “hidden”
, style
“white-space”
“nowrap”
]
[ text s.names
, br
, text (String.fromFloat s.width1)
, br
, text (String.fromFloat s.width2)
, br
, text (String.fromFloat s.width3)
, br
, button [ onClick (SetText “Some much longer text”) ] [ text “update” ]
]
– SUBSCRIPTIONS
subscriptions _ =
Sub.none
None of the values that are shown match the scrollWidth as reported when inspecting the “stop” element in JavaScript.