Skip to content

Commit 845b17d

Browse files
committed
Update the box-shadow implementation
Update the implementation of the `box-shadow` CSS property and make it similar to how it is currently implemented in Clay. This is a breaking change.
1 parent f86824b commit 845b17d

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

src/CSS.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module CSS (module X) where
33
import CSS.Animation (AnimationDirection(..), AnimationName(..), FillMode(..), IterationCount(..), alternate, alternateReverse, animation, backwards, forwards, infinite, iterationCount, normalAnimationDirection, reverse) as X
44
import CSS.Background (class Background, class Loc, BackgroundAttachment, BackgroundClip, BackgroundImage, BackgroundOrigin, BackgroundPosition, BackgroundRepeat, BackgroundSize, Direction, Location, Side, angular, attachFixed, attachScroll, background, backgroundAttachment, backgroundAttachments, backgroundClip, backgroundClips, backgroundColor, backgroundImage, backgroundImages, backgroundOrigin, backgroundOrigins, backgroundPosition, backgroundPositions, backgroundRepeat, backgroundRepeats, backgroundSize, backgroundSizes, boxClip, by, contain, cover, location, noRepeat, origin, placed, positioned, repeat, repeatX, repeatY, round, sideBottom, sideCenter, sideLeft, sideMiddle, sideRight, sideTop, space, straight, url, xyRepeat) as X
55
import CSS.Border (Stroke(..), border, borderBottom, borderColor, borderLeft, borderRadius, borderRight, borderTop, dashed, dotted, double, groove, inset, outset, ridge, solid, wavy) as X
6-
import CSS.Box (BoxType, borderBox, boxShadow, boxSizing, contentBox, insetBoxShadow, paddingBox) as X
6+
import CSS.Box (BoxType, BoxShadow, borderBox, boxShadow, boxSizing, contentBox, paddingBox) as X
77
import CSS.Color (Color, ColorSpace(..), aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black, blanchedalmond, blue, blueviolet, brightness, brown, burlywood, cadetblue, chartreuse, chocolate, complementary, contrast, coral, cornflowerblue, cornsilk, crimson, cssStringHSLA, cssStringRGBA, cyan, darkblue, darkcyan, darken, darkgoldenrod, darkgray, darkgreen, darkgrey, darkkhaki, darkmagenta, darkolivegreen, darkorange, darkorchid, darkred, darksalmon, darkseagreen, darkslateblue, darkslategray, darkslategrey, darkturquoise, darkviolet, deeppink, deepskyblue, desaturate, dimgray, dimgrey, distance, dodgerblue, firebrick, floralwhite, forestgreen, fromHexString, fromInt, fuchsia, gainsboro, ghostwhite, gold, goldenrod, gray, graytone, green, greenyellow, grey, honeydew, hotpink, hsl, hsla, indianred, indigo, isLight, isReadable, ivory, khaki, lab, lavender, lavenderblush, lawngreen, lch, lemonchiffon, lightblue, lightcoral, lightcyan, lighten, lightgoldenrodyellow, lightgray, lightgreen, lightgrey, lightpink, lightsalmon, lightseagreen, lightskyblue, lightslategray, lightslategrey, lightsteelblue, lightyellow, lime, limegreen, linen, luminance, magenta, maroon, mediumaquamarine, mediumblue, mediumorchid, mediumpurple, mediumseagreen, mediumslateblue, mediumspringgreen, mediumturquoise, mediumvioletred, midnightblue, mintcream, mistyrose, mix, moccasin, navajowhite, navy, oldlace, olive, olivedrab, orange, orangered, orchid, palegoldenrod, palegreen, paleturquoise, palevioletred, papayawhip, peachpuff, peru, pink, plum, powderblue, purple, red, rgb, rgb', rgba, rgba', rosybrown, rotateHue, royalblue, saddlebrown, salmon, sandybrown, saturate, seagreen, seashell, sienna, silver, skyblue, slateblue, slategray, slategrey, snow, springgreen, steelblue, tan, teal, textColor, thistle, toGray, toHSLA, toHexString, toLCh, toLab, toRGBA, toRGBA', toXYZ, tomato, turquoise, violet, wheat, white, whitesmoke, xyz, yellow, yellowgreen) as X
88
import CSS.Display (ClearFloat(..), Display(..), Float(..), Position(..), absolute, block, clear, clearBoth, clearInlineEnd, clearInlineStart, clearLeft, clearRight, display, displayInherit, displayNone, fixed, flex, float, floatLeft, floatRight, grid, inline, inlineBlock, inlineFlex, inlineGrid, inlineTable, listItem, position, relative, runIn, static, table, tableCaption, tableCell, tableColumn, tableColumnGroup, tableFooterGroup, tableHeaderGroup, tableRow, tableRowGroup, zIndex, opacity) as X
99
import CSS.Elements (a, abbr, address, area, article, aside, audio, b, bdi, bdo, blockquote, body, br, button, canvas, caption, cite, code, col, colgroup, datalist, dd, del, details, dfn, div, dl, dt, embed, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hr, html, i, iframe, img, input, ins, kbd, label, legend, li, main, map, mark, meter, nav, object, ol, optgroup, output, p, pre, progress, q, s, samp, section, small, span, strong, sub, summary, sup, tbody, td, textarea, tfoot, th, thead, tr, u, ul, var) as X

src/CSS/Box.purs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
module CSS.Box
22
( BoxType
3+
, BoxShadow
34
, paddingBox, borderBox, contentBox
45
, boxSizing
56
, boxShadow
6-
, insetBoxShadow
7+
, shadow
8+
, shadowWithBlur
9+
, shadowWithSpread
10+
, bsInset
11+
, bsColor
712
)
813
where
914

1015
import Prelude
1116

12-
import CSS.Border (Stroke)
17+
import Data.NonEmpty (NonEmpty)
1318
import CSS.Color (Color)
1419
import CSS.Common (class Inherit, browsers)
15-
import CSS.Property (class Val, Value, (!))
20+
import CSS.Property (class Val, Value, value, (!))
1621
import CSS.Size (Size)
1722
import CSS.String (class IsString, fromString)
1823
import CSS.Stylesheet (CSS, prefixed, key)
@@ -50,12 +55,47 @@ boxSizing = key $ fromString "box-sizing"
5055

5156
-------------------------------------------------------------------------------
5257

53-
boxShadow :: forall a. Size a -> Size a -> Size a -> Color -> CSS
54-
boxShadow x y w c =
55-
prefixed (browsers <> fromString "box-shadow") (x ! y ! w ! c)
56-
57-
-------------------------------------------------------------------------------
58-
59-
insetBoxShadow ::
60-
forall a. Stroke -> Size a -> Size a -> Size a -> Color -> CSS
61-
insetBoxShadow x y w c z = prefixed (browsers <> fromString "box-shadow") (x ! y ! w ! c ! z)
58+
newtype BoxShadow = BoxShadow Value
59+
60+
derive instance eqBoxShadow :: Eq BoxShadow
61+
derive instance ordBoxShadow :: Ord BoxShadow
62+
63+
instance valBoxShadow :: Val BoxShadow where
64+
value (BoxShadow v) = v
65+
66+
-- | This function will usually take a singleton list, but requiring a (non-empty) list
67+
-- | prevents accidentally applying the modifiers (`bsInset`, `bsColor`) incorrectly.
68+
-- |
69+
-- | `singleton` (from "Data.NonEmpty") creates a singleton list.
70+
-- |
71+
-- | ```purescript
72+
-- | boxShadow $ singleton $ shadow (px 1.0) (px 1.0)
73+
-- | ```
74+
-- |
75+
-- | For supplying multiple `BoxShadow` values:
76+
-- |
77+
-- | ```purescript
78+
-- | boxShadow $
79+
-- | red `bsColor` shadow (px 3.0) (px 3.0) :|
80+
-- | [olive `bsColor` shadowWithBlur (em (-1.0)) (em 0.0) (em 0.4)]
81+
-- | ```
82+
boxShadow :: NonEmpty Array BoxShadow -> CSS
83+
boxShadow = prefixed (browsers <> fromString "box-shadow") <<< value
84+
85+
shadow :: forall a. Size a -> Size a -> BoxShadow
86+
shadow x y = BoxShadow <<< value $ (x ! y)
87+
88+
shadowWithBlur :: forall a. Size a -> Size a -> Size a -> BoxShadow
89+
shadowWithBlur x y w = BoxShadow <<< value $ (x ! y ! w)
90+
91+
shadowWithSpread :: forall a. Size a -> Size a -> Size a -> Size a -> BoxShadow
92+
shadowWithSpread x y blurRadius spreadRadius =
93+
BoxShadow <<< value $ (x ! y ! blurRadius ! spreadRadius)
94+
95+
-- | Adapt the provided `box-shadow` with the `inset` prefix.
96+
bsInset :: BoxShadow -> BoxShadow
97+
bsInset (BoxShadow v) = BoxShadow <<< value $ ("inset" ! v)
98+
99+
-- | Supply a color to the provided `box-shadow`.
100+
bsColor :: Color -> BoxShadow -> BoxShadow
101+
bsColor c (BoxShadow v) = BoxShadow <<< value $ (v ! c)

test/Main.purs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import Prelude
44

55
import Effect (Effect)
66
import Effect.Exception (error, throwException)
7-
import CSS (Rendered, Path(..), Predicate(..), Refinement(..), Selector(..), FontFaceSrc(..), FontFaceFormat(..), renderedSheet, renderedInline, fromString, selector, block, display, render, borderBox, boxSizing, contentBox, blue, color, body, a, p, px, dashed, border, inlineBlock, red, (?), (&), (|>), (|*), (|+), byId, byClass, (@=), (^=), ($=), (*=), (~=), (|=), hover, fontFaceSrc, fontStyle, deg, zIndex, textOverflow, opacity)
7+
import CSS (Rendered, Path(..), Predicate(..), Refinement(..), Selector(..), FontFaceSrc(..), FontFaceFormat(..), renderedSheet, renderedInline, fromString, selector, block, display, render, borderBox, boxSizing, contentBox, blue, color, body, a, p, px, dashed, border, inlineBlock, red, gold, teal, olive, black, (?), (&), (|>), (|*), (|+), byId, byClass, (@=), (^=), ($=), (*=), (~=), (|=), hover, fontFaceSrc, fontStyle, deg, rgba, em, zIndex, textOverflow, opacity)
88
import CSS.FontStyle as FontStyle
99
import CSS.Text.Overflow as TextOverflow
10+
import CSS.Box (boxShadow, shadow, shadowWithBlur, shadowWithSpread, bsColor, bsInset)
1011
import Data.Maybe (Maybe(..))
11-
import Data.NonEmpty (singleton)
12+
import Data.NonEmpty (singleton, (:|))
1213

1314
example1 :: Rendered
1415
example1 = render do
@@ -115,6 +116,28 @@ exampleTextOverflow2 :: Rendered
115116
exampleTextOverflow2 = render do
116117
textOverflow $ TextOverflow.custom "foobar"
117118

119+
singleShadow :: Rendered
120+
singleShadow = render do
121+
boxShadow $ singleton $ bsColor teal $ shadow (px 60.0) (px (-16.0))
122+
123+
singleShadowWithBlur :: Rendered
124+
singleShadowWithBlur = render do
125+
boxShadow $ singleton $ bsColor black $ shadowWithBlur (px 10.0) (px 5.0) (px 5.0)
126+
127+
singleShadowWithSpread :: Rendered
128+
singleShadowWithSpread = render do
129+
boxShadow $ singleton $ rgba 0 0 0 0.2 `bsColor` shadowWithSpread (px 2.0) (px 2.0) (px 2.0) (px 1.0)
130+
131+
singleInsetShadow :: Rendered
132+
singleInsetShadow = render do
133+
boxShadow $ singleton $ bsInset $ gold `bsColor` shadow (em 5.0) (em 1.0)
134+
135+
multipleShadows :: Rendered
136+
multipleShadows = render do
137+
boxShadow $
138+
red `bsColor` shadow (px 3.0) (px 3.0) :|
139+
[olive `bsColor` shadowWithBlur (em (-1.0)) (em 0.0) (em 0.4)]
140+
118141
nestedNodes :: Rendered
119142
nestedNodes = render do
120143
fromString "#parent" ? do
@@ -171,3 +194,7 @@ main = do
171194
renderedSheet attrSpace `assertEqual` Just "p[foo~='bar'] { display: block }\n"
172195
renderedSheet attrHyph `assertEqual` Just "p[foo|='bar'] { display: block }\n"
173196

197+
renderedInline singleShadow `assertEqual` Just "-webkit-box-shadow: 60.0px -16.0px hsl(180.0, 100.0%, 25.1%); -moz-box-shadow: 60.0px -16.0px hsl(180.0, 100.0%, 25.1%); -ms-box-shadow: 60.0px -16.0px hsl(180.0, 100.0%, 25.1%); -o-box-shadow: 60.0px -16.0px hsl(180.0, 100.0%, 25.1%); box-shadow: 60.0px -16.0px hsl(180.0, 100.0%, 25.1%)"
198+
renderedInline singleShadowWithBlur `assertEqual` Just "-webkit-box-shadow: 10.0px 5.0px 5.0px hsl(0.0, 0.0%, 0.0%); -moz-box-shadow: 10.0px 5.0px 5.0px hsl(0.0, 0.0%, 0.0%); -ms-box-shadow: 10.0px 5.0px 5.0px hsl(0.0, 0.0%, 0.0%); -o-box-shadow: 10.0px 5.0px 5.0px hsl(0.0, 0.0%, 0.0%); box-shadow: 10.0px 5.0px 5.0px hsl(0.0, 0.0%, 0.0%)"
199+
renderedInline singleShadowWithSpread `assertEqual` Just "-webkit-box-shadow: 2.0px 2.0px 2.0px 1.0px hsla(0.0, 0.0%, 0.0%, 0.2); -moz-box-shadow: 2.0px 2.0px 2.0px 1.0px hsla(0.0, 0.0%, 0.0%, 0.2); -ms-box-shadow: 2.0px 2.0px 2.0px 1.0px hsla(0.0, 0.0%, 0.0%, 0.2); -o-box-shadow: 2.0px 2.0px 2.0px 1.0px hsla(0.0, 0.0%, 0.0%, 0.2); box-shadow: 2.0px 2.0px 2.0px 1.0px hsla(0.0, 0.0%, 0.0%, 0.2)"
200+
renderedInline multipleShadows `assertEqual` Just "-webkit-box-shadow: 3.0px 3.0px hsl(0.0, 100.0%, 50.0%), -1.0em 0.0em 0.4em hsl(60.0, 100.0%, 25.1%); -moz-box-shadow: 3.0px 3.0px hsl(0.0, 100.0%, 50.0%), -1.0em 0.0em 0.4em hsl(60.0, 100.0%, 25.1%); -ms-box-shadow: 3.0px 3.0px hsl(0.0, 100.0%, 50.0%), -1.0em 0.0em 0.4em hsl(60.0, 100.0%, 25.1%); -o-box-shadow: 3.0px 3.0px hsl(0.0, 100.0%, 50.0%), -1.0em 0.0em 0.4em hsl(60.0, 100.0%, 25.1%); box-shadow: 3.0px 3.0px hsl(0.0, 100.0%, 50.0%), -1.0em 0.0em 0.4em hsl(60.0, 100.0%, 25.1%)"

0 commit comments

Comments
 (0)