Skip to content
This repository was archived by the owner on Jan 27, 2024. It is now read-only.

Commit e9f905d

Browse files
committed
feat(shadow): added support for shadow tokens
1 parent 23d6c8c commit e9f905d

File tree

6 files changed

+119
-32
lines changed

6 files changed

+119
-32
lines changed

packages/css/.storybook/preview-head.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
html {
1717
font-size: 10px;
1818
}
19+
20+
:root {
21+
--cool-shadow: 1px 2px 0 0 #f36;
22+
}
1923
</style>
2024

2125
<link rel="preconnect" href="https://fonts.googleapis.com" />

packages/css/__tests__/AncestorCss_Custom.res

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,9 @@ include AncestorCss.Make(
1515
| #secondary => #hex("363636")
1616
}
1717
},
18-
{
19-
type spacing = int
20-
let spacing = v => #px(v * 8)
21-
},
22-
{
23-
type radius = int
24-
let radius = v => #px(v * 8)
25-
},
26-
{
27-
type zIndex = int
28-
let zIndex = v => v
29-
},
30-
{
31-
type fontFamily = AncestorCss_WrappedTypes.FontFamily.t
32-
type fontSize = Css_AtomicTypes.Length.t
33-
type fontWeight = Css_AtomicTypes.FontWeight.t
34-
type lineHeight = AncestorCss_WrappedTypes.LineHeight.t
35-
type letterSpacing = Css_AtomicTypes.Length.t
36-
37-
let fontFamily = v => v
38-
let fontSize = v => v
39-
let fontWeight = v => v
40-
let lineHeight = v => v
41-
let letterSpacing = v => v
42-
},
18+
AncestorCss.Defaults.Spacing,
19+
AncestorCss.Defaults.Radius,
20+
AncestorCss.Defaults.ZIndex,
21+
AncestorCss.Defaults.Typography,
22+
AncestorCss.Defaults.Shadows,
4323
)

packages/css/src/AncestorCss.res

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/*
2+
* NOTE: AncestorCss defaults.
3+
*/
14
module Defaults = {
25
let identity = v => v
36

@@ -65,6 +68,21 @@ module Defaults = {
6568
include LineHeight
6669
include LetterSpacing
6770
}
71+
72+
module BoxShadows = {
73+
type boxShadow = AncestorCss_WrappedTypes.BoxShadow.t
74+
let boxShadow = identity
75+
}
76+
77+
module TextShadows = {
78+
type textShadow = AncestorCss_WrappedTypes.TextShadow.t
79+
let textShadow = identity
80+
}
81+
82+
module Shadows = {
83+
include BoxShadows
84+
include TextShadows
85+
}
6886
}
6987

7088
module Make = (
@@ -74,16 +92,10 @@ module Make = (
7492
CustomRadius: AncestorCss_Config.Radius,
7593
CustomZIndex: AncestorCss_Config.ZIndex,
7694
CustomTypography: AncestorCss_Config.Typography,
95+
CustomShadows: AncestorCss_Config.Shadows,
7796
) => {
7897
include CssJs
7998

80-
module TokenizedShadow = {
81-
let box = (~x=?, ~y=?, ~blur=?, ~spread=?, ~inset=?, color) =>
82-
CssJs.Shadow.box(~x?, ~y?, ~blur?, ~spread?, ~inset?, color)
83-
84-
let text = (~x=?, ~y=?, ~blur=?, color) => CssJs.Shadow.text(~x?, ~y?, ~blur?, color)
85-
}
86-
8799
let zIndex = x => Css_Js_Core.zIndex(x->CustomZIndex.zIndex)
88100
/*
89101
* Colors
@@ -190,6 +202,51 @@ module Make = (
190202
let lineHeight = x => x->CustomTypography.lineHeight->Css_Js_Core.lineHeight
191203
let letterSpacing = x => x->CustomTypography.letterSpacing->Css_Js_Core.letterSpacing
192204

205+
/*
206+
* Shadows
207+
*/
208+
module TokenizedShadow = {
209+
let box = (~x=?, ~y=?, ~blur=?, ~spread=?, ~inset=?, color) =>
210+
CssJs.Shadow.box(~x?, ~y?, ~blur?, ~spread?, ~inset?, color)
211+
212+
let text = (~x=?, ~y=?, ~blur=?, color) => CssJs.Shadow.text(~x?, ~y?, ~blur?, color)
213+
}
214+
let boxShadow = x => x->CustomShadows.boxShadow->CssJs.boxShadow
215+
let textShadow = x => x->CustomShadows.textShadow->CssJs.textShadow
216+
/*
217+
* HACK: Unfortunately we need to override these two fucntions
218+
* because we can't convert an array of tokens into an array of box-shadows.
219+
*/
220+
let boxShadows = x => {
221+
let value =
222+
x
223+
->Js.Array2.map(CustomShadows.boxShadow)
224+
->Js.Array2.map(x =>
225+
switch x {
226+
| #...Css_Js_Core.Shadow.t as value => Css_Js_Core.Shadow.toString(value)
227+
| #...Css_AtomicTypes.Var.t as value => Css_AtomicTypes.Var.toString(value)
228+
}
229+
)
230+
->Js.Array2.joinWith(", ")
231+
232+
CssJs.unsafe("boxShadow", value)
233+
}
234+
235+
let textShadows = x => {
236+
let value =
237+
x
238+
->Js.Array2.map(CustomShadows.textShadow)
239+
->Js.Array2.map(x =>
240+
switch x {
241+
| #...Css_Js_Core.Shadow.t as value => Css_Js_Core.Shadow.toString(value)
242+
| #...Css_AtomicTypes.Var.t as value => Css_AtomicTypes.Var.toString(value)
243+
}
244+
)
245+
->Js.Array2.joinWith(", ")
246+
247+
CssJs.unsafe("textShadow", value)
248+
}
249+
193250
/*
194251
* Aliases to make the DX compatible with @ancestor-ui/core
195252
*/

packages/css/src/AncestorCss_Config.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ module type Typography = {
3636
let lineHeight: lineHeight => AncestorCss_WrappedTypes.LineHeight.t
3737
let letterSpacing: letterSpacing => Css_AtomicTypes.Length.t
3838
}
39+
40+
module type Shadows = {
41+
type textShadow
42+
type boxShadow
43+
44+
let textShadow: textShadow => AncestorCss_WrappedTypes.TextShadow.t
45+
let boxShadow: boxShadow => AncestorCss_WrappedTypes.BoxShadow.t
46+
}

packages/css/src/AncestorCss_Stories.res

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ module CustomCss = AncestorCss.Make(
1313
AncestorCss.Defaults.Radius,
1414
AncestorCss.Defaults.ZIndex,
1515
AncestorCss.Defaults.Typography,
16+
{
17+
include AncestorCss.Defaults.TextShadows
18+
type boxShadow = [
19+
| #simple
20+
| #cool
21+
]
22+
23+
let boxShadow = x =>
24+
switch x {
25+
| #simple => CssJs.Shadow.box(~x=1->#px, ~y=2->#px, #hex("363636"))
26+
| #cool => #var("--cool-shadow")
27+
}
28+
},
1629
)
1730

1831
let overview = () => {
@@ -21,9 +34,12 @@ let overview = () => {
2134
let className = style(. [
2235
width(124->#px),
2336
height(124->#px),
37+
boxShadow(#cool),
38+
bgColor(#secondary),
2439
breakpoint(
2540
#sm,
2641
[
42+
boxShadow(#simple),
2743
bgColor(#primary),
2844
padding(4),
2945
borderRadius(2),

packages/css/src/AncestorCss_WrappedTypes.res

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,25 @@ module Color = {
6868

6969
let toRule: t => CssJs.rule = CssJs.color
7070
}
71+
72+
module BoxShadow = {
73+
type t = [
74+
| #none
75+
| #shadow(CssJs.Shadow.value<CssJs.Shadow.box>)
76+
| #var(string)
77+
| #varDefault(string, string)
78+
]
79+
80+
let toRule: t => CssJs.rule = CssJs.boxShadow
81+
}
82+
83+
module TextShadow = {
84+
type t = [
85+
| #none
86+
| #shadow(CssJs.Shadow.value<CssJs.Shadow.text>)
87+
| #var(string)
88+
| #varDefault(string, string)
89+
]
90+
91+
let toRule: t => CssJs.rule = CssJs.textShadow
92+
}

0 commit comments

Comments
 (0)