Orasund/elm-layout/2.1.0

NOTE: the latest version is 3.0.0

Layout

write HTML like elm-ui

Html elements

Layouting

none : Html msg
none =
    Html.text ""
container : List (Attribute msg) -> Html msg -> Html msg

Fills the entire screen. Should be the outer most element.

container attrs =
    el
        ([ Html.Attributes.style "width" "100%"
         , Html.Attributes.style "height" "100%"
         , Html.Attributes.style "position" "absolute"
         , Html.Attributes.style "left" "0px"
         , Html.Attributes.style "top" "0px"
         ]
            ++ attrs
        )
el : List (Attribute msg) -> Html msg -> Html msg
el attrs content =
    Html.div
        (Html.Attributes.style "display" "flex"
            :: attrs
        )
        [ content ]
column : List (Attribute msg) -> List (Html msg) -> Html msg
column attrs =
    Html.div
        ([ Html.Attributes.style "display" "flex"
         , Html.Attributes.style "flex-direction" "column"
         ]
            ++ attrs
        )
row : List (Attribute msg) -> List (Html msg) -> Html msg
row attrs =
    Html.div
        ([ Html.Attributes.style "display" "flex"
         , Html.Attributes.style "flex-direction" "row"
         , Html.Attributes.style "flex-wrap" "wrap"
         ]
            ++ attrs
        )

Will wrap into the next line if there is not enough space.

If you don't want this behavior, you can just add noWrap:

unwrappedRow attrs =
    Layout.row (Layout.noWrap ++ attrs)
List (Attribute msg)
-> List (List (Attribute msg) -> Html msg)
-> Html msg
-> Html msg

A stack places the content on top of each other. The first element is the lowest.

withStack : List (Attribute msg) -> List (List (Attribute msg) -> Html msg) -> Html msg -> Html msg
withStack attrs list base =
    list
        |> List.map
            (\fun ->
                fun [ Html.Attributes.style "position" "absolute" ]
            )
        |> (::) base
        |> Html.div
            ([ Html.Attributes.style "display" "flex"
             , Html.Attributes.style "position" "relative"
             ]
                ++ attrs
            )
List (Attribute msg)
-> List ( List (Attribute msg), Html msg )
-> Html msg
orderedList attrs content =
    Html.ol
        (Html.Attributes.style "display" "flex" :: attrs)
        (content
            |> List.map (\( a, h ) -> Html.li a [ h ])
        )
List (Attribute msg)
-> List ( List (Attribute msg), Html msg )
-> Html msg
unorderedList attrs content =
    Html.ul
        (Html.Attributes.style "display" "flex" :: attrs)
        (content
            |> List.map (\( a, h ) -> Html.li a [ h ])
        )
List (Attribute msg)
-> List
{ description : ( List (Attribute msg), Html msg )
, item : ( List (Attribute msg), Html msg )
}
-> Html msg
descriptionList attrs content =
    Html.dl
        (Html.Attributes.style "display" "flex" :: attrs)
        (content
            |> List.concatMap
                (\{ description, item } ->
                    [ description |> (\( a, h ) -> Html.dt a [ h ])
                    , item |> (\( a, h ) -> Html.dd a [ h ])
                    ]
                )
        )

Elements

heading1 : List (Attribute msg) -> Html msg -> Html msg
heading1 attrs content =
    Html.h1
        (Html.Attributes.style "display" "flex" :: attrs)
        [ content ]
heading2 : List (Attribute msg) -> Html msg -> Html msg
heading2 attrs content =
    Html.h2
        (Html.Attributes.style "display" "flex" :: attrs)
        [ content ]
heading3 : List (Attribute msg) -> Html msg -> Html msg
heading3 attrs content =
    Html.h3
        (Html.Attributes.style "display" "flex" :: attrs)
        [ content ]
heading4 : List (Attribute msg) -> Html msg -> Html msg
heading4 attrs content =
    Html.h4
        (Html.Attributes.style "display" "flex" :: attrs)
        [ content ]
heading5 : List (Attribute msg) -> Html msg -> Html msg
heading5 attrs content =
    Html.h5
        (Html.Attributes.style "display" "flex" :: attrs)
        [ content ]
heading6 : List (Attribute msg) -> Html msg -> Html msg
heading6 attrs content =
    Html.h6
        (Html.Attributes.style "display" "flex" :: attrs)
        [ content ]
text : List (Attribute msg) -> String -> Html msg
text : List (Attribute msg) -> String -> Html msg
text attrs content =
    el attrs (Html.text content)
List (Attribute msg)
-> { src : String
, description : String
}
-> Html msg
image attrs args =
    Html.img
        ([ Html.Attributes.src args.src
         , Html.Attributes.alt args.description
         ]
            ++ attrs
        )
        []
linkTo : List (Attribute msg) -> String -> Html msg -> Html msg
linkTo attrs link content =
    Html.a
        (Html.Attributes.href link
            :: attrs
        )
        [ content ]
linkToNewTab : List (Attribute msg) -> String -> Html msg -> Html msg
linkToNewTab attrs link content =
    Html.a
        ([ Html.Attributes.href link
         , Html.Attributes.target "_blank"
         ]
            ++ attrs
        )
        [ content ]
List (Attribute msg)
-> { onPress : Maybe msg
, label : String
}
-> Html msg
textButton attrs args =
    Html.button (asButton args ++ attrs)
        [ Html.text args.label ]
List (Attribute msg)
-> { onPress : Maybe msg
, label : String
}
-> Html msg
-> Html msg
button args attrs content =
    Html.button (asEl :: asButton args ++ attrs)
        [ content ]
List (Attribute msg)
-> { onChange : Bool -> msg
, checked : Bool
, label : String
}
-> Html msg
radio attrs args =
    Html.input
        ([ Html.Attributes.type_ "radio"
         , Html.Attributes.checked args.checked
         , Html.Attributes.attribute "aria-label" args.label
         ]
            ++ attrs
        )
        []
paragraph : List (Attribute msg) -> Html msg -> Html msg
paragraph attrs content =
    Html.p
        (Html.Attributes.style "display" "flex" :: attrs)
        [ content ]
lineBreak : Html msg
lineBreak =
    Html.br [] []
horizontalRuler : List (Attribute msg) -> Html msg
horizontalRuler attrs =
    Html.hr attrs []

Attributes

centered : List (Attribute msg)
centered =
    [ contentCentered, alignAtCenter ]
noWrap : Attribute msg
noWrap =
    Html.Attributes.style "flex-wrap" "nowrap"
wrap : Attribute msg
wrap =
    Html.Attributes.style "flex-wrap" "wrap"
contentAtStart : Attribute msg
contentAtStart =
    Html.Attributes.style "justify-content" "flex-start"
contentAtEnd : Attribute msg
contentAtEnd =
    Html.Attributes.style "justify-content" "flex-end"
contentCentered : Attribute msg
contentCentered =
    Html.Attributes.style "justify-content" "center"
contentWithSpaceBetween : Attribute msg
contentWithSpaceBetween =
    Html.Attributes.style "justify-content" "space-between"
alignAtStart : Attribute msg
alignAtStart =
    Html.Attributes.style "align-items" "flex-start"
alignAtEnd : Attribute msg
alignAtEnd =
    Html.Attributes.style "align-items" "flex-end"
alignAtBaseline : Attribute msg
alignAtBaseline =
    Html.Attributes.style "align-items" "baseline"
alignAtCenter : Attribute msg
alignAtCenter =
    Html.Attributes.style "align-items" "center"
alignStretched : Attribute msg
alignStretched =
    Html.Attributes.style "align-items" "stretch"
gap : Float -> Attribute msg
gap n =
    Html.Attributes.style "gap" (String.fromFloat n ++ "px")

Size

fill : Attribute msg
fill =
    fillPortion 1
fillPortion : Int -> Attribute msg
fillPortion n =
    Html.Attributes.style "flex" (String.fromInt n)

Sticky

sticky : List (Attribute msg)
sticky =
    [ Html.Attributes.style "position" "sticky"
    , Html.Attributes.style "z-index" "99999"
    ]
stickyOnBottom : List (Attribute msg)
stickyOnBottom =
    [ Html.Attributes.style "position" "sticky"
    , Html.Attributes.style "bottom" "0"
    , Html.Attributes.style "z-index" "99999"
    ]
stickyOnTop : List (Attribute msg)
stickyOnTop =
    [ Html.Attributes.style "position" "sticky"
    , Html.Attributes.style "top" "0"
    , Html.Attributes.style "z-index" "99999"
    ]

Role

asEl : Attribute msg
asEl =
    Html.Attributes.style "display" "flex"
{ onPress : Maybe msg
, label : String
}
-> List (Attribute msg)

Turns anything into a button

asButton args =
    [ Html.Attributes.style "cursor" "pointer"
    , Html.Attributes.attribute "aria-label" args.label
    , Html.Attributes.attribute "role" "button"
    ]
        ++ (args.onPress
                |> Maybe.map (\msg -> [ Html.Events.onClick msg ])
                |> Maybe.withDefault []
           )