Lightweight Elm component for animating height using CSS transitions. Slide up/down the element to any specific height.
animationstart/animationend event lifecycles.Set an initial state in your model.
By default, the height of the container is
0pxand all content inside of it is hidden.
type alias Model = AnimateHeight.State
init : Model
init = init (identifier "my-unique-id")Subscribe to AnimateHeight subscriptions.
type alias Model = AnimateHeight.State
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.map AnimateMsg (AnimateHeight.subscriptions model)Set up an update Msg in your update function.
Ignoring the
transitionfor now, the update below shows how to persist theStateand map theAnimateHeight.Msgto your programsCmd Msg.
type alias Model = AnimateHeight.State
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
AnimateMsg animateMsg ->
let
(transition, newState, cmds) = update animateMsg model
in
(newState, Cmd.map AnimateHeight cmds)Respond to the Transition events in your update function.
The
Transitionevents let you know the progress of the animation. TheFloatvalues represent the final height of the container.
TransitionStart 200 -- Animation has started and it's final height will be 200pxTransitionEnd 200 -- Animation has ended and it's final height is 200pxThese Transition events are handy to find out if your content is
visible or hidden.
type alias Model = AnimateHeight.State
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
AnimateMsg animateMsg ->
let
(transition, newState, cmds) = update animateMsg model
isContentVisible =
case transition of
Just (TransitionEnd pxValue) ->
if pxValue == 0 then
-- Your content is hidden
else
-- Your content is visible
_ ->
-- Nothing going on
in
(newState, Cmd.map AnimateHeight cmds)Render your view in the AnimateHeight container.
When the container is at
0pxAnimateHeighthides your content and sets the relevantAriavalues for accessibility.
type alias Model = AnimateHeight.State
yourCoolView : Html Msg
yourCoolView :
-- cool view stuff --
view : Model -> Msg
view model =
container
(make AnimateMsg
|> content yourCoolView
|> state model
)Set the height of the container in your update function.
We are using
autohere which will animate to the height of the content within the container.
If the content is 300px
autowill animate the container to 300px.
Check out fixed to animate to a specific height.
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
ShowContainerStuff ->
let
newState = height auto model
in
(newState, Cmd.none)
HideContainerStuff ->
let
newState = height (fixed 0) model
in
(newState, Cmd.none)Switch
For animating height when switching between views check out the Switch variant.
Animating the height of a container does mean paying a performance cost depending on how much work the browser needs to perform on layout and paint operations.
elm-animate-height uses Css transitions only, which minimises scripting time and ensures the browser is doing the animation calculations.