@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.
- package/README.md +90 -19
- package/dist/figma-vars-hooks.js +312 -296
- package/dist/figma-vars-hooks.umd.cjs +3 -3
- package/dist/src/api/fetcher.d.ts +28 -0
- package/dist/src/api/index.d.ts +25 -0
- package/dist/src/api/mutator.d.ts +33 -0
- package/dist/src/api/mutator.test.d.ts +1 -0
- package/dist/src/constants/index.d.ts +89 -0
- package/dist/src/contexts/FigmaVarsProvider.d.ts +56 -0
- package/dist/src/contexts/index.d.ts +21 -0
- package/dist/src/hooks/index.d.ts +143 -0
- package/dist/src/hooks/useBulkUpdateVariables.d.ts +46 -0
- package/dist/src/hooks/useCreateVariable.d.ts +45 -0
- package/dist/src/hooks/useDeleteVariable.d.ts +33 -0
- package/dist/src/hooks/useFigmaToken.d.ts +21 -0
- package/dist/src/hooks/useMutation.d.ts +53 -0
- package/dist/src/hooks/useUpdateVariable.d.ts +40 -0
- package/dist/src/hooks/useVariableCollections.d.ts +35 -0
- package/dist/src/hooks/useVariableModes.d.ts +32 -0
- package/dist/src/hooks/useVariables.d.ts +32 -0
- package/dist/src/index.d.ts +80 -0
- package/dist/src/types/contexts.d.ts +77 -0
- package/dist/src/types/figma.d.ts +244 -0
- package/dist/src/types/hooks.d.ts +67 -0
- package/dist/src/types/index.d.ts +30 -0
- package/dist/src/types/mutations.d.ts +314 -0
- package/dist/src/utils/filterVariables.d.ts +31 -0
- package/dist/src/utils/index.d.ts +21 -0
- package/dist/tests/FigmaVarsProvider.test.d.ts +1 -0
- package/dist/tests/api/fetcher.test.d.ts +1 -0
- package/dist/tests/api/mutator.test.d.ts +1 -0
- package/dist/tests/constants/index.test.d.ts +1 -0
- package/dist/tests/hooks/useBulkUpdateVariables.test.d.ts +1 -0
- package/dist/tests/hooks/useCreateVariable.test.d.ts +1 -0
- package/dist/tests/hooks/useDeleteVariable.test.d.ts +1 -0
- package/dist/tests/hooks/useMutation.test.d.ts +1 -0
- package/dist/tests/hooks/useUpdateVariable.test.d.ts +1 -0
- package/dist/tests/hooks/useVariableCollections.test.d.ts +1 -0
- package/dist/tests/hooks/useVariableModes.test.d.ts +1 -0
- package/dist/tests/hooks/useVariables.test.d.ts +1 -0
- package/dist/tests/mocks/variables.d.ts +2 -0
- package/dist/tests/setup.d.ts +0 -0
- package/dist/tests/test-utils.d.ts +10 -0
- package/dist/tests/test-utils.test.d.ts +1 -0
- package/package.json +9 -3
- package/dist/index.d.ts +0 -1221
package/README.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
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
|

|
|
8
10
|

|
|
11
|
+
[](https://codecov.io/gh/marklearst/figma-vars-hooks)
|
|
9
12
|

|
|
10
13
|

|
|
11
14
|

|
|
@@ -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
|
|
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
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
|
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
|
|
159
|
-
- `useUpdateVariable()`: Updates an existing variable. The `mutate` function expects an object
|
|
160
|
-
- `useDeleteVariable()`: Deletes a variable. The `mutate` function expects the `
|
|
161
|
-
- `useBulkUpdateVariables()`:
|
|
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 `
|
|
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
|