Skip to content

Commit ecf2a20

Browse files
committed
Docs: React Hooks
1 parent 677a4dc commit ecf2a20

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
- [Running the plugin locally](readme.md#running-the-plugin-locally)
44
- [Using the dev. console](readme.md#debugging-using-the-dev-console)
55
- [Making HTTP requests](readme.md#making-http-requests)
6-
7-
- [About](#)
86
- [Folder Structure](readme.md#folder-structure)
7+
- [Helpers](readme.md#react-helpers)

docs/readme.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,54 @@ Code updates are dynamically reflected through hot-reload
5050

5151
##### Making HTTP requests
5252

53-
This project includes Axios to make HTTP calls from the React UI App. Use the pre-configured Axios client from `ui/app/utils/axiosClient.ts` to make requests.
53+
54+
This project includes Axios to make HTTP calls from the React UI App. The pre-configured Axios client in `ui/app/utils/axiosClient.ts` allows users to make quick API calls with minimal setup.
5455

5556
Remember to add any new domains you want to make requests to in the `manifest.json` file under `networkAccess.allowedDomains`.
5657

58+
`````js
59+
import axiosClient from '../utils/axiosClient';
60+
..
61+
....
62+
const response = await axiosClient.get('https://dummyjson.com/quotes/random');
63+
const data = response.data;
64+
`````
65+
66+
67+
68+
### Folder Structure
69+
70+
Figma plugins typically follow a specific structure to separate concerns and facilitate development. The main components are the UI (user interface) and the plugin controller, which interacts with the Figma API. The `manifest.json` file in the root directory defines the plugin's metadata, permissions, and entry points.
71+
72+
- The `/ui` directory contains the plugin's user interface (React app). This includes components, styles, and logic for the visual part of the plugin that users interact with. `/ui/app/hooks` and `/ui/app/utils` have reusable hooks and utility functions.
5773

74+
- The `/plugin` directory contains the Figma controller. This is where the core plugin logic resides, including interactions with the Figma API, document manipulation, and communication with the UI. The `/plugin/helpers` has resuable code.
5875

5976

6077

6178

79+
### React Helpers
6280

81+
The `useFigmaMessaging` hook simplifies communication between your React app and Figma.
6382

64-
## Folder Structure
83+
`````js
84+
const { sendToFigma, onFigmaMessage } = useFigmaMessaging({
85+
targetOrigin: 'https://www.figma.com', // * may be used for local testing, less secure
86+
debounceMs: 300, // debounce; to prevent spamming Figma with messages
87+
});
88+
``````
6589

66-
- The `/ui` directory contains the React code for the plugin's user interface.
67-
- The `/plugin` directory contains the Figma controller
90+
````js
91+
// Example
92+
sendToFigma({ type: 'FETCH_DATA' });
93+
// Implement 'FETCH_DATA' handling in pluginController.ts > figma.ui.onmessage
94+
..
95+
...
96+
// Handling messages sent from the Plugin
97+
onFigmaMessage((msg) => {
98+
if (msg.type === 'FETCH_DATA_RESPONSE') {
99+
console.log('Received data from Figma:', msg.data);
100+
}
101+
});
102+
````
103+
This setup allows you to easily send messages to Figma and handle responses.

0 commit comments

Comments
 (0)