aforemny/material-components-web-elm/2.0.1

NOTE: the latest version is 9.1.0

Material.Card

Cards contain content and actions about a single subject.

Table of Contents

Resources

Basic Usage

import Html exposing (Html, text)
import Material.Button exposing (buttonConfig)
import Material.Card
    exposing
        ( card
        , cardActionButton
        , cardActionIcon
        , cardActions
        , cardBlock
        , cardConfig
        )
import Material.IconButton exposing (iconButtonConfig)


main =
    card cardConfig
        { blocks =
            [ cardBlock <|
                Html.div []
                    [ Html.h2 [] [ text "Title" ]
                    , Html.h3 [] [ text "Subtitle" ]
                    ]
            , cardBlock <|
                Html.div []
                    [ Html.p [] [ text "Lorem ipsum…" ] ]
            ]
        , actions =
            Just <|
                cardActions
                    { buttons =
                        [ cardActionButton buttonConfig
                            "Visit"
                        ]
                    , icons =
                        [ cardActionIcon iconButtonConfig
                            "favorite"
                        ]
                    }
        }

Card

card : CardConfig msg -> CardContent msg -> Html msg

Card view function

cardConfig : CardConfig msg

Card view function

type alias CardConfig msg =
{ outlined : Bool
, additionalAttributes : List (Attribute msg)
}

Configuration of a card

type alias CardContent msg =
{ blocks : List (CardBlock msg)
, actions : Maybe (CardActions msg)
}

The content of a card is comprised of blocks and actions.

Outlined Card

A card may have a border by settings its outlined configuration field to True.

card { cardConfig | outlined = True }
    { blocks =
        [ cardBlock <|
            Html.div [] [ Html.h1 [] [ text "Card" ] ]
        ]
    , actions = Nothing
    }

Card Blocks

A card's content is primary comprised of blocks. Blocks may be comprised of arbitrary HTML (generic cardBlock) or a media element (cardMedia). Optionally, a group of card blocks can be marked as the card's primary action which makes the group of card blocks interactable.

type CardBlock msg

Card's content block

Generic Block

Generic card blocks are the most common and allow you to specify card content using arbitrary HTML. Note that you will have to carefully adjust styling such as padding and typography yourself.

cardBlock <|
    Html.div []
        [ Html.h2 [] [ text "Title" ]
        , Html.h3 [] [ text "Subtitle" ]
        ]
cardBlock : Html msg -> CardBlock msg

An arbitrary card block

cardBlock <|
    Html.div [] [ text "Lorem ipsum…" ]

Media Block

Card may contain a media block usually as the first content block. The media will be displayed using a background image, and you may chose from square or a 16 to 9 aspect ratio.

cardMedia : CardMediaConfig msg -> String -> CardBlock msg

Card media block view function

cardMedia cardMediaConfig "media-image.jpg"
cardMediaConfig : CardMediaConfig msg

Default configuration of a card's media block

type alias CardMediaConfig msg =
{ aspect : Maybe CardMediaAspect
, additionalAttributes : List (Attribute msg)
}

Configuration of a card's media block

= Square
| SixteenToNine

Card media block's aspect ratio

Primary Action Block

A group of card blocks can be marked as the primary action of the card. A primary action block may be clicked upon and displays a visual interaction effect.

cardPrimaryAction
    { cardPrimaryActionConfig
        | onClick = Just CardClicked
    }
    [ cardBlock <|
        Html.h2 [] [ text "Title" ]
    , cardBlock <|
        Html.p [] [ text "Lorem ipsum…" ]
    ]
cardPrimaryActionConfig : PrimaryActionConfig msg

Default configuration of a card's primary action block

PrimaryActionConfig msg
-> List (CardBlock msg)
-> List (CardBlock msg)

A card's primary action block

cardPrimaryAction
    { cardPrimaryActionConfig
        | onClick = Just CardClicked
    }
    [ cardBlock <|
        Html.div []
            [ Html.h1 [] [ text "Title" ] ]
    , cardBlock <|
        Html.div []
            [ Html.p [] [ text "Lorem ipsum…" ] ]
    ]

Card Actions

Card actions are comprised of buttons and icons. These are exposed as variants to the standard buttons and icons, but they do share the same configuration.

cardActions
    { buttons =
        [ cardActionButton buttonConfig "View" ]
    , actions =
        [ cardActionIcon iconButtonConfig "favorite" ]
    }
{ buttons : List (Button msg)
, icons : List (Icon msg)
}
-> CardActions msg

Card actions

A card may contain as actions buttons as well as icons.

type CardActions msg

Card actions type

cardActionButton : ButtonConfig msg -> String -> Button msg

A card action button

cardActionButton buttonConfig "Visit"
cardActionIcon : IconButtonConfig msg -> String -> Icon msg

Card action icon

cardActionIcon iconButtonConfig "favorite"

Card Full Bleed Actions

While a card's action buttons are usually left-aligned, a special case exists when there is only a single button as card action.

cardFullBleedActions : Button msg -> CardActions msg

Card full bleed action

If a card's action is comprised of a single button, that button can be made full width by using cardFullBleedActions.

cardFullBleedActions
    (cardActionButton buttonConfig [ text "Visit" ])