Skip to content

Commit b9a9284

Browse files
committed
Add combinators for length units
1 parent 60bd9bd commit b9a9284

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"purescript-react-basic": "^13.0.0",
1717
"purescript-unsafe-reference": "^3.0.1",
1818
"purescript-colors": "^5.0.0",
19-
"purescript-foreign": "^5.0.0"
19+
"purescript-foreign": "^5.0.0",
20+
"purescript-numbers": "^7.0.0"
2021
},
2122
"devDependencies": {
2223
"purescript-psci-support": "^4.0.0",

src/React/Basic/Emotion.purs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module React.Basic.Emotion
1818
, unset
1919
, url
2020
, color
21+
, px, px', cm, mm, inches, pt, pc
22+
, em, ex, ch, rem, vw, vh, vmin, vmax, percent
2123
) where
2224

2325
import Prelude
@@ -27,6 +29,8 @@ import Control.Monad.Except (runExcept)
2729
import Data.Array as Array
2830
import Data.Either (Either(..))
2931
import Data.Function.Uncurried (Fn2, runFn2)
32+
import Data.Int as Int
33+
import Data.Number.Format (toString) as Number
3034
import Foreign as F
3135
import Prim.TypeError (class Warn, Text)
3236
import React.Basic (JSX, ReactComponent)
@@ -153,3 +157,77 @@ url (URL url') = str ("url(" <> url' <> ")")
153157

154158
color :: Color -> StyleProperty
155159
color = str <<< cssStringHSLA
160+
161+
-- Absolute length units
162+
163+
-- | Pixels. This function does not take a `Number` because approaches to
164+
-- | subpixel rendering vary among browser implementations.
165+
px :: Int -> StyleProperty
166+
px x = str $ Int.toStringAs Int.decimal x <> "px"
167+
168+
-- | Pixels and subpixels.
169+
-- |
170+
-- | WARNING: Approaches to subpixel rendering vary among browser
171+
-- | implementations. This means that non-integer pixel values may be displayed
172+
-- | differently in different browsers.
173+
px' :: Number -> StyleProperty
174+
px' x = str $ Number.toString x <> "px"
175+
176+
-- | Centimeters
177+
cm :: Number -> StyleProperty
178+
cm x = str $ Number.toString x <> "cm"
179+
180+
-- | Milimeters
181+
mm :: Number -> StyleProperty
182+
mm x = str $ Number.toString x <> "mm"
183+
184+
-- | Inches (1in ≈ 2.54cm)
185+
inches :: Number -> StyleProperty
186+
inches x = str $ Number.toString x <> "in"
187+
188+
-- | Points (1pt = 1/72 of 1in)
189+
pt :: Number -> StyleProperty
190+
pt x = str $ Number.toString x <> "pt"
191+
192+
-- | Picas (1pc = 12 pt)
193+
pc :: Number -> StyleProperty
194+
pc x = str $ Number.toString x <> "pc"
195+
196+
-- Relative length units
197+
198+
-- | Relative to the font-size of the element (2em means 2 times the size of
199+
-- | the current font).
200+
em :: Number -> StyleProperty
201+
em x = str $ Number.toString x <> "em"
202+
203+
-- | Relative to the x-height of the current font (rarely used).
204+
ex :: Number -> StyleProperty
205+
ex x = str $ Number.toString x <> "ex"
206+
207+
-- | Relative to the width of the "0" (zero) character.
208+
ch :: Number -> StyleProperty
209+
ch x = str $ Number.toString x <> "ch"
210+
211+
-- | Relative to font-size of the root element.
212+
rem :: Number -> StyleProperty
213+
rem x = str $ Number.toString x <> "rem"
214+
215+
-- | Relative to 1% of the width of the viewport.
216+
vw :: Number -> StyleProperty
217+
vw x = str $ Number.toString x <> "vw"
218+
219+
-- | Relative to 1% of the height of the viewport.
220+
vh :: Number -> StyleProperty
221+
vh x = str $ Number.toString x <> "vh"
222+
223+
-- | Relative to 1% of viewport's smaller dimension.
224+
vmin :: Number -> StyleProperty
225+
vmin x = str $ Number.toString x <> "vmin"
226+
227+
-- | Relative to 1% of viewport's larger dimension.
228+
vmax :: Number -> StyleProperty
229+
vmax x = str $ Number.toString x <> "vmax"
230+
231+
-- | Relative to the parent element.
232+
percent :: Number -> StyleProperty
233+
percent x = str $ Number.toString x <> "%"

0 commit comments

Comments
 (0)