@charz/dom-to-figma 0.0.0-stage → 0.2.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Figit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,173 @@
1
- # Temporary Holding Version
1
+ # @figit/dom-to-figma
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Convert any DOM tree to a Figma clipboard payload, in the browser.
4
+
5
+ ```ts
6
+ import { createFigmaConverter } from "@figit/dom-to-figma";
7
+
8
+ const figma = createFigmaConverter();
9
+
10
+ const result = await figma.convert({
11
+ element: document.getElementById("design"),
12
+ width: 1280,
13
+ height: 800,
14
+ name: "Hero",
15
+ });
16
+
17
+ await navigator.clipboard.write([result.toClipboardItem()]);
18
+ ```
19
+
20
+ Paste in Figma. Done.
21
+
22
+ ## Install
23
+
24
+ ```sh
25
+ pnpm add @figit/dom-to-figma
26
+ ```
27
+
28
+ ## What it does
29
+
30
+ Walks a real DOM tree, reads computed styles, and produces what Figma reads on paste. Text becomes editable text; images, vectors, gradients, shadows, borders, and form placeholders all carry over.
31
+
32
+ Used in production by Sleek to copy designs straight from the browser into Figma.
33
+
34
+ ## Layout
35
+
36
+ By default the converter infers **native Figma auto-layout**: flex containers, plain block flow, wrapping rows, and grids become real stacks (`stackMode`, spacing, padding, hug/fill/stretch sizing), and absolutely positioned children ride along as absolute-positioned layers. Inference is per-container and verified against the browser's measured geometry — any container it can't reproduce exactly falls back to absolute positioning, so the result is never worse than a fixed-position paste.
37
+
38
+ ```ts
39
+ const figma = createFigmaConverter(); // layout: "auto" (default)
40
+ ```
41
+
42
+ Pass `layout: "absolute"` to disable inference and position every frame absolutely (the pre-1.0 behavior):
43
+
44
+ ```ts
45
+ const figma = createFigmaConverter({ layout: "absolute" });
46
+ ```
47
+
48
+ ## Multi-frame canvas
49
+
50
+ Pass `frames` instead of a single `element` to copy several DOM trees onto one Figma canvas:
51
+
52
+ ```ts
53
+ const result = await figma.convert({
54
+ frames: [
55
+ { element: a, width: 800, height: 600, x: 0, y: 0, name: "Hero" },
56
+ { element: b, width: 800, height: 600, x: 900, y: 0, name: "Pricing" },
57
+ ],
58
+ canvasName: "Landing",
59
+ });
60
+
61
+ await navigator.clipboard.write([result.toClipboardItem()]);
62
+ ```
63
+
64
+ ## Result shape
65
+
66
+ ```ts
67
+ type ConvertResult = {
68
+ document: FigmaClipboard; // raw node-change document
69
+ bytes: Uint8Array; // encoded .fig-style binary
70
+ base64: string; // base64 of bytes
71
+ toClipboardItem(): ClipboardItem; // ready for navigator.clipboard.write
72
+ toClipboardHtml(): string; // raw HTML envelope Figma reads on paste
73
+ };
74
+ ```
75
+
76
+ ## Trace mode
77
+
78
+ Pass `trace: true` to also get a map from every emitted Figma node back to the DOM element it came from. It is off by default, adds no cost when disabled, and never changes the payload bytes — it exists for tooling (visual diffing, debugging) rather than production copies.
79
+
80
+ ```ts
81
+ const figma = createFigmaConverter({ trace: true });
82
+ const result = await figma.convert({ element, width: 1280, height: 800 });
83
+
84
+ for (const entry of result.trace.entries) {
85
+ // entry.guid → the emitted node's Figma GUID
86
+ // entry.domPath → ":scope > div:nth-child(2) > p:nth-child(1)"
87
+ // entry.rect → source rect (getBoundingClientRect) at convert time
88
+ // entry.kind / entry.tag / entry.text
89
+ }
90
+ ```
91
+
92
+ `domPath` resolves the source element with `root.querySelector(domPath)` (the root element is `:scope`). Text nodes carry their owner element's path plus a `::text[i]` suffix, and each wrapped line segment of one text node shares that path. `result.trace` is `undefined` unless the converter was created with `{ trace: true }`.
93
+
94
+ ## Customizing loaders
95
+
96
+ The converter takes three optional hooks:
97
+
98
+ ```ts
99
+ const figma = createFigmaConverter({
100
+ fontLoader, // (props) => Promise<FontFile>
101
+ imageLoader, // (req) => Promise<ImageFile>
102
+ classify, // (element, defaultKind) => ElementKind
103
+ });
104
+ ```
105
+
106
+ Repeated `convert()` calls on the same converter reuse cached fonts and images. Call `figma.clearCache()` to drop them.
107
+
108
+ ### Fonts
109
+
110
+ Default: `createFontsourceLoader()` pulls fonts from fontsource via jsDelivr's CDN. Covers all Google Fonts plus other open-source families. No API key required.
111
+
112
+ ```ts
113
+ import { createFontsourceLoader } from "@figit/dom-to-figma";
114
+
115
+ const figma = createFigmaConverter({
116
+ fontLoader: createFontsourceLoader({ subset: "latin-ext" }),
117
+ });
118
+ ```
119
+
120
+ For non-fontsource fonts, write your own loader:
121
+
122
+ ```ts
123
+ import type { FontLoader } from "@figit/dom-to-figma";
124
+
125
+ const myFontLoader: FontLoader = async ({ family, weight, italic }) => {
126
+ const url = await myFontCDN(family, weight, italic);
127
+ const response = await fetch(url);
128
+ return { bytes: await response.arrayBuffer() };
129
+ };
130
+ ```
131
+
132
+ ### Images
133
+
134
+ Default: `createDirectImageLoader()` does a direct `fetch(src)`. Cross-origin images without CORS need a custom loader, typically a proxy chain:
135
+
136
+ ```ts
137
+ import type { ImageLoader } from "@figit/dom-to-figma";
138
+
139
+ const myImageLoader: ImageLoader = async ({ src }) => {
140
+ const url = `https://my-proxy.example/?url=${encodeURIComponent(src)}`;
141
+ const response = await fetch(url);
142
+ const blob = await response.blob();
143
+ return {
144
+ bytes: await blob.arrayBuffer(),
145
+ mimeType: blob.type,
146
+ };
147
+ };
148
+ ```
149
+
150
+ The package re-encodes WebP/AVIF/etc. to PNG internally, so the loader only needs to return raw bytes and a mime type.
151
+
152
+ ### Classification
153
+
154
+ `classify` lets you override how DOM elements map to Figma node kinds. The default classifier returns `text`, `image`, `vector`, `frame`, `group`, `form-with-placeholder`, or `skip`:
155
+
156
+ ```ts
157
+ import type { Classify } from "@figit/dom-to-figma";
158
+
159
+ const classify: Classify = (element, defaultKind) => {
160
+ if (element.dataset.role === "text-wrapper") {
161
+ return "text";
162
+ }
163
+ return defaultKind;
164
+ };
165
+ ```
166
+
167
+ ## Runtime requirements
168
+
169
+ Runs in any modern browser. Depends on `window.getComputedStyle`, `Blob`, `ClipboardItem`, `document.createElement`, `crypto.subtle.digest`, `Image`, and `<canvas>`.
170
+
171
+ ## Disclaimer
172
+
173
+ Not affiliated with or endorsed by Figma.