@figma-vars/hooks 1.1.1 → 1.3.1

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 CHANGED
@@ -1,16 +1,25 @@
1
1
  # @figma-vars/hooks
2
2
 
3
- A modern, type-safe, and flexible React hooks library for the [Figma Variables API](https://www.figma.com/developers/api#variables).
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 and utilities 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.
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
+ ![Status](https://img.shields.io/badge/status-stable-brightgreen)
8
+ ![CI](https://github.com/marklearst/figma-vars-hooks/actions/workflows/publish.yml/badge.svg)
9
+ ![License](https://img.shields.io/github/license/marklearst/figma-vars-hooks)
10
+ ![GitHub last commit](https://img.shields.io/github/last-commit/marklearst/figma-vars-hooks)
11
+ ![GitHub code size](https://img.shields.io/github/languages/code-size/marklearst/figma-vars-hooks)
12
+ ![TypeScript](https://img.shields.io/badge/TypeScript-Strict-blue?logo=typescript)
13
+ ![npm](https://img.shields.io/npm/v/@figma-vars/hooks)
14
+ ![npm downloads](https://img.shields.io/npm/dm/@figma-vars/hooks)
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, built on top of `swr` for efficient caching, revalidation, and performance.
13
- - **✍️ Mutation Utilities**: Simple, promise-based functions for creating, updating, and deleting variables.
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`, `react-dom`, and `swr`.
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
- Now, you can use the hooks anywhere in your app:
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, collections, and modes for the file key provided to the `FigmaVarsProvider`.
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
- ### Mutations
154
+ ### Mutation Hooks
155
+
156
+ All mutation hooks return an object with the following shape: `{ mutate, data, isLoading, error }`.
100
157
 
101
- - `createVariable(token, fileKey, variableData)`
102
- - `updateVariable(token, fileKey, variableId, newData)`
103
- - `deleteVariable(token, fileKey, variableId)`
104
- - `bulkUpdateVariables(token, fileKey, variables)`
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
- ## 📄 License
197
+ ## 📝 License
141
198
 
142
- MIT
199
+ This project is licensed under the [MIT License](LICENSE).
200
+ © 2024–2025 Mark Learst