@figma-vars/hooks 1.0.10 β†’ 1.1.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,62 +1,85 @@
1
- # figma-vars-hooks
1
+ # @figma-vars/hooks
2
2
 
3
- A modern, type-safe React hooks & utilities library for the [Figma Variables API](https://www.figma.com/developers/api#variables). Fetch, update, and sync design tokens between Figma and your app, Storybook, or design system dashboard.
3
+ A modern, type-safe, and flexible React hooks library for the [Figma Variables API](https://www.figma.com/developers/api#variables).
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.
4
6
 
5
7
  ---
6
8
 
7
9
  ## πŸš€ Features
8
10
 
9
- - **React 19+ hooks** for fetching and updating Figma variables, collections, and modes
10
- - **Mutation utilities** for creating, updating, and deleting variables
11
- - **TypeScript-first**: strict, ergonomic types for all API surfaces
12
- - **Storybook/Next.js ready** for live token dashboards
13
- - **Experimental/advanced hooks** for power users
11
+ - **βœ… 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.
14
+ - **πŸ”’ TypeScript-first**: Strictly typed for an ergonomic and safe developer experience. Get autocompletion for all API responses.
15
+ - **πŸ“– Storybook & Next.js Ready**: Perfect for building live design token dashboards or style guides.
14
16
 
15
17
  ---
16
18
 
17
19
  ## πŸ“¦ Install
18
20
 
19
21
  ```bash
20
- npm install figma-vars-hooks
22
+ npm install @figma-vars/hooks
21
23
  # or
22
- yarn add figma-vars-hooks
24
+ yarn add @figma-vars/hooks
23
25
  # or
24
- pnpm add figma-vars-hooks
26
+ pnpm add @figma-vars/hooks
25
27
  ```
26
28
 
27
- > **Peer dependency:** React 19+
29
+ > **Peer dependencies:** You'll need `react`, `react-dom`, and `swr`.
28
30
 
29
31
  ---
30
32
 
31
- ## πŸ› οΈ Setup: Figma API Token
33
+ ## πŸ› οΈ Setup & Usage
32
34
 
33
- Set your Figma Personal Access Token as an environment variable in your app (Vite example):
35
+ The library is designed to be as flexible as possible. You provide the Figma token and file key, and the hooks handle the rest.
34
36
 
35
- ```env
36
- VITE_FIGMA_TOKEN=your-figma-token-here
37
- ```
37
+ Wrap your application (or the relevant component tree) with the `FigmaVarsProvider`. This makes the Figma token and file key available to all the hooks.
38
38
 
39
- ---
39
+ ```tsx
40
+ // src/main.tsx or App.tsx
41
+ import React from 'react'
42
+ import ReactDOM from 'react-dom/client'
43
+ import { FigmaVarsProvider } from '@figma-vars/hooks'
44
+ import App from './App'
45
+
46
+ // The token can come from anywhere: .env, localStorage, state, etc.
47
+ const FIGMA_TOKEN = import.meta.env.VITE_FIGMA_TOKEN
48
+ const FIGMA_FILE_KEY = 'your-figma-file-key'
49
+
50
+ ReactDOM.createRoot(document.getElementById('root')!).render(
51
+ <React.StrictMode>
52
+ <FigmaVarsProvider
53
+ token={FIGMA_TOKEN}
54
+ fileKey={FIGMA_FILE_KEY}>
55
+ <App />
56
+ </FigmaVarsProvider>
57
+ </React.StrictMode>
58
+ )
59
+ ```
40
60
 
41
- ## ⚑ Quick Start Example
61
+ Now, you can use the hooks anywhere in your app:
42
62
 
43
63
  ```tsx
44
- import { useVariables } from 'figma-vars-hooks'
64
+ // src/components/TokenList.tsx
65
+ import { useVariables } from '@figma-vars/hooks'
45
66
 
46
- export function TokenList({ fileKey }: { fileKey: string }) {
47
- const { variables, loading, error, refresh } = useVariables(fileKey)
67
+ export function TokenList() {
68
+ const { data, isLoading, error } = useVariables()
48
69
 
49
- if (loading) return <div>Loading…</div>
70
+ if (isLoading) return <div>Loading tokens...</div>
50
71
  if (error) return <div>Error: {error.message}</div>
51
72
 
73
+ // The 'data' object contains variables, collections, and modes
74
+ const variables = Object.values(data?.variables ?? {})
75
+
52
76
  return (
53
77
  <ul>
54
- {variables?.map((v) => (
55
- <li key={v.id}>
56
- {v.name}: {v.value}
78
+ {variables.map((variable) => (
79
+ <li key={variable.id}>
80
+ {variable.name}: {JSON.stringify(variable.valuesByMode)}
57
81
  </li>
58
82
  ))}
59
- <button onClick={refresh}>Refresh</button>
60
83
  </ul>
61
84
  )
62
85
  }
@@ -68,51 +91,43 @@ export function TokenList({ fileKey }: { fileKey: string }) {
68
91
 
69
92
  ### Core Hooks
70
93
 
71
- - `useFigmaToken()` – Get the current Figma API token
72
- - `useVariables(fileKey, options?)` – Fetch variables for a Figma file (with polling/refresh)
73
- - `useVariableCollections(fileKey)` – Fetch variable collections for a file
74
- - `useVariableModes(collectionId)` – Fetch variable modes for a collection
94
+ - `useVariables()`: Fetches all local variables, collections, and modes for the file key provided to the `FigmaVarsProvider`.
95
+ - `useVariableCollections()`: A convenience hook that returns just the variable collections from the main `useVariables` data.
96
+ - `useVariableModes()`: A convenience hook that returns just the variable modes from the main `useVariables` data.
97
+ - `useFigmaToken()`: A simple hook to access the token and file key from the context.
75
98
 
76
99
  ### Mutations
77
100
 
78
- - `createVariable(fileKey, variableData)` – Create a new variable
79
- - `updateVariable(fileKey, variableId, newData)` – Update a variable
80
- - `deleteVariable(fileKey, variableId)` – Delete a variable
81
- - `updateVariableValues(variableId, values)` – Update variable values across modes
82
-
83
- ### Utilities
84
-
85
- - `filterVariables(variables, { type?, name? })` – Filter variables by type/name
86
- - `VariablesCache` – Simple in-memory cache for variable lists
87
- - `fetchHelpers`, `authHelpers` – Internal helpers (advanced)
101
+ - `createVariable(token, fileKey, variableData)`
102
+ - `updateVariable(token, fileKey, variableId, newData)`
103
+ - `deleteVariable(token, fileKey, variableId)`
104
+ - `bulkUpdateVariables(token, fileKey, variables)`
88
105
 
89
106
  ### Types
90
107
 
91
- All types are exported from `figma-vars-hooks/types` (see `src/types/`).
92
-
93
- ---
94
-
95
- ## πŸ§ͺ Experimental/Advanced Hooks
96
-
97
- - `useVariableAliases` – Manage local variable aliases (not in Figma API)
98
- - `useVariableBindings` – Bind variables to UI components (not in Figma API)
99
- - `usePublishVars`, `useSync` – Placeholders for future publish/sync features
100
-
101
- > These are opt-in and may changeβ€”see `src/experimental/` for details.
108
+ All types are exported from `@figma-vars/hooks`. The core response type from Figma is `Variables_LocalVariablesResponse`.
102
109
 
103
110
  ---
104
111
 
105
112
  ## πŸ“š Storybook & Next.js Integration
106
113
 
107
- Use these hooks in your Storybook stories or Next.js pages to build live design token dashboards. Example:
114
+ The provider model makes integration trivial. Simply wrap your Storybook stories or Next.js pages with the `FigmaVarsProvider`.
108
115
 
109
116
  ```tsx
110
117
  // In a Storybook story
111
- import { useVariables } from 'figma-vars-hooks'
112
-
113
- export const TokensStory = () => {
114
- const { variables } = useVariables('YOUR_FILE_KEY')
115
- return <pre>{JSON.stringify(variables, null, 2)}</pre>
118
+ import { FigmaVarsProvider, useVariables } from '@figma-vars/hooks'
119
+
120
+ export const TokensStory = () => (
121
+ <FigmaVarsProvider
122
+ token="YOUR_TOKEN"
123
+ fileKey="YOUR_FILE_KEY">
124
+ <TokenList />
125
+ </FigmaVarsProvider>
126
+ )
127
+
128
+ const TokenList = () => {
129
+ const { data } = useVariables()
130
+ return <pre>{JSON.stringify(data?.variables, null, 2)}</pre>
116
131
  }
117
132
  ```
118
133
 
@@ -120,14 +135,8 @@ export const TokensStory = () => {
120
135
 
121
136
  ## πŸ“ Contributing
122
137
 
123
- PRs and issues welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
138
+ PRs and issues are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
124
139
 
125
140
  ## πŸ“„ License
126
141
 
127
142
  MIT
128
-
129
- ---
130
-
131
- ## πŸ™ Credits
132
-
133
- Built by Mark Learst and contributors. Inspired by the Figma community and the need for seamless design token workflows.
@@ -1,11 +1,3 @@
1
1
  export declare const FIGMA_API_BASE_URL = "https://api.figma.com";
2
2
  export declare const FIGMA_FILES_ENDPOINT = "https://api.figma.com/v1/files";
3
3
  export declare const FIGMA_VARIABLES_ENDPOINT: (fileKey: string) => string;
4
- export declare const FIGMA_COLLECTIONS_ENDPOINT: (fileKey: string) => string;
5
- export declare const CREATE_VARIABLE_ACTION = "CREATE";
6
- export declare const UPDATE_VARIABLE_ACTION = "UPDATE";
7
- export declare const DELETE_VARIABLE_ACTION = "DELETE";
8
- export declare const DEFAULT_HEADERS: {
9
- 'Content-Type': string;
10
- };
11
- export declare const DEFAULT_ERROR_MESSAGE = "An error occurred while communicating with the Figma API.";
@@ -0,0 +1,11 @@
1
+ import { ReactNode } from 'react';
2
+ interface FigmaTokenContextType {
3
+ token: string | null;
4
+ }
5
+ interface FigmaVarsProviderProps {
6
+ children: ReactNode;
7
+ token: string | null;
8
+ }
9
+ export declare const FigmaVarsProvider: ({ children, token, }: FigmaVarsProviderProps) => import("react/jsx-runtime").JSX.Element;
10
+ export declare const useFigmaTokenContext: () => FigmaTokenContextType;
11
+ export {};