Skip to content

Commit 1138121

Browse files
adds adaptor for elm-css (#25)
1 parent a4ff845 commit 1138121

File tree

7 files changed

+3102
-209
lines changed

7 files changed

+3102
-209
lines changed

README.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ So you can use your own version of `elm/svg` and `mdgriffith/elm-ui` (or whateve
101101

102102
## Use with SVG
103103

104-
A working example: https://ellie-app.com/cZTwtp3TsdSa1
104+
A working example: https://ellie-app.com/fYCGytWFDD7a1
105105

106106
So we can create animated `Svg`s, create an animated wrapper function using `Svg.Attributes.class` and `Simple.Animation.Animated.svg`
107107

@@ -180,7 +180,7 @@ this lets you wrap regular `Element`s to create animated ones:
180180

181181
```elm
182182
animatedEl : Animation -> List (Element.Attribute msg) -> Element msg -> Element msg
183-
ael =
183+
animatedEl =
184184
animatedUi Element.el
185185

186186

@@ -232,6 +232,87 @@ animatedUi =
232232
}
233233
```
234234

235+
## Use with elm-css
236+
237+
So we can create animated versions of `elm-css` `div`s, `p`s or any other `Styled` element, create an animated wrapper using `Simple.Animation.Animated.elmCss` and the following:
238+
239+
```elm
240+
animatedElmCssNode =
241+
Animated.elmCss
242+
{ text = Html.Styled.text
243+
, node = Html.Styled.node
244+
, class = Html.Styled.Attributes.class
245+
}
246+
```
247+
248+
this lets you wrap regular `Html.Styled` nodes to create animated ones:
249+
250+
```elm
251+
animatedDiv : Animation -> List (Html.Styled.Attribute msg) -> List (Html.Styled.Html msg) -> Html.Styled.Html msg
252+
animatedDiv =
253+
animatedElmCssNode Html.Styled.div
254+
255+
256+
animatedLi : Animation -> List (Html.Styled.Attribute msg) -> List (Html.Styled.Html msg) -> Html.Styled.Html msg
257+
animatedLi =
258+
animatedElmCssNode Html.Styled.li
259+
```
260+
261+
Here's an animated square using `elm-css`
262+
263+
```elm
264+
import Css
265+
import Html exposing (Html)
266+
import Html.Styled as Styled
267+
import Simple.Animation as Animation exposing (Animation)
268+
import Simple.Animation.Animated
269+
import Simple.Animation.Property as P
270+
import Html.Styled.Attributes as StyledAttributes
271+
272+
273+
mySquare : Html msg
274+
mySquare =
275+
Styled.toUnstyled
276+
(animatedDiv hover
277+
[]
278+
[ Styled.div
279+
[ StyledAttributes.css
280+
[ Css.width (Css.px 50)
281+
, Css.height (Css.px 50)
282+
, Css.backgroundColor (Css.hex "ff0000")
283+
]
284+
]
285+
[]
286+
]
287+
)
288+
289+
290+
hover : Animation
291+
hover =
292+
Animation.steps
293+
{ startAt = [ P.y 0 ]
294+
, options = [ Animation.loop, Animation.easeInOutQuad ]
295+
}
296+
[ Animation.step 500 [ P.y 20 ]
297+
, Animation.step 650 [ P.y 0 ]
298+
]
299+
300+
301+
animatedDiv : Animation -> List (Styled.Attribute msg) -> List (Styled.Html msg) -> Styled.Html msg
302+
animatedDiv =
303+
animatedElmCssNode Styled.div
304+
305+
306+
animatedElmCssNode =
307+
Simple.Animation.Animated.elmCss
308+
{ text = Styled.text
309+
, node = Styled.node
310+
, class = StyledAttributes.class
311+
}
312+
313+
314+
```
315+
235316
## Use a Custom Renderer
236317

237318
In case you want to completely customise how to render animations you can use the low level `Simple.Animation.Animated.custom` - which gives you access to the raw animation `stylesheet` and `class` name that will apply the animation.

examples/elm.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
"elm/html": "1.0.0",
1313
"elm/svg": "1.0.1",
1414
"elm-community/typed-svg": "6.0.0",
15-
"mdgriffith/elm-ui": "1.1.8"
15+
"mdgriffith/elm-ui": "1.1.8",
16+
"rtfeldman/elm-css": "17.0.1"
1617
},
1718
"indirect": {
1819
"avh4/elm-color": "1.0.0",
1920
"elm/json": "1.1.3",
2021
"elm/time": "1.0.0",
2122
"elm/url": "1.0.0",
22-
"elm/virtual-dom": "1.0.2"
23+
"elm/virtual-dom": "1.0.2",
24+
"rtfeldman/elm-hex": "1.0.0"
2325
}
2426
},
2527
"test-dependencies": {

examples/src/Examples/Animations/Renderers.elm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module Examples.Animations.Renderers exposing (examples)
22

3+
import Css
34
import Element exposing (Element)
45
import Html exposing (Html)
56
import Html.Attributes
7+
import Html.Styled as Styled
8+
import Html.Styled.Attributes
69
import Simple.Animation as Animation exposing (Animation)
710
import Simple.Animation.Animated as Animated
811
import Simple.Animation.Property as P
@@ -46,6 +49,25 @@ htmlExample =
4649

4750

4851

52+
-- Elm CSS
53+
54+
55+
elmCssExample : Html msg
56+
elmCssExample =
57+
Styled.toUnstyled
58+
(Animated.animatedElmCssDiv flash
59+
[ Html.Styled.Attributes.class "text"
60+
, Html.Styled.Attributes.css
61+
[ Css.fontWeight Css.bold
62+
, Css.border3 (Css.px 2) Css.solid (Css.hex "#123123")
63+
]
64+
]
65+
[ Styled.text "I'm animating!"
66+
]
67+
)
68+
69+
70+
4971
-- Svg
5072

5173

@@ -127,6 +149,7 @@ examples : Element msg -> Element msg
127149
examples =
128150
groups
129151
[ group "With HTML" (html htmlExample)
152+
, group "With Elm CSS" (html elmCssExample)
130153
, group "With SVG" (html svgExample)
131154
, group "With Typed SVG" (html typedSvgExample)
132155
]

examples/src/Utils/Animated.elm

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
module Utils.Animated exposing
2-
( circle
2+
( animatedElmCssDiv
3+
, circle
34
, el
45
, g
56
, path
67
, typedSvgG
78
)
89

910
import Element exposing (Element)
11+
import Html.Styled as Styled
12+
import Html.Styled.Attributes
1013
import Simple.Animation exposing (Animation)
1114
import Simple.Animation.Animated as Animated
1215
import Svg exposing (Svg)
@@ -40,6 +43,29 @@ animatedUi =
4043

4144

4245

46+
-- Elm CSS
47+
48+
49+
animatedElmCssDiv : Animation -> List (Styled.Attribute msg) -> List (Styled.Html msg) -> Styled.Html msg
50+
animatedElmCssDiv =
51+
animatedElmCssNode Styled.div
52+
53+
54+
animatedElmCssNode :
55+
(List (Styled.Attribute msg) -> List (Styled.Html msg) -> Styled.Html msg)
56+
-> Animation
57+
-> List (Styled.Attribute msg)
58+
-> List (Styled.Html msg)
59+
-> Styled.Html msg
60+
animatedElmCssNode =
61+
Animated.elmCss
62+
{ text = Styled.text
63+
, node = Styled.node
64+
, class = Html.Styled.Attributes.class
65+
}
66+
67+
68+
4369
-- Svg
4470

4571

0 commit comments

Comments
 (0)