Skip to content

Commit f1d2049

Browse files
adds empty animation (#26)
1 parent ac6ab5f commit f1d2049

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/Internal/Animation.elm

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,15 @@ renderIteration i =
174174

175175

176176
name_ : Animation -> String
177-
name_ (Animation d options frames) =
178-
"anim-" ++ String.fromInt d ++ optionNames options ++ framesNames frames
177+
name_ anim =
178+
if isEmpty anim then
179+
"anim-empty"
180+
181+
else
182+
"anim-"
183+
++ String.fromInt (duration_ anim)
184+
++ optionNames (rawOptions_ anim)
185+
++ framesNames (frames_ anim)
179186

180187

181188
optionNames : List Option -> String
@@ -231,6 +238,11 @@ iterationName i =
231238
-- Helpers
232239

233240

241+
isEmpty : Animation -> Bool
242+
isEmpty anim =
243+
duration_ anim == 0
244+
245+
234246
options_ : Animation -> Options
235247
options_ =
236248
rawOptions_

src/Simple/Animation.elm

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Simple.Animation exposing
2-
( Animation, Millis, fromTo, steps
2+
( Animation, Millis, fromTo, steps, empty
33
, Step, step, set, wait, waitTillComplete
44
, Option, loop, count, delay, reverse, yoyo
55
, linear, easeIn, easeOut, easeInOut, cubic
@@ -12,7 +12,7 @@ module Simple.Animation exposing
1212
1313
# Create an Animation
1414
15-
@docs Animation, Millis, fromTo, steps
15+
@docs Animation, Millis, fromTo, steps, empty
1616
1717
1818
# Steps
@@ -152,6 +152,29 @@ steps { options, startAt } steps_ =
152152
)
153153

154154

155+
{-| Create an Empty Animation - Useful when you want to conditionally animate something
156+
157+
fadeIf : Bool -> Animation
158+
fadeIf shouldFade =
159+
if shouldFade then
160+
Animation.fromTo
161+
{ duration = 1000
162+
, options = []
163+
}
164+
[ P.opacity 0 ]
165+
[ P.opacity 1 ]
166+
167+
else
168+
Animation.empty
169+
170+
`Animation.empty` is equivalent to an animation with `0` duration
171+
172+
-}
173+
empty : Animation
174+
empty =
175+
fromTo { duration = 0, options = [] } [] []
176+
177+
155178

156179
-- Step
157180

tests/FromToTest.elm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module FromToTest exposing (suite)
22

3+
import Expect
4+
import Internal.Animation as Animation
35
import Simple.Animation as Animation exposing (Animation)
46
import Simple.Animation.Property as P
57
import Test exposing (..)
@@ -47,4 +49,12 @@ suite =
4749
, "animation-duration: 1000ms"
4850
, "animation-timing-function: linear"
4951
]
52+
, test "Empty Animations have formatted name" <|
53+
\_ ->
54+
[ Animation.empty
55+
, Animation.fromTo { duration = 0, options = [] } [] []
56+
, Animation.steps { startAt = [], options = [] } []
57+
]
58+
|> List.map Animation.name_
59+
|> Expect.equal (List.repeat 3 "anim-empty")
5060
]

0 commit comments

Comments
 (0)