Skip to content

Commit d851b85

Browse files
committed
Add srcset attribute
1 parent f677105 commit d851b85

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/Html/Attributes.elm

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Html.Attributes exposing
2-
( style, property, attribute, map
2+
( SrcsetImageSizeDescriptor
3+
, style, property, attribute, map
34
, class, classList, id, title, hidden
45
, type_, value, checked, placeholder, selected
56
, accept, acceptCharset, action, autocomplete, autofocus
@@ -9,7 +10,7 @@ module Html.Attributes exposing
910
, cols, rows, wrap
1011
, href, target, download, hreflang, media, ping, rel
1112
, ismap, usemap, shape, coords
12-
, src, height, width, alt
13+
, src, srcset, height, width, alt
1314
, autoplay, controls, loop, preload, poster, default, kind, srclang
1415
, sandbox, srcdoc
1516
, reversed, start
@@ -52,7 +53,7 @@ just search the page for `video` if you want video stuff.
5253
5354
5455
# Embedded Content
55-
@docs src, height, width, alt
56+
@docs src, srcset, height, width, alt
5657
5758
## Audio and Video
5859
@docs autoplay, controls, loop, preload, poster, default, kind, srclang
@@ -309,6 +310,36 @@ src url =
309310
stringProperty "src" (Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri url)
310311

311312

313+
{-| Image size descriptor for `srcset` source
314+
-}
315+
type SrcsetImageSizeDescriptor
316+
= PixelWidth Int
317+
| PixelDensity Number
318+
319+
320+
{-| Declare the source set of a `source` within a `picture`
321+
-}
322+
srcset : List ( String, Maybe SrcsetImageSizeDescriptor ) -> Attribute msg
323+
srcset sources =
324+
List.map
325+
(\( src, size ) ->
326+
Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri src
327+
++ (case size of
328+
Just (Width px) ->
329+
" " ++ String.fromInt px ++ "w"
330+
331+
Just (PixelDensity d) ->
332+
" " ++ String.fromFloat d ++ "x"
333+
334+
Nothing ->
335+
""
336+
)
337+
)
338+
sources
339+
|> String.join ", "
340+
|> stringProperty "srcset"
341+
342+
312343
{-| Declare the height of a `canvas`, `embed`, `iframe`, `img`, `input`,
313344
`object`, or `video`.
314345
-}

0 commit comments

Comments
 (0)