context_drop 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  import type { ElementCandidateInfo, ElementReferenceSnapshot } from "./types.ts";
2
2
  import {
3
3
  captureElementSnapshot,
4
- computeSelector,
5
4
  describeElementCandidate,
6
5
  isSelectableElement,
7
6
  resolveAncestorChain,
@@ -9,48 +8,101 @@ import {
9
8
  } from "./sanitizer.ts";
10
9
  import {
11
10
  ICON_CHECK,
12
- ICON_CHEVRON_DOWN,
13
- ICON_CHEVRON_RIGHT,
14
- ICON_CHEVRON_UP,
15
- ICON_CLOSE,
16
11
  ICON_LAYERS,
17
- ICON_PIN
12
+ ICON_PIN,
13
+ ICON_SCROLL_VERTICAL
18
14
  } from "./icons.ts";
19
15
 
16
+ export interface PinnedElementEntry {
17
+ clientRef: string;
18
+ selector: string;
19
+ element: Element;
20
+ }
21
+
22
+ export interface ElementPickerStateSnapshot {
23
+ active: boolean;
24
+ anchored: boolean;
25
+ scrubbing: boolean;
26
+ activeCandidate: ElementCandidateInfo | null;
27
+ candidateStack: ElementCandidateInfo[];
28
+ ancestors: Element[];
29
+ lastAction?: {
30
+ kind: "pinned" | "unpinned";
31
+ selector: string;
32
+ } | null;
33
+ }
34
+
20
35
  export interface ElementPickerCallbacks {
21
36
  onSelect: (snapshot: ElementReferenceSnapshot, element: Element) => void;
37
+ onUnselect?: (clientRef: string, element: Element) => void;
22
38
  onExit: () => void;
23
39
  canSelectMore: () => boolean;
24
40
  getExcludedSelectors: () => string[];
25
- onStateChange?: (state: {
26
- active: boolean;
27
- anchored: boolean;
28
- activeCandidate: ElementCandidateInfo | null;
29
- candidateStack: ElementCandidateInfo[];
30
- ancestors: Element[];
31
- }) => void;
41
+ getPinnedElements?: () => PinnedElementEntry[];
42
+ onStateChange?: (state: ElementPickerStateSnapshot) => void;
43
+ }
44
+
45
+ const WHEEL_STEP_THRESHOLD = 26;
46
+ const WHEEL_STEP_COOLDOWN_MS = 80;
47
+ const SCRUB_JITTER_LOCK_PX = 28;
48
+ const MAX_VISIBLE_STACK_ROWS = 5;
49
+
50
+ function isDomElementNode(node: unknown): node is Element {
51
+ if (!node || typeof node !== "object") return false;
52
+ if (typeof window !== "undefined" && node === window) return false;
53
+ if ((node as { nodeType?: number }).nodeType !== 1) return false;
54
+ const tag = ((node as { tagName?: string }).tagName || "").toUpperCase();
55
+ return tag.length > 0 && tag !== "WINDOW";
56
+ }
57
+
58
+ function formatCandidateLabel(tagName: string, selector: string): string {
59
+ if (selector.startsWith(tagName)) {
60
+ return selector;
61
+ }
62
+ if (selector.startsWith("#") || selector.startsWith(".")) {
63
+ return `${tagName}${selector}`;
64
+ }
65
+ return selector;
66
+ }
67
+
68
+ function isPointInsideRect(
69
+ x: number,
70
+ y: number,
71
+ rect: { left?: number; top?: number; x?: number; y?: number; width: number; height: number },
72
+ tolerance: number = 2
73
+ ): boolean {
74
+ const left = (rect.left ?? rect.x ?? 0) - tolerance;
75
+ const top = (rect.top ?? rect.y ?? 0) - tolerance;
76
+ return x >= left && x <= left + rect.width + tolerance * 2 && y >= top && y <= top + rect.height + tolerance * 2;
32
77
  }
33
78
 
34
79
  export class ElementPicker {
35
80
  private active: boolean = false;
36
- private anchored: boolean = false;
37
- private anchorX: number = 0;
38
- private anchorY: number = 0;
81
+ private isScrubbingLayers: boolean = false;
82
+ private scrubOriginX: number = 0;
83
+ private scrubOriginY: number = 0;
39
84
  private lastPointerX: number = 0;
40
85
  private lastPointerY: number = 0;
41
86
  private wheelAccumulator: number = 0;
87
+ private lastWheelStepAt: number = 0;
42
88
 
43
89
  private callbacks: ElementPickerCallbacks;
44
90
  private overlayEl: HTMLDivElement | null = null;
45
91
  private leafOutlineEl: HTMLDivElement | null = null;
46
- private labelEl: HTMLDivElement | null = null;
92
+ private hudEl: HTMLDivElement | null = null;
47
93
  private pinMarkerEl: HTMLDivElement | null = null;
48
- private popoverEl: HTMLDivElement | null = null;
94
+ private pinnedMarkersContainerEl: HTMLDivElement | null = null;
95
+ private toastEl: HTMLDivElement | null = null;
49
96
  private previewOverlayEl: HTMLDivElement | null = null;
50
97
 
98
+ private pulseTimer: ReturnType<typeof setTimeout> | null = null;
99
+ private nudgeTimer: ReturnType<typeof setTimeout> | null = null;
100
+ private toastTimer: ReturnType<typeof setTimeout> | null = null;
101
+
51
102
  private candidateStack: ElementCandidateInfo[] = [];
52
103
  private activeCandidateIndex: number = 0;
53
104
  private leafElement: Element | null = null;
105
+ private lastAction: { kind: "pinned" | "unpinned"; selector: string } | null = null;
54
106
  private previousCursor: string = "";
55
107
 
56
108
  private boundMouseMove: (e: MouseEvent) => void;
@@ -67,7 +119,7 @@ export class ElementPicker {
67
119
  this.boundPointerCapture = this.handlePointerSuppression.bind(this);
68
120
  this.boundWheelCapture = this.handleWheelCapture.bind(this);
69
121
  this.boundKeyDown = this.handleKeyDown.bind(this);
70
- this.boundScrollOrResize = this.updateOverlayPosition.bind(this);
122
+ this.boundScrollOrResize = this.handleViewportScrollOrResize.bind(this);
71
123
  }
72
124
 
73
125
  public isActive(): boolean {
@@ -75,7 +127,11 @@ export class ElementPicker {
75
127
  }
76
128
 
77
129
  public isAnchored(): boolean {
78
- return this.anchored;
130
+ return false;
131
+ }
132
+
133
+ public isScrubbing(): boolean {
134
+ return this.isScrubbingLayers;
79
135
  }
80
136
 
81
137
  public getActiveCandidate(): ElementCandidateInfo | null {
@@ -91,10 +147,13 @@ export class ElementPicker {
91
147
  return;
92
148
  }
93
149
  this.active = true;
94
- this.anchored = false;
150
+ this.isScrubbingLayers = false;
151
+ this.wheelAccumulator = 0;
152
+ this.lastWheelStepAt = 0;
95
153
  this.candidateStack = [];
96
154
  this.activeCandidateIndex = 0;
97
155
  this.leafElement = null;
156
+ this.lastAction = null;
98
157
 
99
158
  this.hideExternalPreview();
100
159
 
@@ -104,6 +163,7 @@ export class ElementPicker {
104
163
  }
105
164
 
106
165
  this.mountSelectionUi();
166
+ this.refreshPinnedMarkers();
107
167
  this.notifyStateChange();
108
168
 
109
169
  window.addEventListener("mousemove", this.boundMouseMove, { capture: true, passive: true });
@@ -120,10 +180,13 @@ export class ElementPicker {
120
180
  public stop(): void {
121
181
  if (!this.active) return;
122
182
  this.active = false;
123
- this.anchored = false;
183
+ this.isScrubbingLayers = false;
184
+ this.wheelAccumulator = 0;
124
185
  this.candidateStack = [];
125
186
  this.activeCandidateIndex = 0;
126
187
  this.leafElement = null;
188
+ this.lastAction = null;
189
+ this.clearTimers();
127
190
 
128
191
  if (typeof document !== "undefined" && document.body) {
129
192
  document.body.style.cursor = this.previousCursor;
@@ -146,9 +209,6 @@ export class ElementPicker {
146
209
  this.callbacks.onExit();
147
210
  }
148
211
 
149
- /**
150
- * Allows the widget or breadcrumb bar to select or preview a specific DOM element directly.
151
- */
152
212
  public focusCandidateElement(el: Element): void {
153
213
  if (!this.active) return;
154
214
  const excluded = this.callbacks.getExcludedSelectors();
@@ -163,10 +223,10 @@ export class ElementPicker {
163
223
  this.activeCandidateIndex = this.candidateStack.length - 1;
164
224
  }
165
225
 
226
+ this.isScrubbingLayers = this.candidateStack.length > 1;
227
+ this.scrubOriginX = this.lastPointerX;
228
+ this.scrubOriginY = this.lastPointerY;
166
229
  this.updateOverlayPosition();
167
- if (this.anchored) {
168
- this.renderLayerPopover();
169
- }
170
230
  this.notifyStateChange();
171
231
  }
172
232
 
@@ -176,16 +236,75 @@ export class ElementPicker {
176
236
  if (!snapshot) return false;
177
237
 
178
238
  this.callbacks.onSelect(snapshot, el);
179
- this.closeLayerPopover();
239
+ this.lastAction = { kind: "pinned", selector: snapshot.selector };
240
+ this.isScrubbingLayers = false;
241
+ this.refreshPinnedMarkers();
180
242
 
181
243
  if (!this.callbacks.canSelectMore()) {
182
244
  this.stop();
183
245
  } else {
246
+ this.updateOverlayPosition();
184
247
  this.notifyStateChange();
185
248
  }
186
249
  return true;
187
250
  }
188
251
 
252
+ public refreshPinnedMarkers(): void {
253
+ if (!this.active || !this.pinnedMarkersContainerEl) return;
254
+ this.pinnedMarkersContainerEl.innerHTML = "";
255
+
256
+ const pinnedEntries = this.callbacks.getPinnedElements ? this.callbacks.getPinnedElements() : [];
257
+ pinnedEntries.forEach((entry, index) => {
258
+ if (!entry.element || typeof entry.element.getBoundingClientRect !== "function") return;
259
+ const rect = entry.element.getBoundingClientRect();
260
+ if (rect.width <= 0 || rect.height <= 0) return;
261
+
262
+ const markerFrame = document.createElement("div");
263
+ markerFrame.setAttribute("data-context-drop-ui", "pinned-badge");
264
+ markerFrame.style.cssText = [
265
+ "position: fixed",
266
+ "pointer-events: none",
267
+ `left: ${rect.left}px`,
268
+ `top: ${rect.top}px`,
269
+ `width: ${rect.width}px`,
270
+ `height: ${rect.height}px`,
271
+ "border: 1.5px dashed rgba(194, 65, 12, 0.72)",
272
+ "background: rgba(194, 65, 12, 0.04)",
273
+ "border-radius: 6px"
274
+ ].join(";");
275
+
276
+ const badge = document.createElement("div");
277
+ const badgeTop = rect.top < 26 ? "4px" : "-22px";
278
+ badge.style.cssText = [
279
+ "position: absolute",
280
+ `top: ${badgeTop}`,
281
+ "right: -1px",
282
+ "display: inline-flex",
283
+ "align-items: center",
284
+ "gap: 4px",
285
+ "padding: 2px 6px",
286
+ "border-radius: 9999px",
287
+ "background: #c2410c",
288
+ "color: #faf9f6",
289
+ "border: 1px solid #faf9f6",
290
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
291
+ "font-size: 10px",
292
+ "font-weight: 700",
293
+ "font-variant-numeric: tabular-nums",
294
+ "line-height: 1.2",
295
+ "max-width: 160px",
296
+ "overflow: hidden",
297
+ "text-overflow: ellipsis",
298
+ "white-space: nowrap",
299
+ "box-shadow: 0 4px 10px -2px rgba(17, 19, 24, 0.25)"
300
+ ].join(";");
301
+ badge.textContent = `#${index + 1} ${entry.selector}`;
302
+
303
+ markerFrame.appendChild(badge);
304
+ this.pinnedMarkersContainerEl?.appendChild(markerFrame);
305
+ });
306
+ }
307
+
189
308
  public showExternalPreview(el: Element | null): void {
190
309
  if (typeof document === "undefined" || !document.body) return;
191
310
  if (!el || this.active) {
@@ -229,22 +348,60 @@ export class ElementPicker {
229
348
  this.previewOverlayEl = null;
230
349
  }
231
350
 
232
- public cycleDepth(delta: number): void {
351
+ public cycleDepth(delta: number, revealStack: boolean = true): void {
233
352
  if (!this.active || this.candidateStack.length <= 1) return;
353
+
354
+ const wasScrubbing = this.isScrubbingLayers;
355
+ if (revealStack) {
356
+ this.isScrubbingLayers = true;
357
+ this.scrubOriginX = this.lastPointerX;
358
+ this.scrubOriginY = this.lastPointerY;
359
+ }
360
+
234
361
  const next = Math.max(
235
362
  0,
236
363
  Math.min(this.candidateStack.length - 1, this.activeCandidateIndex + delta)
237
364
  );
365
+
238
366
  if (next !== this.activeCandidateIndex) {
239
367
  this.activeCandidateIndex = next;
240
368
  this.updateOverlayPosition();
241
- if (this.anchored) {
242
- this.renderLayerPopover();
243
- }
244
369
  this.notifyStateChange();
370
+ } else if (!wasScrubbing && this.isScrubbingLayers) {
371
+ this.updateOverlayPosition();
372
+ this.triggerHudBoundaryNudge(delta < 0 ? -3 : 3);
373
+ this.notifyStateChange();
374
+ } else {
375
+ this.triggerHudBoundaryNudge(delta < 0 ? -3 : 3);
245
376
  }
246
377
  }
247
378
 
379
+ private clearTimers(): void {
380
+ if (this.pulseTimer !== null) {
381
+ clearTimeout(this.pulseTimer);
382
+ this.pulseTimer = null;
383
+ }
384
+ if (this.nudgeTimer !== null) {
385
+ clearTimeout(this.nudgeTimer);
386
+ this.nudgeTimer = null;
387
+ }
388
+ if (this.toastTimer !== null) {
389
+ clearTimeout(this.toastTimer);
390
+ this.toastTimer = null;
391
+ }
392
+ }
393
+
394
+ private findPinnedIndex(el: Element): { index: number; entry: PinnedElementEntry } | null {
395
+ if (!this.callbacks.getPinnedElements) return null;
396
+ const pinned = this.callbacks.getPinnedElements();
397
+ for (let i = 0; i < pinned.length; i += 1) {
398
+ if (pinned[i].element === el) {
399
+ return { index: i, entry: pinned[i] };
400
+ }
401
+ }
402
+ return null;
403
+ }
404
+
248
405
  private notifyStateChange(): void {
249
406
  if (!this.callbacks.onStateChange) return;
250
407
  const activeCandidate = this.getActiveCandidate();
@@ -253,16 +410,27 @@ export class ElementPicker {
253
410
  : [];
254
411
  this.callbacks.onStateChange({
255
412
  active: this.active,
256
- anchored: this.anchored,
413
+ anchored: false,
414
+ scrubbing: this.isScrubbingLayers,
257
415
  activeCandidate,
258
416
  candidateStack: [...this.candidateStack],
259
- ancestors
417
+ ancestors,
418
+ lastAction: this.lastAction
260
419
  });
261
420
  }
262
421
 
263
422
  private mountSelectionUi(): void {
264
423
  if (typeof document === "undefined" || !document.body) return;
265
424
 
425
+ const pinnedMarkers = document.createElement("div");
426
+ pinnedMarkers.setAttribute("data-context-drop-ui", "pinned-markers");
427
+ pinnedMarkers.style.cssText = [
428
+ "position: fixed",
429
+ "inset: 0",
430
+ "pointer-events: none",
431
+ "z-index: 2147483643"
432
+ ].join(";");
433
+
266
434
  // Secondary ghost outline showing the innermost leaf element when cycling up to parent/overlapping layers
267
435
  const leafOutline = document.createElement("div");
268
436
  leafOutline.setAttribute("data-context-drop-ui", "leaf-outline");
@@ -287,40 +455,36 @@ export class ElementPicker {
287
455
  "background: rgba(29, 78, 216, 0.10)",
288
456
  "border-radius: 6px",
289
457
  "display: none",
290
- "transition: left 0.06s cubic-bezier(0.22, 1, 0.36, 1), top 0.06s cubic-bezier(0.22, 1, 0.36, 1), width 0.06s cubic-bezier(0.22, 1, 0.36, 1), height 0.06s cubic-bezier(0.22, 1, 0.36, 1)"
458
+ "transition: left 0.06s cubic-bezier(0.22, 1, 0.36, 1), top 0.06s cubic-bezier(0.22, 1, 0.36, 1), width 0.06s cubic-bezier(0.22, 1, 0.36, 1), height 0.06s cubic-bezier(0.22, 1, 0.36, 1), border-color 0.16s ease, background-color 0.16s ease"
291
459
  ].join(";");
292
460
 
293
- // Floating HUD badge on the highlight box
294
- const label = document.createElement("div");
295
- label.setAttribute("data-context-drop-ui", "label");
296
- label.style.cssText = [
297
- "position: absolute",
298
- "top: -30px",
299
- "left: -2px",
461
+ // Viewport-clamped Floating HUD (Compact 2-line coach on hover, inline 5-row wheel-scrubbed stack on scroll)
462
+ const hud = document.createElement("div");
463
+ hud.setAttribute("data-context-drop-ui", "label");
464
+ hud.style.cssText = [
465
+ "position: fixed",
466
+ "z-index: 2147483646",
300
467
  "background: #111318",
301
468
  "color: #faf9f6",
302
- "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
303
- "font-size: 11px",
304
- "font-variant-numeric: tabular-nums",
305
- "line-height: 1.3",
306
- "padding: 4px 8px",
307
- "border-radius: 6px",
308
- "white-space: nowrap",
469
+ "border: 1px solid rgba(250, 249, 246, 0.16)",
470
+ "border-radius: 10px",
309
471
  "pointer-events: none",
310
- "display: inline-flex",
311
- "align-items: center",
312
- "gap: 6px",
313
- "box-shadow: 0 8px 18px -4px rgba(17, 19, 24, 0.28)"
472
+ "display: none",
473
+ "overflow: hidden",
474
+ "font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif",
475
+ "font-size: 11.5px",
476
+ "line-height: 1.35",
477
+ "box-shadow: 0 16px 34px -8px rgba(17, 19, 24, 0.40), 0 6px 14px -4px rgba(17, 19, 24, 0.22)",
478
+ "transition: transform 0.12s cubic-bezier(0.22, 1, 0.36, 1)"
314
479
  ].join(";");
315
- overlay.appendChild(label);
316
480
 
317
- // Floating Pin marker that follows the pointer or anchors at the clicked coordinate
481
+ // Floating Pin marker that follows the pointer
318
482
  const pinMarker = document.createElement("div");
319
483
  pinMarker.setAttribute("data-context-drop-ui", "pin-marker");
320
484
  pinMarker.style.cssText = [
321
485
  "position: fixed",
322
486
  "pointer-events: none",
323
- "z-index: 2147483646",
487
+ "z-index: 2147483647",
324
488
  "width: 28px",
325
489
  "height: 28px",
326
490
  "margin-left: -6px",
@@ -337,55 +501,68 @@ export class ElementPicker {
337
501
  ].join(";");
338
502
  pinMarker.innerHTML = ICON_PIN;
339
503
 
340
- // Layer & Hierarchy Disambiguation Popover (anchors when clicking overlapping/cascading elements)
341
- const popover = document.createElement("div");
342
- popover.setAttribute("data-context-drop-ui", "layer-popover");
343
- popover.style.cssText = [
504
+ // Tactile floating confirmation pill near cursor on pin / unpin
505
+ const toast = document.createElement("div");
506
+ toast.setAttribute("data-context-drop-ui", "pin-toast");
507
+ toast.style.cssText = [
344
508
  "position: fixed",
509
+ "pointer-events: none",
345
510
  "z-index: 2147483647",
346
- "width: 348px",
347
- "max-width: calc(100vw - 24px)",
348
- "background: #faf9f6",
349
- "color: #111318",
350
- "border: 1px solid #d6d3cd",
351
- "border-radius: 12px",
352
- "box-shadow: 0 22px 44px -12px rgba(17, 19, 24, 0.26), 0 6px 14px -4px rgba(17, 19, 24, 0.10)",
353
- "font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
354
- "font-size: 12px",
355
511
  "display: none",
356
- "overflow: hidden",
357
- "pointer-events: auto"
512
+ "align-items: center",
513
+ "gap: 6px",
514
+ "padding: 6px 10px",
515
+ "border-radius: 9999px",
516
+ "background: #111318",
517
+ "color: #faf9f6",
518
+ "border: 1px solid rgba(250, 249, 246, 0.18)",
519
+ "font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif",
520
+ "font-size: 11.5px",
521
+ "font-weight: 500",
522
+ "white-space: nowrap",
523
+ "box-shadow: 0 12px 24px -6px rgba(17, 19, 24, 0.34)"
358
524
  ].join(";");
359
525
 
526
+ document.body.appendChild(pinnedMarkers);
360
527
  document.body.appendChild(leafOutline);
361
528
  document.body.appendChild(overlay);
529
+ document.body.appendChild(hud);
362
530
  document.body.appendChild(pinMarker);
363
- document.body.appendChild(popover);
531
+ document.body.appendChild(toast);
364
532
 
533
+ this.pinnedMarkersContainerEl = pinnedMarkers;
365
534
  this.leafOutlineEl = leafOutline;
366
535
  this.overlayEl = overlay;
367
- this.labelEl = label;
536
+ this.hudEl = hud;
368
537
  this.pinMarkerEl = pinMarker;
369
- this.popoverEl = popover;
538
+ this.toastEl = toast;
370
539
  }
371
540
 
372
541
  private unmountSelectionUi(): void {
373
- for (const el of [this.leafOutlineEl, this.overlayEl, this.pinMarkerEl, this.popoverEl]) {
542
+ for (const el of [
543
+ this.pinnedMarkersContainerEl,
544
+ this.leafOutlineEl,
545
+ this.overlayEl,
546
+ this.hudEl,
547
+ this.pinMarkerEl,
548
+ this.toastEl
549
+ ]) {
374
550
  if (el && el.parentNode) {
375
551
  el.parentNode.removeChild(el);
376
552
  }
377
553
  }
554
+ this.pinnedMarkersContainerEl = null;
378
555
  this.leafOutlineEl = null;
379
556
  this.overlayEl = null;
380
- this.labelEl = null;
557
+ this.hudEl = null;
381
558
  this.pinMarkerEl = null;
382
- this.popoverEl = null;
559
+ this.toastEl = null;
383
560
  }
384
561
 
385
562
  private isInternalUiEvent(event: Event): boolean {
386
563
  const path = typeof event.composedPath === "function" ? event.composedPath() : [];
387
564
  for (const node of path) {
388
- if (node instanceof Element) {
565
+ if (isDomElementNode(node)) {
389
566
  if (
390
567
  node.tagName.toLowerCase() === "context-drop-widget" ||
391
568
  node.hasAttribute("data-context-drop-ui") ||
@@ -396,7 +573,7 @@ export class ElementPicker {
396
573
  }
397
574
  }
398
575
  }
399
- const rawTarget = event.target instanceof Element ? event.target : null;
576
+ const rawTarget = isDomElementNode(event.target) ? event.target : null;
400
577
  if (
401
578
  rawTarget &&
402
579
  typeof rawTarget.closest === "function" &&
@@ -413,7 +590,7 @@ export class ElementPicker {
413
590
  try {
414
591
  const fromPoint = document.elementsFromPoint(clientX, clientY);
415
592
  for (const el of fromPoint) {
416
- if (el instanceof Element) {
593
+ if (isDomElementNode(el)) {
417
594
  results.push(el);
418
595
  }
419
596
  }
@@ -423,12 +600,12 @@ export class ElementPicker {
423
600
  }
424
601
  if (event && typeof event.composedPath === "function") {
425
602
  for (const node of event.composedPath()) {
426
- if (node instanceof Element && !results.includes(node)) {
603
+ if (isDomElementNode(node) && !results.includes(node)) {
427
604
  results.push(node);
428
605
  }
429
606
  }
430
607
  }
431
- if (event && event.target instanceof Element && !results.includes(event.target)) {
608
+ if (event && isDomElementNode(event.target) && !results.includes(event.target)) {
432
609
  results.unshift(event.target);
433
610
  }
434
611
  return results;
@@ -443,17 +620,39 @@ export class ElementPicker {
443
620
  this.leafElement = null;
444
621
  this.candidateStack = [];
445
622
  this.activeCandidateIndex = 0;
623
+ this.isScrubbingLayers = false;
446
624
  return;
447
625
  }
448
626
 
449
- // Preserve depth selection if user is hovering inside the exact same primary leaf element
627
+ // Preserve depth & inline stack when pointer stays inside the exact same primary leaf element
450
628
  if (this.leafElement === primary && this.candidateStack.length > 0) {
451
629
  return;
452
630
  }
453
631
 
632
+ // Pointer-jitter lock: if user scrubbed to a specific layer and nudges the mouse slightly (<=28px)
633
+ // across child boundaries inside that selected element right before clicking, keep it locked.
634
+ if (this.isScrubbingLayers && this.candidateStack.length > 0) {
635
+ const lockedCandidate = this.getActiveCandidate();
636
+ const travel = Math.hypot(clientX - this.scrubOriginX, clientY - this.scrubOriginY);
637
+ if (
638
+ lockedCandidate &&
639
+ travel <= SCRUB_JITTER_LOCK_PX &&
640
+ (pointElements.includes(lockedCandidate.element) ||
641
+ isPointInsideRect(clientX, clientY, lockedCandidate.element.getBoundingClientRect()))
642
+ ) {
643
+ return;
644
+ }
645
+ }
646
+
454
647
  this.leafElement = primary;
455
648
  this.candidateStack = resolveCandidateStack(primary, pointElements, excluded, 10);
456
649
  this.activeCandidateIndex = 0;
650
+ this.isScrubbingLayers = false;
651
+ }
652
+
653
+ private handleViewportScrollOrResize(): void {
654
+ this.refreshPinnedMarkers();
655
+ this.updateOverlayPosition();
457
656
  }
458
657
 
459
658
  private handleMouseMove(event: MouseEvent): void {
@@ -462,12 +661,9 @@ export class ElementPicker {
462
661
  this.lastPointerX = event.clientX;
463
662
  this.lastPointerY = event.clientY;
464
663
 
465
- if (this.anchored) {
466
- return;
467
- }
468
-
469
664
  if (this.isInternalUiEvent(event)) {
470
665
  if (this.overlayEl) this.overlayEl.style.display = "none";
666
+ if (this.hudEl) this.hudEl.style.display = "none";
471
667
  if (this.leafOutlineEl) this.leafOutlineEl.style.display = "none";
472
668
  if (this.pinMarkerEl) this.pinMarkerEl.style.display = "none";
473
669
  return;
@@ -484,484 +680,697 @@ export class ElementPicker {
484
680
  this.notifyStateChange();
485
681
  }
486
682
 
487
- private updateOverlayPosition(): void {
488
- if (!this.overlayEl || !this.labelEl) return;
489
-
490
- const currentCandidate = this.getActiveCandidate();
491
- if (!currentCandidate) {
492
- this.overlayEl.style.display = "none";
493
- if (this.leafOutlineEl) this.leafOutlineEl.style.display = "none";
494
- return;
495
- }
496
-
497
- const rect = currentCandidate.element.getBoundingClientRect();
498
- if (rect.width <= 0 || rect.height <= 0) {
499
- this.overlayEl.style.display = "none";
500
- return;
501
- }
683
+ private createSpanEl(text: string): HTMLSpanElement {
684
+ const span = document.createElement("span");
685
+ span.textContent = text;
686
+ return span;
687
+ }
502
688
 
503
- this.overlayEl.style.display = "block";
504
- this.overlayEl.style.left = `${rect.left}px`;
505
- this.overlayEl.style.top = `${rect.top}px`;
506
- this.overlayEl.style.width = `${rect.width}px`;
507
- this.overlayEl.style.height = `${rect.height}px`;
689
+ private createKbdEl(text: string): HTMLElement {
690
+ const kbd = document.createElement("kbd");
691
+ kbd.textContent = text;
692
+ kbd.style.cssText = [
693
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
694
+ "font-size: 10px",
695
+ "font-weight: 600",
696
+ "line-height: 1.1",
697
+ "padding: 1px 4px",
698
+ "border-radius: 4px",
699
+ "background: rgba(250, 249, 246, 0.14)",
700
+ "border: 1px solid rgba(250, 249, 246, 0.24)",
701
+ "color: #faf9f6"
702
+ ].join(";");
703
+ return kbd;
704
+ }
508
705
 
509
- // Position the HUD pill inside the top of the box if the element is near the top of the viewport
510
- if (rect.top < 36) {
511
- this.labelEl.style.top = "6px";
512
- this.labelEl.style.left = "6px";
513
- } else {
514
- this.labelEl.style.top = "-30px";
515
- this.labelEl.style.left = "-2px";
516
- }
706
+ private renderCompactHudContent(
707
+ currentCandidate: ElementCandidateInfo,
708
+ rect: { width: number; height: number }
709
+ ): void {
710
+ if (!this.hudEl) return;
711
+ this.hudEl.innerHTML = "";
712
+ this.hudEl.style.width = "auto";
713
+ this.hudEl.style.maxWidth = "min(440px, calc(100vw - 20px))";
517
714
 
518
715
  const total = this.candidateStack.length;
519
716
  const pos = this.activeCandidateIndex + 1;
520
717
  const dims = `${Math.round(rect.width)}×${Math.round(rect.height)}`;
521
- const depthBadge = total > 1 ? ` · ${pos}/${total} layers (↕ scroll)` : "";
522
- this.labelEl.textContent = `${currentCandidate.tagName}${currentCandidate.selector.startsWith(currentCandidate.tagName) ? currentCandidate.selector.slice(currentCandidate.tagName.length) : ` ${currentCandidate.selector}`} · ${dims}${depthBadge}`;
523
-
524
- // Show ghost leaf outline when inspecting an ancestor or overlapping layer
525
- if (this.leafOutlineEl) {
526
- if (this.activeCandidateIndex > 0 && this.leafElement && this.leafElement !== currentCandidate.element) {
527
- const leafRect = this.leafElement.getBoundingClientRect();
528
- this.leafOutlineEl.style.display = "block";
529
- this.leafOutlineEl.style.left = `${leafRect.left}px`;
530
- this.leafOutlineEl.style.top = `${leafRect.top}px`;
531
- this.leafOutlineEl.style.width = `${leafRect.width}px`;
532
- this.leafOutlineEl.style.height = `${leafRect.height}px`;
533
- } else {
534
- this.leafOutlineEl.style.display = "none";
535
- }
536
- }
537
- }
538
-
539
- private handleWheelCapture(event: WheelEvent): void {
540
- if (!this.active) return;
541
-
542
- // Allow natural scrolling inside the layer popover or widget itself
543
- if (this.isInternalUiEvent(event)) {
544
- return;
545
- }
718
+ const pinnedInfo = this.findPinnedIndex(currentCandidate.element);
719
+ const hasOverlap = this.candidateStack.some((c) => c.relation === "overlap");
546
720
 
547
- if (this.candidateStack.length <= 1) {
548
- return;
549
- }
550
-
551
- event.preventDefault();
552
- event.stopPropagation();
553
-
554
- this.wheelAccumulator += event.deltaY;
555
- if (Math.abs(this.wheelAccumulator) >= 24) {
556
- const step = this.wheelAccumulator > 0 ? 1 : -1;
557
- this.wheelAccumulator = 0;
558
- this.cycleDepth(step);
559
- }
560
- }
721
+ // Top Row: Identity + Dimensions + Layer Count
722
+ const topRow = document.createElement("div");
723
+ topRow.style.cssText = [
724
+ "display: flex",
725
+ "align-items: center",
726
+ "gap: 6px",
727
+ "padding: 6px 9px 5px",
728
+ "white-space: nowrap"
729
+ ].join(";");
561
730
 
562
- private handlePointerSuppression(event: Event): void {
563
- if (!this.active) return;
564
- if (this.isInternalUiEvent(event)) {
565
- return;
731
+ if (pinnedInfo) {
732
+ const pinnedPill = document.createElement("span");
733
+ pinnedPill.textContent = `#${pinnedInfo.index + 1} Pinned`;
734
+ pinnedPill.style.cssText = [
735
+ "padding: 1px 5px",
736
+ "border-radius: 4px",
737
+ "background: #c2410c",
738
+ "color: #faf9f6",
739
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
740
+ "font-size: 10px",
741
+ "font-weight: 700",
742
+ "font-variant-numeric: tabular-nums",
743
+ "flex-shrink: 0"
744
+ ].join(";");
745
+ topRow.appendChild(pinnedPill);
566
746
  }
567
747
 
568
- // Intercept host page pointer events in capture phase so links/buttons do not fire
569
- event.preventDefault();
570
- event.stopPropagation();
571
- if (typeof event.stopImmediatePropagation === "function") {
572
- event.stopImmediatePropagation();
573
- }
574
- }
748
+ const selectorSpan = document.createElement("span");
749
+ selectorSpan.textContent = formatCandidateLabel(
750
+ currentCandidate.tagName,
751
+ currentCandidate.selector
752
+ );
753
+ selectorSpan.style.cssText = [
754
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
755
+ "font-size: 11.5px",
756
+ "font-weight: 600",
757
+ "color: #faf9f6",
758
+ "max-width: 230px",
759
+ "overflow: hidden",
760
+ "text-overflow: ellipsis",
761
+ "white-space: nowrap"
762
+ ].join(";");
763
+ topRow.appendChild(selectorSpan);
575
764
 
576
- private handleClickCapture(event: MouseEvent): void {
577
- if (!this.active) return;
578
- if (this.isInternalUiEvent(event)) {
579
- return;
580
- }
765
+ const dimsSpan = document.createElement("span");
766
+ dimsSpan.textContent = dims;
767
+ dimsSpan.style.cssText = [
768
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
769
+ "font-size: 10.5px",
770
+ "font-variant-numeric: tabular-nums",
771
+ "color: #cbd5e1",
772
+ "flex-shrink: 0"
773
+ ].join(";");
774
+ topRow.appendChild(dimsSpan);
581
775
 
582
- event.preventDefault();
583
- event.stopPropagation();
584
- if (typeof event.stopImmediatePropagation === "function") {
585
- event.stopImmediatePropagation();
776
+ if (total > 1) {
777
+ const layerPill = document.createElement("span");
778
+ layerPill.style.cssText = [
779
+ "display: inline-flex",
780
+ "align-items: center",
781
+ "gap: 3px",
782
+ "padding: 1px 6px",
783
+ "border-radius: 9999px",
784
+ "background: rgba(29, 78, 216, 0.36)",
785
+ "border: 1px solid rgba(96, 165, 250, 0.45)",
786
+ "color: #dbeafe",
787
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
788
+ "font-size: 10px",
789
+ "font-weight: 600",
790
+ "font-variant-numeric: tabular-nums",
791
+ "flex-shrink: 0"
792
+ ].join(";");
793
+ const iconWrap = document.createElement("span");
794
+ iconWrap.style.cssText = "display: inline-flex; width: 11px; height: 11px; align-items: center;";
795
+ iconWrap.innerHTML = ICON_LAYERS;
796
+ const countText = document.createElement("span");
797
+ countText.textContent = `${pos}/${total}`;
798
+ layerPill.appendChild(iconWrap);
799
+ layerPill.appendChild(countText);
800
+ topRow.appendChild(layerPill);
586
801
  }
587
802
 
588
- // If popover was already anchored and user clicked elsewhere on the page, re-evaluate at the new click point
589
- this.anchorX = event.clientX;
590
- this.anchorY = event.clientY;
591
- this.updateCandidatesFromPoint(event.clientX, event.clientY, event);
803
+ this.hudEl.appendChild(topRow);
592
804
 
593
- const activeCandidate = this.getActiveCandidate();
594
- if (!activeCandidate) return;
805
+ // Bottom Row: Action Coaching Strip
806
+ const coachRow = document.createElement("div");
807
+ coachRow.style.cssText = [
808
+ "display: flex",
809
+ "align-items: center",
810
+ "gap: 8px",
811
+ "padding: 4px 9px 5px",
812
+ "background: rgba(250, 249, 246, 0.05)",
813
+ "border-top: 1px solid rgba(250, 249, 246, 0.10)",
814
+ "font-size: 10.5px",
815
+ "color: #cbd5e1",
816
+ "white-space: nowrap"
817
+ ].join(";");
595
818
 
596
- // Fast-path: Shift+Click or single-layer target pins immediately
597
- if (event.shiftKey || this.candidateStack.length === 1) {
598
- this.selectElementDirectly(activeCandidate.element);
599
- return;
819
+ const clickHint = document.createElement("span");
820
+ const clickStrong = document.createElement("strong");
821
+ clickStrong.style.cssText = pinnedInfo
822
+ ? "color: #ffedd5; font-weight: 600;"
823
+ : "color: #faf9f6; font-weight: 600;";
824
+ clickStrong.textContent = "Click";
825
+ clickHint.appendChild(clickStrong);
826
+ clickHint.appendChild(this.createSpanEl(pinnedInfo ? " to unpin" : " to pin"));
827
+ coachRow.appendChild(clickHint);
828
+
829
+ if (total > 1) {
830
+ const scrollHint = document.createElement("span");
831
+ scrollHint.style.cssText = "display: inline-flex; align-items: center; gap: 3px;";
832
+ const wheelIcon = document.createElement("span");
833
+ wheelIcon.style.cssText = "display: inline-flex; color: #dbeafe;";
834
+ wheelIcon.innerHTML = ICON_SCROLL_VERTICAL;
835
+ const scrollStrong = document.createElement("strong");
836
+ scrollStrong.style.cssText = hasOverlap
837
+ ? "color: #dbeafe; font-weight: 600;"
838
+ : "color: #faf9f6; font-weight: 600;";
839
+ scrollStrong.textContent = "Scroll wheel";
840
+ scrollHint.appendChild(wheelIcon);
841
+ scrollHint.appendChild(scrollStrong);
842
+ scrollHint.appendChild(
843
+ this.createSpanEl(hasOverlap ? " for element behind" : ` for ${total} layers`)
844
+ );
845
+ coachRow.appendChild(scrollHint);
600
846
  }
601
847
 
602
- // Otherwise anchor the floating pin and open the Layer & Hierarchy Disambiguation Popover
603
- this.anchored = true;
604
- if (this.pinMarkerEl) {
605
- this.pinMarkerEl.style.display = "inline-flex";
606
- this.pinMarkerEl.style.left = `${this.anchorX}px`;
607
- this.pinMarkerEl.style.top = `${this.anchorY}px`;
608
- this.pinMarkerEl.style.transform = "scale(1.08)";
609
- }
610
- this.updateOverlayPosition();
611
- this.renderLayerPopover();
612
- this.notifyStateChange();
613
- }
848
+ const escHint = document.createElement("span");
849
+ escHint.style.cssText = "display: inline-flex; align-items: center; gap: 4px;";
850
+ escHint.appendChild(this.createKbdEl("Esc"));
851
+ escHint.appendChild(this.createSpanEl("done"));
852
+ coachRow.appendChild(escHint);
614
853
 
615
- public closeLayerPopover(): void {
616
- this.anchored = false;
617
- if (this.popoverEl) {
618
- this.popoverEl.style.display = "none";
619
- this.popoverEl.innerHTML = "";
620
- }
621
- if (this.pinMarkerEl) {
622
- this.pinMarkerEl.style.transform = "scale(1)";
623
- this.pinMarkerEl.style.left = `${this.lastPointerX}px`;
624
- this.pinMarkerEl.style.top = `${this.lastPointerY}px`;
625
- }
626
- this.notifyStateChange();
854
+ this.hudEl.appendChild(coachRow);
627
855
  }
628
856
 
629
- private renderLayerPopover(): void {
630
- if (!this.popoverEl || typeof window === "undefined") return;
631
- const activeCandidate = this.getActiveCandidate();
632
- if (!activeCandidate) return;
857
+ private renderScrubbingHudContent(
858
+ currentCandidate: ElementCandidateInfo,
859
+ rect: { width: number; height: number }
860
+ ): void {
861
+ if (!this.hudEl) return;
862
+ this.hudEl.innerHTML = "";
863
+ this.hudEl.style.width = "320px";
864
+ this.hudEl.style.maxWidth = "calc(100vw - 20px)";
633
865
 
634
- this.popoverEl.innerHTML = "";
635
- this.popoverEl.style.display = "block";
866
+ const total = this.candidateStack.length;
867
+ const pos = this.activeCandidateIndex + 1;
636
868
 
637
- // Header: title + Pin Current CTA + close popover
869
+ // Header Strip
638
870
  const header = document.createElement("div");
639
871
  header.style.cssText = [
640
872
  "display: flex",
641
873
  "align-items: center",
642
874
  "justify-content: space-between",
643
875
  "gap: 8px",
644
- "padding: 10px 12px",
645
- "background: #f1efea",
646
- "border-bottom: 1px solid #d6d3cd"
876
+ "padding: 6px 10px",
877
+ "background: rgba(250, 249, 246, 0.05)",
878
+ "border-bottom: 1px solid rgba(250, 249, 246, 0.12)"
647
879
  ].join(";");
648
880
 
649
- const titleGroup = document.createElement("div");
650
- titleGroup.style.cssText = "display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 12px; color: #111318;";
651
- const layersIconSpan = document.createElement("span");
652
- layersIconSpan.style.cssText = "display: inline-flex; color: #c2410c;";
653
- layersIconSpan.innerHTML = ICON_LAYERS;
654
- const titleText = document.createElement("span");
655
- titleText.textContent = `Choose Element Layer (${this.candidateStack.length})`;
656
- titleGroup.appendChild(layersIconSpan);
657
- titleGroup.appendChild(titleText);
658
-
659
- const headerActions = document.createElement("div");
660
- headerActions.style.cssText = "display: flex; align-items: center; gap: 4px;";
661
-
662
- const stepUpBtn = document.createElement("button");
663
- stepUpBtn.type = "button";
664
- stepUpBtn.title = "Step to parent/outer layer (Up arrow)";
665
- stepUpBtn.setAttribute("aria-label", "Step to outer layer");
666
- stepUpBtn.style.cssText = [
667
- "display: inline-flex",
668
- "align-items: center",
669
- "justify-content: center",
670
- "width: 24px",
671
- "height: 24px",
672
- "border-radius: 6px",
673
- "border: 1px solid #d6d3cd",
674
- "background: #faf9f6",
675
- "color: #111318",
676
- "cursor: pointer"
677
- ].join(";");
678
- stepUpBtn.innerHTML = ICON_CHEVRON_UP;
679
- stepUpBtn.disabled = this.activeCandidateIndex >= this.candidateStack.length - 1;
680
- stepUpBtn.addEventListener("click", (ev) => {
681
- ev.stopPropagation();
682
- this.cycleDepth(1);
683
- });
684
-
685
- const stepDownBtn = document.createElement("button");
686
- stepDownBtn.type = "button";
687
- stepDownBtn.title = "Step to inner/child layer (Down arrow)";
688
- stepDownBtn.setAttribute("aria-label", "Step to inner layer");
689
- stepDownBtn.style.cssText = stepUpBtn.style.cssText;
690
- stepDownBtn.innerHTML = ICON_CHEVRON_DOWN;
691
- stepDownBtn.disabled = this.activeCandidateIndex <= 0;
692
- stepDownBtn.addEventListener("click", (ev) => {
693
- ev.stopPropagation();
694
- this.cycleDepth(-1);
695
- });
696
-
697
- const closeBtn = document.createElement("button");
698
- closeBtn.type = "button";
699
- closeBtn.title = "Resume hover selection (Esc)";
700
- closeBtn.setAttribute("aria-label", "Close layer picker");
701
- closeBtn.style.cssText = [
702
- "display: inline-flex",
703
- "align-items: center",
704
- "justify-content: center",
705
- "width: 24px",
706
- "height: 24px",
707
- "border-radius: 6px",
708
- "border: 0",
709
- "background: transparent",
710
- "color: #4b5262",
711
- "cursor: pointer"
712
- ].join(";");
713
- closeBtn.innerHTML = ICON_CLOSE;
714
- closeBtn.addEventListener("click", (ev) => {
715
- ev.stopPropagation();
716
- this.closeLayerPopover();
717
- });
718
-
719
- headerActions.appendChild(stepDownBtn);
720
- headerActions.appendChild(stepUpBtn);
721
- headerActions.appendChild(closeBtn);
722
- header.appendChild(titleGroup);
723
- header.appendChild(headerActions);
724
- this.popoverEl.appendChild(header);
725
-
726
- // Interactive DOM Breadcrumb Bar
727
- const ancestors = resolveAncestorChain(
728
- activeCandidate.element,
729
- this.callbacks.getExcludedSelectors(),
730
- 6
731
- );
732
- if (ancestors.length > 0) {
733
- const breadcrumbBar = document.createElement("div");
734
- breadcrumbBar.setAttribute("data-context-drop-ui", "breadcrumb-bar");
735
- breadcrumbBar.style.cssText = [
736
- "display: flex",
737
- "align-items: center",
738
- "flex-wrap: wrap",
739
- "gap: 3px",
740
- "padding: 7px 12px",
741
- "background: #f5f3ee",
742
- "border-bottom: 1px solid #e4e1da",
743
- "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
744
- "font-size: 11px"
745
- ].join(";");
746
-
747
- ancestors.forEach((ancEl, idx) => {
748
- if (idx > 0) {
749
- const sep = document.createElement("span");
750
- sep.style.cssText = "display: inline-flex; color: #687082;";
751
- sep.innerHTML = ICON_CHEVRON_RIGHT;
752
- breadcrumbBar.appendChild(sep);
753
- }
754
-
755
- const isCurrent = ancEl === activeCandidate.element;
756
- const crumbBtn = document.createElement("button");
757
- crumbBtn.type = "button";
758
- crumbBtn.textContent = computeSelector(ancEl);
759
- crumbBtn.style.cssText = [
760
- "appearance: none",
761
- "border: 1px solid " + (isCurrent ? "#1d4ed8" : "transparent"),
762
- "background: " + (isCurrent ? "#dbeafe" : "transparent"),
763
- "color: " + (isCurrent ? "#1e3a8a" : "#4b5262"),
764
- "font-family: inherit",
765
- "font-size: 11px",
766
- "font-weight: " + (isCurrent ? "600" : "500"),
767
- "padding: 2px 5px",
768
- "border-radius: 4px",
769
- "cursor: pointer",
770
- "max-width: 130px",
771
- "overflow: hidden",
772
- "text-overflow: ellipsis",
773
- "white-space: nowrap"
774
- ].join(";");
775
-
776
- crumbBtn.addEventListener("mouseenter", () => {
777
- this.focusCandidateElement(ancEl);
778
- });
779
- crumbBtn.addEventListener("click", (ev) => {
780
- ev.stopPropagation();
781
- this.selectElementDirectly(ancEl);
782
- });
783
-
784
- breadcrumbBar.appendChild(crumbBtn);
785
- });
786
-
787
- this.popoverEl.appendChild(breadcrumbBar);
788
- }
789
-
790
- // Layer Stack List (Target, Overlapping siblings, Direct children, Ancestor containers)
791
- const list = document.createElement("div");
792
- list.style.cssText = [
793
- "max-height: 230px",
794
- "overflow-y: auto",
795
- "padding: 6px",
881
+ const headerLeft = document.createElement("div");
882
+ headerLeft.style.cssText =
883
+ "display: inline-flex; align-items: center; gap: 5px; font-size: 11px; font-weight: 600; color: #faf9f6;";
884
+ const layersIcon = document.createElement("span");
885
+ layersIcon.style.cssText = "display: inline-flex; color: #60a5fa;";
886
+ layersIcon.innerHTML = ICON_LAYERS;
887
+ const headerTitle = document.createElement("span");
888
+ headerTitle.textContent = "Select Layer";
889
+ headerLeft.appendChild(layersIcon);
890
+ headerLeft.appendChild(headerTitle);
891
+
892
+ const headerRight = document.createElement("div");
893
+ headerRight.style.cssText =
894
+ "display: inline-flex; align-items: center; gap: 5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 10.5px; font-variant-numeric: tabular-nums; color: #cbd5e1;";
895
+ const wheelSpan = document.createElement("span");
896
+ wheelSpan.style.cssText = "display: inline-flex; color: #dbeafe;";
897
+ wheelSpan.innerHTML = ICON_SCROLL_VERTICAL;
898
+ const ratioSpan = document.createElement("span");
899
+ ratioSpan.textContent = `${pos}/${total}`;
900
+ headerRight.appendChild(wheelSpan);
901
+ headerRight.appendChild(ratioSpan);
902
+
903
+ header.appendChild(headerLeft);
904
+ header.appendChild(headerRight);
905
+ this.hudEl.appendChild(header);
906
+
907
+ // Non-scrolling Sliding Window of Layers (max 5 rows)
908
+ const start =
909
+ total <= MAX_VISIBLE_STACK_ROWS
910
+ ? 0
911
+ : Math.max(0, Math.min(this.activeCandidateIndex - 2, total - MAX_VISIBLE_STACK_ROWS));
912
+ const end = Math.min(total, start + MAX_VISIBLE_STACK_ROWS);
913
+
914
+ const listWrap = document.createElement("div");
915
+ listWrap.setAttribute("data-context-drop-ui", "hud-layer-list");
916
+ listWrap.style.cssText = [
796
917
  "display: flex",
797
918
  "flex-direction: column",
798
- "gap: 3px"
919
+ "gap: 2px",
920
+ "padding: 5px"
799
921
  ].join(";");
800
922
 
923
+ if (start > 0) {
924
+ const aboveHint = document.createElement("div");
925
+ aboveHint.textContent = `${start} inner layer${start > 1 ? "s" : ""} above`;
926
+ aboveHint.style.cssText =
927
+ "font-size: 10px; color: #cbd5e1; padding: 1px 7px; font-variant-numeric: tabular-nums;";
928
+ listWrap.appendChild(aboveHint);
929
+ }
930
+
801
931
  const relationLabels: Record<string, string> = {
802
932
  target: "Topmost",
803
- overlap: "Overlap",
933
+ overlap: "Behind",
804
934
  child: "Child",
805
935
  ancestor: "Parent"
806
936
  };
807
937
 
808
- this.candidateStack.forEach((candidate, idx) => {
938
+ for (let idx = start; idx < end; idx += 1) {
939
+ const candidate = this.candidateStack[idx];
809
940
  const isSelected = idx === this.activeCandidateIndex;
810
- const row = document.createElement("button");
811
- row.type = "button";
941
+ const candPinned = this.findPinnedIndex(candidate.element);
942
+
943
+ const row = document.createElement("div");
812
944
  row.setAttribute("data-context-drop-layer-index", String(idx));
945
+ if (isSelected) {
946
+ row.setAttribute("data-selected", "true");
947
+ }
813
948
  row.style.cssText = [
814
- "appearance: none",
815
- "width: 100%",
816
- "text-align: left",
817
949
  "display: flex",
818
- "flex-direction: column",
819
- "gap: 3px",
820
- "padding: 7px 9px",
821
- "border-radius: 8px",
822
- "border: 1px solid " + (isSelected ? "#1d4ed8" : "transparent"),
823
- "background: " + (isSelected ? "#eff6ff" : "transparent"),
824
- "color: #111318",
825
- "cursor: pointer",
826
- "transition: background-color 0.1s ease"
950
+ "align-items: center",
951
+ "justify-content: space-between",
952
+ "gap: 6px",
953
+ "padding: " + (isSelected ? "5px 7px" : "4px 7px"),
954
+ "border-radius: 7px",
955
+ "border: 1px solid " + (isSelected ? "#60a5fa" : "transparent"),
956
+ "background: " + (isSelected ? "rgba(29, 78, 216, 0.34)" : "transparent")
827
957
  ].join(";");
828
958
 
829
- const topLine = document.createElement("div");
830
- topLine.style.cssText = "display: flex; align-items: center; justify-content: space-between; gap: 6px; width: 100%;";
831
-
832
959
  const leftGroup = document.createElement("div");
833
- leftGroup.style.cssText = "display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1;";
960
+ leftGroup.style.cssText = "display: flex; align-items: center; gap: 5px; min-width: 0; flex: 1;";
834
961
 
835
962
  const relBadge = document.createElement("span");
836
963
  relBadge.textContent = relationLabels[candidate.relation] || "Layer";
964
+ const relBg =
965
+ candidate.relation === "target"
966
+ ? "rgba(255, 237, 213, 0.16)"
967
+ : candidate.relation === "overlap"
968
+ ? "rgba(252, 231, 243, 0.20)"
969
+ : "rgba(226, 232, 240, 0.14)";
970
+ const relColor =
971
+ candidate.relation === "target"
972
+ ? "#ffedd5"
973
+ : candidate.relation === "overlap"
974
+ ? "#fee2e2"
975
+ : "#cbd5e1";
837
976
  relBadge.style.cssText = [
838
- "font-size: 10px",
977
+ "font-size: 9.5px",
839
978
  "font-weight: 600",
840
979
  "padding: 1px 5px",
841
980
  "border-radius: 4px",
842
- "background: " + (candidate.relation === "target" ? "#ffedd5" : candidate.relation === "overlap" ? "#fce7f3" : "#e2e8f0"),
843
- "color: " + (candidate.relation === "target" ? "#9a3412" : candidate.relation === "overlap" ? "#9d174d" : "#334155"),
981
+ `background: ${relBg}`,
982
+ `color: ${relColor}`,
844
983
  "flex-shrink: 0"
845
984
  ].join(";");
846
985
 
847
- const selectorSpan = document.createElement("span");
848
- selectorSpan.textContent = `${candidate.tagName}${candidate.selector.startsWith(candidate.tagName) ? candidate.selector.slice(candidate.tagName.length) : candidate.selector}`;
849
- selectorSpan.style.cssText = [
986
+ const selText = document.createElement("span");
987
+ selText.textContent = formatCandidateLabel(candidate.tagName, candidate.selector);
988
+ selText.style.cssText = [
850
989
  "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
851
- "font-size: 11.5px",
852
- "font-weight: 600",
853
- "color: #111318",
990
+ "font-size: 11px",
991
+ "font-weight: " + (isSelected ? "600" : "500"),
992
+ "color: " + (isSelected ? "#faf9f6" : "#e2e8f0"),
854
993
  "overflow: hidden",
855
994
  "text-overflow: ellipsis",
856
995
  "white-space: nowrap"
857
996
  ].join(";");
858
997
 
859
998
  leftGroup.appendChild(relBadge);
860
- leftGroup.appendChild(selectorSpan);
999
+ leftGroup.appendChild(selText);
861
1000
 
862
- const dimsSpan = document.createElement("span");
863
- dimsSpan.textContent = `${Math.round(candidate.rect.width)}×${Math.round(candidate.rect.height)}`;
864
- dimsSpan.style.cssText = [
865
- "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
866
- "font-size: 10.5px",
867
- "font-variant-numeric: tabular-nums",
868
- "color: #4b5262",
869
- "flex-shrink: 0"
870
- ].join(";");
871
-
872
- topLine.appendChild(leftGroup);
873
- topLine.appendChild(dimsSpan);
874
- row.appendChild(topLine);
875
-
876
- if (candidate.textPreview) {
877
- const previewLine = document.createElement("div");
878
- previewLine.textContent = `"${candidate.textPreview}"`;
879
- previewLine.style.cssText = [
880
- "font-size: 11px",
881
- "color: #4b5262",
882
- "overflow: hidden",
883
- "text-overflow: ellipsis",
884
- "white-space: nowrap",
885
- "padding-left: 2px"
1001
+ if (candPinned) {
1002
+ const pinTag = document.createElement("span");
1003
+ pinTag.textContent = `#${candPinned.index + 1}`;
1004
+ pinTag.style.cssText = [
1005
+ "padding: 0 4px",
1006
+ "border-radius: 4px",
1007
+ "background: #c2410c",
1008
+ "color: #faf9f6",
1009
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
1010
+ "font-size: 9.5px",
1011
+ "font-weight: 700",
1012
+ "flex-shrink: 0"
886
1013
  ].join(";");
887
- row.appendChild(previewLine);
1014
+ leftGroup.appendChild(pinTag);
888
1015
  }
889
1016
 
890
- row.addEventListener("mouseenter", () => {
891
- if (this.activeCandidateIndex !== idx) {
892
- this.activeCandidateIndex = idx;
893
- this.updateOverlayPosition();
894
- this.notifyStateChange();
895
- }
896
- });
1017
+ row.appendChild(leftGroup);
897
1018
 
898
- row.addEventListener("click", (ev) => {
899
- ev.stopPropagation();
900
- this.selectElementDirectly(candidate.element);
901
- });
1019
+ if (isSelected) {
1020
+ const actionBadge = document.createElement("span");
1021
+ actionBadge.textContent = candPinned ? "Click unpin" : "Click to pin";
1022
+ actionBadge.style.cssText = [
1023
+ "padding: 1px 6px",
1024
+ "border-radius: 4px",
1025
+ "background: " + (candPinned ? "rgba(250, 249, 246, 0.18)" : "#c2410c"),
1026
+ "color: #faf9f6",
1027
+ "font-size: 10px",
1028
+ "font-weight: 600",
1029
+ "flex-shrink: 0"
1030
+ ].join(";");
1031
+ row.appendChild(actionBadge);
1032
+ } else {
1033
+ const rowDims = document.createElement("span");
1034
+ rowDims.textContent = `${Math.round(candidate.rect.width)}×${Math.round(candidate.rect.height)}`;
1035
+ rowDims.style.cssText = [
1036
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
1037
+ "font-size: 10px",
1038
+ "font-variant-numeric: tabular-nums",
1039
+ "color: #cbd5e1",
1040
+ "flex-shrink: 0"
1041
+ ].join(";");
1042
+ row.appendChild(rowDims);
1043
+ }
902
1044
 
903
- list.appendChild(row);
904
- });
1045
+ listWrap.appendChild(row);
1046
+ }
1047
+
1048
+ if (end < total) {
1049
+ const belowHint = document.createElement("div");
1050
+ belowHint.textContent = `${total - end} parent layer${total - end > 1 ? "s" : ""} below`;
1051
+ belowHint.style.cssText =
1052
+ "font-size: 10px; color: #cbd5e1; padding: 1px 7px; font-variant-numeric: tabular-nums;";
1053
+ listWrap.appendChild(belowHint);
1054
+ }
905
1055
 
906
- this.popoverEl.appendChild(list);
1056
+ this.hudEl.appendChild(listWrap);
907
1057
 
908
- // Footer with primary Pin confirmation action
1058
+ // Footer Strip
909
1059
  const footer = document.createElement("div");
910
1060
  footer.style.cssText = [
911
1061
  "display: flex",
912
1062
  "align-items: center",
913
1063
  "justify-content: space-between",
914
- "padding: 8px 12px",
915
- "background: #f1efea",
916
- "border-top: 1px solid #d6d3cd"
1064
+ "gap: 8px",
1065
+ "padding: 5px 10px",
1066
+ "background: rgba(250, 249, 246, 0.04)",
1067
+ "border-top: 1px solid rgba(250, 249, 246, 0.10)",
1068
+ "font-size: 10.5px",
1069
+ "color: #cbd5e1"
917
1070
  ].join(";");
918
1071
 
919
- const hint = document.createElement("span");
920
- hint.textContent = "Click any layer or press Enter";
921
- hint.style.cssText = "font-size: 11px; color: #4b5262;";
1072
+ const dimsLabel = document.createElement("span");
1073
+ dimsLabel.textContent = `${Math.round(rect.width)}×${Math.round(rect.height)}`;
1074
+ dimsLabel.style.cssText =
1075
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 10px; font-variant-numeric: tabular-nums;";
1076
+
1077
+ const keysHint = document.createElement("span");
1078
+ keysHint.style.cssText = "display: inline-flex; align-items: center; gap: 4px;";
1079
+ keysHint.appendChild(this.createKbdEl("Enter"));
1080
+ keysHint.appendChild(this.createSpanEl("pin · "));
1081
+ keysHint.appendChild(this.createKbdEl("Esc"));
1082
+ keysHint.appendChild(this.createSpanEl("done"));
1083
+
1084
+ footer.appendChild(dimsLabel);
1085
+ footer.appendChild(keysHint);
1086
+ this.hudEl.appendChild(footer);
1087
+ }
1088
+
1089
+ private positionHudElement(rect: { left: number; top: number; width: number; height: number }): void {
1090
+ if (!this.hudEl || typeof window === "undefined") return;
1091
+
1092
+ const vw = window.innerWidth || 1024;
1093
+ const vh = window.innerHeight || 768;
1094
+ const fallbackWidth = this.isScrubbingLayers ? 320 : 300;
1095
+ const visibleRows = Math.min(this.candidateStack.length, MAX_VISIBLE_STACK_ROWS);
1096
+ const fallbackHeight = this.isScrubbingLayers ? 66 + visibleRows * 30 : 54;
1097
+
1098
+ const hudWidth = (this.hudEl as unknown as { offsetWidth?: number }).offsetWidth || fallbackWidth;
1099
+ const hudHeight = (this.hudEl as unknown as { offsetHeight?: number }).offsetHeight || fallbackHeight;
1100
+
1101
+ const clampedLeft = Math.max(10, Math.min(rect.left, vw - hudWidth - 10));
1102
+ let top = rect.top - hudHeight - 8;
1103
+
1104
+ if (top < 10) {
1105
+ const belowTop = rect.top + rect.height + 8;
1106
+ if (belowTop + hudHeight <= vh - 10) {
1107
+ top = belowTop;
1108
+ } else {
1109
+ // Large container spanning viewport: place near top-left of visible area, stepping below cursor if close
1110
+ const insideTop = Math.max(10, rect.top + 8);
1111
+ if (
1112
+ Math.abs(this.lastPointerY - insideTop) < hudHeight + 18 &&
1113
+ this.lastPointerX < clampedLeft + hudWidth + 24
1114
+ ) {
1115
+ top = Math.min(vh - hudHeight - 10, this.lastPointerY + 22);
1116
+ } else {
1117
+ top = insideTop;
1118
+ }
1119
+ }
1120
+ }
1121
+
1122
+ this.hudEl.style.left = `${Math.round(clampedLeft)}px`;
1123
+ this.hudEl.style.top = `${Math.round(Math.max(10, top))}px`;
1124
+ }
1125
+
1126
+ private updateOverlayPosition(): void {
1127
+ if (!this.overlayEl || !this.hudEl) return;
1128
+
1129
+ const currentCandidate = this.getActiveCandidate();
1130
+ if (!currentCandidate) {
1131
+ this.overlayEl.style.display = "none";
1132
+ this.hudEl.style.display = "none";
1133
+ if (this.leafOutlineEl) this.leafOutlineEl.style.display = "none";
1134
+ return;
1135
+ }
1136
+
1137
+ const rect = currentCandidate.element.getBoundingClientRect();
1138
+ if (rect.width <= 0 || rect.height <= 0) {
1139
+ this.overlayEl.style.display = "none";
1140
+ this.hudEl.style.display = "none";
1141
+ return;
1142
+ }
1143
+
1144
+ this.overlayEl.style.display = "block";
1145
+ this.overlayEl.style.left = `${rect.left}px`;
1146
+ this.overlayEl.style.top = `${rect.top}px`;
1147
+ this.overlayEl.style.width = `${rect.width}px`;
1148
+ this.overlayEl.style.height = `${rect.height}px`;
1149
+
1150
+ this.hudEl.style.display = "block";
1151
+ if (this.isScrubbingLayers && this.candidateStack.length > 1) {
1152
+ this.renderScrubbingHudContent(currentCandidate, rect);
1153
+ } else {
1154
+ this.renderCompactHudContent(currentCandidate, rect);
1155
+ }
1156
+ this.positionHudElement(rect);
1157
+
1158
+ // Show ghost leaf outline when inspecting an ancestor or overlapping layer
1159
+ if (this.leafOutlineEl) {
1160
+ if (
1161
+ this.activeCandidateIndex > 0 &&
1162
+ this.leafElement &&
1163
+ this.leafElement !== currentCandidate.element
1164
+ ) {
1165
+ const leafRect = this.leafElement.getBoundingClientRect();
1166
+ this.leafOutlineEl.style.display = "block";
1167
+ this.leafOutlineEl.style.left = `${leafRect.left}px`;
1168
+ this.leafOutlineEl.style.top = `${leafRect.top}px`;
1169
+ this.leafOutlineEl.style.width = `${leafRect.width}px`;
1170
+ this.leafOutlineEl.style.height = `${leafRect.height}px`;
1171
+ } else {
1172
+ this.leafOutlineEl.style.display = "none";
1173
+ }
1174
+ }
1175
+ }
1176
+
1177
+ private triggerHudBoundaryNudge(offsetY: number): void {
1178
+ if (!this.hudEl) return;
1179
+ this.hudEl.style.transform = `translateY(${offsetY}px)`;
1180
+ if (this.nudgeTimer !== null) {
1181
+ clearTimeout(this.nudgeTimer);
1182
+ }
1183
+ this.nudgeTimer = setTimeout(() => {
1184
+ if (this.hudEl) {
1185
+ this.hudEl.style.transform = "translateY(0)";
1186
+ }
1187
+ this.nudgeTimer = null;
1188
+ }, 120);
1189
+ }
922
1190
 
923
- const pinNowBtn = document.createElement("button");
924
- pinNowBtn.type = "button";
925
- pinNowBtn.setAttribute("data-context-drop-ui", "confirm-pin-btn");
926
- pinNowBtn.style.cssText = [
1191
+ private flashSelectionPulse(kind: "pinned" | "unpinned"): void {
1192
+ if (!this.overlayEl) return;
1193
+ if (kind === "pinned") {
1194
+ this.overlayEl.style.borderColor = "#15803d";
1195
+ this.overlayEl.style.background = "rgba(21, 128, 61, 0.14)";
1196
+ } else {
1197
+ this.overlayEl.style.borderColor = "#c2410c";
1198
+ this.overlayEl.style.background = "rgba(194, 65, 12, 0.12)";
1199
+ }
1200
+ if (this.pulseTimer !== null) {
1201
+ clearTimeout(this.pulseTimer);
1202
+ }
1203
+ this.pulseTimer = setTimeout(() => {
1204
+ if (this.overlayEl) {
1205
+ this.overlayEl.style.borderColor = "#1d4ed8";
1206
+ this.overlayEl.style.background = "rgba(29, 78, 216, 0.10)";
1207
+ }
1208
+ this.pulseTimer = null;
1209
+ }, 280);
1210
+ }
1211
+
1212
+ private showCursorToast(selector: string, kind: "pinned" | "unpinned", x: number, y: number): void {
1213
+ if (!this.toastEl || typeof window === "undefined") return;
1214
+ this.toastEl.innerHTML = "";
1215
+
1216
+ const iconBadge = document.createElement("span");
1217
+ iconBadge.style.cssText = [
927
1218
  "display: inline-flex",
928
1219
  "align-items: center",
929
- "gap: 5px",
930
- "padding: 5px 10px",
931
- "border-radius: 6px",
932
- "border: 0",
933
- "background: #c2410c",
1220
+ "justify-content: center",
1221
+ "width: 16px",
1222
+ "height: 16px",
1223
+ "border-radius: 9999px",
1224
+ "background: " + (kind === "pinned" ? "#15803d" : "#4b5262"),
934
1225
  "color: #faf9f6",
935
- "font-size: 11.5px",
936
- "font-weight: 600",
937
- "cursor: pointer"
1226
+ "flex-shrink: 0"
938
1227
  ].join(";");
939
- pinNowBtn.innerHTML = `${ICON_CHECK}<span>Pin Selected</span>`;
940
- pinNowBtn.addEventListener("click", (ev) => {
941
- ev.stopPropagation();
942
- const target = this.getActiveCandidate();
943
- if (target) {
944
- this.selectElementDirectly(target.element);
945
- }
946
- });
947
-
948
- footer.appendChild(hint);
949
- footer.appendChild(pinNowBtn);
950
- this.popoverEl.appendChild(footer);
1228
+ iconBadge.innerHTML = ICON_CHECK;
1229
+ this.toastEl.appendChild(iconBadge);
1230
+
1231
+ const primaryText = document.createElement("span");
1232
+ primaryText.textContent = kind === "pinned" ? "Pinned " : "Removed ";
1233
+ this.toastEl.appendChild(primaryText);
1234
+
1235
+ const monoSel = document.createElement("span");
1236
+ monoSel.textContent = selector;
1237
+ monoSel.style.cssText =
1238
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; font-weight: 600; color: #faf9f6; max-width: 160px; overflow: hidden; text-overflow: ellipsis;";
1239
+ this.toastEl.appendChild(monoSel);
1240
+
1241
+ if (kind === "pinned") {
1242
+ const nextHint = document.createElement("span");
1243
+ nextHint.style.cssText =
1244
+ "display: inline-flex; align-items: center; gap: 4px; color: #cbd5e1; font-size: 10.5px; margin-left: 2px;";
1245
+ nextHint.appendChild(this.createSpanEl("· Click more or"));
1246
+ nextHint.appendChild(this.createKbdEl("Esc"));
1247
+ this.toastEl.appendChild(nextHint);
1248
+ }
951
1249
 
952
- // Clamp popover inside viewport bounds near anchorX, anchorY
1250
+ this.toastEl.style.display = "inline-flex";
953
1251
  const vw = window.innerWidth || 1024;
954
1252
  const vh = window.innerHeight || 768;
955
- const popWidth = 348;
956
- const popHeight = 310;
957
- const left = Math.max(12, Math.min(this.anchorX + 14, vw - popWidth - 12));
958
- const top =
959
- this.anchorY + popHeight + 20 > vh
960
- ? Math.max(12, this.anchorY - popHeight - 12)
961
- : Math.max(12, this.anchorY + 14);
1253
+ const left = Math.max(10, Math.min(x + 14, vw - 310));
1254
+ const top = Math.max(10, Math.min(y + 16, vh - 44));
1255
+ this.toastEl.style.left = `${Math.round(left)}px`;
1256
+ this.toastEl.style.top = `${Math.round(top)}px`;
1257
+
1258
+ if (this.toastTimer !== null) {
1259
+ clearTimeout(this.toastTimer);
1260
+ }
1261
+ this.toastTimer = setTimeout(() => {
1262
+ if (this.toastEl) {
1263
+ this.toastEl.style.display = "none";
1264
+ }
1265
+ this.toastTimer = null;
1266
+ }, 1450);
1267
+ }
1268
+
1269
+ private commitCandidateSelection(el: Element, x: number, y: number): void {
1270
+ const existingPinned = this.findPinnedIndex(el);
1271
+ if (existingPinned && this.callbacks.onUnselect) {
1272
+ this.callbacks.onUnselect(existingPinned.entry.clientRef, el);
1273
+ this.lastAction = { kind: "unpinned", selector: existingPinned.entry.selector };
1274
+ this.isScrubbingLayers = false;
1275
+ this.refreshPinnedMarkers();
1276
+ this.flashSelectionPulse("unpinned");
1277
+ this.showCursorToast(existingPinned.entry.selector, "unpinned", x, y);
1278
+ this.updateOverlayPosition();
1279
+ this.notifyStateChange();
1280
+ return;
1281
+ }
1282
+
1283
+ const excluded = this.callbacks.getExcludedSelectors();
1284
+ const snapshot = captureElementSnapshot(el, excluded);
1285
+ if (!snapshot) return;
1286
+
1287
+ this.callbacks.onSelect(snapshot, el);
1288
+ this.lastAction = { kind: "pinned", selector: snapshot.selector };
1289
+ this.isScrubbingLayers = false;
1290
+ this.refreshPinnedMarkers();
1291
+
1292
+ if (!this.callbacks.canSelectMore()) {
1293
+ this.stop();
1294
+ return;
1295
+ }
1296
+
1297
+ this.flashSelectionPulse("pinned");
1298
+ this.showCursorToast(snapshot.selector, "pinned", x, y);
1299
+ this.updateOverlayPosition();
1300
+ this.notifyStateChange();
1301
+ }
1302
+
1303
+ private handleWheelCapture(event: WheelEvent): void {
1304
+ if (!this.active) return;
1305
+ if (this.isInternalUiEvent(event)) return;
1306
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
1307
+
1308
+ if (this.candidateStack.length <= 1) {
1309
+ return;
1310
+ }
1311
+
1312
+ event.preventDefault();
1313
+ event.stopPropagation();
1314
+
1315
+ const normalizedDelta = event.deltaMode === 1 ? event.deltaY * 32 : event.deltaY;
1316
+ if (normalizedDelta === 0) return;
1317
+
1318
+ // Reset accumulator immediately when the user reverses scroll direction
1319
+ if (this.wheelAccumulator * normalizedDelta < 0) {
1320
+ this.wheelAccumulator = 0;
1321
+ }
1322
+
1323
+ this.wheelAccumulator += normalizedDelta;
1324
+ if (Math.abs(this.wheelAccumulator) < WHEEL_STEP_THRESHOLD) {
1325
+ return;
1326
+ }
1327
+
1328
+ const now = Date.now();
1329
+ if (now - this.lastWheelStepAt < WHEEL_STEP_COOLDOWN_MS) {
1330
+ this.wheelAccumulator = 0;
1331
+ return;
1332
+ }
1333
+
1334
+ const step = this.wheelAccumulator > 0 ? 1 : -1;
1335
+ this.wheelAccumulator = 0;
1336
+ this.lastWheelStepAt = now;
1337
+ this.cycleDepth(step, true);
1338
+ }
1339
+
1340
+ private handlePointerSuppression(event: Event): void {
1341
+ if (!this.active) return;
1342
+ if (this.isInternalUiEvent(event)) {
1343
+ return;
1344
+ }
1345
+
1346
+ // Intercept host page pointer events in capture phase so links/buttons do not fire
1347
+ event.preventDefault();
1348
+ event.stopPropagation();
1349
+ if (typeof event.stopImmediatePropagation === "function") {
1350
+ event.stopImmediatePropagation();
1351
+ }
1352
+ }
1353
+
1354
+ private handleClickCapture(event: MouseEvent): void {
1355
+ if (!this.active) return;
1356
+ if (this.isInternalUiEvent(event)) {
1357
+ return;
1358
+ }
962
1359
 
963
- this.popoverEl.style.left = `${left}px`;
964
- this.popoverEl.style.top = `${top}px`;
1360
+ event.preventDefault();
1361
+ event.stopPropagation();
1362
+ if (typeof event.stopImmediatePropagation === "function") {
1363
+ event.stopImmediatePropagation();
1364
+ }
1365
+
1366
+ this.lastPointerX = event.clientX;
1367
+ this.lastPointerY = event.clientY;
1368
+ this.updateCandidatesFromPoint(event.clientX, event.clientY, event);
1369
+
1370
+ const activeCandidate = this.getActiveCandidate();
1371
+ if (!activeCandidate) return;
1372
+
1373
+ this.commitCandidateSelection(activeCandidate.element, event.clientX, event.clientY);
965
1374
  }
966
1375
 
967
1376
  private handleKeyDown(event: KeyboardEvent): void {
@@ -970,25 +1379,21 @@ export class ElementPicker {
970
1379
  if (event.key === "Escape") {
971
1380
  event.preventDefault();
972
1381
  event.stopPropagation();
973
- if (this.anchored) {
974
- this.closeLayerPopover();
975
- } else {
976
- this.stop();
977
- }
1382
+ this.stop();
978
1383
  return;
979
1384
  }
980
1385
 
981
1386
  if (event.key === "ArrowUp" || event.key === "[") {
982
1387
  event.preventDefault();
983
1388
  event.stopPropagation();
984
- this.cycleDepth(1);
1389
+ this.cycleDepth(1, true);
985
1390
  return;
986
1391
  }
987
1392
 
988
1393
  if (event.key === "ArrowDown" || event.key === "]") {
989
1394
  event.preventDefault();
990
1395
  event.stopPropagation();
991
- this.cycleDepth(-1);
1396
+ this.cycleDepth(-1, true);
992
1397
  return;
993
1398
  }
994
1399
 
@@ -997,7 +1402,11 @@ export class ElementPicker {
997
1402
  if (candidate) {
998
1403
  event.preventDefault();
999
1404
  event.stopPropagation();
1000
- this.selectElementDirectly(candidate.element);
1405
+ this.commitCandidateSelection(
1406
+ candidate.element,
1407
+ this.lastPointerX || 80,
1408
+ this.lastPointerY || 80
1409
+ );
1001
1410
  }
1002
1411
  }
1003
1412
  }