Snackbars provide brief messages about the application's processes at the bottom of the screen.
import Material.Snackbar as Snackbar
type alias Model =
{ queue : Snackbar.Queue Msg }
initialModel : Model
initialModel =
{ queue = Snackbar.initialQueue }
type Msg
= SnackbarMsg (Snackbar.Msg Msg)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SnackbarMsg snackbarMsg ->
let
( newQueue, cmd ) =
Snackbar.update SnackbarMsg
snackbarMsg
model.queue
in
( { model | queue = newQueue }, cmd )
main =
Snackbar.snackbar SnackbarMsg
Snackbar.config
initialModel.queueSpecify whether the snackbar's messages should close when the user presses escape
Specify additional attributes
You will have to maintain a queue of snackbar messages inside your
application's model. To do so, add a field queue : Queue msg and initialize
it to initialQueue.
To add messages to the queue, you also have to tag Snackbar.Msgs within your
application's Msg type.
type alias Model =
{ queue : Snackbar.Queue Msg }
initialModel =
{ queue = Snackbar.initialQueue }
type Msg
= SnackbarMsg (Snackbar.Msg Msg)Then from your application's update function, call update to handle
Snackbar.Msg. Note that the first argument to update is SnackbarMsg.
type Msg
= SnackbarMsg (Snackbar.Msg Msg)
update msg model =
case msg of
SnackbarMsg snackbarMsg ->
Snackbar.update SnackbarMsg snackbarMsg model.queue
|> Tuple.mapFirst
(\newQueue -> { model | queue = newQueue })Now you are ready to call addMessage from your application's update function.
Queue of messages
Initial empty queue
Snackbar message type
Note that addMessage takes SnackbarMsg as first parameter.
type Msg
= SnackbarMsg (Snackbar.Msg Msg)
| SomethingHappened
update msg model =
case msg of
SomethingHappened ->
let
message =
Snackbar.message
|> Snackbar.setLabel (Just "Something happened")
in
( model, Snackbar.addMessage SnackbarMsg message )
SnackbarMsg snackbarMsg ->
Snackbar.update SnackbarMsg snackbarMsg model.queue
|> Tuple.mapFirst
(\newQueue -> { model | queue = newQueue })Adds a message to the queue
At the minimum, a message contains only a label. To specify the label, specify
it using the setLabel configuration option.
Snackbar.message
|> Snackbar.setLabel (Just "Something happened")Snackbar message
Specify a message's action button label
Specify a message when the user clicks on a message's action button
Specify a message's action icon
Specify a message when the user clicks on a message's action icon
Specify whether a message should display leading
Messages are by default centered within the viewport. On larger screens, they
can optionally be displyed on the leading edge of the screen. To display a
message as leading, set its setLeading configuration option to True.
Specify whether a message should be stacked
Stacked messages display their label above their action button or icon. This works best for messages with a long label.
Specify a message's timeout in milliseconds
Messages may contain an action button that the user can click. To display an
action button, set the message's setActionButton configuration option to a
string, and handle the event in onActionButtonClick.
Snackbar.message
|> Snackbar.setLabel (Just "Something happened")
|> Snackbar.setActionButton (Just "Take action")
|> Snackbar.setOnActionButtonClick ActionButtonClickedMessages may contain an action icon that the user can click. To display an
action icon, set the message's setActionIcon configuration option to a string
representing a Material Icon, and handle the event in onActionIconClick.
Snackbar.message
|> Snackbar.setLabel (Just "Something happened")
|> Snackbar.setActionIcon (Just "close")
|> Snackbar.setOnActionIconClick DismissedMessages with a long label and action button should display the action button
below the label. To archieve this, set the message's setStacked configuration
option to True.
Snackbar.message
|> Snackbar.setLabel (Just "Something happened")
|> Snackbar.setActionButton (Just "Take action")
|> Snackbar.setStacked TrueMessages are by default centered within the viewport. On larger screens, they
can optionally be displyed on the leading edge of the screen. To display a
message as leading, set its setLeading configuration option to True.
Snackbar.message
|> Snackbar.setLabel (Just "Something happened")
|> Snackbar.setLeading TrueTo set a custom timeout for a message, set its setTimeoutMs configuration
option to a floating point value, representing the on-screen time in
milliseconds.
Snackbar.message
|> Snackbar.setLabel (Just "Something happened")
|> Snackbar.setTimeoutMs 5000
Configuration of a snackbar