@seed-design/figma 0.0.0-alpha-20260324091316

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 (181) hide show
  1. package/lib/codegen/index.cjs +23543 -0
  2. package/lib/codegen/index.d.ts +2957 -0
  3. package/lib/codegen/index.d.ts.map +1 -0
  4. package/lib/codegen/index.js +23514 -0
  5. package/lib/codegen/targets/react/index.cjs +31980 -0
  6. package/lib/codegen/targets/react/index.d.ts +308 -0
  7. package/lib/codegen/targets/react/index.d.ts.map +1 -0
  8. package/lib/codegen/targets/react/index.js +31961 -0
  9. package/lib/index.cjs +26905 -0
  10. package/lib/index.d.ts +221 -0
  11. package/lib/index.d.ts.map +1 -0
  12. package/lib/index.js +26884 -0
  13. package/package.json +56 -0
  14. package/src/codegen/component-properties.archive.ts +1019 -0
  15. package/src/codegen/component-properties.ts +369 -0
  16. package/src/codegen/core/codegen.ts +112 -0
  17. package/src/codegen/core/component-handler.ts +23 -0
  18. package/src/codegen/core/component-type-helper.ts +35 -0
  19. package/src/codegen/core/element-transformer.ts +13 -0
  20. package/src/codegen/core/index.ts +19 -0
  21. package/src/codegen/core/infer-layout.test.ts +286 -0
  22. package/src/codegen/core/infer-layout.ts +416 -0
  23. package/src/codegen/core/jsx.ts +174 -0
  24. package/src/codegen/core/props-converter.ts +78 -0
  25. package/src/codegen/core/value-resolver.ts +381 -0
  26. package/src/codegen/default-services.ts +44 -0
  27. package/src/codegen/index.ts +3 -0
  28. package/src/codegen/skip-components.ts +7 -0
  29. package/src/codegen/targets/figma/frame.ts +38 -0
  30. package/src/codegen/targets/figma/index.ts +6 -0
  31. package/src/codegen/targets/figma/instance.ts +36 -0
  32. package/src/codegen/targets/figma/pipeline.ts +106 -0
  33. package/src/codegen/targets/figma/props.ts +262 -0
  34. package/src/codegen/targets/figma/shape.ts +65 -0
  35. package/src/codegen/targets/figma/text.ts +30 -0
  36. package/src/codegen/targets/figma/value-resolver.ts +75 -0
  37. package/src/codegen/targets/index.ts +2 -0
  38. package/src/codegen/targets/react/component/deps.interface.ts +7 -0
  39. package/src/codegen/targets/react/component/handlers/action-button.ts +149 -0
  40. package/src/codegen/targets/react/component/handlers/alert-dialog.ts +120 -0
  41. package/src/codegen/targets/react/component/handlers/app-bar.ts +169 -0
  42. package/src/codegen/targets/react/component/handlers/archive/action-button.ts +144 -0
  43. package/src/codegen/targets/react/component/handlers/archive/alert-dialog.ts +122 -0
  44. package/src/codegen/targets/react/component/handlers/archive/app-bar.ts +149 -0
  45. package/src/codegen/targets/react/component/handlers/archive/avatar-stack.ts +35 -0
  46. package/src/codegen/targets/react/component/handlers/archive/avatar.ts +55 -0
  47. package/src/codegen/targets/react/component/handlers/archive/badge.ts +18 -0
  48. package/src/codegen/targets/react/component/handlers/archive/bottom-sheet.ts +70 -0
  49. package/src/codegen/targets/react/component/handlers/archive/callout.ts +88 -0
  50. package/src/codegen/targets/react/component/handlers/archive/checkbox.ts +43 -0
  51. package/src/codegen/targets/react/component/handlers/archive/checkmark.ts +29 -0
  52. package/src/codegen/targets/react/component/handlers/archive/chip.ts +90 -0
  53. package/src/codegen/targets/react/component/handlers/archive/contextual-floating-button.ts +52 -0
  54. package/src/codegen/targets/react/component/handlers/archive/divider.ts +25 -0
  55. package/src/codegen/targets/react/component/handlers/archive/field-button.ts +197 -0
  56. package/src/codegen/targets/react/component/handlers/archive/field.ts +167 -0
  57. package/src/codegen/targets/react/component/handlers/archive/floating-action-button.ts +48 -0
  58. package/src/codegen/targets/react/component/handlers/archive/help-bubble.ts +73 -0
  59. package/src/codegen/targets/react/component/handlers/archive/identity-placeholder.ts +21 -0
  60. package/src/codegen/targets/react/component/handlers/archive/index.ts +40 -0
  61. package/src/codegen/targets/react/component/handlers/archive/legacy-select-box.ts +89 -0
  62. package/src/codegen/targets/react/component/handlers/archive/legacy-text-field.ts +198 -0
  63. package/src/codegen/targets/react/component/handlers/archive/list-header.ts +20 -0
  64. package/src/codegen/targets/react/component/handlers/archive/list-item.ts +162 -0
  65. package/src/codegen/targets/react/component/handlers/archive/manner-temp-badge.ts +21 -0
  66. package/src/codegen/targets/react/component/handlers/archive/manner-temp.ts +18 -0
  67. package/src/codegen/targets/react/component/handlers/archive/menu-sheet.ts +108 -0
  68. package/src/codegen/targets/react/component/handlers/archive/page-banner.ts +101 -0
  69. package/src/codegen/targets/react/component/handlers/archive/progress-circle.ts +55 -0
  70. package/src/codegen/targets/react/component/handlers/archive/radio-group.ts +31 -0
  71. package/src/codegen/targets/react/component/handlers/archive/radiomark.ts +27 -0
  72. package/src/codegen/targets/react/component/handlers/archive/reaction-button.ts +37 -0
  73. package/src/codegen/targets/react/component/handlers/archive/result-section.ts +67 -0
  74. package/src/codegen/targets/react/component/handlers/archive/segmented-control.ts +64 -0
  75. package/src/codegen/targets/react/component/handlers/archive/skeleton.ts +26 -0
  76. package/src/codegen/targets/react/component/handlers/archive/slider.ts +114 -0
  77. package/src/codegen/targets/react/component/handlers/archive/snackbar.ts +25 -0
  78. package/src/codegen/targets/react/component/handlers/archive/switch.ts +39 -0
  79. package/src/codegen/targets/react/component/handlers/archive/switchmark.ts +26 -0
  80. package/src/codegen/targets/react/component/handlers/archive/tabs.ts +297 -0
  81. package/src/codegen/targets/react/component/handlers/archive/tag-group.ts +86 -0
  82. package/src/codegen/targets/react/component/handlers/archive/text-field.ts +264 -0
  83. package/src/codegen/targets/react/component/handlers/archive/toggle-button.ts +43 -0
  84. package/src/codegen/targets/react/component/handlers/avatar-stack.ts +38 -0
  85. package/src/codegen/targets/react/component/handlers/avatar.ts +58 -0
  86. package/src/codegen/targets/react/component/handlers/badge.ts +18 -0
  87. package/src/codegen/targets/react/component/handlers/bottom-sheet.ts +74 -0
  88. package/src/codegen/targets/react/component/handlers/callout.ts +88 -0
  89. package/src/codegen/targets/react/component/handlers/checkbox.ts +129 -0
  90. package/src/codegen/targets/react/component/handlers/checkmark.ts +29 -0
  91. package/src/codegen/targets/react/component/handlers/chip.ts +93 -0
  92. package/src/codegen/targets/react/component/handlers/content-placeholder.ts +20 -0
  93. package/src/codegen/targets/react/component/handlers/contextual-floating-button.ts +52 -0
  94. package/src/codegen/targets/react/component/handlers/divider.ts +25 -0
  95. package/src/codegen/targets/react/component/handlers/field-button.ts +192 -0
  96. package/src/codegen/targets/react/component/handlers/field.ts +164 -0
  97. package/src/codegen/targets/react/component/handlers/floating-action-button.ts +45 -0
  98. package/src/codegen/targets/react/component/handlers/help-bubble.ts +73 -0
  99. package/src/codegen/targets/react/component/handlers/identity-placeholder.ts +20 -0
  100. package/src/codegen/targets/react/component/handlers/image-frame.ts +147 -0
  101. package/src/codegen/targets/react/component/handlers/index.ts +43 -0
  102. package/src/codegen/targets/react/component/handlers/legacy-select-box.ts +87 -0
  103. package/src/codegen/targets/react/component/handlers/legacy-text-field.ts +196 -0
  104. package/src/codegen/targets/react/component/handlers/list-header.ts +20 -0
  105. package/src/codegen/targets/react/component/handlers/list-item.ts +163 -0
  106. package/src/codegen/targets/react/component/handlers/manner-temp-badge.ts +21 -0
  107. package/src/codegen/targets/react/component/handlers/manner-temp.ts +18 -0
  108. package/src/codegen/targets/react/component/handlers/menu-sheet.ts +111 -0
  109. package/src/codegen/targets/react/component/handlers/page-banner.ts +106 -0
  110. package/src/codegen/targets/react/component/handlers/progress-circle.ts +55 -0
  111. package/src/codegen/targets/react/component/handlers/radio-group.ts +109 -0
  112. package/src/codegen/targets/react/component/handlers/radiomark.ts +27 -0
  113. package/src/codegen/targets/react/component/handlers/reaction-button.ts +37 -0
  114. package/src/codegen/targets/react/component/handlers/result-section.ts +67 -0
  115. package/src/codegen/targets/react/component/handlers/segmented-control.ts +63 -0
  116. package/src/codegen/targets/react/component/handlers/select-box.ts +333 -0
  117. package/src/codegen/targets/react/component/handlers/skeleton.ts +26 -0
  118. package/src/codegen/targets/react/component/handlers/slider.ts +117 -0
  119. package/src/codegen/targets/react/component/handlers/snackbar.ts +25 -0
  120. package/src/codegen/targets/react/component/handlers/switch.ts +35 -0
  121. package/src/codegen/targets/react/component/handlers/switchmark.ts +26 -0
  122. package/src/codegen/targets/react/component/handlers/tabs.ts +298 -0
  123. package/src/codegen/targets/react/component/handlers/tag-group.ts +90 -0
  124. package/src/codegen/targets/react/component/handlers/text-field.ts +253 -0
  125. package/src/codegen/targets/react/component/handlers/toggle-button.ts +43 -0
  126. package/src/codegen/targets/react/component/index.ts +24 -0
  127. package/src/codegen/targets/react/component/size.ts +22 -0
  128. package/src/codegen/targets/react/element-factories.ts +59 -0
  129. package/src/codegen/targets/react/frame.ts +96 -0
  130. package/src/codegen/targets/react/icon.ts +55 -0
  131. package/src/codegen/targets/react/index.ts +7 -0
  132. package/src/codegen/targets/react/instance.ts +82 -0
  133. package/src/codegen/targets/react/pipeline.ts +133 -0
  134. package/src/codegen/targets/react/props.ts +417 -0
  135. package/src/codegen/targets/react/shape.ts +47 -0
  136. package/src/codegen/targets/react/text.ts +31 -0
  137. package/src/codegen/targets/react/value-resolver.ts +93 -0
  138. package/src/entities/component.interface.ts +7 -0
  139. package/src/entities/component.repository.ts +16 -0
  140. package/src/entities/data/__generated__/archive/component-sets/index.d.ts +2074 -0
  141. package/src/entities/data/__generated__/archive/component-sets/index.mjs +2074 -0
  142. package/src/entities/data/__generated__/archive/components/index.d.ts +116 -0
  143. package/src/entities/data/__generated__/archive/components/index.mjs +116 -0
  144. package/src/entities/data/__generated__/archive/styles/index.d.ts +3 -0
  145. package/src/entities/data/__generated__/archive/styles/index.mjs +429 -0
  146. package/src/entities/data/__generated__/archive/variable-collections/index.d.ts +3 -0
  147. package/src/entities/data/__generated__/archive/variable-collections/index.mjs +501 -0
  148. package/src/entities/data/__generated__/archive/variables/index.d.ts +3 -0
  149. package/src/entities/data/__generated__/archive/variables/index.mjs +7019 -0
  150. package/src/entities/data/__generated__/component-sets/index.d.ts +4325 -0
  151. package/src/entities/data/__generated__/component-sets/index.mjs +4325 -0
  152. package/src/entities/data/__generated__/components/index.d.ts +378 -0
  153. package/src/entities/data/__generated__/components/index.mjs +378 -0
  154. package/src/entities/data/__generated__/icons/index.d.ts +3 -0
  155. package/src/entities/data/__generated__/icons/index.mjs +3476 -0
  156. package/src/entities/data/__generated__/styles/index.d.ts +3 -0
  157. package/src/entities/data/__generated__/styles/index.mjs +436 -0
  158. package/src/entities/data/__generated__/variable-collections/index.d.ts +3 -0
  159. package/src/entities/data/__generated__/variable-collections/index.mjs +479 -0
  160. package/src/entities/data/__generated__/variables/index.d.ts +3 -0
  161. package/src/entities/data/__generated__/variables/index.mjs +6969 -0
  162. package/src/entities/icon.interface.ts +5 -0
  163. package/src/entities/icon.repository.ts +11 -0
  164. package/src/entities/icon.service.ts +26 -0
  165. package/src/entities/index.ts +60 -0
  166. package/src/entities/style.interface.ts +5 -0
  167. package/src/entities/style.repository.ts +27 -0
  168. package/src/entities/style.service.ts +36 -0
  169. package/src/entities/variable.interface.ts +18 -0
  170. package/src/entities/variable.repository.ts +57 -0
  171. package/src/entities/variable.service.ts +101 -0
  172. package/src/index.ts +3 -0
  173. package/src/normalizer/from-plugin.ts +602 -0
  174. package/src/normalizer/from-rest.ts +577 -0
  175. package/src/normalizer/index.ts +3 -0
  176. package/src/normalizer/types.ts +208 -0
  177. package/src/utils/common.ts +38 -0
  178. package/src/utils/css.ts +19 -0
  179. package/src/utils/figma-gradient.ts +72 -0
  180. package/src/utils/figma-node.ts +95 -0
  181. package/src/utils/figma-variable.ts +49 -0
@@ -0,0 +1,174 @@
1
+ import { ensureArray, exists } from "@/utils/common";
2
+
3
+ export interface ElementNode {
4
+ __IS_JSX_ELEMENT_NODE: true;
5
+ tag: string;
6
+ props: Record<string, string | number | boolean | ElementNode | object | undefined>;
7
+ children: (ElementNode | string)[];
8
+
9
+ meta: {
10
+ comment?: string;
11
+ source?: string;
12
+ importPath?: string;
13
+ };
14
+ }
15
+
16
+ export function createElement(
17
+ tag: string,
18
+ props: Record<string, string | number | boolean | object | undefined> = {},
19
+ children?: ElementNode | string | undefined | (ElementNode | string | undefined)[],
20
+ meta?: ElementNode["meta"],
21
+ ): ElementNode {
22
+ return {
23
+ __IS_JSX_ELEMENT_NODE: true,
24
+ tag,
25
+ props,
26
+ children: ensureArray(children).filter(exists),
27
+ meta: meta ?? {},
28
+ };
29
+ }
30
+
31
+ export function cloneElement(
32
+ element: ElementNode,
33
+ props: Record<string, string | number | boolean | object | undefined> = {},
34
+ children?: ElementNode | string | undefined | (ElementNode | string | undefined)[],
35
+ ) {
36
+ return {
37
+ ...element,
38
+ props: { ...element.props, ...props },
39
+ children: children ? ensureArray(children).filter(exists) : element.children,
40
+ };
41
+ }
42
+
43
+ export function appendSource(element: ElementNode, source: string) {
44
+ return {
45
+ ...element,
46
+ source,
47
+ };
48
+ }
49
+
50
+ export function isElement(node: unknown): node is ElementNode {
51
+ return (
52
+ typeof node === "object" &&
53
+ node != null &&
54
+ "__IS_JSX_ELEMENT_NODE" in node &&
55
+ node.__IS_JSX_ELEMENT_NODE === true
56
+ );
57
+ }
58
+
59
+ export function stringifyElement(element: ElementNode, options: { printSource?: boolean } = {}) {
60
+ const importMap = new Map<string, Set<string>>();
61
+
62
+ function recursive(node: ElementNode | string, depth: number): string {
63
+ if (typeof node === "string") {
64
+ return node;
65
+ }
66
+
67
+ const {
68
+ tag,
69
+ props,
70
+ children,
71
+ meta: { comment, source, importPath },
72
+ } = node;
73
+
74
+ if (importPath) {
75
+ const existing = importMap.get(importPath);
76
+
77
+ const [namespace] = tag.split(".");
78
+
79
+ if (existing) {
80
+ existing.add(namespace);
81
+ } else {
82
+ importMap.set(importPath, new Set([namespace]));
83
+ }
84
+ }
85
+
86
+ const propEntries = Object.entries(
87
+ options.printSource ? { ...props, "data-figma-node-id": source } : props,
88
+ );
89
+ const propFragments = propEntries
90
+ .map(([key, value]) => {
91
+ if (typeof value === "string") {
92
+ if (value.includes("\n")) {
93
+ return `${key}={"${value.replaceAll("\n", "\\n")}"}`;
94
+ }
95
+
96
+ return `${key}="${value}"`;
97
+ }
98
+
99
+ if (typeof value === "number") {
100
+ return `${key}={${value}}`;
101
+ }
102
+
103
+ if (typeof value === "boolean") {
104
+ if (value === true) return key;
105
+
106
+ return `${key}={${value}}`;
107
+ }
108
+
109
+ if (isElement(value)) {
110
+ const elementStr = recursive(value, depth + 1);
111
+
112
+ const commentMatch = elementStr.match(/\{\/\* (.+?)\*\/\}$/);
113
+
114
+ if (commentMatch) {
115
+ const elementWithoutComment = elementStr.replace(/\{\/\* .+? \*\/\}$/, "");
116
+
117
+ return `${key}={${elementWithoutComment}}/* ${commentMatch[1]} */`;
118
+ }
119
+
120
+ return `${key}={${elementStr}}`;
121
+ }
122
+
123
+ if (typeof value === "object") {
124
+ return `${key}={${JSON.stringify(value)}}`;
125
+ }
126
+
127
+ if (typeof value === "undefined") {
128
+ return undefined;
129
+ }
130
+
131
+ return undefined;
132
+ })
133
+ .filter(exists);
134
+
135
+ const oneLiner = propFragments.join(" ");
136
+ const propsString =
137
+ propEntries.length === 0
138
+ ? ""
139
+ : ` ${
140
+ oneLiner.length < 80
141
+ ? oneLiner
142
+ : `\n${" ".repeat(depth + 1)}${propFragments.join(
143
+ `\n${" ".repeat(depth + 1)}`,
144
+ )}\n${" ".repeat(depth)}`
145
+ }`;
146
+
147
+ if (children == null || children.length === 0) {
148
+ return `<${tag}${propsString} />${comment ? `{/* ${comment} */}` : ""}`;
149
+ }
150
+
151
+ const result = [
152
+ `<${tag}${propsString}>`,
153
+ ...ensureArray(children)
154
+ .filter(exists)
155
+ .map((child) => recursive(child, depth + 1))
156
+ .map((str) => " ".repeat(depth + 1) + str),
157
+ `${" ".repeat(depth)}</${tag}>${comment ? `{/* ${comment} */}` : ""}`,
158
+ ].join("\n");
159
+
160
+ return result;
161
+ }
162
+
163
+ const jsx = recursive(element, 0);
164
+
165
+ const imports = Array.from(importMap.entries())
166
+ .sort((a, b) => a[0].localeCompare(b[0]))
167
+ .map(([importPath, tags]) => `import { ${Array.from(tags).join(", ")} } from "${importPath}";`)
168
+ .join("\n");
169
+
170
+ return {
171
+ imports,
172
+ jsx,
173
+ };
174
+ }
@@ -0,0 +1,78 @@
1
+ import type { VariableValueResolved } from "@/entities";
2
+ import { objectEntries } from "@/utils/common";
3
+
4
+ export type PropsConverter<
5
+ T extends Record<string, any> = Record<string, any>,
6
+ R extends Record<string, any> = Record<string, any>,
7
+ > = (node: T) => R;
8
+
9
+ export function definePropsConverter<T extends Record<string, any>, R extends Record<string, any>>(
10
+ converter: PropsConverter<T, R>,
11
+ ) {
12
+ return converter;
13
+ }
14
+
15
+ type Handlers<
16
+ TTrait extends Record<string, VariableValueResolved>,
17
+ TProps extends Record<string, any>,
18
+ HandlerKeys extends keyof TProps = keyof TProps,
19
+ > = {
20
+ [K in HandlerKeys]: (node: TTrait) => TProps[K];
21
+ };
22
+
23
+ type Shorthands<TProps extends Record<string, any>, HandlerKeys extends keyof TProps> = Record<
24
+ Exclude<keyof TProps, HandlerKeys>,
25
+ HandlerKeys[]
26
+ >;
27
+
28
+ export interface CreatePropsConverterConfig<
29
+ TTrait extends Record<string, any>,
30
+ TProps extends Record<string, any>,
31
+ HandlerKeys extends keyof TProps,
32
+ > {
33
+ _types: {
34
+ trait: TTrait;
35
+ props: TProps;
36
+ };
37
+ handlers: Handlers<TTrait, TProps, HandlerKeys>;
38
+ shorthands?: Shorthands<TProps, HandlerKeys>;
39
+ defaults?: Partial<TProps>;
40
+ }
41
+
42
+ export function createPropsConverter<
43
+ TTrait extends Record<string, any>,
44
+ TProps extends Record<string, any>,
45
+ HandlerKeys extends keyof TProps,
46
+ >({
47
+ handlers,
48
+ shorthands,
49
+ defaults,
50
+ }: CreatePropsConverterConfig<TTrait, TProps, HandlerKeys>): PropsConverter<TTrait, TProps> {
51
+ return definePropsConverter((node: TTrait) => {
52
+ const result = {} as TProps;
53
+
54
+ for (const [prop, handler] of objectEntries(handlers)) {
55
+ const value = handler(node);
56
+ if (value !== undefined && (!defaults || value !== defaults[prop as keyof TProps])) {
57
+ result[prop as keyof TProps] = value as any;
58
+ }
59
+ }
60
+
61
+ if (shorthands) {
62
+ for (const [shorthand, props] of objectEntries(shorthands)) {
63
+ const values = props.map((prop) => result[prop as keyof TProps]);
64
+ const allDefined = values.every((value) => value !== undefined);
65
+ const allEqual = allDefined && values.every((value) => value === values[0]);
66
+
67
+ if (allEqual && values[0] !== undefined) {
68
+ result[shorthand as keyof TProps] = values[0] as any;
69
+ for (const prop of props) {
70
+ delete result[prop as keyof TProps];
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ return result;
77
+ });
78
+ }
@@ -0,0 +1,381 @@
1
+ import type { StyleService, VariableValueResolved } from "@/entities";
2
+ import type {
3
+ NormalizedCornerTrait,
4
+ NormalizedHasEffectsTrait,
5
+ NormalizedHasFramePropertiesTrait,
6
+ NormalizedHasGeometryTrait,
7
+ NormalizedHasLayoutTrait,
8
+ NormalizedIsLayerTrait,
9
+ NormalizedTypePropertiesTrait,
10
+ } from "@/normalizer";
11
+ import {
12
+ getFirstFillVariable,
13
+ getFirstSolidFill,
14
+ getFirstStroke,
15
+ getFirstStrokeVariable,
16
+ } from "@/utils/figma-node";
17
+ import type { RGBA } from "@figma/rest-api-spec";
18
+ import type { VariableService } from "../../entities/variable.service";
19
+
20
+ export interface ValueResolver<TColor, TGradient, TDimension, TFontDimension, TFontWeight> {
21
+ getFormattedValue: {
22
+ frameFill: (
23
+ node: NormalizedHasGeometryTrait & NormalizedIsLayerTrait,
24
+ ) => string | TColor | TGradient | undefined;
25
+ shapeFill: (
26
+ node: NormalizedHasGeometryTrait & NormalizedIsLayerTrait,
27
+ ) => string | TColor | undefined;
28
+ textFill: (
29
+ node: NormalizedHasGeometryTrait & NormalizedIsLayerTrait,
30
+ ) => string | TColor | undefined;
31
+ stroke: (
32
+ node: NormalizedHasGeometryTrait & NormalizedIsLayerTrait,
33
+ ) => string | TColor | undefined;
34
+ width: (
35
+ node: NormalizedHasLayoutTrait & NormalizedIsLayerTrait,
36
+ ) => string | TDimension | undefined;
37
+ height: (
38
+ node: NormalizedHasLayoutTrait & NormalizedIsLayerTrait,
39
+ ) => string | TDimension | undefined;
40
+ minWidth: (
41
+ node: NormalizedHasLayoutTrait & NormalizedIsLayerTrait,
42
+ ) => string | TDimension | undefined;
43
+ minHeight: (
44
+ node: NormalizedHasLayoutTrait & NormalizedIsLayerTrait,
45
+ ) => string | TDimension | undefined;
46
+ maxWidth: (
47
+ node: NormalizedHasLayoutTrait & NormalizedIsLayerTrait,
48
+ ) => string | TDimension | undefined;
49
+ maxHeight: (
50
+ node: NormalizedHasLayoutTrait & NormalizedIsLayerTrait,
51
+ ) => string | TDimension | undefined;
52
+ paddingLeft: (
53
+ node: NormalizedHasFramePropertiesTrait & NormalizedIsLayerTrait,
54
+ ) => string | TDimension | undefined;
55
+ paddingRight: (
56
+ node: NormalizedHasFramePropertiesTrait & NormalizedIsLayerTrait,
57
+ ) => string | TDimension | undefined;
58
+ paddingTop: (
59
+ node: NormalizedHasFramePropertiesTrait & NormalizedIsLayerTrait,
60
+ ) => string | TDimension | undefined;
61
+ paddingBottom: (
62
+ node: NormalizedHasFramePropertiesTrait & NormalizedIsLayerTrait,
63
+ ) => string | TDimension | undefined;
64
+ itemSpacing: (
65
+ node: NormalizedHasFramePropertiesTrait & NormalizedIsLayerTrait,
66
+ ) => string | TDimension | undefined;
67
+ counterAxisSpacing: (
68
+ node: NormalizedHasFramePropertiesTrait & NormalizedIsLayerTrait,
69
+ ) => string | TDimension | undefined;
70
+ topLeftRadius: (
71
+ node: NormalizedCornerTrait & NormalizedIsLayerTrait,
72
+ ) => string | TDimension | undefined;
73
+ topRightRadius: (
74
+ node: NormalizedCornerTrait & NormalizedIsLayerTrait,
75
+ ) => string | TDimension | undefined;
76
+ bottomLeftRadius: (
77
+ node: NormalizedCornerTrait & NormalizedIsLayerTrait,
78
+ ) => string | TDimension | undefined;
79
+ bottomRightRadius: (
80
+ node: NormalizedCornerTrait & NormalizedIsLayerTrait,
81
+ ) => string | TDimension | undefined;
82
+ fontSize: (
83
+ node: NormalizedTypePropertiesTrait & NormalizedIsLayerTrait,
84
+ ) => string | TFontDimension | undefined;
85
+ fontWeight: (
86
+ node: NormalizedTypePropertiesTrait & NormalizedIsLayerTrait,
87
+ ) => string | TFontWeight | undefined;
88
+ lineHeight: (
89
+ node: NormalizedTypePropertiesTrait & NormalizedIsLayerTrait,
90
+ ) => string | TFontDimension | undefined;
91
+ boxShadow: (node: NormalizedHasEffectsTrait & NormalizedIsLayerTrait) => string | undefined;
92
+ };
93
+ getTextStyleValue: (
94
+ node: NormalizedTypePropertiesTrait & NormalizedIsLayerTrait,
95
+ ) => string | undefined; // TODO: we might turn this into a generic; not sure yet
96
+ getEffectStyleValue: (
97
+ node: NormalizedHasEffectsTrait & NormalizedIsLayerTrait,
98
+ ) => string | undefined;
99
+ }
100
+
101
+ export interface ValueResolverDeps<TColor, TGradient, TDimension, TFontDimension, TFontWeight> {
102
+ variableService: VariableService;
103
+ variableNameFormatter: (props: { slug: string[] }) => string;
104
+ styleService: StyleService;
105
+ textStyleNameFormatter: (props: { slug: string[] }) => string;
106
+ effectStyleNameFormatter: (props: { slug: string[] }) => string;
107
+ fillStyleResolver: (props: { slug: string[] }) => TGradient | undefined;
108
+ rawValueFormatters: {
109
+ color: (value: RGBA) => string | TColor;
110
+ dimension: (value: number) => string | TDimension;
111
+ fontDimension: (value: number) => string | TFontDimension;
112
+ fontWeight: (value: number) => string | TFontWeight;
113
+ boxShadow: (value: {
114
+ type: "DROP_SHADOW" | "INNER_SHADOW";
115
+ color: RGBA;
116
+ offset: { x: number; y: number };
117
+ radius: number;
118
+ spread?: number;
119
+ }) => string;
120
+ };
121
+ shouldInferVariableName: boolean;
122
+ }
123
+
124
+ export function createValueResolver<TColor, TGradient, TDimension, TFontDimension, TFontWeight>({
125
+ variableService,
126
+ variableNameFormatter,
127
+ styleService,
128
+ textStyleNameFormatter,
129
+ effectStyleNameFormatter,
130
+ fillStyleResolver,
131
+ rawValueFormatters,
132
+ shouldInferVariableName,
133
+ }: ValueResolverDeps<TColor, TGradient, TDimension, TFontDimension, TFontWeight>): ValueResolver<
134
+ TColor,
135
+ TGradient,
136
+ TDimension,
137
+ TFontDimension,
138
+ TFontWeight
139
+ > {
140
+ function getVariableName(key: string) {
141
+ const slug = variableService.getSlug(key);
142
+
143
+ if (!slug) {
144
+ return undefined;
145
+ }
146
+
147
+ return variableNameFormatter({ slug });
148
+ }
149
+
150
+ function inferVariableName(value: VariableValueResolved, scope: VariableScope) {
151
+ if (!shouldInferVariableName) {
152
+ return undefined;
153
+ }
154
+
155
+ try {
156
+ const inferred = variableService.infer(value, scope);
157
+
158
+ if (!inferred) {
159
+ return undefined;
160
+ }
161
+
162
+ return getVariableName(inferred.key);
163
+ } catch {
164
+ return undefined;
165
+ }
166
+ }
167
+
168
+ function processColor(
169
+ id: string | undefined,
170
+ value: RGBA | undefined,
171
+ scope: "FRAME_FILL" | "SHAPE_FILL" | "STROKE_COLOR" | "TEXT_FILL",
172
+ ) {
173
+ if (id) {
174
+ return getVariableName(id);
175
+ }
176
+
177
+ if (value !== undefined) {
178
+ return inferVariableName(value, scope) ?? rawValueFormatters.color(value);
179
+ }
180
+
181
+ return undefined;
182
+ }
183
+
184
+ function processFillStyle(key: string) {
185
+ const slug = styleService.getSlug(key);
186
+
187
+ if (!slug) {
188
+ return undefined;
189
+ }
190
+
191
+ return fillStyleResolver({ slug });
192
+ }
193
+
194
+ function processDimension(
195
+ id: string | undefined,
196
+ value: number | undefined,
197
+ scope: "WIDTH_HEIGHT" | "GAP" | "CORNER_RADIUS",
198
+ ) {
199
+ if (id) {
200
+ return getVariableName(id);
201
+ }
202
+
203
+ if (value !== undefined) {
204
+ return inferVariableName(value, scope) ?? rawValueFormatters.dimension(value);
205
+ }
206
+
207
+ return undefined;
208
+ }
209
+
210
+ function processFontDimension(
211
+ id: string | undefined,
212
+ value: number | undefined,
213
+ scope: "FONT_SIZE" | "LINE_HEIGHT",
214
+ ) {
215
+ if (id) {
216
+ return getVariableName(id);
217
+ }
218
+
219
+ if (value !== undefined) {
220
+ return inferVariableName(value, scope) ?? rawValueFormatters.fontDimension(value);
221
+ }
222
+
223
+ return undefined;
224
+ }
225
+
226
+ function processFontWeight(id: string | undefined, value: number | undefined) {
227
+ if (id) {
228
+ return getVariableName(id);
229
+ }
230
+
231
+ if (value !== undefined) {
232
+ const fontWeightToString: Record<number, string> = {
233
+ 100: "thin",
234
+ 200: "extra-light",
235
+ 300: "light",
236
+ 400: "regular",
237
+ 500: "medium",
238
+ 600: "semi-bold",
239
+ 700: "bold",
240
+ 800: "extra-bold",
241
+ 900: "black",
242
+ };
243
+
244
+ return (
245
+ inferVariableName(value, "FONT_WEIGHT") ??
246
+ inferVariableName(fontWeightToString[value], "FONT_STYLE") ??
247
+ rawValueFormatters.fontWeight(value)
248
+ );
249
+ }
250
+
251
+ return undefined;
252
+ }
253
+
254
+ const getFormattedValue: ValueResolver<
255
+ TColor,
256
+ TGradient,
257
+ TDimension,
258
+ TFontDimension,
259
+ TFontWeight
260
+ >["getFormattedValue"] = {
261
+ width: (node) =>
262
+ processDimension(
263
+ node.boundVariables?.size?.x?.id,
264
+ node.absoluteBoundingBox?.width,
265
+ "WIDTH_HEIGHT",
266
+ ),
267
+ height: (node) =>
268
+ processDimension(
269
+ node.boundVariables?.size?.y?.id,
270
+ node.absoluteBoundingBox?.height,
271
+ "WIDTH_HEIGHT",
272
+ ),
273
+ minWidth: (node) =>
274
+ processDimension(node.boundVariables?.minWidth?.id, node.minWidth, "WIDTH_HEIGHT"),
275
+ minHeight: (node) =>
276
+ processDimension(node.boundVariables?.minHeight?.id, node.minHeight, "WIDTH_HEIGHT"),
277
+ maxWidth: (node) =>
278
+ processDimension(node.boundVariables?.maxWidth?.id, node.maxWidth, "WIDTH_HEIGHT"),
279
+ maxHeight: (node) =>
280
+ processDimension(node.boundVariables?.maxHeight?.id, node.maxHeight, "WIDTH_HEIGHT"),
281
+ paddingLeft: (node) =>
282
+ processDimension(node.boundVariables?.paddingLeft?.id, node.paddingLeft, "GAP"),
283
+ paddingRight: (node) =>
284
+ processDimension(node.boundVariables?.paddingRight?.id, node.paddingRight, "GAP"),
285
+ paddingTop: (node) =>
286
+ processDimension(node.boundVariables?.paddingTop?.id, node.paddingTop, "GAP"),
287
+ paddingBottom: (node) =>
288
+ processDimension(node.boundVariables?.paddingBottom?.id, node.paddingBottom, "GAP"),
289
+ itemSpacing: (node) =>
290
+ processDimension(node.boundVariables?.itemSpacing?.id, node.itemSpacing, "GAP"),
291
+ counterAxisSpacing: (node) =>
292
+ processDimension(node.boundVariables?.counterAxisSpacing?.id, node.counterAxisSpacing, "GAP"),
293
+ frameFill: (node) =>
294
+ node.fillStyleKey
295
+ ? processFillStyle(node.fillStyleKey)
296
+ : processColor(
297
+ getFirstFillVariable(node)?.id,
298
+ getFirstSolidFill(node)?.color,
299
+ "FRAME_FILL",
300
+ ),
301
+ shapeFill: (node) =>
302
+ processColor(getFirstFillVariable(node)?.id, getFirstSolidFill(node)?.color, "SHAPE_FILL"),
303
+ textFill: (node) =>
304
+ processColor(getFirstFillVariable(node)?.id, getFirstSolidFill(node)?.color, "TEXT_FILL"),
305
+ stroke: (node) =>
306
+ processColor(getFirstStrokeVariable(node)?.id, getFirstStroke(node)?.color, "STROKE_COLOR"),
307
+ topLeftRadius: (node) =>
308
+ processDimension(
309
+ node.boundVariables?.topLeftRadius?.id,
310
+ node.rectangleCornerRadii?.[0] ?? node.cornerRadius,
311
+ "CORNER_RADIUS",
312
+ ),
313
+ topRightRadius: (node) =>
314
+ processDimension(
315
+ node.boundVariables?.topRightRadius?.id,
316
+ node.rectangleCornerRadii?.[1] ?? node.cornerRadius,
317
+ "CORNER_RADIUS",
318
+ ),
319
+ bottomLeftRadius: (node) =>
320
+ processDimension(
321
+ node.boundVariables?.bottomLeftRadius?.id,
322
+ node.rectangleCornerRadii?.[2] ?? node.cornerRadius,
323
+ "CORNER_RADIUS",
324
+ ),
325
+ bottomRightRadius: (node) =>
326
+ processDimension(
327
+ node.boundVariables?.bottomRightRadius?.id,
328
+ node.rectangleCornerRadii?.[3] ?? node.cornerRadius,
329
+ "CORNER_RADIUS",
330
+ ),
331
+ fontSize: (node) =>
332
+ processFontDimension(
333
+ node.boundVariables?.fontSize?.[0]?.id,
334
+ node.style.fontSize,
335
+ "FONT_SIZE",
336
+ ),
337
+ fontWeight: (node) =>
338
+ processFontWeight(node.boundVariables?.fontWeight?.[0]?.id, node.style.fontWeight),
339
+ lineHeight: (node) =>
340
+ processFontDimension(
341
+ node.boundVariables?.lineHeight?.[0]?.id,
342
+ node.style.lineHeightPx,
343
+ "LINE_HEIGHT",
344
+ ),
345
+ boxShadow: (node) => {
346
+ if (node.effects.length === 0) return undefined;
347
+
348
+ return node.effects.map(rawValueFormatters.boxShadow).join(", ");
349
+ },
350
+ };
351
+
352
+ function getTextStyleValue(node: NormalizedTypePropertiesTrait & NormalizedIsLayerTrait) {
353
+ if (!node.textStyleKey) return undefined;
354
+
355
+ const slug = styleService.getSlug(node.textStyleKey);
356
+
357
+ if (!slug) {
358
+ return undefined;
359
+ }
360
+
361
+ return textStyleNameFormatter({ slug });
362
+ }
363
+
364
+ function getEffectStyleValue(node: NormalizedHasEffectsTrait & NormalizedIsLayerTrait) {
365
+ if (!node.effectStyleKey) return undefined;
366
+
367
+ const slug = styleService.getSlug(node.effectStyleKey);
368
+
369
+ if (!slug) {
370
+ return undefined;
371
+ }
372
+
373
+ return effectStyleNameFormatter({ slug });
374
+ }
375
+
376
+ return {
377
+ getFormattedValue,
378
+ getTextStyleValue,
379
+ getEffectStyleValue,
380
+ };
381
+ }
@@ -0,0 +1,44 @@
1
+ import {
2
+ createIconService,
3
+ createStyleService,
4
+ createVariableService,
5
+ iconRepository,
6
+ styleRepository,
7
+ variableRepository,
8
+ } from "@/entities";
9
+
10
+ export const styleService = createStyleService({
11
+ styleRepository,
12
+ });
13
+ export const variableService = createVariableService({
14
+ variableRepository,
15
+ inferCompareFunction: (a, b) => {
16
+ const scoreFn = (name: string) => {
17
+ let score = 0;
18
+ if (name.includes("bg")) {
19
+ score += 100;
20
+ }
21
+ if (name.includes("fg")) {
22
+ score += 100;
23
+ }
24
+ if (name.includes("stroke")) {
25
+ score += 100;
26
+ }
27
+ if (name.includes("spacing-x")) {
28
+ score -= 100;
29
+ }
30
+ if (name.includes("spacing-y")) {
31
+ score -= 100;
32
+ }
33
+ if (name.endsWith("pressed")) {
34
+ score -= 100;
35
+ }
36
+ return score;
37
+ };
38
+
39
+ return scoreFn(b.name) - scoreFn(a.name);
40
+ },
41
+ });
42
+ export const iconService = createIconService({
43
+ iconRepository,
44
+ });
@@ -0,0 +1,3 @@
1
+ export * from "./component-properties.archive";
2
+ export * from "./core";
3
+ export * from "./targets";
@@ -0,0 +1,7 @@
1
+ import * as metadata from "../entities/data/__generated__/component-sets";
2
+
3
+ export const SKIP_COMPONENT_KEYS: Set<string> = new Set([
4
+ metadata.componentOsBottomIndicatorFigmaOnly.key,
5
+ metadata.componentOsKeyboardFigmaOnly.key,
6
+ metadata.componentOsStatusBarFigmaOnly.key,
7
+ ]);