@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.
Files changed (46) hide show
  1. package/README.md +85 -19
  2. package/dist/figma-vars-hooks.js +312 -296
  3. package/dist/figma-vars-hooks.umd.cjs +3 -3
  4. package/dist/src/api/fetcher.d.ts +28 -0
  5. package/dist/src/api/index.d.ts +25 -0
  6. package/dist/src/api/mutator.d.ts +33 -0
  7. package/dist/src/api/mutator.test.d.ts +1 -0
  8. package/dist/src/constants/index.d.ts +89 -0
  9. package/dist/src/contexts/FigmaVarsProvider.d.ts +56 -0
  10. package/dist/src/contexts/index.d.ts +21 -0
  11. package/dist/src/hooks/index.d.ts +143 -0
  12. package/dist/src/hooks/useBulkUpdateVariables.d.ts +46 -0
  13. package/dist/src/hooks/useCreateVariable.d.ts +45 -0
  14. package/dist/src/hooks/useDeleteVariable.d.ts +33 -0
  15. package/dist/src/hooks/useFigmaToken.d.ts +21 -0
  16. package/dist/src/hooks/useMutation.d.ts +53 -0
  17. package/dist/src/hooks/useUpdateVariable.d.ts +40 -0
  18. package/dist/src/hooks/useVariableCollections.d.ts +35 -0
  19. package/dist/src/hooks/useVariableModes.d.ts +32 -0
  20. package/dist/src/hooks/useVariables.d.ts +32 -0
  21. package/dist/src/index.d.ts +80 -0
  22. package/dist/src/types/contexts.d.ts +77 -0
  23. package/dist/src/types/figma.d.ts +244 -0
  24. package/dist/src/types/hooks.d.ts +67 -0
  25. package/dist/src/types/index.d.ts +30 -0
  26. package/dist/src/types/mutations.d.ts +314 -0
  27. package/dist/src/utils/filterVariables.d.ts +31 -0
  28. package/dist/src/utils/index.d.ts +21 -0
  29. package/dist/tests/FigmaVarsProvider.test.d.ts +1 -0
  30. package/dist/tests/api/fetcher.test.d.ts +1 -0
  31. package/dist/tests/api/mutator.test.d.ts +1 -0
  32. package/dist/tests/constants/index.test.d.ts +1 -0
  33. package/dist/tests/hooks/useBulkUpdateVariables.test.d.ts +1 -0
  34. package/dist/tests/hooks/useCreateVariable.test.d.ts +1 -0
  35. package/dist/tests/hooks/useDeleteVariable.test.d.ts +1 -0
  36. package/dist/tests/hooks/useMutation.test.d.ts +1 -0
  37. package/dist/tests/hooks/useUpdateVariable.test.d.ts +1 -0
  38. package/dist/tests/hooks/useVariableCollections.test.d.ts +1 -0
  39. package/dist/tests/hooks/useVariableModes.test.d.ts +1 -0
  40. package/dist/tests/hooks/useVariables.test.d.ts +1 -0
  41. package/dist/tests/mocks/variables.d.ts +2 -0
  42. package/dist/tests/setup.d.ts +0 -0
  43. package/dist/tests/test-utils.d.ts +10 -0
  44. package/dist/tests/test-utils.test.d.ts +1 -0
  45. package/package.json +4 -2
  46. package/dist/index.d.ts +0 -1221
@@ -0,0 +1,30 @@
1
+ /****
2
+ * @packageDocumentation
3
+ *
4
+ * OSS barrel file for all public types and interfaces in @figma-vars/hooks.
5
+ *
6
+ * @remarks
7
+ * This module re-exports all Figma domain types (variables, collections, modes, API response models), mutation argument/result types, and all context/provider types for robust type-safe integration. Import types directly from this barrel for application code, plugins, API adapters, or custom UI tooling. All official Figma variable, mutation, and provider types are available from this entry point—see below for usage.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * // Importing multiple types for advanced integration and custom hooks:
12
+ * import type {
13
+ * FigmaVariable,
14
+ * FigmaCollection,
15
+ * VariableMode,
16
+ * UpdateVariableArgs,
17
+ * FigmaVarsProviderProps,
18
+ * MutationResult,
19
+ * } from '@figma-vars/hooks';
20
+ *
21
+ * // Use for type-safe props, mutation payloads, and UI mapping:
22
+ * function MyFeature(props: { variable: FigmaVariable; onUpdate: (args: UpdateVariableArgs) => void }) {
23
+ * // ...
24
+ * }
25
+ * ```
26
+ */
27
+ export * from './figma';
28
+ export * from './hooks';
29
+ export * from './mutations';
30
+ export * from './contexts';
@@ -0,0 +1,314 @@
1
+ import { ResolvedType, VariableScope, VariableValue } from './figma';
2
+ /**
3
+ * Payload for creating a new Figma variable in a specific collection.
4
+ *
5
+ * @remarks
6
+ * Use with the `useCreateVariable` hook to specify the properties of the new variable.
7
+ * Optional fields allow customization of description, publishing visibility, scopes, and code syntax.
8
+ *
9
+ * @property name - The human-readable name of the variable.
10
+ * @property variableCollectionId - The ID of the collection this variable belongs to.
11
+ * @property resolvedType - The data type of the variable value (e.g., 'COLOR', 'FLOAT').
12
+ * @property description - Optional description text for documentation or tooling.
13
+ * @property hiddenFromPublishing - Optional flag to hide the variable from published styles.
14
+ * @property scopes - Optional list of scopes restricting where the variable can be applied.
15
+ * @property codeSyntax - Optional mapping of language identifiers to code snippets for this variable.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import type { CreateVariablePayload } from '@figma-vars/hooks';
20
+ *
21
+ * const newVariable: CreateVariablePayload = {
22
+ * name: 'Primary Color',
23
+ * variableCollectionId: 'VariableCollectionId:123:456',
24
+ * resolvedType: 'COLOR',
25
+ * description: 'Main brand color',
26
+ * hiddenFromPublishing: false,
27
+ * scopes: ['ALL_FILLS'],
28
+ * codeSyntax: { css: 'var(--primary-color)' },
29
+ * }
30
+ * ```
31
+ *
32
+ * @public
33
+ */
34
+ export interface CreateVariablePayload {
35
+ name: string;
36
+ variableCollectionId: string;
37
+ resolvedType: ResolvedType;
38
+ description?: string;
39
+ hiddenFromPublishing?: boolean;
40
+ scopes?: VariableScope[];
41
+ codeSyntax?: Record<string, string>;
42
+ }
43
+ /**
44
+ * Payload for updating properties of an existing Figma variable.
45
+ *
46
+ * @remarks
47
+ * Use with the `useUpdateVariable` hook to specify fields to update.
48
+ * All fields are optional, allowing partial updates.
49
+ *
50
+ * @property name - New name for the variable.
51
+ * @property description - New description text.
52
+ * @property hiddenFromPublishing - Update publishing visibility.
53
+ * @property scopes - Update scopes.
54
+ * @property codeSyntax - Update code syntax mapping.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * import type { UpdateVariablePayload } from '@figma-vars/hooks';
59
+ *
60
+ * const updatePayload: UpdateVariablePayload = {
61
+ * name: 'Updated Color Name',
62
+ * description: 'Updated description',
63
+ * }
64
+ * ```
65
+ *
66
+ * @public
67
+ */
68
+ export interface UpdateVariablePayload {
69
+ name?: string;
70
+ description?: string;
71
+ hiddenFromPublishing?: boolean;
72
+ scopes?: VariableScope[];
73
+ codeSyntax?: Record<string, string>;
74
+ }
75
+ /**
76
+ * Enum of possible mutation actions for variables, collections, or modes.
77
+ *
78
+ * @remarks
79
+ * Used internally and in bulk update payloads to indicate the type of change.
80
+ *
81
+ * @public
82
+ */
83
+ export type VariableAction = 'CREATE' | 'UPDATE' | 'DELETE';
84
+ /**
85
+ * Represents a change operation on a Figma variable collection.
86
+ *
87
+ * @remarks
88
+ * Use in bulk update payloads to create, update, or delete collections.
89
+ *
90
+ * @property action - The mutation action to perform.
91
+ * @property id - Unique ID of the collection affected.
92
+ * @property name - Optional new name for the collection.
93
+ * @property initialModeId - Optional mode to set as default for the collection.
94
+ * @property hiddenFromPublishing - Optional flag to update publishing visibility.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * import type { VariableCollectionChange } from '@figma-vars/hooks';
99
+ *
100
+ * const change: VariableCollectionChange = {
101
+ * action: 'UPDATE',
102
+ * id: 'VariableCollectionId:123:456',
103
+ * name: 'New Collection Name',
104
+ * initialModeId: 'MODE:dark',
105
+ * }
106
+ * ```
107
+ *
108
+ * @public
109
+ */
110
+ export interface VariableCollectionChange {
111
+ action: VariableAction;
112
+ id: string;
113
+ name?: string;
114
+ initialModeId?: string;
115
+ hiddenFromPublishing?: boolean;
116
+ }
117
+ /**
118
+ * Represents a change operation on a Figma variable mode.
119
+ *
120
+ * @remarks
121
+ * Use in bulk update payloads to create, update, or delete modes.
122
+ *
123
+ * @property action - The mutation action to perform.
124
+ * @property id - Unique ID of the mode affected.
125
+ * @property name - Optional new name for the mode.
126
+ * @property variableCollectionId - The ID of the collection this mode belongs to.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * import type { VariableModeChange } from '@figma-vars/hooks';
131
+ *
132
+ * const modeChange: VariableModeChange = {
133
+ * action: 'CREATE',
134
+ * id: 'MODE:light',
135
+ * name: 'Light Mode',
136
+ * variableCollectionId: 'VariableCollectionId:123:456',
137
+ * }
138
+ * ```
139
+ *
140
+ * @public
141
+ */
142
+ export interface VariableModeChange {
143
+ action: VariableAction;
144
+ id: string;
145
+ name?: string;
146
+ variableCollectionId: string;
147
+ }
148
+ /**
149
+ * Represents a change operation on a Figma variable.
150
+ *
151
+ * @remarks
152
+ * Use in bulk update payloads to create, update, or delete variables.
153
+ *
154
+ * @property action - The mutation action to perform.
155
+ * @property id - Unique ID of the variable affected.
156
+ * @property name - Optional new name for the variable.
157
+ * @property variableCollectionId - Optional new collection ID for the variable.
158
+ * @property resolvedType - Optional new data type for the variable.
159
+ * @property description - Optional new description.
160
+ * @property hiddenFromPublishing - Optional update to publishing visibility.
161
+ * @property scopes - Optional update to scopes.
162
+ * @property codeSyntax - Optional update to code syntax.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import type { VariableChange } from '@figma-vars/hooks';
167
+ *
168
+ * const varChange: VariableChange = {
169
+ * action: 'DELETE',
170
+ * id: 'VariableID:123:456',
171
+ * }
172
+ * ```
173
+ *
174
+ * @public
175
+ */
176
+ export interface VariableChange {
177
+ action: VariableAction;
178
+ id: string;
179
+ name?: string;
180
+ variableCollectionId?: string;
181
+ resolvedType?: ResolvedType;
182
+ description?: string;
183
+ hiddenFromPublishing?: boolean;
184
+ scopes?: VariableScope[];
185
+ codeSyntax?: Record<string, string>;
186
+ }
187
+ /**
188
+ * Value assignment for a specific Figma variable in a specific mode.
189
+ *
190
+ * @remarks
191
+ * Used to represent the value of a variable for a given mode in bulk updates and payloads.
192
+ *
193
+ * @property variableId - The Figma variable ID being set.
194
+ * @property modeId - The mode ID (e.g., 'MODE:dark') this value applies to.
195
+ * @property value - The variable value, which can be a string, number, boolean, Color, or VariableAlias.
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * import type { VariableModeValue } from '@figma-vars/hooks';
200
+ *
201
+ * const modeValue: VariableModeValue = {
202
+ * variableId: 'VariableID:123:456',
203
+ * modeId: 'MODE:dark',
204
+ * value: { r: 0, g: 0, b: 0, a: 1 },
205
+ * };
206
+ * ```
207
+ *
208
+ * @public
209
+ */
210
+ export interface VariableModeValue {
211
+ variableId: string;
212
+ modeId: string;
213
+ value: VariableValue;
214
+ }
215
+ /**
216
+ * Payload for performing a bulk update of Figma variables, collections, modes, and values.
217
+ *
218
+ * @remarks
219
+ * Use with the `useBulkUpdateVariables` hook to perform atomic multi-entity updates.
220
+ *
221
+ * @property variableCollections - Optional array of collection changes.
222
+ * @property variableModes - Optional array of mode changes.
223
+ * @property variables - Optional array of variable changes.
224
+ * @property variableModeValues - Optional array of variable-mode value assignments.
225
+ *
226
+ * @example
227
+ * ```ts
228
+ * import type { BulkUpdatePayload } from '@figma-vars/hooks';
229
+ *
230
+ * const payload: BulkUpdatePayload = {
231
+ * variableCollections: [{ action: 'UPDATE', id: 'VariableCollectionId:123', name: 'New Name' }],
232
+ * variableModes: [{ action: 'CREATE', id: 'MODE:light', name: 'Light', variableCollectionId: 'VariableCollectionId:123' }],
233
+ * variables: [{ action: 'DELETE', id: 'VariableID:456' }],
234
+ * variableModeValues: [{ variableId: 'VariableID:789', modeId: 'MODE:dark', value: true }],
235
+ * }
236
+ * ```
237
+ *
238
+ * @public
239
+ */
240
+ export interface BulkUpdatePayload {
241
+ variableCollections?: VariableCollectionChange[];
242
+ variableModes?: VariableModeChange[];
243
+ variables?: VariableChange[];
244
+ variableModeValues?: VariableModeValue[];
245
+ }
246
+ /**
247
+ * Response from bulk update mutation API calls.
248
+ *
249
+ * @remarks
250
+ * Contains status, error indication, and optional mapping of temporary to real IDs.
251
+ *
252
+ * @property error - Boolean indicating if an error occurred.
253
+ * @property status - HTTP status code from the API response.
254
+ * @property message - Optional human-readable error or status message.
255
+ * @property meta - Optional metadata including temporary-to-real ID mapping.
256
+ *
257
+ * @example
258
+ * ```ts
259
+ * import type { BulkUpdateResponse } from '@figma-vars/hooks';
260
+ *
261
+ * function handleResponse(response: BulkUpdateResponse) {
262
+ * if (response.error) {
263
+ * console.error('Update failed:', response.message);
264
+ * } else {
265
+ * console.log('Update succeeded, IDs:', response.meta?.tempIdToRealId);
266
+ * }
267
+ * }
268
+ * ```
269
+ *
270
+ * @public
271
+ */
272
+ export interface BulkUpdateResponse {
273
+ error: boolean;
274
+ status: number;
275
+ message?: string;
276
+ meta?: {
277
+ tempIdToRealId: Record<string, string>;
278
+ };
279
+ }
280
+ /**
281
+ * Generic state shape for mutation hooks.
282
+ *
283
+ * @remarks
284
+ * Tracks mutation lifecycle states (`idle`, `loading`, `success`, `error`), along with data and error info.
285
+ *
286
+ * @typeParam TData - The type of data returned by the mutation.
287
+ *
288
+ * @public
289
+ */
290
+ export interface MutationState<TData> {
291
+ status: 'idle' | 'loading' | 'success' | 'error';
292
+ data: TData | null;
293
+ error: Error | null;
294
+ }
295
+ /**
296
+ * Return value of mutation hooks.
297
+ *
298
+ * @remarks
299
+ * Combines mutation state with a `mutate` trigger function accepting a payload, along with convenient booleans for status.
300
+ *
301
+ * @typeParam TData - The type of data returned by the mutation.
302
+ * @typeParam TPayload - The payload type accepted by the mutation trigger.
303
+ *
304
+ * @public
305
+ */
306
+ export interface MutationResult<TData, TPayload> {
307
+ mutate: (payload: TPayload) => Promise<TData | undefined>;
308
+ status: 'idle' | 'loading' | 'success' | 'error';
309
+ data: TData | null;
310
+ error: Error | null;
311
+ isLoading: boolean;
312
+ isSuccess: boolean;
313
+ isError: boolean;
314
+ }
@@ -0,0 +1,31 @@
1
+ import { FigmaVariable, ResolvedType } from 'types';
2
+ /**
3
+ * Utility function to filter Figma variables by type and/or substring name match.
4
+ *
5
+ * @remarks
6
+ * Returns a new array of variables matching the provided criteria. Use for building type pickers, variable search, dashboard views, or bulk edit tools. Filtering is case-sensitive for `name`. Pass no criteria to return all variables unfiltered.
7
+ *
8
+ * @param variables - The array of FigmaVariable objects to filter.
9
+ * @param criteria - Object specifying filter fields. Provide a `resolvedType` (e.g., 'COLOR', 'FLOAT') and/or a `name` substring to match variable names.
10
+ * @returns Array of FigmaVariable objects that match all provided criteria. Returns an empty array if no matches found. Returns all variables if criteria is empty.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { filterVariables } from '@figma-vars/hooks';
15
+ *
16
+ * // Example 1: Filter all color variables
17
+ * const colorVars = filterVariables(allVars, { resolvedType: 'COLOR' });
18
+ *
19
+ * // Example 2: Filter variables containing 'brand' in their name (case-sensitive)
20
+ * const brandVars = filterVariables(allVars, { name: 'brand' });
21
+ *
22
+ * // Example 3: Filter variables that are COLOR and include 'brand' in name
23
+ * const filtered = filterVariables(allVars, { resolvedType: 'COLOR', name: 'brand' });
24
+ * ```
25
+ *
26
+ * @public
27
+ */
28
+ export declare function filterVariables(variables: FigmaVariable[], criteria: {
29
+ resolvedType?: ResolvedType;
30
+ name?: string;
31
+ }): FigmaVariable[];
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Barrel file for stateless utility functions in @figma-vars/hooks.
4
+ *
5
+ * @summary
6
+ * Provides centralized exports of utility functions for manipulating and querying Figma variables and design tokens.
7
+ *
8
+ * @remarks
9
+ * Utilities exported here are pure functions, fully typed, and designed for use in UI filtering, scripting, and custom workflows.
10
+ * Import from this barrel for consistent and type-safe access to these helpers.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { filterVariables } from '@figma-vars/hooks/utils';
15
+ *
16
+ * const filtered = filterVariables(allVariables, { resolvedType: 'COLOR' });
17
+ * ```
18
+ *
19
+ * @public
20
+ */
21
+ export { filterVariables } from './filterVariables';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { LocalVariablesResponse } from '../../src/types';
2
+ export declare const mockVariablesResponse: LocalVariablesResponse;
File without changes
@@ -0,0 +1,10 @@
1
+ import { ReactNode } from 'react';
2
+ export declare const TestWrapper: ({ children }: {
3
+ children: ReactNode;
4
+ }) => import("react/jsx-runtime").JSX.Element;
5
+ /**
6
+ * Custom renderHook function that automatically wraps hooks with the TestWrapper.
7
+ * @param hook The hook to render.
8
+ */
9
+ export declare const renderHookWithWrapper: (hook: () => any) => import('@testing-library/react').RenderHookResult<any, unknown>;
10
+ export * from '@testing-library/react';
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@figma-vars/hooks",
3
- "version": "1.3.3",
3
+ "version": "1.4.3",
4
4
  "description": "Typed React hooks for managing Figma Variables, modes, tokens, and bindings via API.",
5
5
  "author": "Mark Learst",
6
6
  "license": "MIT",
@@ -60,6 +60,7 @@
60
60
  "@vitejs/plugin-react": "^4.5.2",
61
61
  "@vitest/coverage-v8": "^2.0.5",
62
62
  "@vitest/ui": "2.1.9",
63
+ "dotenv": "^16.5.0",
63
64
  "jsdom": "^26.1.0",
64
65
  "react": "^18.3.1",
65
66
  "react-dom": "^18.3.1",
@@ -84,6 +85,7 @@
84
85
  "test:ui": "vitest --ui",
85
86
  "test:coverage": "vitest run --coverage",
86
87
  "test:watch": "vitest watch",
87
- "postversion": "git push && git push --tags"
88
+ "postversion": "git push && git push --tags",
89
+ "create:docs": "npx typedoc src/index.ts --out docs-site/api"
88
90
  }
89
91
  }