Skip to content

Commit

Permalink
Add srcset attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Jun 15, 2022
1 parent f677105 commit d851b85
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/Html/Attributes.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Html.Attributes exposing
( style, property, attribute, map
( SrcsetImageSizeDescriptor
, style, property, attribute, map
, class, classList, id, title, hidden
, type_, value, checked, placeholder, selected
, accept, acceptCharset, action, autocomplete, autofocus
Expand All @@ -9,7 +10,7 @@ module Html.Attributes exposing
, cols, rows, wrap
, href, target, download, hreflang, media, ping, rel
, ismap, usemap, shape, coords
, src, height, width, alt
, src, srcset, height, width, alt
, autoplay, controls, loop, preload, poster, default, kind, srclang
, sandbox, srcdoc
, reversed, start
Expand Down Expand Up @@ -52,7 +53,7 @@ just search the page for `video` if you want video stuff.
# Embedded Content
@docs src, height, width, alt
@docs src, srcset, height, width, alt
## Audio and Video
@docs autoplay, controls, loop, preload, poster, default, kind, srclang
Expand Down Expand Up @@ -309,6 +310,36 @@ src url =
stringProperty "src" (Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri url)


{-| Image size descriptor for `srcset` source
-}
type SrcsetImageSizeDescriptor
= PixelWidth Int
| PixelDensity Number


{-| Declare the source set of a `source` within a `picture`
-}
srcset : List ( String, Maybe SrcsetImageSizeDescriptor ) -> Attribute msg
srcset sources =
List.map
(\( src, size ) ->
Elm.Kernel.VirtualDom.noJavaScriptOrHtmlUri src
++ (case size of
Just (Width px) ->
" " ++ String.fromInt px ++ "w"

Just (PixelDensity d) ->
" " ++ String.fromFloat d ++ "x"

Nothing ->
""
)
)
sources
|> String.join ", "
|> stringProperty "srcset"


{-| Declare the height of a `canvas`, `embed`, `iframe`, `img`, `input`,
`object`, or `video`.
-}
Expand Down

0 comments on commit d851b85

Please sign in to comment.