-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
-
630
-
631
-
632
-
633
-
634
-
635
-
636
-
637
-
638
-
639
-
640
-
641
-
642
-
643
-
644
-
645
-
646
-
647
-
648
-
649
-
650
-
651
-
652
-
653
-
654
-
655
-
656
-
657
-
658
-
659
-
660
-
661
-
662
-
663
-
664
-
665
-
666
-
667
-
668
-
669
-
670
-
671
-
672
-
673
-
674
-
675
-
676
-
677
-
678
-
679
-
680
-
681
-
682
-
683
-
684
-
685
-
686
-
687
-
688
-
689
-
690
-
691
-
692
-
693
-
694
-
695
-
696
-
697
-
698
-
699
-
700
-
701
-
702
-
703
-
704
-
705
-
706
-
707
-
708
-
709
-
710
-
711
-
712
-
713
-
714
-
715
-
716
-
717
-
718
-
719
-
720
-
721
-
722
-
723
-
724
-
725
-
726
-
727
-
728
-
729
-
730
-
731
-
732
-
733
-
734
-
735
-
736
-
737
-
738
-
739
-
740
-
741
-
742
-
743
-
744
-
745
-
746
-
747
-
748
-
749
-
750
-
751
-
752
-
753
-
754
-
755
-
756
-
757
-
758
-
759
-
760
-
761
-
762
-
763
-
764
-
765
-
766
-
767
-
768
-
769
-
770
-
771
-
772
-
773
-
774
-
775
-
776
-
777
-
778
-
779
-
780
-
781
-
782
-
783
-
784
-
785
-
786
import * as figma from "../../../figma";
import { type Preferences } from "../../../preferences";
import { isTransparent, srgbToDisplayP3 } from "./colors";
import { CSSStyle, CSSStyleValue, CSSStyleValueTypes } from "./CSSStyle";
import { getLinearGradientAngle } from "./gradient";
import { serializeValue } from "./serialize";
export function fromNode(
node: figma.Node,
preferences: Readonly<Preferences>,
): readonly CSSStyle[] {
let styles: CSSStyle[] = [];
if (figma.hasBoundingBox(node)) {
styles.push(
{
propertyName: "width",
value: px(node.absoluteBoundingBox.width, preferences),
},
{
propertyName: "height",
value: px(node.absoluteBoundingBox.height, preferences),
},
);
}
if (figma.hasPadding(node)) {
styles.push({
propertyName: "padding",
value: padding(
node.paddingTop,
node.paddingRight,
node.paddingBottom,
node.paddingLeft,
preferences,
),
});
} else if (figma.hasLegacyPadding(node)) {
styles.push({
propertyName: "padding",
value: padding(
node.verticalPadding,
node.horizontalPadding,
node.verticalPadding,
node.horizontalPadding,
preferences,
),
});
}
if (figma.hasTypeStyle(node)) {
styles.push(
{
propertyName: "font-family",
value: {
type: CSSStyleValueTypes.String,
value: node.style.fontFamily,
},
},
{
propertyName: "font-size",
value: px(node.style.fontSize, preferences),
},
{
propertyName: "font-weight",
value: {
type: CSSStyleValueTypes.Number,
value: node.style.fontWeight,
precision: 0,
},
},
{
propertyName: "line-height",
value:
typeof node.style.lineHeightPercentFontSize === "number"
? {
type: CSSStyleValueTypes.List,
separator: " ",
head: {
type: CSSStyleValueTypes.Number,
value: node.style.lineHeightPercentFontSize / 100,
precision: 3,
},
tail: [
{
type: CSSStyleValueTypes.Comment,
text: `or ${node.style.lineHeightPx}px`,
},
],
}
: px(node.style.lineHeightPx, preferences),
},
{
// NOTE: This CSS generation uses physical properties instead of logical properties,
// due to Figma API does not have/return text flow related information.
propertyName: "text-align",
value: {
type: CSSStyleValueTypes.Keyword,
ident:
node.style.textAlignHorizontal === "JUSTIFIED"
? "justify"
: node.style.textAlignHorizontal.toLowerCase(),
},
},
);
if (node.style.letterSpacing) {
styles.push({
propertyName: "letter-spacing",
value: px(node.style.letterSpacing, preferences),
});
}
if (node.style.italic) {
styles.push({
propertyName: "font-style",
value: {
type: CSSStyleValueTypes.Keyword,
ident: "italic",
},
});
}
// There is no equivalent `text-transform` value for `SMALL_CAPS(_FORCED)`
switch (node.style.textCase) {
case "LOWER":
styles.push({
propertyName: "text-transform",
value: {
type: CSSStyleValueTypes.Keyword,
ident: "lowercase",
},
});
break;
case "UPPER":
styles.push({
propertyName: "text-transform",
value: {
type: CSSStyleValueTypes.Keyword,
ident: "uppercase",
},
});
break;
case "TITLE":
styles.push({
propertyName: "text-transform",
value: {
type: CSSStyleValueTypes.Keyword,
ident: "capitalize",
},
});
break;
}
}
if (figma.hasStroke(node) && node.strokes[0]) {
// Outputting `border-image` is impossible due to lacking of source value(s) for
// `border-image-slice`.
if (node.strokes[0].type === "SOLID") {
styles.push({
propertyName: "border",
value: {
type: CSSStyleValueTypes.List,
separator: " ",
head: px(node.strokeWeight, preferences),
tail: [
{
type: CSSStyleValueTypes.Keyword,
ident: "solid",
},
figmaPaintToCssPaint(node.strokes[0], preferences),
],
},
});
}
}
if (figma.hasFills(node) && node.fills.length > 0) {
const props = fills(node, preferences);
if (props.length > 0) {
styles.push(...props);
}
}
if (figma.hasRadius(node) && node.cornerRadius > 0) {
styles.push({
propertyName: "border-radius",
value: px(node.cornerRadius, preferences),
});
} else if (figma.hasRadii(node)) {
const [head, ...tail] = node.rectangleCornerRadii;
styles.push({
propertyName: "border-radius",
value: {
type: CSSStyleValueTypes.List,
head: px(head, preferences),
tail: tail.map((value) => {
return px(value, preferences);
}),
separator: " ",
},
});
}
if (figma.hasEffects(node)) {
const { shadows, layerBlurs, bgBlurs } = node.effects.reduce<{
shadows: readonly figma.ShadowEffect[];
layerBlurs: readonly figma.Effect[];
bgBlurs: readonly figma.Effect[];
}>(
({ shadows, layerBlurs, bgBlurs }, effect) => {
if (!effect.visible) {
return { shadows, layerBlurs, bgBlurs };
}
if (figma.isShadowEffect(effect)) {
return { shadows: [...shadows, effect], layerBlurs, bgBlurs };
}
switch (effect.type) {
case "LAYER_BLUR":
return { shadows, layerBlurs: [...layerBlurs, effect], bgBlurs };
case "BACKGROUND_BLUR":
return { shadows, layerBlurs, bgBlurs: [...bgBlurs, effect] };
default:
return { shadows, layerBlurs, bgBlurs };
}
},
{ shadows: [], layerBlurs: [], bgBlurs: [] },
);
if (shadows.length > 0) {
const [head, ...tail] = shadows;
styles.push({
propertyName: "box-shadow",
value: {
type: CSSStyleValueTypes.List,
separator: ", ",
head: shadow(head, preferences),
tail: tail.map((s) => shadow(s, preferences)),
},
});
}
if (layerBlurs.length > 0) {
const [head, ...tail] = layerBlurs;
styles.push({
propertyName: "filter",
value: {
type: CSSStyleValueTypes.List,
separator: " ",
head: effectToBlur(head, preferences),
tail: tail.map((blur) => effectToBlur(blur, preferences)),
},
});
}
if (bgBlurs.length > 0) {
const [head, ...tail] = bgBlurs;
styles.push({
propertyName: "backdrop-filter",
value: {
type: CSSStyleValueTypes.List,
separator: " ",
head: effectToBlur(head, preferences),
tail: tail.map((blur) => effectToBlur(blur, preferences)),
},
});
}
}
return styles;
}
function padding(
top: number,
right: number,
bottom: number,
left: number,
preferences: Readonly<Preferences>,
): CSSStyleValue {
if (top === bottom && right === left) {
if (top !== right) {
return {
type: CSSStyleValueTypes.List,
separator: " ",
head: px(top, preferences),
tail: [px(right, preferences)],
};
}
return px(top, preferences);
}
return {
type: CSSStyleValueTypes.List,
separator: " ",
head: px(top, preferences),
tail: [
px(right, preferences),
px(bottom, preferences),
px(left, preferences),
],
};
}
function effectToBlur(
effect: figma.Effect,
preferences: Readonly<Preferences>,
): CSSStyleValue {
return {
type: CSSStyleValueTypes.FunctionCall,
functionName: "blur",
args: px(effect.radius, preferences),
};
}
function shadow(
shadow: figma.ShadowEffect,
preferences: Readonly<Preferences>,
): CSSStyleValue {
const tail: (CSSStyleValue | null)[] = [
px(shadow.offset.y, preferences),
px(shadow.radius, preferences),
shadow.spread ? px(shadow.spread, preferences) : null,
withColorPreview(colorToValue(shadow.color, preferences), preferences),
shadow.type === "INNER_SHADOW"
? {
type: CSSStyleValueTypes.Keyword,
ident: "inset",
}
: null,
];
return {
type: CSSStyleValueTypes.List,
separator: " ",
head: px(shadow.offset.x, preferences),
tail: tail.filter((value): value is CSSStyleValue => !!value),
};
}
function fills(
node: figma.Node,
preferences: Readonly<Preferences>,
): readonly CSSStyle[] {
if (!figma.hasFills(node)) {
return [];
}
const visibleFills = node.fills.filter((fill) => fill.visible !== false);
if (!visibleFills.length) {
return [];
}
if (node.type === "TEXT") {
const fill = visibleFills[0]!;
// Text fill can't be other than solid
if (fill.type !== "SOLID") {
return [];
}
return [
{
propertyName: "color",
value: withColorPreview(
colorToValue(fill.color, preferences),
preferences,
),
},
];
}
// `fills` is bloated property: it is foreground color of TEXT, background color
// of rectangle shaped nodes, and fill color of Vector (not limited to VECTOR) nodes.
// In order to make generated code both correct and relatable, only support background
// color usage. (Foreground handling is already done, see above)
switch (node.type) {
case "FRAME":
case "COMPONENT":
case "COMPONENT_SET":
case "INSTANCE":
case "RECTANGLE":
break;
default:
return [];
}
const [head, ...tail] = visibleFills;
if (tail.length === 0) {
switch (head.type) {
case "SOLID": {
return [
{
propertyName: "background-color",
value: figmaPaintToCssPaint(head, preferences),
},
];
}
case "IMAGE":
case "GRADIENT_ANGULAR":
case "GRADIENT_DIAMOND":
case "GRADIENT_LINEAR":
case "GRADIENT_RADIAL": {
return [
{
propertyName: "background-image",
value: figmaPaintToCssPaint(head, preferences),
},
];
}
default: {
return [];
}
}
}
return [
{
propertyName: "background",
value: {
type: CSSStyleValueTypes.List,
separator: ", ",
head: figmaPaintToCssPaint(head, preferences),
tail: tail.map((paint) => figmaPaintToCssPaint(paint, preferences)),
},
},
];
}
function withColorPreview(
value: CSSStyleValue,
preferences: Readonly<Preferences>,
): CSSStyleValue {
if (!preferences.enableColorPreview) {
return value;
}
return {
type: CSSStyleValueTypes.Color,
value,
color: serializeValue(value, preferences),
};
}
function px(size: number, preferences: Readonly<Preferences>): CSSStyleValue {
if (preferences.lengthUnit === "px") {
return {
type: CSSStyleValueTypes.Number,
value: size,
unit: "px",
};
}
return {
type: CSSStyleValueTypes.Number,
value: size / preferences.rootFontSizeInPx,
unit: "rem",
precision: 2 + preferences.decimalPlaces,
};
}
function figmaPaintToCssPaint(
paint: figma.Paint,
preferences: Readonly<Preferences>,
): CSSStyleValue {
switch (paint.type) {
case "SOLID": {
const opacity = paint.opacity ?? 1;
return withColorPreview(
colorToValue(
{ ...paint.color, a: paint.color.a * opacity },
preferences,
),
preferences,
);
}
case "EMOJI":
case "VIDEO": {
return {
type: CSSStyleValueTypes.Comment,
text: `This fill is unavailable as a CSS background (${paint.type})`,
};
}
case "IMAGE": {
return {
type: CSSStyleValueTypes.FunctionCall,
functionName: "url",
args: {
type: CSSStyleValueTypes.Comment,
text: "image file",
},
};
}
case "GRADIENT_LINEAR": {
return withColorPreview(
{
type: CSSStyleValueTypes.FunctionCall,
functionName: "linear-gradient",
args: {
type: CSSStyleValueTypes.List,
separator: ", ",
head: {
type: CSSStyleValueTypes.Number,
value: getLinearGradientAngle(
paint.gradientHandlePositions[0],
paint.gradientHandlePositions[1],
),
precision: 2,
unit: "deg",
},
tail: paint.gradientStops.map((stop) => {
return {
type: CSSStyleValueTypes.List,
separator: " ",
head: withColorPreview(
colorToValue(stop.color, preferences),
preferences,
),
tail: [
{
type: CSSStyleValueTypes.Number,
value: stop.position * 100,
precision: 2,
unit: "%",
},
],
};
}),
},
},
preferences,
);
}
case "GRADIENT_ANGULAR":
case "GRADIENT_DIAMOND":
case "GRADIENT_RADIAL": {
return {
type: CSSStyleValueTypes.Comment,
text: "Not implemented",
};
}
}
}
function colorToValue(
color: figma.Color,
preferences: Readonly<Preferences>,
): CSSStyleValue {
if (isTransparent(color)) {
return {
type: CSSStyleValueTypes.Keyword,
ident: "transparent",
};
}
switch (preferences.cssColorNotation) {
case "hex": {
return toHex(color);
}
case "color-srgb": {
return toColorSrgb(color);
}
case "rgb": {
return toRgb(color);
}
case "hsl": {
return toHsl(color);
}
case "display-p3": {
return toDisplayP3(color);
}
case "srgb-to-display-p3": {
return toDisplayP3SrgbSrc(color);
}
}
}
function toHex(color: figma.Color): CSSStyleValue {
const r = (color.r * 0xff) | 0;
const g = (color.g * 0xff) | 0;
const b = (color.b * 0xff) | 0;
const a = color.a;
return {
type: CSSStyleValueTypes.Literal,
text:
"#" +
r.toString(16).padStart(2, "0") +
g.toString(16).padStart(2, "0") +
b.toString(16).padStart(2, "0") +
(a === 1 ? "" : ((a * 0xff) | 0).toString(16).padStart(2, "0")),
};
}
function toRgb(color: figma.Color): CSSStyleValue {
return {
type: CSSStyleValueTypes.FunctionCall,
functionName: "rgb",
args: {
type: CSSStyleValueTypes.List,
separator: " ",
head: {
type: CSSStyleValueTypes.Number,
value: color.r * 0xff,
precision: 0,
},
tail: [
{
type: CSSStyleValueTypes.Number,
value: color.g * 0xff,
precision: 0,
},
{
type: CSSStyleValueTypes.Number,
value: color.b * 0xff,
precision: 0,
},
{
type: CSSStyleValueTypes.Literal,
text: "/",
},
{
type: CSSStyleValueTypes.Number,
value: color.a,
precision: 2,
},
],
},
};
}
function toColorSrgb(color: figma.Color): CSSStyleValue {
return {
type: CSSStyleValueTypes.FunctionCall,
functionName: "color",
args: {
type: CSSStyleValueTypes.List,
separator: " ",
head: {
type: CSSStyleValueTypes.Keyword,
ident: "srgb",
},
tail: [
{
type: CSSStyleValueTypes.Number,
value: color.r,
precision: 6,
},
{
type: CSSStyleValueTypes.Number,
value: color.g,
precision: 6,
},
{
type: CSSStyleValueTypes.Number,
value: color.b,
precision: 6,
},
{
type: CSSStyleValueTypes.Literal,
text: "/",
},
{
type: CSSStyleValueTypes.Number,
value: color.a,
precision: 6,
},
],
},
};
}
/**
* Generate `color(display-p3 r g b)` assuming the `color` being Display P3.
*/
function toDisplayP3(color: figma.Color): CSSStyleValue {
return {
type: CSSStyleValueTypes.FunctionCall,
functionName: "color",
args: {
type: CSSStyleValueTypes.List,
separator: " ",
head: {
type: CSSStyleValueTypes.Keyword,
ident: "display-p3",
},
tail: [
{
type: CSSStyleValueTypes.Number,
value: color.r,
precision: 6,
},
{
type: CSSStyleValueTypes.Number,
value: color.g,
precision: 6,
},
{
type: CSSStyleValueTypes.Number,
value: color.b,
precision: 6,
},
{
type: CSSStyleValueTypes.Literal,
text: "/",
},
{
type: CSSStyleValueTypes.Number,
value: color.a,
precision: 6,
},
],
},
};
}
/**
* Generate `color(display-p3 r g b)` assuming the `color` being sRGB.
* This produces a perceptually identical color in Display P3 space.
*
* Implements color conversion defined in CSS Color Module Level 4 draft.
* https://drafts.csswg.org/css-color-4/#color-conversion
*/
function toDisplayP3SrgbSrc(color: figma.Color): CSSStyleValue {
return toDisplayP3(srgbToDisplayP3(color));
}
function toHsl(color: figma.Color): CSSStyleValue {
// https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
const v = Math.max(color.r, color.g, color.b);
const c = v - Math.min(color.r, color.g, color.b);
const l = v - c / 2;
const h =
60 *
(c === 0
? 0
: v === color.r
? ((color.g - color.b) / c) % 6
: v === color.g
? (color.b - color.r) / c + 2
: (color.r - color.g) / c + 4);
const s = l === 0 || l === 1 ? 0 : (v - l) / Math.min(l, 1 - l);
return {
type: CSSStyleValueTypes.FunctionCall,
functionName: "hsl",
args: {
type: CSSStyleValueTypes.List,
separator: " ",
head: {
type: CSSStyleValueTypes.Number,
value: h,
unit: "deg",
},
tail: [
{
type: CSSStyleValueTypes.Number,
value: s * 100,
unit: "%",
},
{
type: CSSStyleValueTypes.Number,
value: l * 100,
unit: "%",
},
{
type: CSSStyleValueTypes.Literal,
text: "/",
},
{
type: CSSStyleValueTypes.Number,
value: color.a,
},
],
},
};
}