|
5 | 5 | export const tailwindShadow = (node: BlendMixin): string[] => { |
6 | 6 | // [when testing] node.effects can be undefined |
7 | 7 | if (node.effects && node.effects.length > 0) { |
8 | | - const dropShadow = node.effects.filter( |
9 | | - (d) => d.type === "DROP_SHADOW" && d.visible, |
10 | | - ); |
11 | | - let boxShadow = ""; |
12 | | - // simple shadow from tailwind |
13 | | - if (dropShadow.length > 0) { |
14 | | - boxShadow = "shadow"; |
15 | | - } |
| 8 | + const EPSILON = 0.0001; // Small tolerance for floating-point comparison |
16 | 9 |
|
17 | | - const innerShadow = |
18 | | - node.effects.filter((d) => d.type === "INNER_SHADOW").length > 0 |
19 | | - ? "shadow-inner" |
20 | | - : ""; |
| 10 | + const dropShadow = node.effects.map((d) => { |
| 11 | + if (d.type === "DROP_SHADOW") { |
| 12 | + if ( |
| 13 | + d.offset?.x === 0 && |
| 14 | + d.offset?.y === 1 && |
| 15 | + d.radius === 2 && |
| 16 | + d.spread === 0 && |
| 17 | + d.color?.r === 0 && |
| 18 | + d.color?.g === 0 && |
| 19 | + d.color?.b === 0 && |
| 20 | + Math.abs(d.color?.a - 0.05) < EPSILON |
| 21 | + ) { |
| 22 | + return "shadow-sm"; |
| 23 | + } else if ( |
| 24 | + d.offset?.x === 0 && |
| 25 | + d.offset?.y === 1 && |
| 26 | + d.radius === 3 && |
| 27 | + d.spread === 0 && |
| 28 | + d.color?.r === 0 && |
| 29 | + d.color?.g === 0 && |
| 30 | + d.color?.b === 0 && |
| 31 | + Math.abs(d.color?.a - 0.1) < EPSILON |
| 32 | + ) { |
| 33 | + return "shadow"; |
| 34 | + } else if ( |
| 35 | + d.offset?.x === 0 && |
| 36 | + d.offset?.y === 4 && |
| 37 | + d.radius === 6 && |
| 38 | + d.spread === -1 && |
| 39 | + d.color?.r === 0 && |
| 40 | + d.color?.g === 0 && |
| 41 | + d.color?.b === 0 && |
| 42 | + Math.abs(d.color?.a - 0.1) < EPSILON |
| 43 | + ) { |
| 44 | + return "shadow-md"; |
| 45 | + } else if ( |
| 46 | + d.offset?.x === 0 && |
| 47 | + d.offset?.y === 10 && |
| 48 | + d.radius === 15 && |
| 49 | + d.spread === -3 && |
| 50 | + d.color?.r === 0 && |
| 51 | + d.color?.g === 0 && |
| 52 | + d.color?.b === 0 && |
| 53 | + Math.abs(d.color?.a - 0.1) < EPSILON |
| 54 | + ) { |
| 55 | + return "shadow-lg"; |
| 56 | + } else if ( |
| 57 | + d.offset?.x === 0 && |
| 58 | + d.offset?.y === 20 && |
| 59 | + d.radius === 25 && |
| 60 | + d.spread === -5 && |
| 61 | + d.color?.r === 0 && |
| 62 | + d.color?.g === 0 && |
| 63 | + d.color?.b === 0 && |
| 64 | + Math.abs(d.color?.a - 0.1) < EPSILON |
| 65 | + ) { |
| 66 | + return "shadow-xl"; |
| 67 | + } else if ( |
| 68 | + d.offset?.x === 0 && |
| 69 | + d.offset?.y === 25 && |
| 70 | + d.radius === 50 && |
| 71 | + d.spread === -12 && |
| 72 | + d.color?.r === 0 && |
| 73 | + d.color?.g === 0 && |
| 74 | + d.color?.b === 0 && |
| 75 | + Math.abs(d.color?.a - 0.25) < EPSILON |
| 76 | + ) { |
| 77 | + return "shadow-2xl"; |
| 78 | + } else { |
| 79 | + const offsetX = d.offset?.x || 0; |
| 80 | + const offsetY = d.offset?.y || 0; |
| 81 | + const radius = d.radius || 0; |
| 82 | + const spread = d.spread || 0; |
| 83 | + const r = Math.round((d.color?.r || 0) * 255); |
| 84 | + const g = Math.round((d.color?.g || 0) * 255); |
| 85 | + const b = Math.round((d.color?.b || 0) * 255); |
| 86 | + const a = (d.color?.a || 0).toFixed(2); // Limit alpha to 2 decimal, otherwise we will get values like 0.12356587999 |
| 87 | + return `shadow-[${offsetX}px_${offsetY}px_${radius}px_${spread}px_rgba(${r},${g},${b},${a})]`; |
| 88 | + } |
| 89 | + } |
| 90 | + return ""; |
| 91 | + }).filter(Boolean); |
21 | 92 |
|
22 | | - return [boxShadow, innerShadow]; |
| 93 | + const innerShadow = node.effects.map((d) => { |
| 94 | + if (d.type === "INNER_SHADOW") { |
| 95 | + if ( |
| 96 | + d.offset?.x === 0 && |
| 97 | + d.offset?.y === 2 && |
| 98 | + d.radius === 4 && |
| 99 | + d.spread === 0 && |
| 100 | + d.color?.r === 0 && |
| 101 | + d.color?.g === 0 && |
| 102 | + d.color?.b === 0 && |
| 103 | + Math.abs(d.color?.a - 0.05) < EPSILON |
| 104 | + ) { |
| 105 | + return "shadow-inner"; |
| 106 | + } else { |
| 107 | + const offsetX = d.offset?.x || 0; |
| 108 | + const offsetY = d.offset?.y || 0; |
| 109 | + const radius = d.radius || 0; |
| 110 | + const spread = d.spread || 0; |
| 111 | + const r = Math.round((d.color?.r || 0) * 255); |
| 112 | + const g = Math.round((d.color?.g || 0) * 255); |
| 113 | + const b = Math.round((d.color?.b || 0) * 255); |
| 114 | + const a = (d.color?.a || 0).toFixed(2); // Limit alpha to 2 decimal, otherwise we will get values like 0.12356587999 |
| 115 | + return `shadow-[inset_${offsetX}px_${offsetY}px_${radius}px_${spread}px_rgba(${r},${g},${b},${a})]`; |
| 116 | + } |
| 117 | + } |
| 118 | + return ""; |
| 119 | + }).filter(Boolean); |
23 | 120 |
|
24 | | - // todo customize the shadow |
25 | | - // TODO layer blur, shadow-outline |
| 121 | + return [...dropShadow, ...innerShadow]; |
26 | 122 | } |
27 | 123 | return []; |
28 | 124 | }; |
0 commit comments