Skip to content

Instantly share code, notes, and snippets.

@colinhunt
Forked from anonymous/BootstrapLayout.elm
Created April 16, 2017 17:08
Show Gist options
  • Save colinhunt/69efd479ac13c3b8e2cbf34367ddad79 to your computer and use it in GitHub Desktop.
Save colinhunt/69efd479ac13c3b8e2cbf34367ddad79 to your computer and use it in GitHub Desktop.
elm-bootstrap-layout
module BootstrapLayout exposing (main)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Navigation exposing (Location)
import UrlParser exposing ((</>))
import Bootstrap.Navbar as Navbar
import Bootstrap.Grid as Grid
import Bootstrap.Grid.Col as Col
import Bootstrap.Card as Card
import Bootstrap.Button as Button
import Bootstrap.ListGroup as Listgroup
import Bootstrap.Modal as Modal
main : Program Never Model Msg
main =
Navigation.program UrlChange
{ view = view
, update = update
, subscriptions = subscriptions
, init = init
}
type alias Model =
{ page : Page
, navState : Navbar.State
, modalState : Modal.State
}
type Page
= Home
| GettingStarted
| Modules
| NotFound
init : Location -> ( Model, Cmd Msg )
init location =
let
( navState, navCmd ) =
Navbar.initialState NavMsg
( model, urlCmd ) =
urlUpdate location { navState = navState, page = Home, modalState = Modal.hiddenState }
in
( model, Cmd.batch [ urlCmd, navCmd ] )
type Msg
= UrlChange Location
| NavMsg Navbar.State
| ModalMsg Modal.State
subscriptions : Model -> Sub Msg
subscriptions model =
Navbar.subscriptions model.navState NavMsg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UrlChange location ->
urlUpdate location model
NavMsg state ->
( { model | navState = state }
, Cmd.none
)
ModalMsg state ->
( { model | modalState = state }
, Cmd.none
)
urlUpdate : Navigation.Location -> Model -> ( Model, Cmd Msg )
urlUpdate location model =
case decode location of
Nothing ->
( { model | page = NotFound }, Cmd.none )
Just route ->
( { model | page = route }, Cmd.none )
decode : Location -> Maybe Page
decode location =
UrlParser.parseHash routeParser location
routeParser : UrlParser.Parser (Page -> a) a
routeParser =
UrlParser.oneOf
[ UrlParser.map Home UrlParser.top
, UrlParser.map GettingStarted (UrlParser.s "getting-started")
, UrlParser.map Modules (UrlParser.s "modules")
]
view : Model -> Html Msg
view model =
div []
[ menu model
, mainContent model
, modal model
]
menu : Model -> Html Msg
menu model =
Navbar.config NavMsg
|> Navbar.withAnimation
|> Navbar.container
|> Navbar.brand [ href "#" ] [ text "Elm Bootstrap" ]
|> Navbar.items
[ Navbar.itemLink [ href "#getting-started" ] [ text "Getting started" ]
, Navbar.itemLink [ href "#modules" ] [ text "Modules" ]
]
|> Navbar.view model.navState
mainContent : Model -> Html Msg
mainContent model =
Grid.container [] <|
case model.page of
Home ->
pageHome model
GettingStarted ->
pageGettingStarted model
Modules ->
pageModules model
NotFound ->
pageNotFound
pageHome : Model -> List (Html Msg)
pageHome model =
[ h1 [] [ text "Home" ]
, Grid.row []
[ Grid.col []
[ Card.config [ Card.outlinePrimary ]
|> Card.headerH4 [] [ text "Getting started" ]
|> Card.block []
[ Card.text [] [ text "Getting started is real easy. Just click the start button." ]
, Card.custom <|
Button.linkButton
[ Button.primary, Button.attrs [ href "#getting-started" ] ]
[ text "Start" ]
]
|> Card.view
]
, Grid.col []
[ Card.config [ Card.outlineDanger ]
|> Card.headerH4 [] [ text "Modules" ]
|> Card.block []
[ Card.text [] [ text "Check out the modules overview" ]
, Card.custom <|
Button.linkButton
[ Button.primary, Button.attrs [ href "#modules" ] ]
[ text "Module" ]
]
|> Card.view
]
]
]
pageGettingStarted : Model -> List (Html Msg)
pageGettingStarted model =
[ h2 [] [ text "Getting started" ]
, Button.button
[ Button.success
, Button.large
, Button.block
, Button.attrs [ onClick <| ModalMsg Modal.visibleState ]
]
[ text "Click me" ]
]
pageModules : Model -> List (Html Msg)
pageModules model =
[ h1 [] [ text "Modules" ]
, Listgroup.ul
[ Listgroup.li [] [ text "Alert" ]
, Listgroup.li [] [ text "Badge" ]
, Listgroup.li [] [ text "Card" ]
]
]
pageNotFound : List (Html Msg)
pageNotFound =
[ h1 [] [ text "Not found" ]
, text "SOrry couldn't find that page"
]
modal : Model -> Html Msg
modal model =
Modal.config ModalMsg
|> Modal.small
|> Modal.h4 [] [ text "Getting started ?" ]
|> Modal.body []
[ Grid.containerFluid []
[ Grid.row []
[ Grid.col
[ Col.xs6 ]
[ text "Col 1" ]
, Grid.col
[ Col.xs6 ]
[ text "Col 2" ]
]
]
]
|> Modal.view model.modalState
{
"version": "1.0.0",
"summary": "A small sample app to illustrate a simple page layout.",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 5.1.1",
"elm-lang/html": "2.0.0 <= v < 2.0.0",
"rundis/elm-bootstrap": "1.0.0 <= v < 1.0.0",
"elm-lang/navigation": "2.1.0 <= v < 2.1.0",
"evancz/url-parser": "2.0.1 <= v < 2.0.1"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf8" />
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>
<main></main>
<script>
var main = document.querySelector("main")
var app = Elm.BootstrapLayout.embed(main)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment