@figma-vars/hooks 1.2.0 → 1.3.2
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 +73 -15
- package/dist/figma-vars-hooks.js +559 -799
- package/dist/figma-vars-hooks.umd.cjs +3 -24
- package/dist/index.d.ts +1221 -11
- package/package.json +17 -8
- package/dist/constants/index.d.ts +0 -3
- package/dist/contexts/FigmaTokenContext.d.ts +0 -49
- package/dist/hooks/useFigmaToken.d.ts +0 -17
- package/dist/hooks/useVariableCollections.d.ts +0 -26
- package/dist/hooks/useVariableModes.d.ts +0 -16
- package/dist/hooks/useVariables.d.ts +0 -25
- package/dist/mutations/bulkUpdateVariables.d.ts +0 -25
- package/dist/mutations/createVariable.d.ts +0 -26
- package/dist/mutations/deleteVariable.d.ts +0 -19
- package/dist/mutations/updateVariable.d.ts +0 -26
- package/dist/types/figma.d.ts +0 -111
- package/dist/types/hooks.d.ts +0 -12
- package/dist/types/index.d.ts +0 -3
- package/dist/types/mutations.d.ts +0 -114
- package/dist/utils/fetcher.d.ts +0 -13
- package/dist/utils/filterVariables.d.ts +0 -31
package/README.md
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
# @figma-vars/hooks
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
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
4
|
|
|
5
|
-
Built for the modern web, this library provides a suite of hooks
|
|
5
|
+
Built for the modern web, this library provides a suite of hooks to fetch, manage, and mutate your design tokens, making it easy to sync them between Figma and your React applications, Storybooks, or design system dashboards.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+

|
|
12
|
+

|
|
13
|
+

|
|
14
|
+

|
|
6
15
|
|
|
7
16
|
---
|
|
8
17
|
|
|
9
18
|
## 🚀 Features
|
|
10
19
|
|
|
11
20
|
- **✅ Token Agnostic for the Best DX**: Our library doesn't care _how_ you get your Figma token. Use environment variables, `localStorage`, a state management library, or even a simple input field. This framework-agnostic approach means it works seamlessly with Vite, Next.js, Create React App, and more, without locking you into a specific build tool.
|
|
12
|
-
- **⚛️ Modern React Hooks**: A full suite of hooks for fetching Figma variables, collections, and modes
|
|
13
|
-
- **✍️
|
|
21
|
+
- **⚛️ Modern React Hooks**: A full suite of hooks for fetching and mutating Figma variables, collections, and modes.
|
|
22
|
+
- **✍️ Ergonomic Mutations**: A `useMutation`-style API for creating, updating, and deleting variables, providing clear loading and error states.
|
|
14
23
|
- **🔒 TypeScript-first**: Strictly typed for an ergonomic and safe developer experience. Get autocompletion for all API responses.
|
|
15
24
|
- **📖 Storybook & Next.js Ready**: Perfect for building live design token dashboards or style guides.
|
|
16
25
|
|
|
@@ -26,7 +35,7 @@ yarn add @figma-vars/hooks
|
|
|
26
35
|
pnpm add @figma-vars/hooks
|
|
27
36
|
```
|
|
28
37
|
|
|
29
|
-
> **Peer dependencies:** You'll need `react
|
|
38
|
+
> **Peer dependencies:** You'll need `react` and `react-dom`.
|
|
30
39
|
|
|
31
40
|
---
|
|
32
41
|
|
|
@@ -58,7 +67,9 @@ ReactDOM.createRoot(document.getElementById('root')!).render(
|
|
|
58
67
|
)
|
|
59
68
|
```
|
|
60
69
|
|
|
61
|
-
|
|
70
|
+
### Fetching Data
|
|
71
|
+
|
|
72
|
+
Now, you can use the query hooks anywhere in your app:
|
|
62
73
|
|
|
63
74
|
```tsx
|
|
64
75
|
// src/components/TokenList.tsx
|
|
@@ -85,27 +96,73 @@ export function TokenList() {
|
|
|
85
96
|
}
|
|
86
97
|
```
|
|
87
98
|
|
|
99
|
+
### Mutating Data
|
|
100
|
+
|
|
101
|
+
To create, update, or delete variables, use the provided mutation hooks. They follow a standard pattern, returning a `mutate` function and states for `data`, `isLoading`, and `error`.
|
|
102
|
+
|
|
103
|
+
Here's an example of creating a new variable:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
// src/components/CreateVariableForm.tsx
|
|
107
|
+
import { useCreateVariable } from '@figma-vars/hooks'
|
|
108
|
+
|
|
109
|
+
function CreateVariableForm({ collectionId, modeId }) {
|
|
110
|
+
const { mutate, data, isLoading, error } = useCreateVariable()
|
|
111
|
+
|
|
112
|
+
const handleSubmit = (event) => {
|
|
113
|
+
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
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<form onSubmit={handleSubmit}>
|
|
127
|
+
<input
|
|
128
|
+
name="variableName"
|
|
129
|
+
placeholder="New variable name"
|
|
130
|
+
/>
|
|
131
|
+
<button
|
|
132
|
+
type="submit"
|
|
133
|
+
disabled={isLoading}>
|
|
134
|
+
{isLoading ? 'Creating...' : 'Create Variable'}
|
|
135
|
+
</button>
|
|
136
|
+
{error && <p>Error: {error.message}</p>}
|
|
137
|
+
{data && <p>Created variable with ID: {data.id}</p>}
|
|
138
|
+
</form>
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
88
143
|
---
|
|
89
144
|
|
|
90
145
|
## 🧩 API Reference
|
|
91
146
|
|
|
92
147
|
### Core Hooks
|
|
93
148
|
|
|
94
|
-
- `useVariables()`: Fetches all local variables
|
|
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.
|
|
95
150
|
- `useVariableCollections()`: A convenience hook that returns just the variable collections from the main `useVariables` data.
|
|
96
151
|
- `useVariableModes()`: A convenience hook that returns just the variable modes from the main `useVariables` data.
|
|
97
152
|
- `useFigmaToken()`: A simple hook to access the token and file key from the context.
|
|
98
153
|
|
|
99
|
-
###
|
|
154
|
+
### Mutation Hooks
|
|
155
|
+
|
|
156
|
+
All mutation hooks return an object with the following shape: `{ mutate, data, isLoading, error }`.
|
|
100
157
|
|
|
101
|
-
- `
|
|
102
|
-
- `
|
|
103
|
-
- `
|
|
104
|
-
- `
|
|
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.
|
|
105
162
|
|
|
106
163
|
### Types
|
|
107
164
|
|
|
108
|
-
All types are exported from `@figma-vars/hooks`. The core response type from Figma is `Variables_LocalVariablesResponse`.
|
|
165
|
+
All types are exported from `@figma-vars/hooks`. The core response type from Figma for local variables is `Variables_LocalVariablesResponse`.
|
|
109
166
|
|
|
110
167
|
---
|
|
111
168
|
|
|
@@ -137,6 +194,7 @@ const TokenList = () => {
|
|
|
137
194
|
|
|
138
195
|
PRs and issues are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
139
196
|
|
|
140
|
-
##
|
|
197
|
+
## 📝 License
|
|
141
198
|
|
|
142
|
-
MIT
|
|
199
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
200
|
+
© 2024–2025 Mark Learst
|