Skip to content

Commit 3001071

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 2513f2e commit 3001071

File tree

4 files changed

+98
-16
lines changed

4 files changed

+98
-16
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/Background.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ import CSS.String (class IsString, fromString)
8686
import CSS.Stylesheet (CSS, key)
8787

8888
-- | We implement the generic background property as a type class that accepts
89-
-- multiple value types. This allows us to combine different background aspects
90-
-- into a shorthand syntax.
89+
-- | multiple value types. This allows us to combine different background aspects
90+
-- | into a shorthand syntax.
9191

9292
class Val a <= Background a where
9393
background :: a -> CSS

src/CSS/Box.purs

Lines changed: 66 additions & 11 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)
14-
import CSS.Common (class Inherit, browsers)
15-
import CSS.Property (class Val, Value, (!))
19+
import CSS.Common (class None, class Inherit, class Initial, class Unset, browsers)
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,62 @@ 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)
58+
newtype BoxShadow = BoxShadow Value
5659

57-
-------------------------------------------------------------------------------
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+
instance isStringBoxShadow :: IsString BoxShadow where
67+
fromString = BoxShadow <<< fromString
68+
69+
instance noneBoxShadow :: None BoxShadow where
70+
none = fromString "none"
71+
72+
instance inheritBoxShadow :: Inherit BoxShadow where
73+
inherit = fromString "inherit"
5874

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)
75+
instance initialBoxShadow :: Initial BoxShadow where
76+
initial = fromString "initial"
77+
78+
instance unsetBoxShadow :: Unset BoxShadow where
79+
unset = fromString "unset"
80+
81+
-- | This function will usually take a singleton list, but requiring a (non-empty) list
82+
-- | prevents accidentally applying the modifiers (`bsInset`, `bsColor`) incorrectly.
83+
-- |
84+
-- | `singleton` (from "Data.NonEmpty") creates a singleton list.
85+
-- |
86+
-- | ```purescript
87+
-- | boxShadow $ singleton $ shadow (px 1.0) (px 1.0)
88+
-- | ```
89+
-- |
90+
-- | For supplying multiple `BoxShadow` values:
91+
-- |
92+
-- | ```purescript
93+
-- | boxShadow $
94+
-- | red `bsColor` shadow (px 3.0) (px 3.0) :|
95+
-- | [olive `bsColor` shadowWithBlur (em (-1.0)) (em 0.0) (em 0.4)]
96+
-- | ```
97+
boxShadow :: NonEmpty Array BoxShadow -> CSS
98+
boxShadow = prefixed (browsers <> fromString "box-shadow") <<< value
99+
100+
shadow :: forall a. Size a -> Size a -> BoxShadow
101+
shadow x y = BoxShadow <<< value $ (x ! y)
102+
103+
shadowWithBlur :: forall a. Size a -> Size a -> Size a -> BoxShadow
104+
shadowWithBlur x y w = BoxShadow <<< value $ (x ! y ! w)
105+
106+
shadowWithSpread :: forall a. Size a -> Size a -> Size a -> Size a -> BoxShadow
107+
shadowWithSpread x y blurRadius spreadRadius =
108+
BoxShadow <<< value $ (x ! y ! blurRadius ! spreadRadius)
109+
110+
-- | Adapt the provided `box-shadow` with the `inset` prefix.
111+
bsInset :: BoxShadow -> BoxShadow
112+
bsInset (BoxShadow v) = BoxShadow <<< value $ ("inset" ! v)
113+
114+
-- | Supply a color to the provided `box-shadow`.
115+
bsColor :: Color -> BoxShadow -> BoxShadow
116+
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,12 +4,13 @@ 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, transform)
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, transform)
88
import CSS.FontStyle as FontStyle
99
import CSS.Text.Overflow as TextOverflow
1010
import CSS.Transform as Transform
11+
import CSS.Box (boxShadow, shadow, shadowWithBlur, shadowWithSpread, bsColor, bsInset)
1112
import Data.Maybe (Maybe(..))
12-
import Data.NonEmpty (singleton)
13+
import Data.NonEmpty (singleton, (:|))
1314

1415
example1 :: Rendered
1516
example1 = render do
@@ -126,6 +127,28 @@ exampleTextOverflow2 :: Rendered
126127
exampleTextOverflow2 = render do
127128
textOverflow $ TextOverflow.custom "foobar"
128129

130+
singleShadow :: Rendered
131+
singleShadow = render do
132+
boxShadow $ singleton $ bsColor teal $ shadow (px 60.0) (px (-16.0))
133+
134+
singleShadowWithBlur :: Rendered
135+
singleShadowWithBlur = render do
136+
boxShadow $ singleton $ bsColor black $ shadowWithBlur (px 10.0) (px 5.0) (px 5.0)
137+
138+
singleShadowWithSpread :: Rendered
139+
singleShadowWithSpread = render do
140+
boxShadow $ singleton $ rgba 0 0 0 0.2 `bsColor` shadowWithSpread (px 2.0) (px 2.0) (px 2.0) (px 1.0)
141+
142+
singleInsetShadow :: Rendered
143+
singleInsetShadow = render do
144+
boxShadow $ singleton $ bsInset $ gold `bsColor` shadow (em 5.0) (em 1.0)
145+
146+
multipleShadows :: Rendered
147+
multipleShadows = render do
148+
boxShadow $
149+
red `bsColor` shadow (px 3.0) (px 3.0) :|
150+
[olive `bsColor` shadowWithBlur (em (-1.0)) (em 0.0) (em 0.4)]
151+
129152
nestedNodes :: Rendered
130153
nestedNodes = render do
131154
fromString "#parent" ? do
@@ -185,3 +208,7 @@ main = do
185208
renderedInline scaleTransform1 `assertEqual` Just "transform: scaleX(1.0); transform: scaleY(0.5); transform: scaleZ(0.5)"
186209
renderedInline scaleTransform2 `assertEqual` Just "transform: scale(0.2, 0.8)"
187210

211+
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%)"
212+
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%)"
213+
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)"
214+
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)