-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwidget-api.d.ts
More file actions
1826 lines (1818 loc) · 60.5 KB
/
widget-api.d.ts
File metadata and controls
1826 lines (1818 loc) · 60.5 KB
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* widget-typings are auto-generated. Do not update them directly. See developer-docs/ for instructions. */
/**
* @see https://developers.figma.com/docs/widgets/api/api-reference
*/
interface WidgetAPI {
/**
* Used to register your widget. **This is the main entry point to widgets.**
*
* This function expects a widget function that describes the widget and returns a Figma element
* (eg. one of the components AutoLayout, Frame, Text etc).
*
* @remarks
*
* The provided function will be called any time a widget is inserted and anytime the widget’s state is updated.
*
* Caution: The `widget.register` function should only be called once when the `manifest.main` file runs.
*
* ### Usage Example
*
* ```tsx title="First widget"
* const { widget } = figma
* const { Text } = widget
*
* function MyFirstWidget() {
* return <Text>Hello Widget</Text>
* }
*
* widget.register(MyFirstWidget)
* ```
*/
register(component: FunctionalWidget<any>): void
h(...args: any[]): FigmaDeclarativeNode
/**
* The `useWidgetId` hook gives you a way to reference the active widget node in event handlers like `onClick`. It returns a node `id` which can be used to retrieve and identify the active WidgetNode via the plugin API (eg. `figma.getNodeById`).
*
* @remarks
*
* Caution: Note that `figma.getNodeById` shouldn’t be called when rendering a widget (this will throw an error). Instead, this should be used inside event handlers where using the plugin API is allowed. See the [Rendering Code](https://developers.figma.com/docs/widgets/how-widgets-run#rendering-code) for more details.
*
* ### Usage Example
*
* ```tsx title="useWidgetId example"
* const { widget } = figma
* const { Text, useWidgetId } = widget
*
* function UseWidgetIdExample() {
* const widgetId = useWidgetId()
*
* return (
* <Text
* onClick={() => {
* const widgetNode = figma.getNodeById(widgetId) as WidgetNode;
* const clonedWidget = widgetNode.clone();
*
* // Position the cloned widget beside this widget
* widgetNode.parent!.appendChild(clonedWidget);
* clonedWidget.x = widgetNode.x + widgetNode.width + 50;
* clonedWidget.y = widgetNode.y;
* }}
* >
* Make a copy
* </Text>
* )
* }
*
* widget.register(UseWidgetIdExample)
* ```
*/
/** @deprecated Use useWidgetNodeId instead. */
useWidgetId(): string
useWidgetNodeId(): string
/**
* The `useSyncedState` hook lets you declare that your widget relies on some state. You give `useSyncedState` a storage key and a default value and it returns the current value stored and a function to update the value.
*
* @param
*
* | Parameter | Description |
* | ------------- | ----------- |
* | `name` | A storage "key" to store this syncedState. It is important that this is unique across multiple `useSyncedState` calls in a single widget. |
* | `defaultValue` | The default value to return if no existing value is found. Values have to be JSON serializable. You can also pass a function that returns a defaultValue, this is useful if `defaultValue` is expensive to compute or if it is a plugin api method that isn't allowed during widget rendering. |
*
*
* @remarks
*
* Note: If you're familiar with React, `useSyncedState` is very similar to `React.useState`. The main difference being that we require you to explicitly "key" your state values to explicitly manage the namespace of your widget state. This is important for widgets because syncedState lives across different clients who might be running different versions of your widget.
*
* Whenever a state value is updated, the corresponding widget is re-rendered to reflect the latest "state" of the widget.
*
* Updating state values is not allowed when rendering the widget. See [State Updating Code](https://developers.figma.com/docs/widgets/how-widgets-run#state-updating-code) for more information.
*
* ### Usage Example
*
* ```tsx title="useSyncedState example"
* const { widget } = figma
* const { Text, useSyncedState } = widget
*
* function SyncedStateExample() {
* const [count, setCount] = useSyncedState("count", 0)
* return (
* <Text
* onClick={() => {
* // Update the count
* setCount(count + 1)
* }}
* >
* The count is: {count}
* </Text>
* )
* }
*
* widget.register(SyncedStateExample)
* ```
*
* ### Lazy initial state
*
* This is useful if your initial state is expensive or is a plugin api method that isn't allowed during widget rendering (eg. figma.activeUsers). Instead of specifying a value directly, you can specify a function that will only be called if a value doesn’t already exist.
*
* ```tsx title="Lazy initial state"
* function Widget() {
* const [expensiveState, setExpensiveState] = useSyncedState("expensiveState", () => {
* return expensiveStateComputation()
* })
*
* const [activeUsers, setActiveUsers] = useSyncedState("activeUsers", () => figma.activeUsers)
* ...
* }
* ```
*
* ### Functional updates
*
* This is useful if your new state is computed using the previous state. Instead of setting the state directly with a value, you can pass a function that will be called with the current state value and set to the return value of the function.
*
* ```tsx title="Functional updates"
* function Widget() {
* const [count, setCount] = useSyncedState("count", 0)
* return (
* <Text onClick={() => setCount(prevCount => prevCount + 1)}>
* {count}
* </Text>
* )
* }
* ```
*
* Besides `useSyncedState`, another way to store data on a widget is `useSyncedMap`. These have different characteristics and use cases. See [Widget State & Multiplayer](https://developers.figma.com/docs/widgets/widget-state-and-multiplayer) for more information on when you should use one over the other.
*/
useSyncedState<T>(
name: string,
defaultValue: T | (() => T),
): [T, (newValue: T | ((currValue: T) => T)) => void]
/**
* The `useSyncedMap` hook works similarly to `useSyncedState`, but each value within the map is updated last-writer-wins, instead of the entire map being overwritten last-writer-wins.
*
* @param
*
* | Parameter | Description |
* | ------------- | ----------- |
* | `name` | A storage name assigned to this syncedMap. If your widgets uses multiple synced maps, it is important that this is unique across the multiple `useSyncedMap calls. |
*
* @remarks
*
* The main use case of `useSyncedMap` is to support multiple clients updating widget data concurrently. When this happens, the synced map will merge the changes in the order they are received by the server, adding / updating / removing keys. In comparison, a similar value in `useSyncedState` will be clobbered by the last client.
*
* The return value of `useSyncedMap` is a `Map` like JavaScript object that implements methods like `get`, `set`, `delete`, `keys()` etc.
*
* Note: Besides `useSyncedMap`, another way to store data on a widget is `useSyncedState`. These have different characteristics and use cases. See [Widget State & Multiplayer](https://developers.figma.com/docs/widgets/widget-state-and-multiplayer) for more information on when you should use one over the other.
*
* ### Usage Example
*
* ```tsx title="useSyncedMap example"
* const { widget } = figma
* const { useSyncedMap, Rectangle } = widget
*
* function SyncedMapExample() {
* const voteMap = useSyncedMap<number>("sessionIdToVotes")
*
* return (
* <Rectangle
* onClick={() => {
* const sessionId = figma.activeUsers[0].sessionId
* if (!voteMap.get(sessionId)) {
* voteMap.set(sessionId, 1)
* }
* }}
* />
* )
* }
*
* widget.register(SyncedMapExample)
* ```
*/
useSyncedMap<T>(name: string): SyncedMap<T>
/**
* The `usePropertyMenu` hook lets you specify the property menu to show when the widget is selected (See image below).
*
* @param
*
* | Parameter | Description |
* | ------------- | ----------- |
* | `items` | A list of `WidgetPropertyMenuItem`s to render when the widget is clicked |
* | `onChange` | The function to call when a menu item is clicked. This function is called with an object containing the `propertyName` of the item that was clicked on. |
*
* @remarks
*
* When building your widget, the property menu is a way to provide a menu for your widget. When a user clicks on the property menu, the associated callback is triggered.
*
* 
*
*
* ### Usage Example
*
*```tsx title="usePropertyMenu example"
* const { widget } = figma
* const { useSyncedState, usePropertyMenu, AutoLayout, Text } = widget
*
*function PropertyMenuWidget() {
* const [color, setColor] = useSyncedState("theme", "#e06666")
* const [fruit, setFruit] = useSyncedState("fruit", "mango")
* const fruitOptions = [{option: "mango", label: "Mango"}, {option: "apple", label: "Apple"}]
* usePropertyMenu(
* [
* {
* itemType: 'action',
* tooltip: 'Action',
* propertyName: 'action',
* },
* {
* itemType: 'separator',
* },
* {
* itemType: 'color-selector',
* propertyName: 'colors',
* tooltip: 'WidgetJSX.Color selector',
* selectedOption: color,
* options: [{option: "#e06666", tooltip: "Red"}, {option: "#ffe599", tooltip: "Yellow"} ],
* },
* {
* itemType: 'dropdown',
* propertyName: 'fruits',
* tooltip: 'Fruit selector',
* selectedOption: fruit,
* options: fruitOptions,
* },
* {
* itemType: 'link',
* propertyName: 'fruitLink',
* tooltip: 'Learn about fruit!',
* icon: null,
* href: 'https://en.wikipedia.org/wiki/Fruit',
* },
* ],
* ({propertyName, propertyValue}) => {
* if (propertyName === "colors") {
* setColor(propertyValue)
* } else if (propertyName === "fruits") {
* setFruit(propertyValue)
* } else if (propertyName === "action") {
* console.log(propertyName)
* }
* },
* )
* return (
* <AutoLayout
* verticalAlignItems={'center'}
* padding={16}
* >
* <Text fontSize={32} width={200} horizontalAlignText={'center'} fill={color}>
* {fruitOptions.find(f => f.option === fruit).label}
* </Text>
* </AutoLayout>
* )
* }
*
* widget.register(PropertyMenuWidget)
*```
*/
usePropertyMenu(
items: WidgetPropertyMenuItem[],
onChange: (event: WidgetPropertyEvent) => void | Promise<void>,
): void
/**
* The `useEffect` hook can be useful for running code that should run anytime the state of a widget changes or a widget is interacted with. You can use it to do data fetching when a component mounts (by using it with `waitForTask` ) or keeping state in sync between an iframe and the widget.
*
* @param
*
* | Parameter | Description |
* | ------------ | ----------- |
* |`effect` | A function that is executed whenever a widget's state is updated. If a function is returned by this function, the returned function will be called prior to running effects again. |
*
* Note: Note: Because of [How Widgets Run](https://developers.figma.com/docs/widgets/how-widgets-run), this function should handle being called multiple times with the same state.
*
* @remarks
*
* There are three main use cases of `useEffect`:
*
* **Initializing network or plugin API-dependent widget state**
*
* [Rendering code](https://developers.figma.com/docs/widgets/how-widgets-run#rendering-code) is synchronous and should only depend on widget state - if you wish to initialize widget state using information from the network (eg. HTTP requests in an iframe) or using information about the file (eg. using `figma.currentPage.selection`) - you can do this in `useEffect`. After the widget has rendered for the first time, any callback to `useEffect` is executed. Code in the function passed to `useEffect` is able to update widget state and perform asynchronous tasks (when paired with `waitForTask`).
*
* **Setting up event handlers**
*
* You might have multiple calls to `figma.showUI` in various event handler (eg. `onClick` on various nodes or via `usePropertyMenu` actions) and want to consolidate message handling in one place. `useEffect` is a great place for this. Effects are guaranteed to have run before any event handler code is executed and after a widget re-renders (eg. in response to state changes).
*
* ```tsx title="useEffect with figma.ui.onmessage"
* const { widget } = figma
* const { Text, useEffect, waitForTask } = widget
*
* function EventHandlerExample() {
* useEffect(() => {
* waitForTask(new Promise(resolve => {
* figma.ui.onmessage = (msg) => {
* console.log(msg)
* resolve()
* }
* }))
* })
*
* return <Text>Event handler example</Text>
* }
*
* widget.register(EventHandlerExample)
* ```
*
* Note: Note: `useEffect` is called **every time** a widget's state is changed. This means that if you are setting up an event listener using [`figma.on`](https://developers.figma.com/docs/plugins/api/properties/figma-on/) (or [`figma.ui.on`](https://developers.figma.com/docs/plugins/api/properties/figma-ui-on)), you need to make sure to remove the listener using the corresponding `off` function in the function returned by your `useEffect` callback. Not removing an event listener can lead to unexpected behavior where your code responds to an event multiple times.
*
* Here's an example of how to use `useEffect` to set up an event handler and
* clean it up when the widget is unmounted. In this example, we're using
* figma.on("selectionchange") to render the number of selected nodes as part
* of the widget.
*
* ```tsx title="useEffect with figma.on('selectionchange')"
* const { widget } = figma
* const { Text, useEffect, waitForTask, useSyncedState } = widget
*
* function EventHandlerExample() {
* const [numNodes, setNumNodes] = useSyncedState('count', () => {
* return figma.currentPage.selection.length
* })
* useEffect(() => {
* let resolvePromise;
* const onSelectionChange = () => {
* setNumNodes(figma.currentPage.selection.length)
* resolvePromise?.()
* }
* waitForTask(new Promise(resolve => {
* resolvePromise = resolve;
* figma.on('selectionchange', onSelectionChange)
* }))
* return () => {
* figma.off('selectionchange', onSelectionChange)
* }
* })
*
* return <Text>Number of selected nodes: {numNodes}</Text>
* }
*
* widget.register(EventHandlerExample)
* ```
*
* **Consolidating state updating side-effects**
*
* Because `useEffect` callbacks are run whenever a widget state changes - they are good candidates for performing any side-effects. This is especially useful if you have multiple functions in your widget that might update a widget's state and you want to consistently trigger the same side-effects based the final widget's state.
*
* ## Usage Example
*
* ```tsx title="useEffect example"
* const { widget } = figma
* const { Text, useEffect } = widget
*
* function UseEffectExample() {
* useEffect(() => {
* console.log("useEffect callback called")
* })
*
* return <Text>useEffect example</Text>
* }
*
* widget.register(UseEffectExample)
* ```
*/
useEffect(effect: () => (() => void) | void): void
/**
* Note: This API is only available in FigJam
*
* `useStickable` is a hook that makes your widget stick to other nodes when dragged over them. This behavior is similar to how stamp nodes work in Figma.
*
* @param
* | Parameter | Description |
* | --------------------- | ----------- |
* |`onStuckStatusChanged` | An optional callback that is called whenever a widget is stuck or removed from a node. It takes a **[`WidgetStuckEvent`](https://developers.figma.com/docs/widgets/api/type-WidgetStuckEvent)** as an argument. |
*
*
* @remarks
*
* ### Basic Usage
*
* ```tsx title="useStickable without callback"
* const { useStickable, Rectangle } = figma.widget;
* function Widget() {
* // This widget sticks to other nodes now!
* useStickable();
* return <Rectangle width={100} height={100} fill="#F00" />;
* }
* figma.widget.register(Widget);
* ```
*
* 
*
*
* ### Example
*
* This example changes the color of the widget depending on what type of node it is stuck to.
*
* ```tsx title="useStickable with callback"
* const { useStickable, Rectangle, useWidgetId, useSyncedState } = figma.widget;
* function Widget() {
* const widgetId = useWidgetId();
* const [color, setColor] = useSyncedState("color", "#000");
*
* // This widget sticks to other nodes now!
* useStickable(() => {
* const widget = figma.getNodeById(widgetId);
* const { stuckTo } = widget;
* if (!stuckTo) {
* // Set the color to black if the widget isn't stuck to anything.
* setColor("#000");
* return;
* }
*
* switch (stuckTo.type) {
* case "STICKY":
* // Make the widget red if we are attacked to a sticky
* setColor("#F00");
* return;
* case "SHAPE_WITH_TEXT":
* // Make the widget green if we are attached to a shape with text
* setColor("#0F0");
* return;
* default:
* // If we are attached to anything else make the widget blue
* setColor("#00F");
* return;
* }
* });
*
* return <Rectangle width={100} height={100} fill={color} />;
* }
* figma.widget.register(Widget);
* ```
*
* ### Other Rules
*
* - In FigJam a node is either a stickable or a stickable host, but never both.
* - You cannot call `useStickable` and `useStickableHost` in the same render of a widget; it can only be one or the other.
* - By default all widgets are stickable hosts and can let stamps and other stickables stick to them.
*/
useStickable(onStuckStatusChanged?: (e: WidgetStuckEvent) => void | Promise<void>): void
/**
* Note: This API is only available in FigJam
*
* `useStickableHost` lets your widget run a callback when a stickable is added or removed to your widget. By default all widgets are already stickable hosts so you don't have to call this if you just want stamps to stick to your widget.
*
* @param
*
* | Parameter | Description |
* | --------------------- | ----------- |
* |`onAttachedStickablesChanged` | An optional callback that is called whenever stickables are added or removed from this widget. It takes a **[`WidgetAttachedStickablesChangedEvent`](https://developers.figma.com/docs/widgets/api/type-WidgetAttachedStickablesChangedEvent)** as an argument. |
*
* @remarks
*
* ### Example
*
* This widget is a rectangle that you can stamps in and it will display how many of each stamp type are present on the widget.
*
* ```jsx title="Stamp Vote with useStickableHost"
* function StampVote() {
* const [votes, setVotes] = useSyncedState("votes", {});
* const widgetID = useWidgetId();
*
* useStickableHost(() => {
* const widget = figma.getNodeById(widgetID);
* if (!widget) {
* return;
* }
* const newVotes: { [key: string]: number } = {};
* const { stuckNodes } = widget;
* for (const node of stuckNodes) {
* if (!node || node.type !== "STAMP") {
* continue;
* }
*
* newVotes[node.name] = newVotes[node.name] || 0;
* newVotes[node.name] += 1;
* }
*
* setVotes(newVotes);
* });
*
* return (
* <AutoLayout
* fill={"#FFFFFF"}
* stroke={"#E6E6E6"}
* padding={16}
* direction="vertical"
* width={300}
* height={300}
* verticalAlignItems="end"
* >
* {Object.keys(votes).map((name) => (
* <Text>
* {name}: {votes[name]}
* </Text>
* ))}
* </AutoLayout>
* );
* }
* ```
*
* ### Other Rules
*
* - In FigJam a node is either a stickable or a stickable host, but never both.
* - You cannot call `useStickable` and `useStickableHost` in the same render of a widget; it can only be one or the other.
* - By default all widgets are stickable hosts and can let stamps and other stickables stick to them.
* - Calling `useStickableHost` doesn't let non stickables attach to your widget.
*/
useStickableHost(
onAttachmentsChanged?: (e: WidgetAttachedStickablesChangedEvent) => void | Promise<void>,
): void
/**
* The `waitForTask` function is useful for doing asynchronous work (eg. data fetching) in `useEffect`. It takes a promise and keeps the widget alive until the promise is resolved (or if there’s an explicit call to `figma.closePlugin`).
*
* @param
*
* | Parameter | Description |
* | ------------- | ----------- |
* | `task` | The widget will only be terminated when the given task is resolved |
*
* @remarks
*
* One of the main use cases of `waitForTask` is doing data fetching when a widget is inserted onto the canvas. When paired with `useEffect`, you can make a network request via an iframe and persist the response in a widget state. The `waitForTask` call will prevent the widget from being terminated until the given promise has resolved.
*
* ### Usage Example:
*
* ```tsx title="waitForTask example"
* const { widget } = figma
* const { Text, useEffect, waitForTask, useSyncedState } = widget
*
* function WaitForTaskExample() {
* const [textContent, setTextContent] = useSyncedState("text", "Initial")
*
* useEffect(() => {
* waitForTask(new Promise(resolve => {
* // Simulate async work
* setTimeout(() => {
* if (textContent !== "Final") {
* setTextContent("Final")
* }
* // Resolve the task
* resolve()
* }, 1000)
* }))
* })
*
* return <Text>{textContent}</Text>
* }
*
* widget.register(WaitForTaskExample)
* ```
*/
waitForTask(task: Promise<any>): void
/**
* Note: This API is only available in FigJam
* The `colorMapToOptions` takes in a [ColorPalette](https://developers.figma.com/docs/plugins/api/ColorPalette), a map from color names to values, and returns `WidgetPropertyMenuColorSelectorOption[]`. This helper function enables developers to use `figma.constants.colors.*`, official FigJam color palettes, in the `PropertyMenu`.
*
*
* @remarks
*
*
* ### Usage Example:
*
* ```tsx title="colorMapToOptions example"
* const { widget } = figma
* const { colorMapToOptions, useSyncedState, usePropertyMenu, Text } = widget
*
* function colorPaletteExample() {
* const [color, setColor] = useSyncedState("theme", figma.constants.colors.figJamBase.black)
* usePropertyMenu(
* [
* {
* itemType: 'color-selector',
* propertyName: 'colors',
* tooltip: 'WidgetJSX.Color selector',
* selectedOption: color,
* options: [
* ...figma.widget.colorMapToOptions(figma.constants.colors.figJamBase)
* {option: '#f5427b', tooltip: 'Hot Pink'}
* ],
* },
* ],
* ({propertyName, propertyValue}) => {
* if (propertyName === "colors") {
* setColor(propertyValue)
* }
* },
* )
* return (
* <Text fill={color}>
* String(color)
* </Text>
* )
* }
*
* widget.register(colorPaletteExample)
* ```
*/
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
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-WidgetClickEvent
*/
interface WidgetClickEvent {
/**
* canvasX is the x position of the mouse relative to the canvas.
* This is the same as the absolute position that is used to position a node.
*/
canvasX: number
/**
* canvasY is the y position of the mouse relative to the canvas.
* This is the same as the absolute position that is used to position a node.
*/
canvasY: number
/**
* offsetX is the X coordinate of the mouse relative to the component
* that was clicked.
*/
offsetX: number
/**
* offsetY is the Y coordinate of the mouse relative to the component
* that was clicked.
*/
offsetY: number
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-WidgetStuckEvent
*/
interface WidgetStuckEvent {
/**
* This is the id of the new node that your widget is stuck to
* or null if it is no longer stuck to anything.
*/
newHostId: string | null
/**
* This is the id of the node that your widget was stuck to
* or null if it is no longer stuck to anything.
*/
oldHostId: string | null
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-WidgetAttachedStickablesChangedEvent
*/
interface WidgetAttachedStickablesChangedEvent {
/**
* These are the node IDs that are newly stuck to your widget.
*/
stuckNodeIds: string[]
/**
* These are the node IDs that were removed from your widget.
* Note that these nodes could also have been deleted.
*/
unstuckNodeIds: string[]
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-SyncedMap
*/
interface SyncedMap<T> {
/**
* @deprecated Use size instead.
*/
/** @deprecated Use size instead. */
readonly length: number
/**
* Returns the number of keys in this map.
*/
readonly size: number
/**
* Returns whether the given key exists.
*/
has(key: string): boolean
/**
* Returns the value of the given key if one exists.
*/
get(key: string): T | undefined
/**
* Persist the given key/value pair on the map.
*
* Note: Note: value has to be JSON-serializable.
*/
set(key: string, value: T): void
/**
* Removes the given key and its value from the map if it exists.
*/
delete(key: string): void
/**
* Returns an array of keys in the map.
*/
keys(): string[]
/**
* Returns an array of values in the map.
*/
values(): T[]
/**
* Returns an array of [key, value] tuples in the map.
*/
entries(): [string, T][]
}
/**
* @see https://developers.figma.com/docs/widgets/api/component-AutoLayout
*/
type AutoLayout = FunctionalWidget<AutoLayoutProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Frame
*/
type Frame = FunctionalWidget<FrameProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Rectangle
*/
type Rectangle = FunctionalWidget<RectangleProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Image
*/
type ImageComponent = FunctionalWidget<ImageProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Ellipse
*/
type Ellipse = FunctionalWidget<EllipseProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Line
*/
type Line = FunctionalWidget<LineProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Text
*/
type TextComponent = FunctionalWidget<TextProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Input
*/
type Input = FunctionalWidget<InputProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-SVG
*/
type SVG = FunctionalWidget<SVGProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Fragment
*/
type Fragment = FunctionalWidget<FragmentProps>
/**
* @see https://developers.figma.com/docs/widgets/api/component-Span
*/
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>
type FunctionalWidget<T> = (props: T) => FigmaDeclarativeNode
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
type PropertyMenuItemType =
| 'action'
| 'separator'
| 'color-selector'
| 'dropdown'
| 'toggle'
| 'link'
interface PropertyMenuItem {
tooltip: string
propertyName: string
itemType: PropertyMenuItemType
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
interface WidgetPropertyMenuActionItem extends PropertyMenuItem {
/**
* Specifies the action item type.
*/
itemType: 'action'
/**
* The tooltip of the button.
*
* Used as the button label if an icon is not specified.
*/
tooltip: string
/**
* Identifies the menu item. This is used to indicate which item was clicked in the callback.
*/
propertyName: string
/**
* If specified, it will be used to render the button; otherwise, we'll fallback to the tooltip as the button label.
*
* Note: The provided svg should contain the following attribute to be valid: xmlns="http://www.w3.org/2000/svg"
*/
icon?: string
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
interface WidgetPropertyMenuSeparatorItem {
/**
* Specifies the separator item type.
*/
itemType: 'separator'
}
interface WidgetPropertyMenuColorSelectorOption {
/**
* The tooltip of the selector option.
*/
tooltip: string
/**
* A selector option. If selected, will correspond to the `propertyValue` passed to the property menu callback.
*
*/
option: HexCode
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
interface WidgetPropertyMenuColorItem extends PropertyMenuItem {
/**
* Specifies the color selector item type.
*/
itemType: 'color-selector'
/**
* The tooltip of the selector.
*/
tooltip: string
/**
* Identifies the menu item. This is used to indicate which item was clicked in the callback.
*/
propertyName: string
/**
* Array of color options to display to the user when selected. This array cannot be empty.
*/
options: WidgetPropertyMenuColorSelectorOption[]
/**
* The currently selected color. This option string should match one of the `option` values specified in `options`.
*/
selectedOption: string
}
interface WidgetPropertyMenuDropdownOption {
/**
* Value of the dropdown option. If selected, will correspond to `propertyValue` pass to the property menu callback.
*/
option: string
/**
* The display label for the option.
*/
label: string
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
interface WidgetPropertyMenuDropdownItem extends PropertyMenuItem {
/**
* Specifies the dropdown item type.
*/
itemType: 'dropdown'
/**
* The tooltip of the dropdown component.
*/
tooltip: string
/**
* Identifies the menu item. This is used to indicate which item was clicked in the callback.
*/
propertyName: string
/**
* An array of options. This array cannot be empty.
*/
options: WidgetPropertyMenuDropdownOption[]
/**
* The currently selected option. This option string should match one of the `option` values specified in `options
*/
selectedOption: string
}
interface WidgetPropertyMenuToggleItem extends PropertyMenuItem {
/**
* Specifies the toggle item type.
*/
itemType: 'toggle'
/**
* The tooltip of the button.
*
* Used as the button label if an icon is not specified.
*/
tooltip: string
/**
* Identifies the menu item. This is used to indicate which item was clicked in the callback.
*/
propertyName: string
/**
* The state of the toggle.
*/
isToggled: boolean
/**
* If specified, it will be used to render the button; otherwise, we'll fallback to the tooltip as the button label.
*
* Note: The provided svg should contain the following attribute to be valid: xmlns="http://www.w3.org/2000/svg"
*/
icon?: string
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
interface WidgetPropertyMenuLinkItem extends PropertyMenuItem {
/**
* Specifies the link item type.
*/
itemType: 'link'
/**
* The tooltip of the link component.
*/
tooltip: string
/**
* Identifies the menu item.
*/
propertyName: string
/**
* The URL that opens when a user clicks the link item.
*/
href: string
/**
* If specified, it will be used to render the button; otherwise, we'll fallback to a default icon that looks like the image below:
*
* 
*
*
* In order to render the tooltip as the button's text, pass `null` as the value of `icon`.
*
* Note: The provided svg should contain the following attribute to be valid: xmlns="http://www.w3.org/2000/svg"
*/
icon?: string | null
}
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
type WidgetPropertyMenuItem =
| WidgetPropertyMenuActionItem
| WidgetPropertyMenuSeparatorItem
| WidgetPropertyMenuColorItem
| WidgetPropertyMenuDropdownItem
| WidgetPropertyMenuToggleItem
| WidgetPropertyMenuLinkItem
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
type WidgetPropertyMenu = WidgetPropertyMenuItem[]
/**
* @see https://developers.figma.com/docs/widgets/api/type-PropertyMenu
*/
interface WidgetPropertyEvent {
/**
* The propertyName of the item that was clicked.
*/
propertyName: string
/**
* The propertyValue of the item that was selected. This value will be a string value for `"dropdown"` and `"color-selector"` item types.
*/
propertyValue?: string | undefined
}
interface TextChildren {
children?:
| FigmaVirtualNode<'span'>
| string
| number
| (FigmaVirtualNode<'span'> | string | number)[]
}
interface TextProps extends BaseProps, WidgetJSX.WidgetJSXTextProps, TextChildren {
font?: {