Skip to content

Commit bb94e7c

Browse files
authored
Image object-fit (#206)
* imgStyle props * contain vs. cover style modifiers
1 parent 2571f4a commit bb94e7c

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

docs/Examples2/Image.example.purs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,28 @@ import React.Basic.Classic (JSX)
1212

1313
docs :: JSX
1414
docs =
15-
let flexo = "https://s3.amazonaws.com/lumi-blog/avatars/_x600/flexo.jpg"
15+
let flexo = "https://i.picsum.photos/id/985/1000/300.jpg?hmac=t_lmj43iuzwGqZaRMY1ee9udE_pCzfYLgCD49YrCPjw"
16+
-- "https://s3.amazonaws.com/lumi-blog/avatars/_x600/flexo.jpg"
1617
in Array.intercalate (vspace S16)
1718
[ h4_ "Image default (will respect image's original aspect ratio & dimensions)"
1819
, example
1920
$ Image.image
2021
$$$ flexo
21-
, h4_ "Image + resize { width: 120px, height: 40px }, the image will fill the height and width of its parent (object-fit: cover), maintaining its aspect ratio but cropping the image"
22+
, h4_ "Image + resize { width: 40px, height: 120px }, the image will fill the height and width of its parent (object-fit: cover), maintaining its aspect ratio but cropping the image"
2223
, example
2324
$ Image.image
24-
$ Image.resize { width: 120, height: 40 }
25+
$ Image.resize { width: 40, height: 120 }
2526
$$$ flexo
26-
, h4_ "Thumbnail default (will always have a square aspect ratio)"
27+
, h4_ "Thumbnail default (will always have a square aspect ratio); and defaults to object-fit: cover"
2728
, example
2829
$ Image.thumbnail
2930
$$$ flexo
31+
, h4_ "Thumbnail default (will always have a square aspect ratio); but can be override with object-fit: contain"
32+
, example
33+
$ Image.thumbnail
34+
$ Image.contain
35+
$ _ { content = flexo
36+
}
3037
, h4_ "Thumbnail + resize 400px"
3138
, example
3239
$ Image.thumbnail

src/Lumi/Components2/Image.purs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Lumi.Components2.Image
1111
, resize
1212
, resizeSquare
1313
, round
14+
, cover
15+
, contain
1416
, ImageProps
1517
, ThumbnailProps
1618
, Image(..)
@@ -28,7 +30,7 @@ import Data.Traversable (traverse)
2830
import Data.Tuple.Nested ((/\))
2931
import Effect.Timer (clearTimeout, setTimeout)
3032
import Effect.Unsafe (unsafePerformEffect)
31-
import Lumi.Components (LumiComponent, PropsModifier, lumiComponent, ($$$))
33+
import Lumi.Components (LumiComponent, PropsModifier, lumiComponent, propsModifier, ($$$))
3234
import Lumi.Components.Loader (loader)
3335
import Lumi.Components.Svg (placeholderSvg)
3436
import Lumi.Components2.Box as Box
@@ -50,6 +52,7 @@ type ImageProps
5052
= ( component :: Image
5153
, content :: String
5254
, placeholder :: Maybe JSX
55+
, imgStyle :: Style
5356
)
5457

5558
-- | An image has no size restrictions
@@ -113,6 +116,7 @@ image =
113116
, objectFit: E.str "cover"
114117
, display: E.str $ if loaded then "block" else "none"
115118
}
119+
<> props.imgStyle
116120
, onLoad: handler_ $ setLoaded true
117121
, onError: handler_ $ setLoaded true
118122
}
@@ -126,6 +130,7 @@ image =
126130
{ component: Image
127131
, content: ""
128132
, placeholder: Nothing
133+
, imgStyle: E.css {}
129134
}
130135

131136
defaultImageStyle :: LumiTheme -> Style
@@ -141,13 +146,13 @@ image =
141146
, borderColor: E.color colors.black4
142147
}
143148

144-
145149
data Thumbnail = Thumbnail
146150

147151
type ThumbnailProps
148152
= ( component :: Thumbnail
149153
, content :: String
150154
, placeholder :: Maybe JSX
155+
, imgStyle :: Style
151156
)
152157

153158
-- | A thumbnail can support size restrictions
@@ -161,13 +166,15 @@ thumbnail =
161166
$ _ { content = props.content
162167
, placeholder = props.placeholder
163168
, css = toCSS defaultSize <> props.css
169+
, imgStyle = props.imgStyle
164170
}
165171

166172
where
167173
defaults =
168174
{ component: Thumbnail
169175
, content: ""
170176
, placeholder: Nothing
177+
, imgStyle: E.css {}
171178
}
172179

173180
-- | The `c` type parameter lets us constrain the type of component to which
@@ -187,6 +194,18 @@ resize props =
187194
round :: ImageModifier Thumbnail
188195
round = style_ mkRound
189196

197+
cover :: PropsModifier ThumbnailProps
198+
cover =
199+
propsModifier \props -> props
200+
{ imgStyle = props.imgStyle <> E.css { objectFit: E.str "cover" }
201+
}
202+
203+
contain :: PropsModifier ThumbnailProps
204+
contain =
205+
propsModifier \props -> props
206+
{ imgStyle = props.imgStyle <> E.css { objectFit: E.str "contain" }
207+
}
208+
190209
resizeSquare :: Int -> ImageModifier Thumbnail
191210
resizeSquare size =
192211
style \(LumiTheme theme) ->

0 commit comments

Comments
 (0)