dtwrks/elm-book/1.5.1

ElmBook.StatefulOptions

Attributes used by ElmBook.withStatefulOptions.

The attributes below are mostly used for Stateful Books. Take a look at the "Stateful Chapters" guide for more details.

initialState : state -> StatefulOptions state -> StatefulOptions state

Add an initial state to your book.

type alias SharedState =
    { value : String }




book : Book SharedState
book "MyApp"
    |> withStatefulOptions [
        initialState { value = "" }
    ]
(Value -> Key -> state)
-> StatefulOptions state
-> StatefulOptions state

Improved version of initialState.

Receives your application's flags as JSON encode value and the Browser.Navigation.Key as inputs to your custom state init function.

type alias SharedState =
    { flags : Json.Encode.Value
    , navKey : Browser.Navigation.Key
    }




book : Book SharedState
book "MyApp"
    |> withStatefulOptions [
        initialStateExtra (\flags navKey ->
            { flags = flags
            , navKey = navKey
            }
        )
    ]
List (Sub (Msg state))
-> StatefulOptions state
-> StatefulOptions state

Add subscriptions to your book.

import ElmBook.Actions exposing (updateState)


type alias SharedState =
    { value : String
    , timestamp : Posix
    }




updateAnimationState : Posix -> SharedState -> SharedState
updateAnimationState posix state =
    { state | timestamp = posix }




book : Book SharedState
book "MyApp"
    |> withStatefulOptions [
        subscriptions [
            Browser.Events.onAnimationFrame
                (updateStateWith updateAnimationState)
        ]
    ]
(state -> Sub (Msg state))
-> StatefulOptions state
-> StatefulOptions state

Improved version of subscriptions.

Receives your custom state as an input to your subscriptions.

(Bool -> state -> state)
-> StatefulOptions state
-> StatefulOptions state

Change your book's state based on the themes current mode.

This can be useful for showcasing your own dark themed components when using elm-book's dark mode.

type alias SharedState =
    { darkMode : Bool
    }


initialState : SharedState
initialState =
    { darkMode = False
    }


book : Book SharedState
book "MyApp"
    |> withStatefulOptions
        [ ElmBook.StatefulOptions.initialState
            initialState
        , ElmBook.StatefulOptions.onDarkModeChange
            (\darkMode state ->
                { state | darkMode = darkMode }
            )
        ]

Types

type alias Attribute state =
StatefulOptions state -> StatefulOptions state