-
Notifications
You must be signed in to change notification settings - Fork 6
/
widget-api.d.ts
534 lines (534 loc) · 14.5 KB
/
widget-api.d.ts
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
/* widget-typings are auto-generated. Do not update them directly. See widget-docs/ for instructions. */
interface WidgetAPI {
register(component: FunctionalWidget<any>): void
h(...args: any[]): FigmaDeclarativeNode
/** @deprecated Use useWidgetNodeId instead. */
useWidgetId(): string
useWidgetNodeId(): string
useSyncedState<T>(
name: string,
defaultValue: T | (() => T),
): [T, (newValue: T | ((currValue: T) => T)) => void]
useSyncedMap<T>(name: string): SyncedMap<T>
usePropertyMenu(
items: WidgetPropertyMenuItem[],
onChange: (event: WidgetPropertyEvent) => void | Promise<void>,
): void
useEffect(effect: () => (() => void) | void): void
useStickable(onStuckStatusChanged?: (e: WidgetStuckEvent) => void | Promise<void>): void
useStickableHost(
onAttachmentsChanged?: (e: WidgetAttachedStickablesChangedEvent) => void | Promise<void>,
): void
waitForTask(task: Promise<any>): void
colorMapToOptions(colorPalette: {
[key: string]: string
}): WidgetPropertyMenuColorSelectorOption[]
AutoLayout: AutoLayout
Frame: Frame
Image: ImageComponent
Rectangle: Rectangle
Ellipse: Ellipse
Text: TextComponent
Input: Input
SVG: SVG
Line: Line
Fragment: Fragment
Span: Span
}
interface WidgetClickEvent {
canvasX: number
canvasY: number
offsetX: number
offsetY: number
}
interface WidgetStuckEvent {
newHostId: string | null
oldHostId: string | null
}
interface WidgetAttachedStickablesChangedEvent {
stuckNodeIds: string[]
unstuckNodeIds: string[]
}
interface SyncedMap<T> {
/** @deprecated Use size instead. */
readonly length: number
readonly size: number
has(key: string): boolean
get(key: string): T | undefined
set(key: string, value: T): void
delete(key: string): void
keys(): string[]
values(): T[]
entries(): [string, T][]
}
declare type AutoLayout = FunctionalWidget<AutoLayoutProps>
declare type Frame = FunctionalWidget<FrameProps>
declare type Rectangle = FunctionalWidget<RectangleProps>
declare type ImageComponent = FunctionalWidget<ImageProps>
declare type Ellipse = FunctionalWidget<EllipseProps>
declare type Line = FunctionalWidget<LineProps>
declare type TextComponent = FunctionalWidget<TextProps>
declare type Input = FunctionalWidget<InputProps>
declare type SVG = FunctionalWidget<SVGProps>
declare type Fragment = FunctionalWidget<FragmentProps>
declare type Span = (props: WidgetJSX.SpanProps) => FigmaVirtualNode<'span'>
declare type FigmaVirtualNode<T> = {
__type: T
}
declare type FigmaDeclarativeChildren<T> =
| FigmaVirtualNode<T>
| FigmaDeclarativeChildren<T>[]
| string
| null
| undefined
| false
declare type FigmaDeclarativeNode = FigmaDeclarativeChildren<any>
declare type FunctionalWidget<T> = (props: T) => FigmaDeclarativeNode
declare type PropertyMenuItemType =
| 'action'
| 'separator'
| 'color-selector'
| 'dropdown'
| 'toggle'
| 'link'
interface PropertyMenuItem {
tooltip: string
propertyName: string
itemType: PropertyMenuItemType
}
interface WidgetPropertyMenuActionItem extends PropertyMenuItem {
itemType: 'action'
tooltip: string
propertyName: string
icon?: string
}
interface WidgetPropertyMenuSeparatorItem {
itemType: 'separator'
}
interface WidgetPropertyMenuColorSelectorOption {
tooltip: string
option: HexCode
}
interface WidgetPropertyMenuColorItem extends PropertyMenuItem {
itemType: 'color-selector'
tooltip: string
propertyName: string
options: WidgetPropertyMenuColorSelectorOption[]
selectedOption: string
}
interface WidgetPropertyMenuDropdownOption {
option: string
label: string
}
interface WidgetPropertyMenuDropdownItem extends PropertyMenuItem {
itemType: 'dropdown'
tooltip: string
propertyName: string
options: WidgetPropertyMenuDropdownOption[]
selectedOption: string
}
interface WidgetPropertyMenuToggleItem extends PropertyMenuItem {
itemType: 'toggle'
tooltip: string
propertyName: string
isToggled: boolean
icon?: string
}
interface WidgetPropertyMenuLinkItem extends PropertyMenuItem {
itemType: 'link'
tooltip: string
propertyName: string
href: string
icon?: string | null
}
declare type WidgetPropertyMenuItem =
| WidgetPropertyMenuActionItem
| WidgetPropertyMenuSeparatorItem
| WidgetPropertyMenuColorItem
| WidgetPropertyMenuDropdownItem
| WidgetPropertyMenuToggleItem
| WidgetPropertyMenuLinkItem
declare type WidgetPropertyMenu = WidgetPropertyMenuItem[]
interface WidgetPropertyEvent {
propertyName: string
propertyValue?: string | undefined
}
interface TextChildren {
children?:
| FigmaVirtualNode<'span'>
| string
| number
| (FigmaVirtualNode<'span'> | string | number)[]
}
interface TextProps extends BaseProps, WidgetJSX.WidgetJSXTextProps, TextChildren {
font?: {
family: string
style: string
}
}
interface TextEditEvent {
characters: string
}
interface PlaceholderProps extends WidgetJSX.BlendProps, Omit<WidgetJSX.TextStyleProps, 'href'> {}
interface InputProps extends Omit<TextProps, 'children' | 'width'> {
onTextEditEnd: (event: TextEditEvent) => void
value: string | null
placeholder?: string
placeholderProps?: PlaceholderProps
inputFrameProps?: Omit<AutoLayoutProps, 'width'>
width?: WidgetJSX.Size
inputBehavior?: 'wrap' | 'truncate' | 'multiline'
}
interface FragmentProps extends HasChildrenProps {
key?: BaseProps['key']
}
interface FrameProps extends BaseProps, WidgetJSX.WidgetJSXFrameProps, HasChildrenProps {}
interface AutoLayoutProps extends BaseProps, WidgetJSX.WidgetJSXAutoLayoutProps, HasChildrenProps {}
interface EllipseProps extends BaseProps, WidgetJSX.WidgetJSXEllipseProps {}
interface RectangleProps extends BaseProps, WidgetJSX.WidgetJSXRectangleProps {}
interface ImageProps extends BaseProps, WidgetJSX.WidgetJSXImageProps {}
interface LineProps extends BaseProps, WidgetJSX.WidgetJSXLineProps {
length?: WidgetJSX.Size
strokeCap?: WidgetJSX.StrokeCap
}
interface SVGProps extends BaseProps, Partial<WidgetJSX.WidgetJSXFrameProps> {
src: string
}
interface BaseProps extends WidgetJSX.WidgetJSXBaseProps {
onClick?: (event: WidgetClickEvent) => Promise<any> | void
key?: string | number
hoverStyle?: WidgetJSX.HoverStyle
tooltip?: string
positioning?: 'auto' | 'absolute'
}
interface HasChildrenProps {
children?: FigmaDeclarativeNode | FigmaDeclarativeNode[]
}
declare type HexCode = string
declare namespace WidgetJSX {
interface Vector {
x: number
y: number
}
interface Color {
r: number
g: number
b: number
a: number
}
type ArcData = {
readonly startingAngle: number
readonly endingAngle: number
readonly innerRadius: number
}
type AlignItems = 'center' | 'start' | 'end' | 'baseline'
type BlendMode =
| 'pass-through'
| 'normal'
| 'multiply'
| 'screen'
| 'overlay'
| 'darken'
| 'lighten'
| 'color-dodge'
| 'color-burn'
| 'hard-light'
| 'soft-light'
| 'difference'
| 'exclusion'
| 'hue'
| 'saturation'
| 'color'
| 'luminosity'
type PaintType =
| 'image'
| 'solid'
| 'gradient-linear'
| 'gradient-radial'
| 'gradient-angular'
| 'gradient-diamond'
interface PaintProps {
type: PaintType
blendMode?: BlendMode
visible?: boolean
opacity?: number
}
interface SolidPaint extends PaintProps {
type: 'solid'
color: Color | HexCode
}
interface ColorStop {
position: number
color: Color
}
interface GradientPaint extends PaintProps {
type: 'gradient-linear' | 'gradient-radial' | 'gradient-angular' | 'gradient-diamond'
gradientHandlePositions: [Vector, Vector, Vector]
gradientStops: ColorStop[]
}
type Transform = [[number, number, number], [number, number, number]]
interface ImagePaint extends PaintProps {
type: 'image'
src: string
imageSize?: {
width: number
height: number
}
scaleMode?: ScaleMode
imageTransform?: Transform
scalingFactor?: number
rotation?: number
imageRef?: string
}
type Paint = SolidPaint | GradientPaint | ImagePaint
type ShadowEffect = DropShadowEffect | InnerShadowEffect
interface DropShadowEffect {
type: 'drop-shadow'
color: HexCode | Color
offset: Vector
blur: number
blendMode?: BlendMode
spread?: number
visible?: boolean
showShadowBehindNode?: boolean
}
interface InnerShadowEffect {
type: 'inner-shadow'
color: HexCode | Color
offset: Vector
blur: number
blendMode?: BlendMode
spread?: number
visible?: boolean
}
interface BlurEffect {
type: 'layer-blur' | 'background-blur'
blur: number
visible?: boolean
}
type Effect = DropShadowEffect | InnerShadowEffect | BlurEffect
type Size = number | 'fill-parent'
type AutolayoutSize = Size | 'hug-contents'
type StrokeAlign = 'inside' | 'outside' | 'center'
type StrokeCap = 'none' | 'round' | 'square' | 'arrow-lines' | 'arrow-equilateral'
type ScaleMode = 'fill' | 'fit' | 'tile' | 'crop'
type Overflow = 'visible' | 'hidden' | 'scroll'
interface TopConstraint {
type: 'top'
offset: number
}
interface BottomConstraint {
type: 'bottom'
offset: number
}
interface TopBottomConstraint {
type: 'top-bottom'
topOffset: number
bottomOffset: number
}
interface LeftConstraint {
type: 'left'
offset: number
}
interface RightConstraint {
type: 'right'
offset: number
}
interface LeftRightConstraint {
type: 'left-right'
leftOffset: number
rightOffset: number
}
interface CenterConstraint {
type: 'center'
offset: number
}
interface HorizontalScaleConstraint {
type: 'horizontal-scale'
leftOffsetPercent: number
rightOffsetPercent: number
}
interface VerticalScaleConstraint {
type: 'vertical-scale'
topOffsetPercent: number
bottomOffsetPercent: number
}
type VerticalConstraint =
| TopConstraint
| BottomConstraint
| TopBottomConstraint
| CenterConstraint
| VerticalScaleConstraint
type HorizontalConstraint =
| LeftConstraint
| RightConstraint
| LeftRightConstraint
| CenterConstraint
| HorizontalScaleConstraint
type CornerRadius =
| number
| {
topLeft?: number
topRight?: number
bottomLeft?: number
bottomRight?: number
}
type Path = {
path: string
windingRule: 'evenodd' | 'nonzero'
}
type FullPadding = {
top?: number
left?: number
bottom?: number
right?: number
}
type VerticalHorizontalPadding = {
vertical?: number
horizontal?: number
}
type Padding = number | FullPadding | VerticalHorizontalPadding
type FontWeightNumerical = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
type FontWeightString =
| 'thin'
| 'extra-light'
| 'light'
| 'normal'
| 'medium'
| 'semi-bold'
| 'bold'
| 'extra-bold'
| 'black'
type FontWeight = FontWeightNumerical | FontWeightString
interface HoverStyle {
fill?: HexCode | Color | Paint | (SolidPaint | GradientPaint)[]
stroke?: HexCode | Color | SolidPaint | GradientPaint | (SolidPaint | GradientPaint)[]
opacity?: number
}
interface WidgetJSXBaseProps extends BlendProps, ConstraintProps {
name?: string
hidden?: boolean
}
interface GeometryProps {
fill?: HexCode | Color | Paint | (SolidPaint | GradientPaint)[]
stroke?: HexCode | Color | SolidPaint | GradientPaint | (SolidPaint | GradientPaint)[]
strokeWidth?: number
strokeAlign?: StrokeAlign
strokeDashPattern?: number[]
}
interface PathProps {
fillPath?: Path[]
strokePath?: Path[]
}
interface SizePropsRequired extends SizeConstraintProps {
width: Size
height: Size
}
interface SizeProps extends SizeConstraintProps {
width?: Size
height?: Size
}
interface SizeConstraintProps {
minWidth?: number
maxWidth?: number
minHeight?: number
maxHeight?: number
}
interface AutoLayoutSizeProps {
width?: AutolayoutSize
height?: AutolayoutSize
}
interface CornerProps {
cornerRadius?: CornerRadius
}
interface BlendProps {
blendMode?: BlendMode
opacity?: number
effect?: Effect | Effect[]
}
interface TransformProps {
rotation?: number
}
interface ConstraintProps {
x?: number | HorizontalConstraint
y?: number | VerticalConstraint
}
interface LayoutGap {
horizontal?: number | 'auto'
vertical?: number | 'auto'
}
interface LayoutProps {
spacing?: number | 'auto' | LayoutGap
padding?: Padding
direction?: 'horizontal' | 'vertical'
horizontalAlignItems?: Omit<AlignItems, 'baseline'>
verticalAlignItems?: AlignItems
wrap?: boolean
}
interface TextStyleProps extends NoHrefTextStyleProps {
href?: string
}
interface NoHrefTextStyleProps {
fontFamily?: string
letterSpacing?: number | string
textDecoration?: 'none' | 'strikethrough' | 'underline'
fontSize?: number
italic?: boolean
textCase?: 'upper' | 'lower' | 'title' | 'original' | 'small-caps' | 'small-caps-forced'
fontWeight?: FontWeight
fontPostScriptName?: string
fill?: HexCode | Color | Paint | (SolidPaint | GradientPaint)[]
}
interface WidgetJSXFrameProps
extends BaseProps,
GeometryProps,
SizePropsRequired,
TransformProps,
CornerProps {
overflow?: Overflow
}
interface WidgetJSXAutoLayoutProps
extends Omit<FrameProps, 'width' | 'height'>,
LayoutProps,
AutoLayoutSizeProps {}
interface WidgetJSXEllipseProps extends BaseProps, GeometryProps, TransformProps, SizeProps {
arcData?: ArcData
}
interface WidgetJSXImageProps extends Omit<RectangleProps, 'fill'> {
src: string | ImagePaint
}
interface WidgetJSXLineProps
extends BaseProps,
TransformProps,
Pick<GeometryProps, 'stroke' | 'strokeWidth' | 'strokeDashPattern'> {}
interface WidgetJSXRectangleProps
extends BaseProps,
GeometryProps,
SizePropsRequired,
TransformProps,
CornerProps {}
interface WidgetJSXSVGProps
extends BaseProps,
GeometryProps,
SizeProps,
TransformProps,
PathProps {}
interface ParagraphProps {
spacing: number
}
interface SpanProps extends TextStyleProps, TextChildren {}
interface WidgetJSXTextProps
extends BaseProps,
AutoLayoutSizeProps,
TransformProps,
Omit<GeometryProps, 'fill'>,
TextStyleProps {
paragraphIndent?: number
paragraphSpacing?: number
horizontalAlignText?: 'left' | 'right' | 'center' | 'justified'
verticalAlignText?: 'top' | 'center' | 'bottom'
lineHeight?: number | string | 'auto'
truncate?: boolean | number
}
type ComponentProps = AutoLayoutProps | FrameProps
}