@figma-vars/hooks 1.3.2 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +90 -19
  2. package/dist/figma-vars-hooks.js +312 -296
  3. package/dist/figma-vars-hooks.umd.cjs +3 -3
  4. package/dist/src/api/fetcher.d.ts +28 -0
  5. package/dist/src/api/index.d.ts +25 -0
  6. package/dist/src/api/mutator.d.ts +33 -0
  7. package/dist/src/api/mutator.test.d.ts +1 -0
  8. package/dist/src/constants/index.d.ts +89 -0
  9. package/dist/src/contexts/FigmaVarsProvider.d.ts +56 -0
  10. package/dist/src/contexts/index.d.ts +21 -0
  11. package/dist/src/hooks/index.d.ts +143 -0
  12. package/dist/src/hooks/useBulkUpdateVariables.d.ts +46 -0
  13. package/dist/src/hooks/useCreateVariable.d.ts +45 -0
  14. package/dist/src/hooks/useDeleteVariable.d.ts +33 -0
  15. package/dist/src/hooks/useFigmaToken.d.ts +21 -0
  16. package/dist/src/hooks/useMutation.d.ts +53 -0
  17. package/dist/src/hooks/useUpdateVariable.d.ts +40 -0
  18. package/dist/src/hooks/useVariableCollections.d.ts +35 -0
  19. package/dist/src/hooks/useVariableModes.d.ts +32 -0
  20. package/dist/src/hooks/useVariables.d.ts +32 -0
  21. package/dist/src/index.d.ts +80 -0
  22. package/dist/src/types/contexts.d.ts +77 -0
  23. package/dist/src/types/figma.d.ts +244 -0
  24. package/dist/src/types/hooks.d.ts +67 -0
  25. package/dist/src/types/index.d.ts +30 -0
  26. package/dist/src/types/mutations.d.ts +314 -0
  27. package/dist/src/utils/filterVariables.d.ts +31 -0
  28. package/dist/src/utils/index.d.ts +21 -0
  29. package/dist/tests/FigmaVarsProvider.test.d.ts +1 -0
  30. package/dist/tests/api/fetcher.test.d.ts +1 -0
  31. package/dist/tests/api/mutator.test.d.ts +1 -0
  32. package/dist/tests/constants/index.test.d.ts +1 -0
  33. package/dist/tests/hooks/useBulkUpdateVariables.test.d.ts +1 -0
  34. package/dist/tests/hooks/useCreateVariable.test.d.ts +1 -0
  35. package/dist/tests/hooks/useDeleteVariable.test.d.ts +1 -0
  36. package/dist/tests/hooks/useMutation.test.d.ts +1 -0
  37. package/dist/tests/hooks/useUpdateVariable.test.d.ts +1 -0
  38. package/dist/tests/hooks/useVariableCollections.test.d.ts +1 -0
  39. package/dist/tests/hooks/useVariableModes.test.d.ts +1 -0
  40. package/dist/tests/hooks/useVariables.test.d.ts +1 -0
  41. package/dist/tests/mocks/variables.d.ts +2 -0
  42. package/dist/tests/setup.d.ts +0 -0
  43. package/dist/tests/test-utils.d.ts +10 -0
  44. package/dist/tests/test-utils.test.d.ts +1 -0
  45. package/package.json +9 -3
  46. package/dist/index.d.ts +0 -1221
package/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # @figma-vars/hooks
1
+ <p align="left">
2
+ <img src="assets/figma-vars-tagline-light.png" alt="FigmaVars Logo" width="700px" />
3
+ </p>
2
4
 
3
5
  A fast, typed React 19 hooks library for the Figma Variables API: fetch, update, and manage design tokens via the official [Figma REST API](https://www.figma.com/developers/api#variables).
4
6
 
@@ -6,6 +8,7 @@ Built for the modern web, this library provides a suite of hooks to fetch, manag
6
8
 
7
9
  ![Status](https://img.shields.io/badge/status-stable-brightgreen)
8
10
  ![CI](https://github.com/marklearst/figma-vars-hooks/actions/workflows/publish.yml/badge.svg)
11
+ [![codecov](https://codecov.io/gh/marklearst/figma-vars-hooks/branch/main/graph/badge.svg)](https://codecov.io/gh/marklearst/figma-vars-hooks)
9
12
  ![License](https://img.shields.io/github/license/marklearst/figma-vars-hooks)
10
13
  ![GitHub last commit](https://img.shields.io/github/last-commit/marklearst/figma-vars-hooks)
11
14
  ![GitHub code size](https://img.shields.io/github/languages/code-size/marklearst/figma-vars-hooks)
@@ -105,21 +108,26 @@ Here's an example of creating a new variable:
105
108
  ```tsx
106
109
  // src/components/CreateVariableForm.tsx
107
110
  import { useCreateVariable } from '@figma-vars/hooks'
111
+ import type { CreateVariablePayload } from '@figma-vars/hooks'
108
112
 
109
- function CreateVariableForm({ collectionId, modeId }) {
113
+ function CreateVariableForm({ collectionId }: { collectionId: string }) {
110
114
  const { mutate, data, isLoading, error } = useCreateVariable()
111
115
 
112
- const handleSubmit = (event) => {
116
+ const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
113
117
  event.preventDefault()
114
- const variableName = event.target.elements.variableName.value
115
-
116
- mutate({
117
- name: `colors/${variableName}`,
118
- collectionId: collectionId,
119
- valuesByMode: {
120
- [modeId]: { r: 1, g: 0, b: 0, a: 1 },
121
- },
122
- })
118
+ const form = event.currentTarget
119
+ const variableName = (
120
+ form.elements.namedItem('variableName') as HTMLInputElement
121
+ )?.value
122
+
123
+ if (!variableName) return
124
+
125
+ const payload: CreateVariablePayload = {
126
+ name: variableName,
127
+ variableCollectionId: collectionId,
128
+ resolvedType: 'COLOR', // Example type
129
+ }
130
+ mutate(payload)
123
131
  }
124
132
 
125
133
  return (
@@ -140,13 +148,76 @@ function CreateVariableForm({ collectionId, modeId }) {
140
148
  }
141
149
  ```
142
150
 
151
+ ### Figma PAT Security
152
+
153
+ When using the Figma API, it's essential to keep your Personal Access Token (PAT) secure. Here are some best practices:
154
+
155
+ - Never hardcode your PAT in your code.
156
+ - Use environment variables or a secure storage mechanism to store your PAT.
157
+ - Limit the scope of your PAT to only the necessary permissions.
158
+ - Rotate your PAT regularly.
159
+
160
+ ### Advanced Usage
161
+
162
+ For advanced use cases, you can use the `useFigmaToken` hook to access the token and file key from the context.
163
+
164
+ ```tsx
165
+ // src/components/AdvancedUsage.tsx
166
+ import { useFigmaToken } from '@figma-vars/hooks'
167
+
168
+ function AdvancedUsage() {
169
+ const { token, fileKey } = useFigmaToken()
170
+
171
+ // Use the token and file key to make custom API requests
172
+ const apiRequest = async () => {
173
+ const response = await fetch(
174
+ `https://api.figma.com/v1/files/${fileKey}/variables`,
175
+ {
176
+ headers: {
177
+ 'X-Figma-Token': token,
178
+ },
179
+ }
180
+ )
181
+
182
+ const data = await response.json()
183
+ console.log(data)
184
+ }
185
+
186
+ return <button onClick={apiRequest}>Make API Request</button>
187
+ }
188
+ ```
189
+
190
+ ### Error Handling
191
+
192
+ All hooks return an `error` state that you can use to handle errors.
193
+
194
+ ```tsx
195
+ // src/components/ErrorHandling.tsx
196
+ import { useVariables } from '@figma-vars/hooks'
197
+
198
+ function ErrorHandling() {
199
+ const { data, isLoading, error } = useVariables()
200
+
201
+ if (error) {
202
+ return (
203
+ <div>
204
+ <h2>Error</h2>
205
+ <p>{error.message}</p>
206
+ </div>
207
+ )
208
+ }
209
+
210
+ // Render data or loading state
211
+ }
212
+ ```
213
+
143
214
  ---
144
215
 
145
216
  ## 🧩 API Reference
146
217
 
147
218
  ### Core Hooks
148
219
 
149
- - `useVariables()`: Fetches all local variables for the file key provided to the `FigmaVarsProvider`. Returns an object with `data`, `isLoading`, and `error` properties. The `data` object contains `variables`, `variableCollections`, and `modes` from the Figma API.
220
+ - `useVariables()`: Fetches all local variables for the file key provided to the `FigmaVarsProvider`. Returns a SWR hook state with `data`, `isLoading`, and `error` properties. The actual Figma response is in `data.data`.
150
221
  - `useVariableCollections()`: A convenience hook that returns just the variable collections from the main `useVariables` data.
151
222
  - `useVariableModes()`: A convenience hook that returns just the variable modes from the main `useVariables` data.
152
223
  - `useFigmaToken()`: A simple hook to access the token and file key from the context.
@@ -155,14 +226,14 @@ function CreateVariableForm({ collectionId, modeId }) {
155
226
 
156
227
  All mutation hooks return an object with the following shape: `{ mutate, data, isLoading, error }`.
157
228
 
158
- - `useCreateVariable()`: Creates a new variable. The `mutate` function expects the variable data as its payload.
159
- - `useUpdateVariable()`: Updates an existing variable. The `mutate` function expects an object with `id` and the `data` to update.
160
- - `useDeleteVariable()`: Deletes a variable. The `mutate` function expects the `id` of the variable to delete.
161
- - `useBulkUpdateVariables()`: Updates multiple variables in a single batch operation. The `mutate` function expects an array of variables to update.
229
+ - `useCreateVariable()`: Creates a new variable. The `mutate` function expects a `CreateVariablePayload` object.
230
+ - `useUpdateVariable()`: Updates an existing variable. The `mutate` function expects an object `{ variableId, payload }` where `payload` is an `UpdateVariablePayload`.
231
+ - `useDeleteVariable()`: Deletes a variable. The `mutate` function expects the `variableId` (string) of the variable to delete.
232
+ - `useBulkUpdateVariables()`: Creates, updates, or deletes multiple entities in a single batch operation. The `mutate` function expects a `BulkUpdatePayload` object.
162
233
 
163
234
  ### Types
164
235
 
165
- All types are exported from `@figma-vars/hooks`. The core response type from Figma for local variables is `Variables_LocalVariablesResponse`.
236
+ All types are exported from `@figma-vars/hooks`. The core response type from Figma for local variables is `LocalVariablesResponse`.
166
237
 
167
238
  ---
168
239
 
@@ -196,5 +267,5 @@ PRs and issues are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for gu
196
267
 
197
268
  ## 📝 License
198
269
 
199
- This project is licensed under the [MIT License](LICENSE).
270
+ This project is licensed under the [MIT License](LICENSE).
200
271
  © 2024–2025 Mark Learst