@figma-vars/hooks 1.3.3 → 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 +85 -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 +4 -2
- package/dist/index.d.ts +0 -1221
package/README.md
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# @figma-vars/hooks
|
|
2
|
-
|
|
3
1
|
<p align="left">
|
|
4
2
|
<img src="assets/figma-vars-tagline-light.png" alt="FigmaVars Logo" width="700px" />
|
|
5
3
|
</p>
|
|
@@ -110,21 +108,26 @@ Here's an example of creating a new variable:
|
|
|
110
108
|
```tsx
|
|
111
109
|
// src/components/CreateVariableForm.tsx
|
|
112
110
|
import { useCreateVariable } from '@figma-vars/hooks'
|
|
111
|
+
import type { CreateVariablePayload } from '@figma-vars/hooks'
|
|
113
112
|
|
|
114
|
-
function CreateVariableForm({ collectionId
|
|
113
|
+
function CreateVariableForm({ collectionId }: { collectionId: string }) {
|
|
115
114
|
const { mutate, data, isLoading, error } = useCreateVariable()
|
|
116
115
|
|
|
117
|
-
const handleSubmit = (event) => {
|
|
116
|
+
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
|
118
117
|
event.preventDefault()
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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)
|
|
128
131
|
}
|
|
129
132
|
|
|
130
133
|
return (
|
|
@@ -145,13 +148,76 @@ function CreateVariableForm({ collectionId, modeId }) {
|
|
|
145
148
|
}
|
|
146
149
|
```
|
|
147
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
|
+
|
|
148
214
|
---
|
|
149
215
|
|
|
150
216
|
## 🧩 API Reference
|
|
151
217
|
|
|
152
218
|
### Core Hooks
|
|
153
219
|
|
|
154
|
-
- `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`.
|
|
155
221
|
- `useVariableCollections()`: A convenience hook that returns just the variable collections from the main `useVariables` data.
|
|
156
222
|
- `useVariableModes()`: A convenience hook that returns just the variable modes from the main `useVariables` data.
|
|
157
223
|
- `useFigmaToken()`: A simple hook to access the token and file key from the context.
|
|
@@ -160,14 +226,14 @@ function CreateVariableForm({ collectionId, modeId }) {
|
|
|
160
226
|
|
|
161
227
|
All mutation hooks return an object with the following shape: `{ mutate, data, isLoading, error }`.
|
|
162
228
|
|
|
163
|
-
- `useCreateVariable()`: Creates a new variable. The `mutate` function expects
|
|
164
|
-
- `useUpdateVariable()`: Updates an existing variable. The `mutate` function expects an object
|
|
165
|
-
- `useDeleteVariable()`: Deletes a variable. The `mutate` function expects the `
|
|
166
|
-
- `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.
|
|
167
233
|
|
|
168
234
|
### Types
|
|
169
235
|
|
|
170
|
-
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`.
|
|
171
237
|
|
|
172
238
|
---
|
|
173
239
|
|