react-grab 0.0.7 → 0.0.11

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/dist/index.js DELETED
@@ -1,452 +0,0 @@
1
- import { getFiberFromHostInstance } from 'bippy';
2
- import { getFiberStackTrace, getOwnerStack } from 'bippy/dist/source';
3
-
4
- /**
5
- * @license MIT
6
- *
7
- * Copyright (c) 2025 Aiden Bai
8
- *
9
- * This source code is licensed under the MIT license found in the
10
- * LICENSE file in the root directory of this source tree.
11
- */
12
-
13
- var getStack = async (element) => {
14
- const fiber = getFiberFromHostInstance(element);
15
- if (!fiber) return null;
16
- const stackTrace = getFiberStackTrace(fiber);
17
- const rawOwnerStack = await getOwnerStack(stackTrace);
18
- const stack = rawOwnerStack.map((item) => ({
19
- componentName: item.name,
20
- fileName: item.source?.fileName
21
- }));
22
- return stack;
23
- };
24
- var filterStack = (stack) => {
25
- return stack.filter(
26
- (item) => item.fileName && !item.fileName.includes("node_modules") && item.componentName.length > 1 && !item.fileName.startsWith("_")
27
- );
28
- };
29
- var findCommonRoot = (paths) => {
30
- if (paths.length === 0) return "";
31
- if (paths.length === 1) {
32
- const lastSlash2 = paths[0].lastIndexOf("/");
33
- return lastSlash2 > 0 ? paths[0].substring(0, lastSlash2 + 1) : "";
34
- }
35
- let commonPrefix = paths[0];
36
- for (let i = 1; i < paths.length; i++) {
37
- const path = paths[i];
38
- let j = 0;
39
- while (j < commonPrefix.length && j < path.length && commonPrefix[j] === path[j]) {
40
- j++;
41
- }
42
- commonPrefix = commonPrefix.substring(0, j);
43
- }
44
- const lastSlash = commonPrefix.lastIndexOf("/");
45
- return lastSlash > 0 ? commonPrefix.substring(0, lastSlash + 1) : "";
46
- };
47
- var serializeStack = (stack) => {
48
- const filePaths = stack.map((item) => item.fileName).filter((path) => !!path);
49
- const commonRoot = findCommonRoot(filePaths);
50
- return stack.map((item) => {
51
- let fileName = item.fileName;
52
- if (fileName && commonRoot) {
53
- fileName = fileName.startsWith(commonRoot) ? fileName.substring(commonRoot.length) : fileName;
54
- }
55
- return `${item.componentName}${fileName ? ` (${fileName})` : ""}`;
56
- }).join("\n");
57
- };
58
- var getHTMLSnippet = (element) => {
59
- const semanticTags = /* @__PURE__ */ new Set([
60
- "article",
61
- "aside",
62
- "footer",
63
- "form",
64
- "header",
65
- "main",
66
- "nav",
67
- "section"
68
- ]);
69
- const hasDistinguishingFeatures = (el) => {
70
- const tagName = el.tagName.toLowerCase();
71
- if (semanticTags.has(tagName)) return true;
72
- if (el.id) return true;
73
- if (el.className && typeof el.className === "string") {
74
- const classes = el.className.trim();
75
- if (classes && classes.length > 0) return true;
76
- }
77
- return Array.from(el.attributes).some(
78
- (attr) => attr.name.startsWith("data-")
79
- );
80
- };
81
- const getAncestorChain = (el, maxDepth = 10) => {
82
- const ancestors2 = [];
83
- let current = el.parentElement;
84
- let depth = 0;
85
- while (current && depth < maxDepth && current.tagName !== "BODY") {
86
- if (hasDistinguishingFeatures(current)) {
87
- ancestors2.push(current);
88
- if (ancestors2.length >= 3) break;
89
- }
90
- current = current.parentElement;
91
- depth++;
92
- }
93
- return ancestors2.reverse();
94
- };
95
- const getCSSPath = (el) => {
96
- const parts = [];
97
- let current = el;
98
- let depth = 0;
99
- const maxDepth = 5;
100
- while (current && depth < maxDepth && current.tagName !== "BODY") {
101
- let selector = current.tagName.toLowerCase();
102
- if (current.id) {
103
- selector += `#${current.id}`;
104
- parts.unshift(selector);
105
- break;
106
- } else if (current.className && typeof current.className === "string" && current.className.trim()) {
107
- const classes = current.className.trim().split(/\s+/).slice(0, 2);
108
- selector += `.${classes.join(".")}`;
109
- }
110
- if (!current.id && (!current.className || !current.className.trim()) && current.parentElement) {
111
- const siblings = Array.from(current.parentElement.children);
112
- const index = siblings.indexOf(current);
113
- if (index >= 0 && siblings.length > 1) {
114
- selector += `:nth-child(${index + 1})`;
115
- }
116
- }
117
- parts.unshift(selector);
118
- current = current.parentElement;
119
- depth++;
120
- }
121
- return parts.join(" > ");
122
- };
123
- const getElementTag = (el, compact = false) => {
124
- const tagName = el.tagName.toLowerCase();
125
- const attrs = [];
126
- if (el.id) {
127
- attrs.push(`id="${el.id}"`);
128
- }
129
- if (el.className && typeof el.className === "string") {
130
- const classes = el.className.trim().split(/\s+/);
131
- if (classes.length > 0 && classes[0]) {
132
- const displayClasses = compact ? classes.slice(0, 3) : classes;
133
- let classStr = displayClasses.join(" ");
134
- if (classStr.length > 30) {
135
- classStr = classStr.substring(0, 30) + "...";
136
- }
137
- attrs.push(`class="${classStr}"`);
138
- }
139
- }
140
- const dataAttrs = Array.from(el.attributes).filter(
141
- (attr) => attr.name.startsWith("data-")
142
- );
143
- const displayDataAttrs = compact ? dataAttrs.slice(0, 1) : dataAttrs;
144
- for (const attr of displayDataAttrs) {
145
- let value = attr.value;
146
- if (value.length > 20) {
147
- value = value.substring(0, 20) + "...";
148
- }
149
- attrs.push(`${attr.name}="${value}"`);
150
- }
151
- const ariaLabel = el.getAttribute("aria-label");
152
- if (ariaLabel && !compact) {
153
- let value = ariaLabel;
154
- if (value.length > 20) {
155
- value = value.substring(0, 20) + "...";
156
- }
157
- attrs.push(`aria-label="${value}"`);
158
- }
159
- return attrs.length > 0 ? `<${tagName} ${attrs.join(" ")}>` : `<${tagName}>`;
160
- };
161
- const getClosingTag = (el) => {
162
- return `</${el.tagName.toLowerCase()}>`;
163
- };
164
- const getTextContent = (el) => {
165
- let text = el.textContent || "";
166
- text = text.trim().replace(/\s+/g, " ");
167
- const maxLength = 60;
168
- if (text.length > maxLength) {
169
- text = text.substring(0, maxLength) + "...";
170
- }
171
- return text;
172
- };
173
- const getSiblingIdentifier = (el) => {
174
- if (el.id) return `#${el.id}`;
175
- if (el.className && typeof el.className === "string") {
176
- const classes = el.className.trim().split(/\s+/);
177
- if (classes.length > 0 && classes[0]) {
178
- return `.${classes[0]}`;
179
- }
180
- }
181
- return null;
182
- };
183
- const lines = [];
184
- lines.push(`Path: ${getCSSPath(element)}`);
185
- lines.push("");
186
- const ancestors = getAncestorChain(element);
187
- for (let i = 0; i < ancestors.length; i++) {
188
- const indent2 = " ".repeat(i);
189
- lines.push(indent2 + getElementTag(ancestors[i], true));
190
- }
191
- const parent = element.parentElement;
192
- let targetIndex = -1;
193
- if (parent) {
194
- const siblings = Array.from(parent.children);
195
- targetIndex = siblings.indexOf(element);
196
- if (targetIndex > 0) {
197
- const prevSibling = siblings[targetIndex - 1];
198
- const prevId = getSiblingIdentifier(prevSibling);
199
- if (prevId && targetIndex <= 2) {
200
- const indent2 = " ".repeat(ancestors.length);
201
- lines.push(`${indent2} ${getElementTag(prevSibling, true)}`);
202
- lines.push(`${indent2} </${prevSibling.tagName.toLowerCase()}>`);
203
- } else if (targetIndex > 0) {
204
- const indent2 = " ".repeat(ancestors.length);
205
- lines.push(`${indent2} ... (${targetIndex} element${targetIndex === 1 ? "" : "s"})`);
206
- }
207
- }
208
- }
209
- const indent = " ".repeat(ancestors.length);
210
- lines.push(indent + " <!-- SELECTED -->");
211
- const textContent = getTextContent(element);
212
- const childrenCount = element.children.length;
213
- if (textContent && childrenCount === 0 && textContent.length < 40) {
214
- lines.push(
215
- `${indent} ${getElementTag(element)}${textContent}${getClosingTag(element)}`
216
- );
217
- } else {
218
- lines.push(indent + " " + getElementTag(element));
219
- if (textContent) {
220
- lines.push(`${indent} ${textContent}`);
221
- }
222
- if (childrenCount > 0) {
223
- lines.push(
224
- `${indent} ... (${childrenCount} element${childrenCount === 1 ? "" : "s"})`
225
- );
226
- }
227
- lines.push(indent + " " + getClosingTag(element));
228
- }
229
- if (parent && targetIndex >= 0) {
230
- const siblings = Array.from(parent.children);
231
- const siblingsAfter = siblings.length - targetIndex - 1;
232
- if (siblingsAfter > 0) {
233
- const nextSibling = siblings[targetIndex + 1];
234
- const nextId = getSiblingIdentifier(nextSibling);
235
- if (nextId && siblingsAfter <= 2) {
236
- lines.push(`${indent} ${getElementTag(nextSibling, true)}`);
237
- lines.push(`${indent} </${nextSibling.tagName.toLowerCase()}>`);
238
- } else {
239
- lines.push(
240
- `${indent} ... (${siblingsAfter} element${siblingsAfter === 1 ? "" : "s"})`
241
- );
242
- }
243
- }
244
- }
245
- for (let i = ancestors.length - 1; i >= 0; i--) {
246
- const indent2 = " ".repeat(i);
247
- lines.push(indent2 + getClosingTag(ancestors[i]));
248
- }
249
- return lines.join("\n");
250
- };
251
-
252
- // src/index.ts
253
- var init = () => {
254
- let metaKeyTimer = null;
255
- let overlay = null;
256
- let isActive = false;
257
- let isLocked = false;
258
- let currentElement = null;
259
- let animationFrame = null;
260
- let pendingCopyText = null;
261
- const copyToClipboard = async (text) => {
262
- if (!document.hasFocus()) {
263
- pendingCopyText = text;
264
- return;
265
- }
266
- try {
267
- await navigator.clipboard.writeText(text);
268
- pendingCopyText = null;
269
- hideOverlay();
270
- } catch {
271
- const textarea = document.createElement("textarea");
272
- textarea.value = text;
273
- textarea.style.position = "fixed";
274
- textarea.style.left = "-999999px";
275
- textarea.style.top = "-999999px";
276
- document.body.appendChild(textarea);
277
- textarea.focus();
278
- textarea.select();
279
- try {
280
- document.execCommand("copy");
281
- pendingCopyText = null;
282
- hideOverlay();
283
- } catch (execErr) {
284
- console.error("Failed to copy to clipboard:", execErr);
285
- hideOverlay();
286
- }
287
- document.body.removeChild(textarea);
288
- }
289
- };
290
- const handleWindowFocus = () => {
291
- if (pendingCopyText) {
292
- void copyToClipboard(pendingCopyText);
293
- }
294
- };
295
- let currentX = 0;
296
- let currentY = 0;
297
- let currentWidth = 0;
298
- let currentHeight = 0;
299
- let targetX = 0;
300
- let targetY = 0;
301
- let targetWidth = 0;
302
- let targetHeight = 0;
303
- let targetBorderRadius = "";
304
- const isInsideInputOrTextarea = () => {
305
- const activeElement = document.activeElement;
306
- return activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement || activeElement?.tagName === "INPUT" || activeElement?.tagName === "TEXTAREA";
307
- };
308
- const createOverlay = () => {
309
- const div = document.createElement("div");
310
- div.style.position = "fixed";
311
- div.style.border = "2px solid #3b82f6";
312
- div.style.backgroundColor = "rgba(59, 130, 246, 0.1)";
313
- div.style.pointerEvents = "none";
314
- div.style.zIndex = "999999";
315
- div.style.transition = "none";
316
- document.body.appendChild(div);
317
- return div;
318
- };
319
- const lerp = (start, end, factor) => {
320
- return start + (end - start) * factor;
321
- };
322
- const updateOverlayPosition = () => {
323
- if (!overlay || !isActive) return;
324
- const factor = 0.5;
325
- currentX = lerp(currentX, targetX, factor);
326
- currentY = lerp(currentY, targetY, factor);
327
- currentWidth = lerp(currentWidth, targetWidth, factor);
328
- currentHeight = lerp(currentHeight, targetHeight, factor);
329
- overlay.style.left = `${currentX}px`;
330
- overlay.style.top = `${currentY}px`;
331
- overlay.style.width = `${currentWidth}px`;
332
- overlay.style.height = `${currentHeight}px`;
333
- overlay.style.borderRadius = targetBorderRadius;
334
- animationFrame = requestAnimationFrame(updateOverlayPosition);
335
- };
336
- const handleMouseMove = (e) => {
337
- if (isLocked) return;
338
- const element = document.elementFromPoint(e.clientX, e.clientY);
339
- if (!element || element === overlay) return;
340
- currentElement = element;
341
- const rect = element.getBoundingClientRect();
342
- const computedStyle = window.getComputedStyle(element);
343
- targetX = rect.left;
344
- targetY = rect.top;
345
- targetWidth = rect.width;
346
- targetHeight = rect.height;
347
- targetBorderRadius = computedStyle.borderRadius;
348
- };
349
- const handleClick = (e) => {
350
- if (!isActive) return;
351
- e.preventDefault();
352
- e.stopPropagation();
353
- e.stopImmediatePropagation();
354
- isLocked = true;
355
- const elementToInspect = currentElement;
356
- if (elementToInspect) {
357
- void getStack(elementToInspect).then((stack) => {
358
- if (!stack) {
359
- hideOverlay();
360
- return;
361
- }
362
- const serializedStack = serializeStack(filterStack(stack));
363
- const htmlSnippet = getHTMLSnippet(elementToInspect);
364
- const payload = `## Referenced element
365
- ${htmlSnippet}
366
-
367
- Import traces:
368
- ${serializedStack}
369
-
370
- Page: ${window.location.href}`;
371
- void copyToClipboard(payload);
372
- });
373
- }
374
- };
375
- const handleMouseDown = (e) => {
376
- if (!isActive) return;
377
- e.preventDefault();
378
- e.stopPropagation();
379
- e.stopImmediatePropagation();
380
- };
381
- const showOverlay = () => {
382
- if (!overlay) {
383
- overlay = createOverlay();
384
- }
385
- isActive = true;
386
- overlay.style.display = "block";
387
- currentX = targetX;
388
- currentY = targetY;
389
- currentWidth = targetWidth;
390
- currentHeight = targetHeight;
391
- updateOverlayPosition();
392
- };
393
- const hideOverlay = () => {
394
- isActive = false;
395
- isLocked = false;
396
- if (overlay) {
397
- overlay.style.display = "none";
398
- }
399
- if (animationFrame) {
400
- cancelAnimationFrame(animationFrame);
401
- animationFrame = null;
402
- }
403
- currentElement = null;
404
- };
405
- const handleKeyDown = (e) => {
406
- if (e.metaKey && !metaKeyTimer && !isActive) {
407
- metaKeyTimer = setTimeout(() => {
408
- if (!isInsideInputOrTextarea()) {
409
- showOverlay();
410
- }
411
- metaKeyTimer = null;
412
- }, 750);
413
- }
414
- };
415
- const handleKeyUp = (e) => {
416
- if (!e.metaKey) {
417
- if (metaKeyTimer) {
418
- clearTimeout(metaKeyTimer);
419
- metaKeyTimer = null;
420
- }
421
- if (isActive && !isLocked) {
422
- hideOverlay();
423
- }
424
- }
425
- };
426
- document.addEventListener("keydown", handleKeyDown);
427
- document.addEventListener("keyup", handleKeyUp);
428
- document.addEventListener("mousemove", handleMouseMove);
429
- document.addEventListener("mousedown", handleMouseDown, true);
430
- document.addEventListener("click", handleClick, true);
431
- window.addEventListener("focus", handleWindowFocus);
432
- return () => {
433
- if (metaKeyTimer) {
434
- clearTimeout(metaKeyTimer);
435
- }
436
- if (animationFrame) {
437
- cancelAnimationFrame(animationFrame);
438
- }
439
- if (overlay && overlay.parentNode) {
440
- overlay.parentNode.removeChild(overlay);
441
- }
442
- document.removeEventListener("keydown", handleKeyDown);
443
- document.removeEventListener("keyup", handleKeyUp);
444
- document.removeEventListener("mousemove", handleMouseMove);
445
- document.removeEventListener("mousedown", handleMouseDown, true);
446
- document.removeEventListener("click", handleClick, true);
447
- window.removeEventListener("focus", handleWindowFocus);
448
- };
449
- };
450
- init();
451
-
452
- export { init };