Skip to content

Commit 2b0a1a5

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

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/React/Basic/Emotion.purs

Lines changed: 70 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, 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,69 @@ 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 varies among browser implementations.
165+
px :: Int -> StyleProperty
166+
px x = str $ Int.toStringAs Int.decimal x <> "px"
167+
168+
-- | Centimeters
169+
cm :: Number -> StyleProperty
170+
cm x = str $ Number.toString x <> "cm"
171+
172+
-- | Milimeters
173+
mm :: Number -> StyleProperty
174+
mm x = str $ Number.toString x <> "mm"
175+
176+
-- | Inches (1in ≈ 2.54cm)
177+
inches :: Number -> StyleProperty
178+
inches x = str $ Number.toString x <> "in"
179+
180+
-- | Points (1pt = 1/72 of 1in)
181+
pt :: Number -> StyleProperty
182+
pt x = str $ Number.toString x <> "pt"
183+
184+
-- | Picas (1pc = 12 pt)
185+
pc :: Number -> StyleProperty
186+
pc x = str $ Number.toString x <> "pc"
187+
188+
-- Relative length units
189+
190+
-- | Relative to the font-size of the element (2em means 2 times the size of
191+
-- | the current font).
192+
em :: Number -> StyleProperty
193+
em x = str $ Number.toString x <> "em"
194+
195+
-- | Relative to the x-height of the current font (rarely used).
196+
ex :: Number -> StyleProperty
197+
ex x = str $ Number.toString x <> "ex"
198+
199+
-- | Relative to the width of the "0" (zero) character.
200+
ch :: Number -> StyleProperty
201+
ch x = str $ Number.toString x <> "ch"
202+
203+
-- | Relative to font-size of the root element.
204+
rem :: Number -> StyleProperty
205+
rem x = str $ Number.toString x <> "rem"
206+
207+
-- | Relative to 1% of the width of the viewport.
208+
vw :: Number -> StyleProperty
209+
vw x = str $ Number.toString x <> "vw"
210+
211+
-- | Relative to 1% of the height of the viewport.
212+
vh :: Number -> StyleProperty
213+
vh x = str $ Number.toString x <> "vh"
214+
215+
-- | Relative to 1% of viewport's smaller dimension.
216+
vmin :: Number -> StyleProperty
217+
vmin x = str $ Number.toString x <> "vmin"
218+
219+
-- | Relative to 1% of viewport's larger dimension.
220+
vmax :: Number -> StyleProperty
221+
vmax x = str $ Number.toString x <> "vmax"
222+
223+
-- | Relative to the parent element.
224+
percent :: Number -> StyleProperty
225+
percent x = str $ Number.toString x <> "%"

0 commit comments

Comments
 (0)