phollyer/elm-motion/1.0.0

NOTE: the latest version is 3.0.4

Anim.Engine.Transition

CSS transitions for simple A→B animations with browser-native performance.

This engine is a good fit for hover effects, focus effects, and other simple state changes.

📖 See Transition Engine Documentation and Engine Overview for details.

Types

type alias AnimState =
AnimState

Holds the Transition engine state.

Keep this in your model.

type alias Model =
    { animState : Transition.AnimState }
type alias AnimGroupName =
String

The name of the animation group you want to target.

Builders

type alias AnimBuilder eng =
AnimBuilder eng

Type alias for the base AnimBuilder type.

Engine Builder

type alias EngineBuilder =
EngineBuilder

Builder type for Transition-only builders.

Use this in type annotations when a builder function should only work with this engine.

📖 See Engine Capabilities for patterns and examples.

Initialize

📖 See Initialize for details.

init : List (EngineBuilder -> EngineBuilder) -> AnimState

Initialize the engine state with a list of property initializers.

import Anim.Engine.Transition as Transition
import Anim.Property.Opacity as Opacity


Transition.init
    [ Opacity.init "animGroupName" 0.5
    , ... -- other property initializers
    ]

Target

for : AnimGroupName -> EngineBuilder -> EngineBuilder

Select the animation group that subsequent property builders will target.

Transition.for "animGroupName"
    >> Opacity.begin
    >> Opacity.to 0.2
    >> Opacity.end

Trigger

📖 See Triggering Animations for details.

animate : AnimState -> (EngineBuilder -> EngineBuilder) -> AnimState

Trigger animations.

import Anim.Engine.Transition as Transition


{ model
    | animState = Transition.animate model.animState entryAnim
}
retarget : AnimState -> (EngineBuilder -> EngineBuilder) -> AnimState

Change the targeted properties instantly to their new values. If currently animating, stop.

This is a convenience function for immediately snapping properties or axes to new values without needing to construct a full animation builder with animate.

Just target the properties or axes you want to change. Any other properties or axes on the group will snap to their targeted end values if mid-flight.

import Anim.Engine.Transition as Transition
import Anim.Property.Translate as Translate


{ model
    | animState =
        Transition.retarget model.animState <|
            Transition.for "animGroupName"
                >> Translate.begin
                >> Translate.toY 0
                >> Translate.end
}

Events

📖 See Event Reference for details.

type alias CurrentTargetId =
Maybe String

The ID of the element that owns the event listener.

type alias TargetId =
Maybe String

The ID of the element that started the event.

This will be different from CurrentTargetId if the event bubbled from a child element.

= Started CurrentTargetId TargetId AnimGroupName
| Ended CurrentTargetId TargetId AnimGroupName
| Cancelled CurrentTargetId TargetId AnimGroupName
| Run CurrentTargetId TargetId AnimGroupName

CSS transition lifecycle events.

Update

📖 See React for details.

type alias AnimMsg =
AnimMsg

Message type used with update.

import Anim.Engine.Transition as Transition


type Msg
    = TransitionMsg Transition.AnimMsg
    | ...
update : AnimMsg -> AnimState -> ( AnimState, Maybe AnimEvent )

Handle messages from this engine.

Returns the updated state and an event for this message, if one should be surfaced.

import Anim.Engine.Transition as Transition


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        TransitionMsg animMsg ->
            let
                ( animState, maybeEvent ) =
                    Transition.update animMsg model.animState


                nextModel =
                    { model | animState = animState }
            in
            case maybeEvent of
                Just event ->
                    handleAnimationEvent event nextModel


                Nothing ->
                    ( nextModel, Cmd.none )


handleAnimationEvent : Transition.AnimEvent -> Model -> ( Model, Cmd Msg )
handleAnimationEvent event model =
    case event of
        ...

View

To render a transition, add attributes to the element you want to animate.

📖 See Render for details.

attributes : AnimGroupName -> AnimState -> List (Attribute msg)

Apply the animation attributes to your element.

import Anim.Engine.Transition as Transition
import Html exposing (div, text)


div
    (Transition.attributes "animGroupName" animState)
    [ text "Animating element" ]

Event Listeners

events : (AnimMsg -> msg) -> List (Attribute msg)

Receive transition lifecycle events.

Add events to your element with a message constructor that wraps AnimMsg.

import Anim.Engine.Transition as Transition
import Html exposing (div, text)


type Msg
    = TransitionMsg Transition.AnimMsg


div
    (Transition.attributes "animGroupName" animState
        ++ Transition.events "animGroupName" TransitionMsg
    )
    [ text "Animating element" ]
eventsStopPropagation : (AnimMsg -> msg) -> List (Attribute msg)

The same as events but with propagation stopped.

import Anim.Engine.Transition as Transition
import Html exposing (div, text)


div
    (Transition.attributes "animGroupName" model.animState
        ++ Transition.eventsStopPropagation "animGroupName" TransitionMsg
    )
    [ text "Animated element" ]

Unit

cssUnit : Unit -> EngineBuilder -> EngineBuilder

Set the default length unit for all length-bearing properties.

responsivePanelMotion : AnimBuilder { eng | withCssUnit : () } -> AnimBuilder { eng | withCssUnit : () }
responsivePanelMotion =
    cssUnit Unit.Vw
        >> slidePanelIn
        >> growPanel
cssUnitX : Unit -> EngineBuilder -> EngineBuilder

Set the default length unit for the X axis.

responsiveDrawerMotion : AnimBuilder { eng | withCssUnit : () } -> AnimBuilder { eng | withCssUnit : () }
responsiveDrawerMotion =
    cssUnitX Unit.Vw
        >> slidePanelIn
        >> growPanel
cssUnitY : Unit -> EngineBuilder -> EngineBuilder

Set the default length unit for the Y axis.

responsiveSheetMotion : AnimBuilder { eng | withCssUnit : () } -> AnimBuilder { eng | withCssUnit : () }
responsiveSheetMotion =
    cssUnitY Unit.Vh
        >> slidePanelIn
        >> growPanel
cssUnitZ : Unit -> EngineBuilder -> EngineBuilder

Set the default length unit for the Z axis.

layeredSceneMotion : AnimBuilder { eng | withCssUnit : () } -> AnimBuilder { eng | withCssUnit : () }
layeredSceneMotion =
    cssUnitZ Unit.Px
        >> slidePanelIn
        >> growPanel
cssUnitWidth : Unit -> EngineBuilder -> EngineBuilder

Set the default length unit used for width values in Transition animations.

responsiveCardWidth : AnimBuilder { eng | withCssUnit : () } -> AnimBuilder { eng | withCssUnit : () }
responsiveCardWidth =
    cssUnitWidth Unit.Vw
        >> slidePanelIn
        >> growPanel
cssUnitHeight : Unit -> EngineBuilder -> EngineBuilder

Set the default length unit used for height values in Transition animations.

responsivePanelHeight : AnimBuilder { eng | withCssUnit : () } -> AnimBuilder { eng | withCssUnit : () }
responsivePanelHeight =
    cssUnitHeight Unit.Vh
        >> slidePanelIn
        >> growPanel

Timing

📖 See Timing for details.

delay : Int -> EngineBuilder -> EngineBuilder

Set the global delay for all animations in this builder.

introAnim : AnimBuilder { eng | withTiming : () } -> AnimBuilder { eng | withTiming : () }
introAnim =
    delay 500
        >> fadeInHeader
        >> slideInSidebar
        >> fadeInContent
duration : Int -> EngineBuilder -> EngineBuilder

Set the global duration for all animations in this builder.

introAnim : AnimBuilder { eng | withTiming : () } -> AnimBuilder { eng | withTiming : () }
introAnim =
    duration 500
        >> fadeInHeader
        >> slideInSidebar
        >> fadeInContent
speed : Float -> EngineBuilder -> EngineBuilder

Set the global speed for all animations in this builder.

introAnim : AnimBuilder { eng | withTiming : () } -> AnimBuilder { eng | withTiming : () }
introAnim =
    speed 300
        >> slideDownHeader
        >> slideInSidebar
        >> slideUpContent

Easing

📖 See Easing for details.

easing : Easing -> EngineBuilder -> EngineBuilder

Set the global easing function.

heroEntrance : AnimBuilder { eng | withEasing : () } -> AnimBuilder { eng | withEasing : () }
heroEntrance =
    easing EaseInOut
        >> fadeInHeroTitle
        >> slideInHeroArtwork
        >> revealPrimaryCta

Animation Control

📖 See Controlling Animations for details.

stop : AnimGroupName -> AnimState -> AnimState

Stop a running animation by instantly jumping to its end state.

import Anim.Engine.Transition as Transition


Transition.stop "animGroup" model.animState
reset : AnimGroupName -> AnimState -> AnimState

Reset an animation by instantly jumping back to its start state.

import Anim.Engine.Transition as Transition


Transition.reset "animGroup" model.animState

Discrete Properties

📖 See Discrete Properties for details.

discreteEntry : String -> String -> EngineBuilder -> EngineBuilder

Add a discrete CSS property for entry animations.

The value is applied as an inline style from the first frame and held throughout the animation. Use this when an element is appearing (e.g., going from display: none to display: block).

This function is a precedence function, so it can operate as a global setting for all groups in the builder chain, or you can set it on a per-group basis which overrides any global setting for that group.

For entry animations, pair this with startingStyleNode so the browser knows what values to transition from.

import Anim.Engine.Transition as Transition
import Anim.Property.Opacity as Opacity


Transition.animate model.animState <|
    Transition.discreteEntry "display" "block"
        >> Transition.for "box"
        >> Opacity.begin
        >> Opacity.to 1
        >> Opacity.end
startingStyleNode : AnimState -> Html msg

Generate a <style> node containing @starting-style rules for all animated elements.

When an element enters the DOM (or changes from display: none), the browser needs to know what values to animate FROM. Without @starting-style, the browser skips the transition.

import Anim.Engine.Transition as Transition
import Html exposing (div, text)


view model =
    div []
        [ Transition.startingStyleNode model.animState
        , div (Transition.attributes "fadeIn" model.animState)
            [ text "I fade in!" ]
        ]
startingStyleNodeFor : AnimGroupName -> AnimState -> Html msg

Generate @starting-style rules for a specific animation group.

import Anim.Engine.Transition as Transition
import Html exposing (div, text)


view model =
    div []
        [ Transition.startingStyleNodeFor "fadeIn" model.animState
        , div (Transition.attributes "fadeIn" model.animState)
            [ text "I fade in!" ]
        ]
discreteExit : String -> String -> String -> EngineBuilder -> EngineBuilder

Add a discrete CSS property for exit animations.

Exit animations need to hold their initial state until the very end of the animation, at which point they flip to the final state. Therefore you need to set both entry and exit values for the property.

This function is a precedence function, so it can operate as a global setting for all groups in the builder chain, or you can set it on a per-group basis which overrides any global setting for that group.

Use when an element is disappearing (e.g., going from display: block to display: none).

import Anim.Engine.Transition as Transition
import Anim.Property.Opacity as Opacity


Transition.animate model.animState <|
    Transition.discreteExit "display" "block" "none"
        >> Transition.for "box"
        >> Opacity.begin
        >> Opacity.to 0
        >> Opacity.end

State Queries

📖 See State Queries for details.

anyRunning : AnimState -> Maybe Bool

Check if any animations are currently running.

Returns Nothing if there are no animations.

isRunning : AnimGroupName -> AnimState -> Maybe Bool

Check if a specific animation group is currently running.

Returns Nothing if there are no animations for the group.

allComplete : AnimState -> Maybe Bool

Check if all animations are complete.

Returns Nothing if there are no animations.

isComplete : AnimGroupName -> AnimState -> Maybe Bool

Check if a specific animation group has completed.

Returns Nothing if there are no animations for the group.

isCancelled : AnimGroupName -> AnimState -> Maybe Bool

Check if a specific animation group was cancelled.

Returns Nothing if there are no animations for the group.

Property Queries

📖 See Property Queries and Properties for details.

Custom Properties

getPropertyEnd : AnimGroupName -> String -> AnimState -> Maybe Float

Get the end value of a custom property animation.

The second argument is the CSS property name.

Returns Nothing if the element has no animation for the given custom property.

Custom Color Properties

getColorPropertyEnd : AnimGroupName -> String -> AnimState -> Maybe Color

Get the end value of a custom color property animation.

The second argument is the CSS property name.

Returns Nothing if the element has no animation for the given custom color property.

Opacity

getOpacityEnd : AnimGroupName -> AnimState -> Maybe Float

Get the end opacity of an element being animated.

Returns Nothing if the element has no opacity animation.

Perspective Origin

AnimGroupName
-> AnimState
-> Maybe
{ x : Float
, y : Float
}

Get the end perspective origin of an element being animated.

Returns Nothing if the element has no perspective origin animation.

Rotate

AnimGroupName
-> AnimState
-> Maybe
{ x : Float
, y : Float
, z : Float
}

Get the end rotation of an element being animated.

Returns Nothing if the element has no rotate animation.

Scale

AnimGroupName
-> AnimState
-> Maybe
{ x : Float
, y : Float
, z : Float
}

Get the end scale of an element being animated.

Returns Nothing if the element has no scale animation.

Size

AnimGroupName
-> AnimState
-> Maybe
{ width : Float
, height : Float
}

Get the end size of an element being animated.

Returns Nothing if the element has no size animation.

Skew

AnimGroupName
-> AnimState
-> Maybe
{ x : Float
, y : Float
}

Get the end skew of an element being animated.

Returns Nothing if the element has no skew animation.

Translate

AnimGroupName
-> AnimState
-> Maybe
{ x : Float
, y : Float
, z : Float
}

Get the end translate of an element being animated.

Returns Nothing if the element has no translate animation.