Skip to content

Commit 85db0b5

Browse files
authoredMay 27, 2020
Add combinators for length units and !important (#3)
* Deprecate 'int' and 'num' * Add combinators for length units * Add combinator for making a property !important
1 parent 939e102 commit 85db0b5

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed
 

‎bower.json

+2-1
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.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ exports.elementKeyed_ = (component, props) =>
3434
exports.global = Emotion.Global;
3535

3636
exports.css = _homogeneousDict => Emotion.css;
37+
38+
exports.important = prop => typeof prop === "string" ? prop + " !important" : prop;

‎src/React/Basic/Emotion.purs

+91-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module React.Basic.Emotion
77
, prop
88
, element
99
, css
10+
, important
1011
, nested
1112
, merge
1213
, str
@@ -18,15 +19,21 @@ module React.Basic.Emotion
1819
, unset
1920
, url
2021
, color
22+
, px, px', cm, mm, inches, pt, pc
23+
, em, ex, ch, rem, vw, vh, vmin, vmax, percent
2124
) where
2225

2326
import Prelude
27+
2428
import Color (Color, cssStringHSLA)
2529
import Control.Monad.Except (runExcept)
2630
import Data.Array as Array
2731
import Data.Either (Either(..))
2832
import Data.Function.Uncurried (Fn2, runFn2)
33+
import Data.Int as Int
34+
import Data.Number.Format (toString) as Number
2935
import Foreign as F
36+
import Prim.TypeError (class Warn, Text)
3037
import React.Basic (JSX, ReactComponent)
3138
import Type.Row.Homogeneous (class Homogeneous)
3239
import Unsafe.Coerce (unsafeCoerce)
@@ -113,6 +120,8 @@ foreign import global :: ReactComponent { styles :: Style }
113120

114121
foreign import css :: forall r. Homogeneous r StyleProperty => { | r } -> Style
115122

123+
foreign import important :: StyleProperty -> StyleProperty
124+
116125
nested :: Style -> StyleProperty
117126
nested = unsafeCoerce
118127

@@ -122,10 +131,16 @@ merge = unsafeCoerce
122131
str :: String -> StyleProperty
123132
str = unsafeCoerce
124133

125-
int :: Int -> StyleProperty
134+
int
135+
:: Warn (Text "`int` is deprecated and may be removed in future versions. Prefer one of the unit combinators like `px` or `em` instead.")
136+
=> Int
137+
-> StyleProperty
126138
int = unsafeCoerce
127139

128-
num :: Number -> StyleProperty
140+
num
141+
:: Warn (Text "`int` is deprecated and may be removed in future versions. Prefer one of the unit combinators like `px` or `em` instead.")
142+
=> Number
143+
-> StyleProperty
129144
num = unsafeCoerce
130145

131146
fallbacks :: Array StyleProperty -> StyleProperty
@@ -145,3 +160,77 @@ url (URL url') = str ("url(" <> url' <> ")")
145160

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

0 commit comments

Comments
 (0)