Skip to content

Instantly share code, notes, and snippets.

@knewter
Created August 9, 2016 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knewter/c4235c873722a18fc13e63a9e822b3a6 to your computer and use it in GitHub Desktop.
Save knewter/c4235c873722a18fc13e63a9e822b3a6 to your computer and use it in GitHub Desktop.
Introduction to elm-mdl - 2
{- This file re-implements the Elm Counter example (1 counter) with elm-mdl
buttons. Use this as a starting point for using elm-mdl components in your own
app.
-}
module Main exposing (..)
import Html.App as App
import Html exposing (..)
import Html.Attributes exposing (href, class, style)
import Material
import Material.Scheme
import Material.Button as Button
import Material.Options exposing (css)
-- MODEL
-- You have to add a field to your model where you track the `Material.Model`.
-- This is referred to as the "model container"
type alias Model =
{ count : Int
, mdl :
Material.Model
-- Boilerplate: model store for any and all Mdl components you use.
}
-- `Material.model` provides the initial model
model : Model
model =
{ count = 0
, mdl =
Material.model
-- Boilerplate: Always use this initial Mdl model store.
}
-- ACTION, UPDATE
-- You need to tag `Msg` that are coming from `Mdl` so you can dispatch them
-- appropriately.
type Msg
= Increase
| Reset
| Mdl (Material.Msg Msg)
-- Boilerplate: Msg clause for internal Mdl messages.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Increase ->
( { model | count = model.count + 1 }
, Cmd.none
)
Reset ->
( { model | count = 0 }
, Cmd.none
)
-- When the `Mdl` messages come through, update appropriately.
Mdl msg' ->
Material.update msg' model
-- VIEW
type alias Mdl =
Material.Model
view : Model -> Html Msg
view model =
div
[ style [ ( "padding", "2rem" ) ] ]
[ text ("Current count: " ++ toString model.count)
{- We construct the instances of the Button component that we need, one
for the increase button, one for the reset button. First, the increase
button. The first three arguments are:
- A Msg constructor (`Mdl`), lifting Mdl messages to the Msg type.
- An instance id (the `[0]`). Every component that uses the same model
collection (model.mdl in this file) must have a distinct instance id.
- A reference to the elm-mdl model collection (`model.mdl`).
Notice that we do not have to add fields for the increase and reset buttons
separately to our model; and we did not have to add to our update messages
to handle their internal events.
Mdl components are configured with `Options`, similar to `Html.Attributes`.
The `Button.onClick Increase` option instructs the button to send the `Increase`
message when clicked. The `css ...` option adds CSS styling to the button.
See `Material.Options` for details on options.
-}
, Button.render Mdl
[ 0 ]
model.mdl
[ Button.onClick Increase
, css "margin" "0 24px"
]
[ text "Increase" ]
, Button.render Mdl
[ 1 ]
model.mdl
[ Button.onClick Reset ]
[ text "Reset" ]
]
|> Material.Scheme.top
-- Load Google Mdl CSS. You'll likely want to do that not in code as we
-- do here, but rather in your master .html file. See the documentation
-- for the `Material` module for details.
main : Program Never
main =
App.program
{ init = ( model, Cmd.none )
, view = view
-- Here we've added no subscriptions, but we'll need to use the `Mdl` subscriptions for some components later.
, subscriptions = always Sub.none
, update = update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment