react-grab 0.0.21 → 0.0.23

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 CHANGED
@@ -1,3 +1,5 @@
1
+ import { render, createComponent, mergeProps, memo, template, effect, style, insert, setStyleProperty, use } from 'solid-js/web';
2
+ import { createRoot, createSignal, createMemo, createEffect, onCleanup, Show, For } from 'solid-js';
1
3
  import { instrument, _fiberRoots, getFiberFromHostInstance } from 'bippy';
2
4
  import { getOwnerStack, getSourcesFromStack } from 'bippy/dist/source';
3
5
 
@@ -10,6 +12,7 @@ import { getOwnerStack, getSourcesFromStack } from 'bippy/dist/source';
10
12
  * LICENSE file in the root directory of this source tree.
11
13
  */
12
14
 
15
+
13
16
  // src/utils/is-keyboard-event-triggered-by-input.ts
14
17
  var FORM_TAGS_AND_ROLES = [
15
18
  "input",
@@ -90,348 +93,433 @@ var mountRoot = () => {
90
93
  var isElementVisible = (element, computedStyle = window.getComputedStyle(element)) => {
91
94
  return computedStyle.display !== "none" && computedStyle.visibility !== "hidden" && computedStyle.opacity !== "0";
92
95
  };
93
-
94
- // src/overlay.ts
95
- var templateCache = /* @__PURE__ */ new Map();
96
- var html = (strings, ...values) => {
97
- const key = strings.join("");
98
- let template = templateCache.get(key);
99
- if (!template) {
100
- template = document.createElement("template");
101
- template.innerHTML = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "");
102
- templateCache.set(key, template);
103
- }
104
- return template.content.firstElementChild.cloneNode(true);
105
- };
96
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
97
+ var _tmpl$2 = /* @__PURE__ */ template(`<span style="display:inline-block;width:8px;height:8px;border:1.5px solid rgb(210, 57, 192);border-top-color:transparent;border-radius:50%;margin-right:4px;vertical-align:middle">`);
98
+ var _tmpl$3 = /* @__PURE__ */ template(`<span style=display:inline-block;margin-right:4px;font-weight:600>\u2713`);
99
+ var _tmpl$4 = /* @__PURE__ */ template(`<span style="font-family:ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;font-variant-numeric:tabular-nums">`);
100
+ var _tmpl$5 = /* @__PURE__ */ template(`<div style="position:fixed;padding:2px 6px;background-color:#fde7f7;color:#b21c8e;border:1px solid #f7c5ec;border-radius:4px;font-size:11px;font-weight:500;font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;pointer-events:none;transition:opacity 0.2s ease-in-out;display:flex;align-items:center;max-width:calc(100vw - (16px + env(safe-area-inset-left) + env(safe-area-inset-right)));overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><span>`);
101
+ var _tmpl$6 = /* @__PURE__ */ template(`<div style="position:fixed;z-index:2147483647;pointer-events:none;transition:opacity 0.1s ease-in-out"><div style="width:32px;height:2px;background-color:rgba(178, 28, 142, 0.2);border-radius:1px;overflow:hidden;position:relative"><div style="height:100%;background-color:#b21c8e;border-radius:1px;transition:width 0.05s cubic-bezier(0.165, 0.84, 0.44, 1)">`);
106
102
  var VIEWPORT_MARGIN_PX = 8;
107
103
  var LABEL_OFFSET_PX = 6;
108
104
  var INDICATOR_CLAMP_PADDING_PX = 4;
109
105
  var INDICATOR_SUCCESS_VISIBLE_MS = 1500;
110
- var INDICATOR_FADE_MS = 200;
106
+ var SELECTION_LERP_FACTOR = 0.95;
107
+ var MARQUEE_LERP_FACTOR = 0.9;
111
108
  var lerp = (start, end, factor) => {
112
109
  return start + (end - start) * factor;
113
110
  };
114
- var SELECTION_LERP_FACTOR = 0.95;
115
- var createSelectionElement = ({
116
- borderRadius,
117
- height,
118
- transform,
119
- width,
120
- x,
121
- y
122
- }) => {
123
- const overlay = html`
124
- <div style="
125
- position: fixed;
126
- top: ${y}px;
127
- left: ${x}px;
128
- width: ${width}px;
129
- height: ${height}px;
130
- border-radius: ${borderRadius};
131
- transform: ${transform};
132
- pointer-events: auto;
133
- border: 1px solid rgb(210, 57, 192);
134
- background-color: rgba(210, 57, 192, 0.2);
135
- z-index: 2147483646;
136
- box-sizing: border-box;
137
- display: none;
138
- "></div>
139
- `;
140
- return overlay;
141
- };
142
- var updateSelectionElement = (element, { borderRadius, height, transform, width, x, y }) => {
143
- const currentTop = parseFloat(element.style.top) || 0;
144
- const currentLeft = parseFloat(element.style.left) || 0;
145
- const currentWidth = parseFloat(element.style.width) || 0;
146
- const currentHeight = parseFloat(element.style.height) || 0;
147
- const topValue = `${lerp(currentTop, y, SELECTION_LERP_FACTOR)}px`;
148
- const leftValue = `${lerp(currentLeft, x, SELECTION_LERP_FACTOR)}px`;
149
- const widthValue = `${lerp(currentWidth, width, SELECTION_LERP_FACTOR)}px`;
150
- const heightValue = `${lerp(currentHeight, height, SELECTION_LERP_FACTOR)}px`;
151
- if (element.style.top !== topValue) {
152
- element.style.top = topValue;
153
- }
154
- if (element.style.left !== leftValue) {
155
- element.style.left = leftValue;
156
- }
157
- if (element.style.width !== widthValue) {
158
- element.style.width = widthValue;
159
- }
160
- if (element.style.height !== heightValue) {
161
- element.style.height = heightValue;
162
- }
163
- if (element.style.borderRadius !== borderRadius) {
164
- element.style.borderRadius = borderRadius;
165
- }
166
- if (element.style.transform !== transform) {
167
- element.style.transform = transform;
168
- }
169
- };
170
- var createSelectionOverlay = (root, onSelectionClick) => {
171
- const element = createSelectionElement({
172
- borderRadius: "0px",
173
- height: 0,
174
- transform: "none",
175
- width: 0,
176
- x: -1e3,
177
- y: -1e3
178
- });
179
- root.appendChild(element);
180
- let visible = false;
111
+ var Overlay = (props) => {
112
+ const [currentX, setCurrentX] = createSignal(props.bounds.x);
113
+ const [currentY, setCurrentY] = createSignal(props.bounds.y);
114
+ const [currentWidth, setCurrentWidth] = createSignal(props.bounds.width);
115
+ const [currentHeight, setCurrentHeight] = createSignal(props.bounds.height);
116
+ const [opacity, setOpacity] = createSignal(1);
181
117
  let hasBeenShown = false;
182
- element.addEventListener(
183
- "mousedown",
184
- (event) => {
185
- event.preventDefault();
186
- event.stopPropagation();
187
- event.stopImmediatePropagation();
188
- if (onSelectionClick) {
189
- onSelectionClick();
190
- }
191
- },
192
- true
193
- );
194
- return {
195
- element,
196
- hide: () => {
197
- visible = false;
198
- hasBeenShown = false;
199
- element.style.display = "none";
200
- },
201
- isVisible: () => visible,
202
- show: () => {
203
- visible = true;
204
- element.style.display = "block";
205
- },
206
- update: (selection) => {
207
- if (!hasBeenShown) {
208
- element.style.top = `${selection.y}px`;
209
- element.style.left = `${selection.x}px`;
210
- element.style.width = `${selection.width}px`;
211
- element.style.height = `${selection.height}px`;
212
- element.style.borderRadius = selection.borderRadius;
213
- element.style.transform = selection.transform;
214
- hasBeenShown = true;
118
+ let animationFrameId = null;
119
+ let targetBounds = props.bounds;
120
+ createEffect(() => {
121
+ targetBounds = props.bounds;
122
+ const factor = props.lerpFactor ?? SELECTION_LERP_FACTOR;
123
+ if (!hasBeenShown) {
124
+ setCurrentX(targetBounds.x);
125
+ setCurrentY(targetBounds.y);
126
+ setCurrentWidth(targetBounds.width);
127
+ setCurrentHeight(targetBounds.height);
128
+ hasBeenShown = true;
129
+ return;
130
+ }
131
+ const EPSILON = 0.5;
132
+ const animate = () => {
133
+ const newX = lerp(currentX(), targetBounds.x, factor);
134
+ const newY = lerp(currentY(), targetBounds.y, factor);
135
+ const newWidth = lerp(currentWidth(), targetBounds.width, factor);
136
+ const newHeight = lerp(currentHeight(), targetBounds.height, factor);
137
+ setCurrentX(newX);
138
+ setCurrentY(newY);
139
+ setCurrentWidth(newWidth);
140
+ setCurrentHeight(newHeight);
141
+ const hasConverged = Math.abs(newX - targetBounds.x) < EPSILON && Math.abs(newY - targetBounds.y) < EPSILON && Math.abs(newWidth - targetBounds.width) < EPSILON && Math.abs(newHeight - targetBounds.height) < EPSILON;
142
+ if (!hasConverged) {
143
+ animationFrameId = requestAnimationFrame(animate);
215
144
  } else {
216
- updateSelectionElement(element, selection);
145
+ animationFrameId = null;
217
146
  }
147
+ };
148
+ if (animationFrameId !== null) {
149
+ cancelAnimationFrame(animationFrameId);
150
+ }
151
+ animationFrameId = requestAnimationFrame(animate);
152
+ onCleanup(() => {
153
+ if (animationFrameId !== null) {
154
+ cancelAnimationFrame(animationFrameId);
155
+ animationFrameId = null;
156
+ }
157
+ });
158
+ });
159
+ createEffect(() => {
160
+ if (props.variant === "grabbed") {
161
+ requestAnimationFrame(() => {
162
+ setOpacity(0);
163
+ });
218
164
  }
165
+ });
166
+ const baseStyle = {
167
+ position: "fixed",
168
+ "box-sizing": "border-box",
169
+ "pointer-events": props.variant === "marquee" ? "none" : "auto",
170
+ "z-index": "2147483646"
219
171
  };
220
- };
221
- var createGrabbedOverlay = (root, selection) => {
222
- const element = document.createElement("div");
223
- element.style.position = "fixed";
224
- element.style.top = `${selection.y}px`;
225
- element.style.left = `${selection.x}px`;
226
- element.style.width = `${selection.width}px`;
227
- element.style.height = `${selection.height}px`;
228
- element.style.borderRadius = selection.borderRadius;
229
- element.style.transform = selection.transform;
230
- element.style.pointerEvents = "none";
231
- element.style.border = "1px solid rgb(210, 57, 192)";
232
- element.style.backgroundColor = "rgba(210, 57, 192, 0.2)";
233
- element.style.zIndex = "2147483646";
234
- element.style.boxSizing = "border-box";
235
- element.style.transition = "opacity 0.3s ease-out";
236
- element.style.opacity = "1";
237
- root.appendChild(element);
238
- requestAnimationFrame(() => {
239
- element.style.opacity = "0";
172
+ const variantStyle = () => {
173
+ if (props.variant === "marquee") {
174
+ return {
175
+ border: "1px dashed rgb(210, 57, 192)",
176
+ "background-color": "rgba(210, 57, 192, 0.1)",
177
+ "will-change": "transform, width, height",
178
+ contain: "layout paint size"
179
+ };
180
+ }
181
+ return {
182
+ border: "1px solid rgb(210, 57, 192)",
183
+ "background-color": "rgba(210, 57, 192, 0.2)",
184
+ transition: props.variant === "grabbed" ? "opacity 0.3s ease-out" : void 0
185
+ };
186
+ };
187
+ return createComponent(Show, {
188
+ get when() {
189
+ return props.visible !== false;
190
+ },
191
+ get children() {
192
+ var _el$ = _tmpl$();
193
+ effect((_$p) => style(_el$, {
194
+ ...baseStyle,
195
+ ...variantStyle(),
196
+ top: `${currentY()}px`,
197
+ left: `${currentX()}px`,
198
+ width: `${currentWidth()}px`,
199
+ height: `${currentHeight()}px`,
200
+ "border-radius": props.bounds.borderRadius,
201
+ transform: props.bounds.transform,
202
+ opacity: opacity()
203
+ }, _$p));
204
+ return _el$;
205
+ }
240
206
  });
241
- setTimeout(() => {
242
- element.remove();
243
- }, 300);
244
207
  };
245
- var createSpinner = () => {
246
- const spinner = html`
247
- <span style="
248
- display: inline-block;
249
- width: 8px;
250
- height: 8px;
251
- border: 1.5px solid rgb(210, 57, 192);
252
- border-top-color: transparent;
253
- border-radius: 50%;
254
- margin-right: 4px;
255
- vertical-align: middle;
256
- "></span>
257
- `;
258
- spinner.animate(
259
- [{ transform: "rotate(0deg)" }, { transform: "rotate(360deg)" }],
260
- {
261
- duration: 600,
262
- easing: "linear",
263
- iterations: Infinity
208
+ var Spinner = (props) => {
209
+ let ref;
210
+ createEffect(() => {
211
+ if (ref) {
212
+ ref.animate([{
213
+ transform: "rotate(0deg)"
214
+ }, {
215
+ transform: "rotate(360deg)"
216
+ }], {
217
+ duration: 600,
218
+ easing: "linear",
219
+ iterations: Infinity
220
+ });
264
221
  }
265
- );
266
- return spinner;
267
- };
268
- var activeIndicator = null;
269
- var createIndicator = () => {
270
- const indicator = html`
271
- <div style="
272
- position: fixed;
273
- top: calc(8px + env(safe-area-inset-top));
274
- padding: 2px 6px;
275
- background-color: #fde7f7;
276
- color: #b21c8e;
277
- border: 1px solid #f7c5ec;
278
- border-radius: 4px;
279
- font-size: 11px;
280
- font-weight: 500;
281
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
282
- z-index: 2147483647;
283
- pointer-events: none;
284
- opacity: 0;
285
- transition: opacity 0.2s ease-in-out;
286
- display: flex;
287
- align-items: center;
288
- max-width: calc(100vw - (16px + env(safe-area-inset-left) + env(safe-area-inset-right)));
289
- overflow: hidden;
290
- text-overflow: ellipsis;
291
- white-space: nowrap;
292
- "></div>
293
- `;
294
- return indicator;
222
+ });
223
+ return (() => {
224
+ var _el$2 = _tmpl$2();
225
+ var _ref$ = ref;
226
+ typeof _ref$ === "function" ? use(_ref$, _el$2) : ref = _el$2;
227
+ effect((_$p) => style(_el$2, {
228
+ ...props.style
229
+ }, _$p));
230
+ return _el$2;
231
+ })();
295
232
  };
296
- var showLabel = (root, selectionLeftPx, selectionTopPx, tagName) => {
297
- let indicator = activeIndicator;
298
- let isNewIndicator = false;
299
- if (!indicator) {
300
- indicator = createIndicator();
301
- root.appendChild(indicator);
302
- activeIndicator = indicator;
303
- isNewIndicator = true;
304
- isProcessing = false;
305
- }
306
- if (!isProcessing) {
307
- const labelText = indicator.querySelector("span");
308
- if (labelText) {
309
- const tagNameMonospace = document.createElement("span");
310
- tagNameMonospace.textContent = tagName ? `<${tagName}>` : "<element>";
311
- tagNameMonospace.style.fontFamily = "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace";
312
- tagNameMonospace.style.fontVariantNumeric = "tabular-nums";
313
- labelText.replaceChildren(tagNameMonospace);
233
+ var Label = (props) => {
234
+ const [opacity, setOpacity] = createSignal(0);
235
+ let ref;
236
+ createEffect(() => {
237
+ if (props.visible !== false) {
238
+ requestAnimationFrame(() => {
239
+ setOpacity(1);
240
+ });
314
241
  } else {
315
- const newLabelText = document.createElement("span");
316
- const tagNameMonospace = document.createElement("span");
317
- tagNameMonospace.textContent = tagName ? `<${tagName}>` : "<element>";
318
- tagNameMonospace.style.fontFamily = "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace";
319
- tagNameMonospace.style.fontVariantNumeric = "tabular-nums";
320
- newLabelText.appendChild(tagNameMonospace);
321
- indicator.appendChild(newLabelText);
242
+ setOpacity(0);
322
243
  }
323
- }
324
- const indicatorRect = indicator.getBoundingClientRect();
325
- const viewportWidthPx = window.innerWidth;
326
- const viewportHeightPx = window.innerHeight;
327
- let indicatorLeftPx = Math.round(selectionLeftPx);
328
- let indicatorTopPx = Math.round(selectionTopPx) - indicatorRect.height - LABEL_OFFSET_PX;
329
- const CLAMPED_PADDING = INDICATOR_CLAMP_PADDING_PX;
330
- const minLeft = VIEWPORT_MARGIN_PX;
331
- const minTop = VIEWPORT_MARGIN_PX;
332
- const maxLeft = viewportWidthPx - indicatorRect.width - VIEWPORT_MARGIN_PX;
333
- const maxTop = viewportHeightPx - indicatorRect.height - VIEWPORT_MARGIN_PX;
334
- const willClampLeft = indicatorLeftPx < minLeft;
335
- const willClampTop = indicatorTopPx < minTop;
336
- const isClamped = willClampLeft || willClampTop;
337
- indicatorLeftPx = Math.max(minLeft, Math.min(indicatorLeftPx, maxLeft));
338
- indicatorTopPx = Math.max(minTop, Math.min(indicatorTopPx, maxTop));
339
- if (isClamped) {
340
- indicatorLeftPx += CLAMPED_PADDING;
341
- indicatorTopPx += CLAMPED_PADDING;
342
- }
343
- indicator.style.left = `${indicatorLeftPx}px`;
344
- indicator.style.top = `${indicatorTopPx}px`;
345
- indicator.style.right = "auto";
346
- if (isNewIndicator) {
347
- requestAnimationFrame(() => {
348
- indicator.style.opacity = "1";
349
- });
350
- } else if (indicator.style.opacity !== "1") {
351
- indicator.style.opacity = "1";
352
- }
353
- };
354
- var isProcessing = false;
355
- var activeGrabbedIndicators = /* @__PURE__ */ new Set();
356
- var updateLabelToProcessing = (root, selectionLeftPx, selectionTopPx) => {
357
- const indicator = createIndicator();
358
- indicator.style.zIndex = "2147483648";
359
- root.appendChild(indicator);
360
- activeGrabbedIndicators.add(indicator);
361
- const positionIndicator = () => {
362
- if (selectionLeftPx === void 0 || selectionTopPx === void 0) return;
363
- const indicatorRect = indicator.getBoundingClientRect();
244
+ });
245
+ createEffect(() => {
246
+ if (props.variant === "success") {
247
+ const fadeOutTimer = setTimeout(() => {
248
+ setOpacity(0);
249
+ }, INDICATOR_SUCCESS_VISIBLE_MS);
250
+ onCleanup(() => clearTimeout(fadeOutTimer));
251
+ }
252
+ });
253
+ const indicatorRect = () => ref?.getBoundingClientRect();
254
+ const computedPosition = () => {
255
+ const rect = indicatorRect();
256
+ if (!rect) return {
257
+ left: props.x,
258
+ top: props.y
259
+ };
364
260
  const viewportWidthPx = window.innerWidth;
365
261
  const viewportHeightPx = window.innerHeight;
366
- let indicatorLeftPx = Math.round(selectionLeftPx);
367
- let indicatorTopPx = Math.round(selectionTopPx) - indicatorRect.height - LABEL_OFFSET_PX;
368
- const CLAMPED_PADDING = INDICATOR_CLAMP_PADDING_PX;
262
+ let indicatorLeftPx = Math.round(props.x);
263
+ let indicatorTopPx = Math.round(props.y) - rect.height - LABEL_OFFSET_PX;
369
264
  const minLeft = VIEWPORT_MARGIN_PX;
370
265
  const minTop = VIEWPORT_MARGIN_PX;
371
- const maxLeft = viewportWidthPx - indicatorRect.width - VIEWPORT_MARGIN_PX;
372
- const maxTop = viewportHeightPx - indicatorRect.height - VIEWPORT_MARGIN_PX;
266
+ const maxLeft = viewportWidthPx - rect.width - VIEWPORT_MARGIN_PX;
267
+ const maxTop = viewportHeightPx - rect.height - VIEWPORT_MARGIN_PX;
373
268
  const willClampLeft = indicatorLeftPx < minLeft;
374
269
  const willClampTop = indicatorTopPx < minTop;
375
270
  const isClamped = willClampLeft || willClampTop;
376
271
  indicatorLeftPx = Math.max(minLeft, Math.min(indicatorLeftPx, maxLeft));
377
272
  indicatorTopPx = Math.max(minTop, Math.min(indicatorTopPx, maxTop));
378
273
  if (isClamped) {
379
- indicatorLeftPx += CLAMPED_PADDING;
380
- indicatorTopPx += CLAMPED_PADDING;
274
+ indicatorLeftPx += INDICATOR_CLAMP_PADDING_PX;
275
+ indicatorTopPx += INDICATOR_CLAMP_PADDING_PX;
381
276
  }
382
- indicator.style.left = `${indicatorLeftPx}px`;
383
- indicator.style.top = `${indicatorTopPx}px`;
384
- indicator.style.right = "auto";
277
+ return {
278
+ left: indicatorLeftPx,
279
+ top: indicatorTopPx
280
+ };
385
281
  };
386
- const loadingSpinner = createSpinner();
387
- const labelText = document.createElement("span");
388
- labelText.textContent = "Grabbing\u2026";
389
- indicator.appendChild(loadingSpinner);
390
- indicator.appendChild(labelText);
391
- positionIndicator();
392
- requestAnimationFrame(() => {
393
- indicator.style.opacity = "1";
282
+ return createComponent(Show, {
283
+ get when() {
284
+ return props.visible !== false;
285
+ },
286
+ get children() {
287
+ var _el$3 = _tmpl$5(), _el$5 = _el$3.firstChild;
288
+ var _ref$2 = ref;
289
+ typeof _ref$2 === "function" ? use(_ref$2, _el$3) : ref = _el$3;
290
+ insert(_el$3, createComponent(Show, {
291
+ get when() {
292
+ return props.variant === "processing";
293
+ },
294
+ get children() {
295
+ return createComponent(Spinner, {});
296
+ }
297
+ }), _el$5);
298
+ insert(_el$3, createComponent(Show, {
299
+ get when() {
300
+ return props.variant === "success";
301
+ },
302
+ get children() {
303
+ return _tmpl$3();
304
+ }
305
+ }), _el$5);
306
+ insert(_el$5, createComponent(Show, {
307
+ get when() {
308
+ return props.variant === "success";
309
+ },
310
+ children: "Grabbed "
311
+ }), null);
312
+ insert(_el$5, createComponent(Show, {
313
+ get when() {
314
+ return props.variant === "processing";
315
+ },
316
+ children: "Grabbing\u2026"
317
+ }), null);
318
+ insert(_el$5, createComponent(Show, {
319
+ get when() {
320
+ return props.variant === "hover";
321
+ },
322
+ get children() {
323
+ var _el$6 = _tmpl$4();
324
+ insert(_el$6, () => props.text);
325
+ return _el$6;
326
+ }
327
+ }), null);
328
+ insert(_el$5, createComponent(Show, {
329
+ get when() {
330
+ return props.variant !== "hover";
331
+ },
332
+ get children() {
333
+ var _el$7 = _tmpl$4();
334
+ insert(_el$7, () => props.text);
335
+ return _el$7;
336
+ }
337
+ }), null);
338
+ effect((_p$) => {
339
+ var _v$ = `${computedPosition().top}px`, _v$2 = `${computedPosition().left}px`, _v$3 = props.zIndex?.toString() ?? "2147483647", _v$4 = opacity();
340
+ _v$ !== _p$.e && setStyleProperty(_el$3, "top", _p$.e = _v$);
341
+ _v$2 !== _p$.t && setStyleProperty(_el$3, "left", _p$.t = _v$2);
342
+ _v$3 !== _p$.a && setStyleProperty(_el$3, "z-index", _p$.a = _v$3);
343
+ _v$4 !== _p$.o && setStyleProperty(_el$3, "opacity", _p$.o = _v$4);
344
+ return _p$;
345
+ }, {
346
+ e: void 0,
347
+ t: void 0,
348
+ a: void 0,
349
+ o: void 0
350
+ });
351
+ return _el$3;
352
+ }
394
353
  });
395
- return (tagName) => {
396
- indicator.textContent = "";
397
- const checkmarkIcon = document.createElement("span");
398
- checkmarkIcon.textContent = "\u2713";
399
- checkmarkIcon.style.display = "inline-block";
400
- checkmarkIcon.style.marginRight = "4px";
401
- checkmarkIcon.style.fontWeight = "600";
402
- const newLabelText = document.createElement("span");
403
- const tagNameMonospace = document.createElement("span");
404
- tagNameMonospace.textContent = tagName ? `<${tagName}>` : "<element>";
405
- tagNameMonospace.style.fontFamily = "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace";
406
- tagNameMonospace.style.fontVariantNumeric = "tabular-nums";
407
- newLabelText.appendChild(document.createTextNode("Grabbed "));
408
- newLabelText.appendChild(tagNameMonospace);
409
- indicator.appendChild(checkmarkIcon);
410
- indicator.appendChild(newLabelText);
411
- requestAnimationFrame(() => {
412
- positionIndicator();
413
- });
414
- setTimeout(() => {
415
- indicator.style.opacity = "0";
416
- setTimeout(() => {
417
- indicator.remove();
418
- activeGrabbedIndicators.delete(indicator);
419
- }, INDICATOR_FADE_MS);
420
- }, INDICATOR_SUCCESS_VISIBLE_MS);
421
- };
422
354
  };
423
- var hideLabel = () => {
424
- if (activeIndicator) {
425
- activeIndicator.remove();
426
- activeIndicator = null;
427
- }
428
- isProcessing = false;
355
+ var ProgressIndicator = (props) => {
356
+ const [opacity, setOpacity] = createSignal(0);
357
+ let ref;
358
+ createEffect(() => {
359
+ if (props.visible !== false) {
360
+ requestAnimationFrame(() => {
361
+ setOpacity(1);
362
+ });
363
+ } else {
364
+ setOpacity(0);
365
+ }
366
+ });
367
+ const computedPosition = () => {
368
+ const rect = ref?.getBoundingClientRect();
369
+ if (!rect) return {
370
+ left: props.mouseX,
371
+ top: props.mouseY
372
+ };
373
+ const viewportWidth = window.innerWidth;
374
+ const viewportHeight = window.innerHeight;
375
+ const CURSOR_OFFSET = 14;
376
+ const VIEWPORT_MARGIN = 8;
377
+ let indicatorLeft = props.mouseX - rect.width / 2;
378
+ let indicatorTop = props.mouseY + CURSOR_OFFSET;
379
+ if (indicatorTop + rect.height + VIEWPORT_MARGIN > viewportHeight) {
380
+ indicatorTop = props.mouseY - rect.height - CURSOR_OFFSET;
381
+ }
382
+ indicatorTop = Math.max(VIEWPORT_MARGIN, Math.min(indicatorTop, viewportHeight - rect.height - VIEWPORT_MARGIN));
383
+ indicatorLeft = Math.max(VIEWPORT_MARGIN, Math.min(indicatorLeft, viewportWidth - rect.width - VIEWPORT_MARGIN));
384
+ return {
385
+ left: indicatorLeft,
386
+ top: indicatorTop
387
+ };
388
+ };
389
+ return createComponent(Show, {
390
+ get when() {
391
+ return props.visible !== false;
392
+ },
393
+ get children() {
394
+ var _el$8 = _tmpl$6(), _el$9 = _el$8.firstChild, _el$0 = _el$9.firstChild;
395
+ var _ref$3 = ref;
396
+ typeof _ref$3 === "function" ? use(_ref$3, _el$8) : ref = _el$8;
397
+ effect((_p$) => {
398
+ var _v$5 = `${computedPosition().top}px`, _v$6 = `${computedPosition().left}px`, _v$7 = opacity(), _v$8 = `${Math.min(100, Math.max(0, props.progress * 100))}%`;
399
+ _v$5 !== _p$.e && setStyleProperty(_el$8, "top", _p$.e = _v$5);
400
+ _v$6 !== _p$.t && setStyleProperty(_el$8, "left", _p$.t = _v$6);
401
+ _v$7 !== _p$.a && setStyleProperty(_el$8, "opacity", _p$.a = _v$7);
402
+ _v$8 !== _p$.o && setStyleProperty(_el$0, "width", _p$.o = _v$8);
403
+ return _p$;
404
+ }, {
405
+ e: void 0,
406
+ t: void 0,
407
+ a: void 0,
408
+ o: void 0
409
+ });
410
+ return _el$8;
411
+ }
412
+ });
429
413
  };
430
- var cleanupGrabbedIndicators = () => {
431
- for (const indicator of activeGrabbedIndicators) {
432
- indicator.remove();
433
- }
434
- activeGrabbedIndicators.clear();
414
+ var ReactGrabOverlay = (props) => {
415
+ return [createComponent(Show, {
416
+ get when() {
417
+ return memo(() => !!props.selectionVisible)() && props.selectionBounds;
418
+ },
419
+ get children() {
420
+ return createComponent(Overlay, {
421
+ variant: "selection",
422
+ get bounds() {
423
+ return props.selectionBounds;
424
+ },
425
+ get visible() {
426
+ return props.selectionVisible;
427
+ },
428
+ lerpFactor: SELECTION_LERP_FACTOR
429
+ });
430
+ }
431
+ }), createComponent(Show, {
432
+ get when() {
433
+ return memo(() => !!props.marqueeVisible)() && props.marqueeBounds;
434
+ },
435
+ get children() {
436
+ return createComponent(Overlay, {
437
+ variant: "marquee",
438
+ get bounds() {
439
+ return props.marqueeBounds;
440
+ },
441
+ get visible() {
442
+ return props.marqueeVisible;
443
+ },
444
+ lerpFactor: MARQUEE_LERP_FACTOR
445
+ });
446
+ }
447
+ }), createComponent(For, {
448
+ get each() {
449
+ return props.grabbedOverlays ?? [];
450
+ },
451
+ children: (overlay) => createComponent(Overlay, {
452
+ variant: "grabbed",
453
+ get bounds() {
454
+ return overlay.bounds;
455
+ },
456
+ visible: true
457
+ })
458
+ }), createComponent(For, {
459
+ get each() {
460
+ return props.successLabels ?? [];
461
+ },
462
+ children: (label) => createComponent(Label, {
463
+ variant: "success",
464
+ get text() {
465
+ return label.text;
466
+ },
467
+ get x() {
468
+ return label.x;
469
+ },
470
+ get y() {
471
+ return label.y;
472
+ },
473
+ visible: true,
474
+ zIndex: 2147483648
475
+ })
476
+ }), createComponent(Show, {
477
+ get when() {
478
+ return memo(() => !!(props.labelVisible && props.labelVariant && props.labelText && props.labelX !== void 0))() && props.labelY !== void 0;
479
+ },
480
+ get children() {
481
+ return createComponent(Label, {
482
+ get variant() {
483
+ return props.labelVariant;
484
+ },
485
+ get text() {
486
+ return props.labelText;
487
+ },
488
+ get x() {
489
+ return props.labelX;
490
+ },
491
+ get y() {
492
+ return props.labelY;
493
+ },
494
+ get visible() {
495
+ return props.labelVisible;
496
+ },
497
+ get zIndex() {
498
+ return props.labelZIndex;
499
+ }
500
+ });
501
+ }
502
+ }), createComponent(Show, {
503
+ get when() {
504
+ return memo(() => !!(props.progressVisible && props.progress !== void 0 && props.mouseX !== void 0))() && props.mouseY !== void 0;
505
+ },
506
+ get children() {
507
+ return createComponent(ProgressIndicator, {
508
+ get progress() {
509
+ return props.progress;
510
+ },
511
+ get mouseX() {
512
+ return props.mouseX;
513
+ },
514
+ get mouseY() {
515
+ return props.mouseY;
516
+ },
517
+ get visible() {
518
+ return props.progressVisible;
519
+ }
520
+ });
521
+ }
522
+ })];
435
523
  };
436
524
  instrument({
437
525
  onCommitFiberRoot(_, fiberRoot) {
@@ -648,7 +736,19 @@ var getHTMLSnippet = (element) => {
648
736
  };
649
737
 
650
738
  // src/utils/copy-content.ts
739
+ var waitForFocus = () => {
740
+ if (document.hasFocus()) return Promise.resolve();
741
+ return new Promise((resolve) => {
742
+ const onFocus = () => {
743
+ window.removeEventListener("focus", onFocus);
744
+ resolve();
745
+ };
746
+ window.addEventListener("focus", onFocus);
747
+ window.focus();
748
+ });
749
+ };
651
750
  var copyContent = async (content) => {
751
+ await waitForFocus();
652
752
  try {
653
753
  if (Array.isArray(content)) {
654
754
  if (!navigator?.clipboard?.write) {
@@ -710,218 +810,455 @@ var copyContentFallback = (content) => {
710
810
  }
711
811
  };
712
812
 
713
- // src/core.ts
813
+ // src/core.tsx
714
814
  var init = (rawOptions) => {
715
815
  const options = {
716
816
  enabled: true,
717
- keyHoldDuration: 500,
817
+ keyHoldDuration: 300,
718
818
  ...rawOptions
719
819
  };
720
820
  if (options.enabled === false) {
721
821
  return;
722
822
  }
723
- let holdTimer = null;
724
- let isHoldingKeys = false;
725
- let overlayRoot = null;
726
- let selectionOverlay = null;
727
- let renderFrameId = null;
728
- let isActive = false;
729
- let isCopying = false;
730
- let hoveredElement = null;
731
- let lastGrabbedElement = null;
732
- let mouseX = -1e3;
733
- let mouseY = -1e3;
734
- const isTargetKeyCombination = (event) => (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "c";
735
- const getElementAtPosition = (x, y) => {
736
- const elementsAtPoint = document.elementsFromPoint(x, y);
737
- for (const candidateElement of elementsAtPoint) {
738
- if (candidateElement.closest(`[${ATTRIBUTE_NAME}]`)) {
739
- continue;
823
+ return createRoot((dispose) => {
824
+ const [isHoldingKeys, setIsHoldingKeys] = createSignal(false);
825
+ const [mouseX, setMouseX] = createSignal(-1e3);
826
+ const [mouseY, setMouseY] = createSignal(-1e3);
827
+ const [isDragging, setIsDragging] = createSignal(false);
828
+ const [dragStartX, setDragStartX] = createSignal(-1e3);
829
+ const [dragStartY, setDragStartY] = createSignal(-1e3);
830
+ const [isCopying, setIsCopying] = createSignal(false);
831
+ const [lastGrabbedElement, setLastGrabbedElement] = createSignal(null);
832
+ const [progressStartTime, setProgressStartTime] = createSignal(null);
833
+ const [progressTick, setProgressTick] = createSignal(0);
834
+ const [grabbedOverlays, setGrabbedOverlays] = createSignal([]);
835
+ const [successLabels, setSuccessLabels] = createSignal([]);
836
+ const [isActivated, setIsActivated] = createSignal(false);
837
+ const [showProgressIndicator, setShowProgressIndicator] = createSignal(false);
838
+ let holdTimerId = null;
839
+ let progressAnimationId = null;
840
+ let progressDelayTimerId = null;
841
+ const isOverlayActive = createMemo(() => isActivated() && !isCopying());
842
+ const isTargetKeyCombination = (event) => (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "c";
843
+ const getElementAtPosition = (x, y) => {
844
+ const elementsAtPoint = document.elementsFromPoint(x, y);
845
+ for (const candidateElement of elementsAtPoint) {
846
+ if (candidateElement.closest(`[${ATTRIBUTE_NAME}]`)) {
847
+ continue;
848
+ }
849
+ const computedStyle = window.getComputedStyle(candidateElement);
850
+ if (!isElementVisible(candidateElement, computedStyle)) {
851
+ continue;
852
+ }
853
+ return candidateElement;
854
+ }
855
+ return null;
856
+ };
857
+ const getElementsInMarquee = (marqueeRect) => {
858
+ const elements = [];
859
+ const allElements = Array.from(document.querySelectorAll("*"));
860
+ const marqueeLeft = marqueeRect.x;
861
+ const marqueeTop = marqueeRect.y;
862
+ const marqueeRight = marqueeRect.x + marqueeRect.width;
863
+ const marqueeBottom = marqueeRect.y + marqueeRect.height;
864
+ for (const candidateElement of allElements) {
865
+ if (candidateElement.closest(`[${ATTRIBUTE_NAME}]`)) {
866
+ continue;
867
+ }
868
+ const computedStyle = window.getComputedStyle(candidateElement);
869
+ if (!isElementVisible(candidateElement, computedStyle)) {
870
+ continue;
871
+ }
872
+ const rect = candidateElement.getBoundingClientRect();
873
+ const elementLeft = rect.left;
874
+ const elementTop = rect.top;
875
+ const elementRight = rect.left + rect.width;
876
+ const elementBottom = rect.top + rect.height;
877
+ const intersectionLeft = Math.max(marqueeLeft, elementLeft);
878
+ const intersectionTop = Math.max(marqueeTop, elementTop);
879
+ const intersectionRight = Math.min(marqueeRight, elementRight);
880
+ const intersectionBottom = Math.min(marqueeBottom, elementBottom);
881
+ const intersectionWidth = Math.max(0, intersectionRight - intersectionLeft);
882
+ const intersectionHeight = Math.max(0, intersectionBottom - intersectionTop);
883
+ const intersectionArea = intersectionWidth * intersectionHeight;
884
+ const elementArea = Math.max(0, rect.width * rect.height);
885
+ const COVERAGE_THRESHOLD = 0.75;
886
+ const hasMajorityCoverage = elementArea > 0 && intersectionArea / elementArea >= COVERAGE_THRESHOLD;
887
+ if (hasMajorityCoverage) {
888
+ elements.push(candidateElement);
889
+ }
740
890
  }
741
- const computedStyle = window.getComputedStyle(candidateElement);
742
- if (!isElementVisible(candidateElement, computedStyle)) {
743
- continue;
891
+ return elements;
892
+ };
893
+ const getElementsInMarqueeLoose = (marqueeRect) => {
894
+ const elements = [];
895
+ const allElements = Array.from(document.querySelectorAll("*"));
896
+ const marqueeLeft = marqueeRect.x;
897
+ const marqueeTop = marqueeRect.y;
898
+ const marqueeRight = marqueeRect.x + marqueeRect.width;
899
+ const marqueeBottom = marqueeRect.y + marqueeRect.height;
900
+ for (const candidateElement of allElements) {
901
+ if (candidateElement.closest(`[${ATTRIBUTE_NAME}]`)) {
902
+ continue;
903
+ }
904
+ const tag = (candidateElement.tagName || "").toUpperCase();
905
+ if (tag === "HTML" || tag === "BODY") continue;
906
+ const computedStyle = window.getComputedStyle(candidateElement);
907
+ if (!isElementVisible(candidateElement, computedStyle)) {
908
+ continue;
909
+ }
910
+ const rect = candidateElement.getBoundingClientRect();
911
+ const elementLeft = rect.left;
912
+ const elementTop = rect.top;
913
+ const elementRight = rect.left + rect.width;
914
+ const elementBottom = rect.top + rect.height;
915
+ const intersects = elementLeft < marqueeRight && elementRight > marqueeLeft && elementTop < marqueeBottom && elementBottom > marqueeTop;
916
+ if (intersects) {
917
+ elements.push(candidateElement);
918
+ }
744
919
  }
745
- return candidateElement;
746
- }
747
- return null;
748
- };
749
- const wrapInReferencedElement = (content) => `
920
+ return elements;
921
+ };
922
+ const wrapInReferencedElement = (content) => `
750
923
 
751
924
  <referenced_element>
752
925
  ${content}
753
926
  </referenced_element>`;
754
- const handleCopy = async (targetElement) => {
755
- const tagName = (targetElement.tagName || "").toLowerCase();
756
- const elementBounds = targetElement.getBoundingClientRect();
757
- const showSuccessIndicator = updateLabelToProcessing(
758
- overlayRoot,
759
- elementBounds.left,
760
- elementBounds.top
761
- );
762
- try {
763
- const elementHtml = getHTMLSnippet(targetElement);
764
- await copyContent(wrapInReferencedElement(elementHtml));
765
- const componentStackTrace = await getSourceTrace(targetElement);
766
- if (componentStackTrace?.length) {
767
- const formattedStackTrace = componentStackTrace.map(
768
- (source) => ` ${source.functionName} - ${source.fileName}:${source.lineNumber}:${source.columnNumber}`
769
- ).join("\n");
770
- await copyContent(
771
- wrapInReferencedElement(
772
- `${elementHtml}
927
+ const wrapInReferencedElements = (content) => `
928
+
929
+ <referenced_elements>
930
+ ${content}
931
+ </referenced_elements>`;
932
+ const addGrabbedOverlay = (bounds) => {
933
+ const id = `grabbed-${Date.now()}-${Math.random()}`;
934
+ setGrabbedOverlays((prev) => [...prev, {
935
+ id,
936
+ bounds
937
+ }]);
938
+ setTimeout(() => {
939
+ setGrabbedOverlays((prev) => prev.filter((overlay) => overlay.id !== id));
940
+ }, 300);
941
+ };
942
+ const addSuccessLabel = (text, x, y) => {
943
+ const id = `success-${Date.now()}-${Math.random()}`;
944
+ setSuccessLabels((prev) => [...prev, {
945
+ id,
946
+ text,
947
+ x,
948
+ y
949
+ }]);
950
+ setTimeout(() => {
951
+ setSuccessLabels((prev) => prev.filter((label) => label.id !== id));
952
+ }, 1700);
953
+ };
954
+ const handleCopy = async (targetElement2) => {
955
+ const elementBounds = targetElement2.getBoundingClientRect();
956
+ const tagName = (targetElement2.tagName || "").toLowerCase();
957
+ addGrabbedOverlay({
958
+ borderRadius: window.getComputedStyle(targetElement2).borderRadius || "0px",
959
+ height: elementBounds.height,
960
+ transform: window.getComputedStyle(targetElement2).transform || "none",
961
+ width: elementBounds.width,
962
+ x: elementBounds.left,
963
+ y: elementBounds.top
964
+ });
965
+ try {
966
+ const elementHtml = getHTMLSnippet(targetElement2);
967
+ await copyContent(wrapInReferencedElement(elementHtml));
968
+ const componentStackTrace = await getSourceTrace(targetElement2);
969
+ if (componentStackTrace?.length) {
970
+ const formattedStackTrace = componentStackTrace.map((source) => ` ${source.functionName} - ${source.fileName}:${source.lineNumber}:${source.columnNumber}`).join("\n");
971
+ await copyContent(wrapInReferencedElement(`${elementHtml}
773
972
 
774
973
  Component owner stack:
775
- ${formattedStackTrace}`
776
- )
777
- );
974
+ ${formattedStackTrace}`));
975
+ }
976
+ } catch {
778
977
  }
779
- showSuccessIndicator(tagName);
780
- } catch {
781
- showSuccessIndicator(tagName);
782
- }
783
- };
784
- const hideOverlayAndLabel = () => {
785
- selectionOverlay?.hide();
786
- if (!isCopying) hideLabel();
787
- };
788
- const handleRender = () => {
789
- if (!isActive) {
790
- hideOverlayAndLabel();
791
- hoveredElement = null;
792
- lastGrabbedElement = null;
793
- return;
794
- }
795
- if (isCopying) return;
796
- const targetElement = getElementAtPosition(mouseX, mouseY);
797
- if (!targetElement) {
798
- hideOverlayAndLabel();
799
- hoveredElement = null;
800
- return;
801
- }
802
- if (lastGrabbedElement) {
803
- if (targetElement !== lastGrabbedElement) {
804
- lastGrabbedElement = null;
805
- } else {
806
- hideOverlayAndLabel();
807
- hoveredElement = targetElement;
978
+ addSuccessLabel(tagName ? `<${tagName}>` : "<element>", elementBounds.left, elementBounds.top);
979
+ };
980
+ const handleMultipleCopy = async (targetElements) => {
981
+ if (targetElements.length === 0) return;
982
+ let minX = Infinity;
983
+ let minY = Infinity;
984
+ for (const element of targetElements) {
985
+ const elementBounds = element.getBoundingClientRect();
986
+ minX = Math.min(minX, elementBounds.left);
987
+ minY = Math.min(minY, elementBounds.top);
988
+ addGrabbedOverlay({
989
+ borderRadius: window.getComputedStyle(element).borderRadius || "0px",
990
+ height: elementBounds.height,
991
+ transform: window.getComputedStyle(element).transform || "none",
992
+ width: elementBounds.width,
993
+ x: elementBounds.left,
994
+ y: elementBounds.top
995
+ });
996
+ }
997
+ try {
998
+ const elementSnippets = [];
999
+ for (const element of targetElements) {
1000
+ const elementHtml = getHTMLSnippet(element);
1001
+ const componentStackTrace = await getSourceTrace(element);
1002
+ if (componentStackTrace?.length) {
1003
+ const formattedStackTrace = componentStackTrace.map((source) => ` ${source.functionName} - ${source.fileName}:${source.lineNumber}:${source.columnNumber}`).join("\n");
1004
+ elementSnippets.push(`${elementHtml}
1005
+
1006
+ Component owner stack:
1007
+ ${formattedStackTrace}`);
1008
+ } else {
1009
+ elementSnippets.push(elementHtml);
1010
+ }
1011
+ }
1012
+ const combinedContent = elementSnippets.join("\n\n---\n\n");
1013
+ await copyContent(wrapInReferencedElements(combinedContent));
1014
+ } catch {
1015
+ }
1016
+ addSuccessLabel(`${targetElements.length} elements`, minX, minY);
1017
+ };
1018
+ const targetElement = createMemo(() => {
1019
+ if (!isOverlayActive() || isDragging()) return null;
1020
+ return getElementAtPosition(mouseX(), mouseY());
1021
+ });
1022
+ const selectionBounds = createMemo(() => {
1023
+ const element = targetElement();
1024
+ if (!element) return void 0;
1025
+ const last = lastGrabbedElement();
1026
+ if (last && element === last) return void 0;
1027
+ const elementBounds = element.getBoundingClientRect();
1028
+ const computedStyle = window.getComputedStyle(element);
1029
+ return {
1030
+ borderRadius: computedStyle.borderRadius || "0px",
1031
+ height: elementBounds.height,
1032
+ transform: computedStyle.transform || "none",
1033
+ width: elementBounds.width,
1034
+ x: elementBounds.left,
1035
+ y: elementBounds.top
1036
+ };
1037
+ });
1038
+ const marqueeBounds = createMemo(() => {
1039
+ if (!isDragging()) return void 0;
1040
+ const marqueeX = Math.min(dragStartX(), mouseX());
1041
+ const marqueeY = Math.min(dragStartY(), mouseY());
1042
+ const marqueeWidth = Math.abs(mouseX() - dragStartX());
1043
+ const marqueeHeight = Math.abs(mouseY() - dragStartY());
1044
+ return {
1045
+ borderRadius: "0px",
1046
+ height: marqueeHeight,
1047
+ transform: "none",
1048
+ width: marqueeWidth,
1049
+ x: marqueeX,
1050
+ y: marqueeY
1051
+ };
1052
+ });
1053
+ const labelText = createMemo(() => {
1054
+ const element = targetElement();
1055
+ if (!element) return "";
1056
+ const tagName = (element.tagName || "").toLowerCase();
1057
+ return tagName ? `<${tagName}>` : "<element>";
1058
+ });
1059
+ const labelPosition = createMemo(() => {
1060
+ const element = targetElement() ?? lastGrabbedElement();
1061
+ if (element) {
1062
+ const rect = element.getBoundingClientRect();
1063
+ return {
1064
+ x: rect.left,
1065
+ y: rect.top
1066
+ };
1067
+ }
1068
+ return {
1069
+ x: mouseX(),
1070
+ y: mouseY()
1071
+ };
1072
+ });
1073
+ const isSameAsLast = createMemo(() => {
1074
+ const current = targetElement();
1075
+ const last = lastGrabbedElement();
1076
+ return !!current && current === last;
1077
+ });
1078
+ createEffect(() => {
1079
+ const current = targetElement();
1080
+ const last = lastGrabbedElement();
1081
+ if (last && current && last !== current) {
1082
+ setLastGrabbedElement(null);
1083
+ }
1084
+ });
1085
+ const progress = createMemo(() => {
1086
+ const startTime = progressStartTime();
1087
+ progressTick();
1088
+ if (startTime === null) return 0;
1089
+ const elapsed = Date.now() - startTime;
1090
+ return Math.min(elapsed / options.keyHoldDuration, 1);
1091
+ });
1092
+ const startProgressAnimation = () => {
1093
+ setProgressStartTime(Date.now());
1094
+ setShowProgressIndicator(false);
1095
+ progressDelayTimerId = window.setTimeout(() => {
1096
+ setShowProgressIndicator(true);
1097
+ progressDelayTimerId = null;
1098
+ }, 150);
1099
+ const animateProgress = () => {
1100
+ if (progressStartTime() === null) return;
1101
+ setProgressTick((t) => t + 1);
1102
+ const currentProgress = progress();
1103
+ if (currentProgress < 1) {
1104
+ progressAnimationId = requestAnimationFrame(animateProgress);
1105
+ }
1106
+ };
1107
+ animateProgress();
1108
+ };
1109
+ const stopProgressAnimation = () => {
1110
+ if (progressAnimationId !== null) {
1111
+ cancelAnimationFrame(progressAnimationId);
1112
+ progressAnimationId = null;
1113
+ }
1114
+ if (progressDelayTimerId !== null) {
1115
+ window.clearTimeout(progressDelayTimerId);
1116
+ progressDelayTimerId = null;
1117
+ }
1118
+ setProgressStartTime(null);
1119
+ setShowProgressIndicator(false);
1120
+ };
1121
+ const activateOverlay = () => {
1122
+ stopProgressAnimation();
1123
+ setIsActivated(true);
1124
+ };
1125
+ const abortController = new AbortController();
1126
+ const signal = abortController.signal;
1127
+ window.addEventListener("keydown", (event) => {
1128
+ if (event.key === "Escape" && isHoldingKeys()) {
1129
+ setIsHoldingKeys(false);
1130
+ setIsActivated(false);
1131
+ if (holdTimerId) window.clearTimeout(holdTimerId);
1132
+ stopProgressAnimation();
808
1133
  return;
809
1134
  }
810
- }
811
- hoveredElement = targetElement;
812
- const elementBounds = targetElement.getBoundingClientRect();
813
- const computedStyle = window.getComputedStyle(targetElement);
814
- selectionOverlay?.update({
815
- borderRadius: computedStyle.borderRadius || "0px",
816
- height: elementBounds.height,
817
- transform: computedStyle.transform || "none",
818
- width: elementBounds.width,
819
- x: elementBounds.left,
820
- y: elementBounds.top
1135
+ if (isKeyboardEventTriggeredByInput(event)) return;
1136
+ if (isTargetKeyCombination(event) && !isHoldingKeys()) {
1137
+ setIsHoldingKeys(true);
1138
+ startProgressAnimation();
1139
+ holdTimerId = window.setTimeout(() => {
1140
+ activateOverlay();
1141
+ options.onActivate?.();
1142
+ }, options.keyHoldDuration);
1143
+ }
1144
+ }, {
1145
+ signal
821
1146
  });
822
- if (!selectionOverlay?.isVisible()) selectionOverlay?.show();
823
- showLabel(
824
- overlayRoot,
825
- elementBounds.left,
826
- elementBounds.top,
827
- (targetElement.tagName || "").toLowerCase()
828
- );
829
- };
830
- const scheduleRender = () => {
831
- if (renderFrameId !== null) return;
832
- renderFrameId = requestAnimationFrame(() => {
833
- renderFrameId = null;
834
- handleRender();
1147
+ window.addEventListener("keyup", (event) => {
1148
+ if (isHoldingKeys() && (!isTargetKeyCombination(event) || event.key.toLowerCase() === "c")) {
1149
+ setIsHoldingKeys(false);
1150
+ setIsActivated(false);
1151
+ if (holdTimerId) window.clearTimeout(holdTimerId);
1152
+ stopProgressAnimation();
1153
+ }
1154
+ }, {
1155
+ signal
835
1156
  });
836
- };
837
- const continuousRender = () => {
838
- scheduleRender();
839
- requestAnimationFrame(continuousRender);
840
- };
841
- const handleMouseMove = (event) => {
842
- mouseX = event.clientX;
843
- mouseY = event.clientY;
844
- scheduleRender();
845
- };
846
- const handleVisibilityChange = () => {
847
- if (document.hidden) {
848
- cleanupGrabbedIndicators();
849
- hideLabel();
850
- }
851
- };
852
- const handleSelectionClick = () => {
853
- if (!hoveredElement || isCopying) return;
854
- isCopying = true;
855
- lastGrabbedElement = hoveredElement;
856
- const targetElement = hoveredElement;
857
- const computedStyle = window.getComputedStyle(targetElement);
858
- const elementBounds = targetElement.getBoundingClientRect();
859
- createGrabbedOverlay(overlayRoot, {
860
- borderRadius: computedStyle.borderRadius || "0px",
861
- height: elementBounds.height,
862
- transform: computedStyle.transform || "none",
863
- width: elementBounds.width,
864
- x: elementBounds.left,
865
- y: elementBounds.top
1157
+ window.addEventListener("mousemove", (event) => {
1158
+ setMouseX(event.clientX);
1159
+ setMouseY(event.clientY);
1160
+ }, {
1161
+ signal
866
1162
  });
867
- void handleCopy(targetElement).finally(() => {
868
- isCopying = false;
869
- isActive = isHoldingKeys;
1163
+ window.addEventListener("mousedown", (event) => {
1164
+ if (!isOverlayActive() || isCopying()) return;
1165
+ setIsDragging(true);
1166
+ setDragStartX(event.clientX);
1167
+ setDragStartY(event.clientY);
1168
+ }, {
1169
+ signal
870
1170
  });
871
- };
872
- const activateOverlay = () => {
873
- if (!overlayRoot) {
874
- overlayRoot = mountRoot();
875
- selectionOverlay = createSelectionOverlay(
876
- overlayRoot,
877
- handleSelectionClick
878
- );
879
- continuousRender();
880
- }
881
- isActive = true;
882
- handleRender();
883
- };
884
- const handleKeyDown = (event) => {
885
- if (event.key === "Escape" && isHoldingKeys) {
886
- isHoldingKeys = false;
887
- if (holdTimer) window.clearTimeout(holdTimer);
888
- isActive = false;
889
- return;
890
- }
891
- if (isKeyboardEventTriggeredByInput(event)) return;
892
- if (isTargetKeyCombination(event) && !isHoldingKeys) {
893
- isHoldingKeys = true;
894
- holdTimer = window.setTimeout(() => {
895
- activateOverlay();
896
- options.onActivate?.();
897
- }, options.keyHoldDuration);
898
- }
899
- };
900
- const handleKeyUp = (event) => {
901
- if (isHoldingKeys && (!isTargetKeyCombination(event) || event.key.toLowerCase() === "c")) {
902
- isHoldingKeys = false;
903
- if (holdTimer) window.clearTimeout(holdTimer);
904
- isActive = false;
905
- }
906
- };
907
- window.addEventListener("keydown", handleKeyDown);
908
- window.addEventListener("keyup", handleKeyUp);
909
- window.addEventListener("mousemove", handleMouseMove);
910
- window.addEventListener("scroll", scheduleRender, true);
911
- window.addEventListener("resize", scheduleRender);
912
- document.addEventListener("visibilitychange", handleVisibilityChange);
913
- return () => {
914
- window.removeEventListener("keydown", handleKeyDown);
915
- window.removeEventListener("keyup", handleKeyUp);
916
- window.removeEventListener("mousemove", handleMouseMove);
917
- window.removeEventListener("scroll", scheduleRender, true);
918
- window.removeEventListener("resize", scheduleRender);
919
- document.removeEventListener("visibilitychange", handleVisibilityChange);
920
- if (holdTimer) window.clearTimeout(holdTimer);
921
- if (renderFrameId) cancelAnimationFrame(renderFrameId);
922
- cleanupGrabbedIndicators();
923
- hideLabel();
924
- };
1171
+ window.addEventListener("mouseup", (event) => {
1172
+ if (!isDragging()) return;
1173
+ const dragDistanceX = Math.abs(event.clientX - dragStartX());
1174
+ const dragDistanceY = Math.abs(event.clientY - dragStartY());
1175
+ const DRAG_THRESHOLD = 5;
1176
+ const wasDrag = dragDistanceX > DRAG_THRESHOLD || dragDistanceY > DRAG_THRESHOLD;
1177
+ setIsDragging(false);
1178
+ if (wasDrag) {
1179
+ const marqueeX = Math.min(dragStartX(), event.clientX);
1180
+ const marqueeY = Math.min(dragStartY(), event.clientY);
1181
+ const marqueeWidth = Math.abs(event.clientX - dragStartX());
1182
+ const marqueeHeight = Math.abs(event.clientY - dragStartY());
1183
+ const elements = getElementsInMarquee({
1184
+ x: marqueeX,
1185
+ y: marqueeY,
1186
+ width: marqueeWidth,
1187
+ height: marqueeHeight
1188
+ });
1189
+ if (elements.length > 0) {
1190
+ setIsCopying(true);
1191
+ void handleMultipleCopy(elements).finally(() => {
1192
+ setIsCopying(false);
1193
+ });
1194
+ } else {
1195
+ const fallbackElements = getElementsInMarqueeLoose({
1196
+ x: marqueeX,
1197
+ y: marqueeY,
1198
+ width: marqueeWidth,
1199
+ height: marqueeHeight
1200
+ });
1201
+ if (fallbackElements.length > 0) {
1202
+ setIsCopying(true);
1203
+ void handleMultipleCopy(fallbackElements).finally(() => {
1204
+ setIsCopying(false);
1205
+ });
1206
+ }
1207
+ }
1208
+ } else {
1209
+ const element = getElementAtPosition(event.clientX, event.clientY);
1210
+ if (!element) return;
1211
+ setIsCopying(true);
1212
+ setLastGrabbedElement(element);
1213
+ void handleCopy(element).finally(() => {
1214
+ setIsCopying(false);
1215
+ });
1216
+ }
1217
+ }, {
1218
+ signal
1219
+ });
1220
+ window.addEventListener("scroll", () => {
1221
+ }, {
1222
+ signal,
1223
+ capture: true
1224
+ });
1225
+ window.addEventListener("resize", () => {
1226
+ }, {
1227
+ signal
1228
+ });
1229
+ document.addEventListener("visibilitychange", () => {
1230
+ if (document.hidden) {
1231
+ setGrabbedOverlays([]);
1232
+ }
1233
+ }, {
1234
+ signal
1235
+ });
1236
+ onCleanup(() => {
1237
+ abortController.abort();
1238
+ if (holdTimerId) window.clearTimeout(holdTimerId);
1239
+ stopProgressAnimation();
1240
+ });
1241
+ const overlayRoot = mountRoot();
1242
+ const overlayProps = createMemo(() => ({
1243
+ selectionVisible: isOverlayActive() && !isDragging() && !!selectionBounds(),
1244
+ selectionBounds: selectionBounds(),
1245
+ marqueeVisible: isOverlayActive() && isDragging(),
1246
+ marqueeBounds: marqueeBounds(),
1247
+ grabbedOverlays: grabbedOverlays(),
1248
+ successLabels: successLabels(),
1249
+ labelVariant: isCopying() ? "processing" : "hover",
1250
+ labelText: labelText(),
1251
+ labelX: labelPosition().x,
1252
+ labelY: labelPosition().y,
1253
+ labelVisible: isOverlayActive() && !isDragging() && !!targetElement() && !isSameAsLast() || isCopying(),
1254
+ progressVisible: isHoldingKeys() && showProgressIndicator(),
1255
+ progress: progress(),
1256
+ mouseX: mouseX(),
1257
+ mouseY: mouseY()
1258
+ }));
1259
+ render(() => createComponent(ReactGrabOverlay, mergeProps(overlayProps)), overlayRoot);
1260
+ return dispose;
1261
+ });
925
1262
  };
926
1263
 
927
1264
  // src/index.ts