|
1
|
import { Effect } from "@/figma" |
|
2
|
import { colorToRGBA } from "./colors" |
|
3
|
|
|
4
|
/** |
|
5
|
* We know that we will need to correct the Y value of the inset shadows |
|
6
|
* on these effects due to the difference in the way Figma and CSS |
|
7
|
* handle shadows. |
|
8
|
*/ |
|
9
|
const SPECIAL_IDENTIFIERS = [ |
|
10
|
"--buttons-colored", |
|
11
|
"--buttons-neutral", |
|
12
|
"--buttons-neutral-focus", |
|
13
|
"--buttons-colored-focus", |
|
14
|
] |
|
15
|
|
|
16
|
function createDropShadowVariable(effects: Effect[], identifier: string) { |
|
17
|
const shadows = effects.filter( |
|
18
|
(effect) => effect.type === "DROP_SHADOW" || effect.type === "INNER_SHADOW" |
|
19
|
) |
|
20
|
|
|
21
|
if (shadows.length === 0) { |
|
22
|
return null |
|
23
|
} |
|
24
|
|
|
25
|
const value = shadows |
|
26
|
.map((shadow) => { |
|
27
|
const { color, offset, radius, spread, type } = shadow |
|
28
|
|
|
29
|
const x = offset?.x ?? 0 |
|
30
|
let y = offset?.y ?? 0 |
|
31
|
|
|
32
|
if ( |
|
33
|
SPECIAL_IDENTIFIERS.includes(identifier) && |
|
34
|
type === "INNER_SHADOW" && |
|
35
|
y > 0 |
|
36
|
) { |
|
37
|
y = y - 1 |
|
38
|
} |
|
39
|
|
|
40
|
const b = radius |
|
41
|
const s = spread ?? 0 |
|
42
|
|
|
43
|
const c = color ? colorToRGBA(color) : "" |
|
44
|
|
|
45
|
const t = type === "INNER_SHADOW" ? "inset" : "" |
|
46
|
|
|
47
|
return `${x}px ${y}px ${b}px ${s}px ${c} ${t}`.trim() |
|
48
|
}) |
|
49
|
.join(", ") |
|
50
|
|
|
51
|
if (value.length === 0) { |
|
52
|
return null |
|
53
|
} |
|
54
|
|
|
55
|
return value |
|
56
|
} |
|
57
|
|
|
58
|
export { createDropShadowVariable } |