mdgriffith/stylish-elephants/6.0.2

NOTE: this package is not compatible with Elm 0.19.1

Element

Basic Elements

type alias Element msg =
Element msg

The basic building block of your layout. Here we create a

import Element


view =
    Element.el [] (Element.text "Hello!")
type alias Attribute msg =
Attribute () msg

Standard attribute which cannot be a decoration.

none : Element msg

Nothing to see here!

text : String -> Element msg

Create some plain text.

text "Hello, you stylish developer!"

Note text does not wrap by default. In order to get text to wrap, check out paragraph!

el : List (Attribute msg) -> Element msg -> Element msg

The basic building block of your layout.

import Color exposing (blue, darkBlue)
import Element exposing (Element)
import Element.Background as Background
import Element.Border as Border


myElement : Element msg
myElement =
    Element.el
        [ Background.color blue
        , Border.color darkBlue
        ]
        (Element.text "You've made a stylish element!")

Rows and Columns

Rows and columns are the most common layouts.

row : List (Attribute msg) -> List (Element msg) -> Element msg

If you want a row of elements, use row!

column : List (Attribute msg) -> List (Element msg) -> Element msg

Text Layout

Text needs it's own layout primitives.

paragraph : List (Attribute msg) -> List (Element msg) -> Element msg

A paragraph will layout all children as wrapped, inline elements.

import Element
import Element.Font as Font


Element.paragraph []
    [ text "lots of text ...."
    , el [ Font.bold ] (text "this is bold")
    , text "lots of text ...."
    ]

This is really useful when you want to markup text by having some parts be bold, or some be links, or whatever you so desire.

Also, if a child element has alignLeft or alignRight, then it will be moved to that side and the text will flow around it, (ah yes, float behavior).

This makes it particularly easy to do something like a dropped capital.

import Element
import Element.Font as Font


Element.paragraph []
    [ el
        [ alignLeft
        , padding 5
        , Font.lineHeight 1
        ]
        (text "S")
    , text "lots of text ...."
    ]

Which will look something like

textColumn : List (Attribute msg) -> List (Element msg) -> Element msg

Now that we have a paragraph, we need someway to attach a bunch of paragraph's together.

To do that we can use a textColumn.

The main difference between a column and a textColumn is that textColumn will flow the text around elements that have alignRight or alignLeft, just like we just saw with paragraph.

In the following example, we have a textColumn where one child has alignLeft.

Element.textColumn [ spacing 10, padding 10 ]
    [ paragraph [] [ text "lots of text ...." ]
    , el [ alignLeft ] none
    , paragraph [] [ text "lots of text ...." ]
    ]

Which will result in something like:

Data Table

type alias Table records msg =
{ data : List records
, columns : List (Column records msg)
}
type alias Column record msg =
{ header : Element msg
, view : record -> Element msg
}
table : List (Attribute msg) -> Table data msg -> Element msg

Show some tabular data.

Start with a list of records and specify how each column should be rendered.

So, if we have a list of persons:

type alias Person =
    { firstName : String
    , lastName : String
    }


persons : List Person
persons =
    [ { firstName = "David"
      , lastName = "Bowie"
      }
    , { firstName = "Florence"
      , lastName = "Welch"
      }
    ]

We could render it using

Element.table []
    { data = persons
    , columns =
        [ { header = Element.text "First Name"
          , view =
                (\person ->
                    Element.text person.firstName
                )
          }
        , { header = Element.text "Last Name"
          , view =
                 (\person ->
                    Element.text person.lastName
                 )
          }
        ]
    }

Note: Sometimes you might not have a list of records directly in your model. In this case it can be really nice to write a function that transforms some part of your model into a list of records before feeding it into Element.table.

type alias IndexedTable records msg =
{ data : List records
, columns : List (IndexedColumn records msg)
}
type alias IndexedColumn record msg =
{ header : Element msg
, view : Int -> record -> Element msg
}
indexedTable : List (Attribute msg) -> IndexedTable data msg -> Element msg

Same as Element.table except the view for each column will also receive the row index as well as the record.

Rendering

layout : List (Attribute msg) -> Element msg -> Html msg

This is your top level node where you can turn Element into Html.

{ options : List Option }
-> List (Attribute msg)
-> Element msg
-> Html msg
type alias Option =
Option

Style elements embeds two StyleSheets, one that is constant, and one that changes dynamically based on styles collected from the elments being rendered.

This option will stop the static/constant stylesheet from rendering.

Make sure to render the constant/static stylesheet at least once on your page!

forceHover : Option

Any hover styles, aka attributes with mouseOver in the name, will be always turned on.

This is useful for when you're targeting a platform that has no mouse, such as mobile.

noHover : Option

Disable all mouseOver styles.

focusStyle : FocusStyle -> Option
type alias FocusStyle =
{ borderColor : Maybe Color
, backgroundColor : Maybe Color
, shadow :
Maybe
{ color : Color
, offset : ( Int, Int )
, blur : Int
, size : Int
}
}

Links and Images

download : List (Attribute msg) -> Link msg -> Element msg

A link to download a file.

List (Attribute msg)
-> { label : Element msg
, filename : String
, url : String
}
-> Element msg

A link to download a file, but you can specify the filename.

List (Attribute msg)
-> { src : String
, description : String
}
-> Element msg

Both a source and a description are required for images. The description is used to describe the image to screen readers.

decorativeImage : List (Attribute msg) -> { src : String } -> Element msg

If an image is purely decorative, you can skip the caption.

Attributes

type alias Attribute msg =
Attribute () msg

Standard attribute which cannot be a decoration.

transparent : Bool -> Attr decorative msg

Make an element transparent and have it ignore any mouse or touch events, though it will stil take up space.

alpha : Float -> Attr decorative msg

A capped value between 0.0 and 1.0, where 0.0 is transparent and 1.0 is fully opaque.

Semantically equavalent to html opacity.

pointer : Attribute msg

Set the cursor to the pointer hand.

width : Length -> Attribute msg
height : Length -> Attribute msg
type alias Length =
Length
px : Int -> Length
shrink : Length

Shrink to an element to fit it's contents.

fill : Length

Fill the available space. The available space will be split evenly between elements that have width fill.

fillPortion : Int -> Length

Sometimes you may not want to split available space evenly. In this case you can use fillPortion to define which elements should have what portion of the available space.

So, two elements, one with width (fillPortion 2) and one with width (fillPortion 3). The first would get 2 portions of the available space, while the second would get 3.

Also: fill == fillPortion 1

minimum : Int -> Length -> Length
maximum : Int -> Length -> Length

Padding and Spacing

There's no concept of margin in style-elements, instead we have padding and spacing.

Padding is what you'd expect, the distance between the outer edge and the content, and spacing is the space between children.

So, if we have the following row, with some padding and spacing.

Element.row [ padding 10, spacing 7 ]
    [ Element.el [] none
    , Element.el [] none
    , Element.el [] none
    ]

Here's what we can expect.

Three boxes spaced 7 pixels apart.  There's a 10 pixel distance from the edge of the parent to the boxes.
padding : Int -> Attribute msg
paddingXY : Int -> Int -> Attribute msg

Set horizontal and vertical padding.

{ bottom : Int
, left : Int
, right : Int
, top : Int
}
-> Attribute msg
spacing : Int -> Attribute msg
spacingXY : Int -> Int -> Attribute msg

In the majority of cases you'll just need to use spacing, which will work as intended.

However for some layouts, like textColumn, you may want to set a different spacing for the x axis compared to the y axis.

spaceEvenly : Attribute msg

Alignment

Alignment can be used to align an Element within another Element.

Element.el [ centerX, alignTop ] (text "I'm centered and aligned top!")

If alignment is set on elements in a layout such as row, then the element will push the other elements in that direction. Here's an example.

Element.row []
    [ Element.el [] Element.none
    , Element.el [ alignLeft ] Element.none
    , Element.el [ centerX ] Element.none
    , Element.el [ alignRight ] Element.none
    ]

will result in a layout like

|-|-|     |-|        |-|

Where there are two elements on the left, one in the center, and on on the right.

centerX : Attribute msg
centerY : Attribute msg
alignLeft : Attribute msg
alignRight : Attribute msg
alignTop : Attribute msg
alignBottom : Attribute msg

Nearby Elements

Let's say we want a dropdown menu. Essentially we want to say: put this element below this other element, but don't affect the layout when you do.

Elemenet.row []
    [ Element.el
        [ Element.below (Element.text "I'm below!")
        ]
        (Element.text "I'm normal!")
    ]

This will result in

|- I'm normal! -|
   I'm below

Where "I'm Below" doesn't change the size of Element.row.

This is very useful for things like dropdown menus or tooltips.

above : Element msg -> Attribute msg
below : Element msg -> Attribute msg
onRight : Element msg -> Attribute msg
onLeft : Element msg -> Attribute msg
inFront : Element msg -> Attribute msg
behind : Element msg -> Attribute msg

Temporary Styling

type alias Attr decorative msg =
Attribute decorative msg

This is a special attribute that counts as both a Attribute msg and a Decoration.

type alias Decoration =
Attribute Never Never

Only decorations

mouseOver : List Decoration -> Attribute msg
mouseDown : List Decoration -> Attribute msg
focused : List Decoration -> Attribute msg

Adjustment

moveRight : Float -> Attr decorative msg
moveUp : Float -> Attr decorative msg
moveLeft : Float -> Attr decorative msg
moveDown : Float -> Attr decorative msg
rotate : Float -> Attr decorative msg
scale : Float -> Attr decorative msg

Clipping and Scrollbars

Clip the content if it overflows.

clip : Attribute msg
clipX : Attribute msg
clipY : Attribute msg

If these are present, the element will add a scrollbar if necessary.

scrollbars : Attribute msg
scrollbarX : Attribute msg
scrollbarY : Attribute msg

Responsiveness

type alias Device =
{ phone : Bool
, tablet : Bool
, desktop : Bool
, bigDesktop : Bool
, portrait : Bool
}
classifyDevice : { window | height : Int, width : Int } -> Device

Takes in a Window.Size and returns a device profile which can be used for responsiveness.

Scaling

modular : Float -> Float -> Int -> Float

When designing it's nice to use a modular scale to set spacial rythms. scaled = Scale.modular 16 1.25

A modular scale starts with a number, and multiplies it by a ratio a number of times. Then, when setting font sizes you can use:

   Font.size (scaled 1) -- results in 16


   Font.size (scaled 2) -- 16 * 1.25 results in 20


   Font.size (scaled 4) -- 16 * 1.25 ^ (4 - 1) results in 31.25

We can also provide negative numbers to scale below 16px.

   Font.size (scaled -1) -- 16 * 1.25 ^ (-1) results in 12.8

Mapping

map : (msg -> msg1) -> Element msg -> Element msg1
mapAttribute : (msg -> msg1) -> Attribute msg -> Attribute msg1

Compatibility

html : Html msg -> Element msg
htmlAttribute : Attribute msg -> Attribute msg