Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize the model using Window.dimensions in Elm?

Tags:

dom

elm

In a sliding puzzle game, I'd like set the initial tile size based on the initial window dimensions (to maximize the screen real estate).

enter image description here

In other words, I'd like to set initialModel based on the the initial value of Window.dimensions.

I couldn't find how to do this, and ended up using ports to get the initial window dimensions:

index.html

Elm.fullscreen(Elm.App, {
  windowSize: [
    document.documentElement.clientWidth,
    document.documentElement.clientHeight
  ]
});

App.elm

port windowSize : (Int, Int)

initialModel =
  -- some function of windowSize

model =
  Signal.foldp update initialModel input

type Action
  = WindowResize (Int, Int)
  | ...

windowResize =
  Signal.map WindowResize Window.dimensions

update action model =
  case action of
    WindowResize dimensions ->
      { model | some change based on dimensions }
  ...

Is there a way to achieve the same result without using ports?

like image 415
Misha Moroshko Avatar asked Oct 18 '22 22:10

Misha Moroshko


1 Answers

You can use Signal.Extra.foldp' from the Apanatshka/elm-signal-extra package to inspect base the initial value of the model on the initial value of the input signal.

Full disclosure: I'm the author of that package.

like image 63
Apanatshka Avatar answered Nov 01 '22 10:11

Apanatshka