Skip to content

Commit 5d86e5c

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 9e78699 commit 5d86e5c

File tree

3 files changed

+83
-15
lines changed

3 files changed

+83
-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) 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: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ 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, px, dashed, border, inlineBlock, red, (?), fontFaceSrc, zIndex)
7+
import CSS (Rendered, Path(..), Predicate(..), Refinement(..), Selector(..), FontFaceSrc(..), FontFaceFormat(..), renderedSheet, renderedInline, fromString, selector, block, display, render, borderBox, boxSizing, contentBox, blue, teal, black, gold, olive, color, body, px, em, dashed, border, inlineBlock, red, rgba, (?), fontFaceSrc, zIndex)
8+
import CSS.Box (boxShadow, shadow, shadowWithBlur, shadowWithSpread, bsColor, bsInset)
89
import Data.Maybe (Maybe(..))
9-
import Data.NonEmpty (singleton)
10+
import Data.NonEmpty (singleton, (:|))
1011

1112
example1 :: Rendered
1213
example1 = render do
@@ -41,6 +42,28 @@ example7 :: Rendered
4142
example7 = render do
4243
zIndex 11
4344

45+
singleShadow :: Rendered
46+
singleShadow = render do
47+
boxShadow $ singleton $ bsColor teal $ shadow (px 60.0) (px (-16.0))
48+
49+
singleShadowWithBlur :: Rendered
50+
singleShadowWithBlur = render do
51+
boxShadow $ singleton $ bsColor black $ shadowWithBlur (px 10.0) (px 5.0) (px 5.0)
52+
53+
singleShadowWithSpread :: Rendered
54+
singleShadowWithSpread = render do
55+
boxShadow $ singleton $ rgba 0 0 0 0.2 `bsColor` shadowWithSpread (px 2.0) (px 2.0) (px 2.0) (px 1.0)
56+
57+
singleInsetShadow :: Rendered
58+
singleInsetShadow = render do
59+
boxShadow $ singleton $ bsInset $ gold `bsColor` shadow (em 5.0) (em 1.0)
60+
61+
multipleShadows :: Rendered
62+
multipleShadows = render do
63+
boxShadow $
64+
red `bsColor` shadow (px 3.0) (px 3.0) :|
65+
[olive `bsColor` shadowWithBlur (em (-1.0)) (em 0.0) (em 0.4)]
66+
4467
nestedNodes :: Rendered
4568
nestedNodes = render do
4669
fromString "#parent" ? do
@@ -76,3 +99,8 @@ main = do
7699
renderedInline example6 `assertEqual` Just "src: url(\"font.woff\") format(\"woff\")"
77100

78101
renderedInline example7 `assertEqual` Just "z-index: 11"
102+
103+
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%)"
104+
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%)"
105+
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)"
106+
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)