Skip to content

Commit c7b79c4

Browse files
committed
Move styled function to Html.Styled module
1 parent b26483b commit c7b79c4

File tree

5 files changed

+64
-50
lines changed

5 files changed

+64
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module MyCss exposing (main)
1313
import Css exposing (..)
1414
import Html
1515
import Html.Styled exposing (..)
16-
import Html.Styled.Attributes exposing (css, href, src, styled)
16+
import Html.Styled.Attributes exposing (css, href, src)
1717
import Html.Styled.Events exposing (onClick)
1818

1919

src/Css.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ module Css
686686
import Css exposing (..)
687687
import Html
688688
import Html.Styled exposing (..)
689-
import Html.Styled.Attributes exposing (css, href, src, styled)
689+
import Html.Styled.Attributes exposing (css, href, src)
690690
import Html.Styled.Events exposing (onClick)
691691
692692
{-| A logo image, with inline styles that change on hover.

src/Html/Styled.elm

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ module Html.Styled
8585
, source
8686
, span
8787
, strong
88+
, styled
8889
, sub
8990
, summary
9091
, sup
@@ -108,9 +109,9 @@ module Html.Styled
108109
)
109110

110111
{-| Drop-in replacement for the `Html` module from the `elm-lang/html` package.
111-
The only functions added are `toUnstyled` and `fromUnstyled`:
112+
The only functions added are `styled`, `toUnstyled` and `fromUnstyled`:
112113
113-
@docs fromUnstyled, toUnstyled
114+
@docs styled, fromUnstyled, toUnstyled
114115
115116
This file is organized roughly in order of popularity. The tags which you'd
116117
expect to use frequently will be closer to the top.
@@ -205,6 +206,8 @@ expect to use frequently will be closer to the top.
205206
206207
-}
207208

209+
import Css exposing (Style)
210+
import Html.Styled.Internal as Internal
208211
import VirtualDom
209212
import VirtualDom.Styled
210213

@@ -254,6 +257,40 @@ map =
254257
VirtualDom.Styled.map
255258

256259

260+
{-| Takes a function that creates an element, and pre-applies styles to it.
261+
262+
bigButton : List (Attribute msg) -> List (Html msg) -> Html msg
263+
bigButton =
264+
styled button
265+
[ padding (px 30)
266+
, fontWeight bold
267+
]
268+
269+
view : Model -> Html msg
270+
view model =
271+
[ text "These two buttons are identical:"
272+
, bigButton [] [ text "Hi!" ]
273+
, button [ css [ padding (px 30), fontWeight bold ] ] [] [ text "Hi!" ]
274+
]
275+
276+
Here, the `bigButton` function we've defined using `styled button` is
277+
identical to the normal `button` function, except that it has pre-applied
278+
the attribute of `css [ padding (px 30), fontWeight bold ]`.
279+
280+
You can pass more attributes to `bigButton` as usual (including other `css`
281+
attributes). They will be applied after the pre-applied styles.
282+
283+
-}
284+
styled :
285+
(List (Attribute msg) -> List (Html msg) -> Html msg)
286+
-> List Style
287+
-> List (Attribute msg)
288+
-> List (Html msg)
289+
-> Html msg
290+
styled fn styles attrs children =
291+
fn (Internal.css styles :: attrs) children
292+
293+
257294
{-| -}
258295
toUnstyled : Html msg -> VirtualDom.Node msg
259296
toUnstyled =

src/Html/Styled/Attributes.elm

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ module Html.Styled.Attributes
9393
, start
9494
, step
9595
, style
96-
, styled
9796
, tabindex
9897
, target
9998
, title
@@ -105,9 +104,9 @@ module Html.Styled.Attributes
105104
)
106105

107106
{-| Drop-in replacement for the `Html.Attributes` module from the `elm-lang/html` package.
108-
The only functions added are `css`, `styled`, and `fromUnstyled`:
107+
The only functions added are `css` and `fromUnstyled`:
109108
110-
@docs css, styled, fromUnstyled
109+
@docs css, fromUnstyled
111110
112111
Helper functions for HTML attributes. They are organized roughly by
113112
category. Each attribute is labeled with the HTML tags it can be used with, so
@@ -204,6 +203,7 @@ Attributes that can be attached to any HTML tag but are less commonly used.
204203

205204
import Css exposing (Style)
206205
import Html.Styled exposing (Attribute, Html)
206+
import Html.Styled.Internal as Internal
207207
import Json.Encode as Json
208208
import VirtualDom
209209
import VirtualDom.Styled
@@ -1163,46 +1163,5 @@ See the [`Css` module documentation](http://package.elm-lang.org/packages/rtfeld
11631163
11641164
-}
11651165
css : List Style -> Attribute msg
1166-
css styles =
1167-
let
1168-
classname =
1169-
VirtualDom.Styled.getClassname styles
1170-
1171-
classProperty =
1172-
VirtualDom.property "className" (Json.string classname)
1173-
in
1174-
VirtualDom.Styled.Property classProperty styles classname
1175-
1176-
1177-
{-| Takes a function that creates an element, and pre-applies styles to it.
1178-
1179-
bigButton : List (Attribute msg) -> List (Html msg) -> Html msg
1180-
bigButton =
1181-
styled button
1182-
[ padding (px 30)
1183-
, fontWeight bold
1184-
]
1185-
1186-
view : Model -> Html msg
1187-
view model =
1188-
[ text "These two buttons are identical:"
1189-
, bigButton [] [ text "Hi!" ]
1190-
, button [ css [ padding (px 30), fontWeight bold ] ] [] [ text "Hi!" ]
1191-
]
1192-
1193-
Here, the `bigButton` function we've defined using `styled button` is
1194-
identical to the normal `button` function, except that it has pre-applied
1195-
the attribute of `css [ padding (px 30), fontWeight bold ]`.
1196-
1197-
You can pass more attributes to `bigButton` as usual (including other `css`
1198-
attributes). They will be applied after the pre-applied styles.
1199-
1200-
-}
1201-
styled :
1202-
(List (Attribute msg) -> List (Html msg) -> Html msg)
1203-
-> List Style
1204-
-> List (Attribute msg)
1205-
-> List (Html msg)
1206-
-> Html msg
1207-
styled fn styles attrs children =
1208-
fn (css styles :: attrs) children
1166+
css =
1167+
Internal.css

src/Html/Styled/Internal.elm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Html.Styled.Internal exposing (css)
2+
3+
import Css
4+
import Json.Encode as Json
5+
import VirtualDom
6+
import VirtualDom.Styled
7+
8+
9+
css : List Css.Style -> VirtualDom.Styled.Property msg
10+
css styles =
11+
let
12+
classname =
13+
VirtualDom.Styled.getClassname styles
14+
15+
classProperty =
16+
VirtualDom.property "className" (Json.string classname)
17+
in
18+
VirtualDom.Styled.Property classProperty styles classname

0 commit comments

Comments
 (0)