JoelQ / elm-toggleable / Toggleable

A representation of values in a toggleable UI as well as functions for common transformations. The Toggleable type models values that are independently toggleable. See it in action in this live example.

If you are looking to implement "accordion"-style behavior, you may want to use a zipper implementation instead such as SelectList.

Definition


type Toggleable a
    = Open a
    | Closed a

Represent a value that might be toggled open or closed. You can put these directly in your model.

{ article = "a lot of text..."
, comments = [ Open comment1, Closed comment2 ]
}

Toggling

toggle : Toggleable a -> Toggleable a

Toggle from Open to Closed or vice-versa.

toggle (Open "hello") -- Closed "hello"

toggle (Closed "hello") -- Open "hello"

toggleIf : (a -> Basics.Bool) -> Toggleable a -> Toggleable a

Only toggle if the given expression is True. The value inside the toggleable remains unchanged.

toggleIf String.isEmpty (Open "") -- Closed ""

toggleIf String.isEmpty (Open "hello") -- Open "hello"

open : Toggleable a -> Toggleable a

Convert a any toggleable value to Open.

open (Closed "hello") -- Open "hello"

open (Open "hello") -- Open "hello

close : Toggleable a -> Toggleable a

Convert a any toggleable value to Closed.

close (Closed "hello") -- Closed "hello"

close (Open "hello") -- Closed "hello

Unwrapping

unwrap : Toggleable a -> a

Get the value inside the toggleable.

unwrap (Open "hello") -- "hello"

Transforming

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

Transform the value inside the toggleable without modifying the toggleable state.

map String.toUpper (Open "hello") -- Open "HELLO"

map String.toUpper (Closed "hello") -- Closed "HELLO"