Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <picture> tag #241

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Html.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Html exposing
, div, p, hr, pre, blockquote
, span, a, code, em, strong, i, b, u, sub, sup, br
, ol, ul, li, dl, dt, dd
, img, iframe, canvas, math
, img, picture, iframe, canvas, math
, form, input, textarea, button, select, option
, section, nav, article, aside, header, footer, address, main_
, figure, figcaption
Expand Down Expand Up @@ -40,7 +40,7 @@ expect to use frequently will be closer to the top.
@docs ol, ul, li, dl, dt, dd

## Embedded Content
@docs img, iframe, canvas, math
@docs img, picture, iframe, canvas, math

## Inputs
@docs form, input, textarea, button, select, option
Expand Down Expand Up @@ -590,6 +590,10 @@ img : List (Attribute msg) -> List (Html msg) -> Html msg
img =
Elm.Kernel.VirtualDom.node "img"

{-| Represents an image using one of many possible sources -}
picture : List (Attribute msg) -> List (Html msg) -> Html msg
picture =
Elm.Kernel.VirtualDom.node "picture"

{-| Embedded an HTML document. -}
iframe : List (Attribute msg) -> List (Html msg) -> Html msg
Expand Down
42 changes: 39 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, sizes, 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 All @@ -324,6 +355,11 @@ width : Int -> Attribute msg
width n =
Elm.Kernel.VirtualDom.attribute "width" (String.fromInt n)

{-| Declare the final rendered sizes of a picture `source` for a set of media queries
-}
sizes : String -> Attribute msg
sizes =
stringProperty "sizes"

{-| Alternative text in case an image can't be displayed. Works with `img`,
`area`, and `input`.
Expand Down