PaackEng/paack-ui/1.0.0

NOTE: the latest version is 7.23.0

UI.Button

The UI.Button is a component that can render as a hyperlink, a togglable button, a stylized button, or a clear padded-icon.

Following Elm-UI standards, this component is accessible.

A button can be created and rendered as in the following pipeline:

Element.column []
    [ -- Some UI.TextFields (...)
    , Button.fromLabel "Submit"
        |> Button.cmd FormSend Button.primary
        |> Button.renderElement renderConfig
    ]

Building

type Button msg

The Button msg type is used for describing the component for later rendering.

toggle : String -> (Bool -> msg) -> Bool -> Button msg

Toggle is a kind-of button that always contains a toggle-icon and visually looks like an embossed-primary button. The purpose of this button is to toggle between showing/hiding some spammed-content.

Button.toggle "Some Hint for Accessibility" TheTogglingMessage model.isSomethingVisible

This Button.success builds an embossed-looking, without-message, greenish-colored button. It's another approach for Button.withSuccessIf, helping when you can't compose a message for the desired action at the occasion.

case event of
    Just id ->
        Button.cmd (TriggerEvent id) Button.primary body


    Nothing ->
        Button.success body

This Button.disabled builds an embossed-looking, without-message, grayish-colored button. It's another approach for Button.withDisabledIf, helping when you can't compose a message for the desired action at the occasion.

case event of
    Just id ->
        Button.cmd (TriggerEvent id) Button.primary body


    Nothing ->
        Button.disabled body
cmd : msg -> ButtonStyle -> ButtonBody -> Button msg

This is the most common builder. It uses a simple message that is triggered on a click and renders as an embossed and themed button.

Button.fromLabel "Click this Button"
    |> Button.cmd SomeSimpleMessage Button.primary
    |> Button.renderElement renderConfig

Similar to Button.cmd, but instead of a message, it redirects to some path.

Button.fromLabel "Click this Link"
    |> Button.redirect "https://elm-lang.org/" Button.hyperlink
    |> Button.renderElement renderConfig

Content

The ButtonBody is required when assembling the most-basic Button msg type. It indicates the contents inside of the desired button, like its label or icon.

fromLabel : String -> ButtonBody

Button.fromLabel initiates a button's body with text-content inside it.

Button.fromLabel "Click here"

Button.fromIcon initiates a button's body with icon-content inside it.

Button.fromIcon (Icon.map "Go to maps")

Style

Non-toggle buttons must-be styled. The currently available styles are Hyperlink and Embossed.

A hyperlink-styled: See Button.hyperlink.

An embossed-styled button has paddings and hovering-effects. It's available through its sub-themes: Primary, Danger, Light, and Clear. These only change background and text color.

The primary action's theme. This button usually commits the main task.

This is the danger-theme, and it's reddish for enforcing the user's attention.

This is the light-theme, mostly used for less-important actions.

This is the clear-theme, mostly used on icons where the background color isn't needed.

Width

Describes a compatible width.

Button.withWidth changes the width of the button.

Button.withWidth Button.widthFull someButton

The button's width will fill its container.

The button will have the exact width to fit its contents.

NOTE: Default behaviour.

Size

withSize : Size -> Button msg -> Button msg

With Button.withSize, you'll be able to scale the button between the standard sizes.

The sizes (in height) are: Large - 60px; Medium - 48px; Small - 36px; Extra Small - 24px.

Button.withSize Size.large someButton

NOTE: Button's default size is Size.medium

Conditional states

withDisabledIf : Bool -> Button msg -> Button msg

After asserting some condition, Button.withDisabledIf will attempt to set the button to a visually-noticeable disabled state (a grayish button where the action can no longer be triggered).

Button.fromLabel "Send Someting"
    |> Button.cmd (QuerySend "Something") Button.primary
    |> Button.withDisabledIf (model.queryResult != QueryNotAsked)
    |> Button.renderElement renderConfig

NOTE: In case the button is a toggle or the condition resolves as False, nothing will happen.

withSuccessIf : Bool -> Button msg -> Button msg

After asserting some condition, Button.withSuccessIf will attempt to set the button to a visually-noticeable success state (a greenish button where the action can no longer be triggered).

Button.fromLabel "Send Someting"
    |> Button.cmd (QuerySend "Something") Button.primary
    |> Button.withSuccessIf (model.queryResult == QueryOkay)
    |> Button.renderElement renderConfig

NOTE: In case the button is a toggle or the condition resolves as False, nothing will happen.

Rendering

renderElement : RenderConfig -> Button msg -> Element msg

End of the builder's life. The result of this function is a ready-to-insert Elm UI's Element.

Component handling

map : (a -> b) -> Button a -> Button b

Transform the messages produced by the component.