@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 +73 -64
- package/dist/constants/index.d.ts +0 -8
- package/dist/contexts/FigmaTokenContext.d.ts +11 -0
- package/dist/figma-vars-hooks.js +1153 -201
- package/dist/figma-vars-hooks.umd.cjs +38 -1
- package/dist/hooks/useFigmaToken.d.ts +3 -2
- package/dist/hooks/useVariableCollections.d.ts +13 -3
- package/dist/hooks/useVariableModes.d.ts +8 -5
- package/dist/hooks/useVariables.d.ts +10 -12
- package/dist/index.d.ts +12 -15
- package/dist/mutations/bulkUpdateVariables.d.ts +7 -0
- package/dist/mutations/createVariable.d.ts +7 -8
- package/dist/mutations/deleteVariable.d.ts +7 -8
- package/dist/mutations/updateVariable.d.ts +7 -9
- package/dist/types/figma.d.ts +33 -5
- package/dist/types/hooks.d.ts +9 -0
- package/dist/types/mutations.d.ts +67 -0
- package/dist/utils/authHelpers.d.ts +0 -4
- package/dist/utils/fetchHelpers.d.ts +1 -1
- package/dist/utils/filterVariables.d.ts +2 -2
- package/package.json +6 -3
- package/dist/experimental/usePublishVars.d.ts +0 -2
- package/dist/experimental/useSync.d.ts +0 -2
- package/dist/experimental/useVariableAliases.d.ts +0 -9
- package/dist/experimental/useVariableBindings.d.ts +0 -7
- package/dist/mutations/updateVariableValues.d.ts +0 -8
- package/dist/utils/cache.d.ts +0 -7
package/README.md
CHANGED
|
@@ -1,62 +1,85 @@
|
|
|
1
|
-
# figma-vars
|
|
1
|
+
# @figma-vars/hooks
|
|
2
2
|
|
|
3
|
-
A modern, type-safe React hooks
|
|
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
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
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
|
|
22
|
+
npm install @figma-vars/hooks
|
|
21
23
|
# or
|
|
22
|
-
yarn add figma-vars
|
|
24
|
+
yarn add @figma-vars/hooks
|
|
23
25
|
# or
|
|
24
|
-
pnpm add figma-vars
|
|
26
|
+
pnpm add @figma-vars/hooks
|
|
25
27
|
```
|
|
26
28
|
|
|
27
|
-
> **Peer
|
|
29
|
+
> **Peer dependencies:** You'll need `react`, `react-dom`, and `swr`.
|
|
28
30
|
|
|
29
31
|
---
|
|
30
32
|
|
|
31
|
-
## π οΈ Setup
|
|
33
|
+
## π οΈ Setup & Usage
|
|
32
34
|
|
|
33
|
-
|
|
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
|
-
|
|
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
|
-
|
|
61
|
+
Now, you can use the hooks anywhere in your app:
|
|
42
62
|
|
|
43
63
|
```tsx
|
|
44
|
-
|
|
64
|
+
// src/components/TokenList.tsx
|
|
65
|
+
import { useVariables } from '@figma-vars/hooks'
|
|
45
66
|
|
|
46
|
-
export function TokenList(
|
|
47
|
-
const {
|
|
67
|
+
export function TokenList() {
|
|
68
|
+
const { data, isLoading, error } = useVariables()
|
|
48
69
|
|
|
49
|
-
if (
|
|
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
|
|
55
|
-
<li key={
|
|
56
|
-
{
|
|
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
|
-
- `
|
|
72
|
-
- `
|
|
73
|
-
- `
|
|
74
|
-
- `
|
|
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)`
|
|
79
|
-
- `updateVariable(fileKey, variableId, newData)`
|
|
80
|
-
- `deleteVariable(fileKey, variableId)`
|
|
81
|
-
- `
|
|
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
|
|
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
|
-
|
|
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
|
|
112
|
-
|
|
113
|
-
export const TokensStory = () =>
|
|
114
|
-
|
|
115
|
-
|
|
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 {};
|