showDetectionBadge function exported

Last updated: 2026-02-24T19:46:21.782Z

Metrics

LOC: 36 Complexity: 7 Params: 4

Signature

showDetectionBadge( element: Element, fieldType: string, method?: string, ): : void

Summary

Shows a colored type badge anchored just above the top-right corner of the given element. The badge fades out automatically after BADGE_LIFETIME_MS.

Source Code

export function showDetectionBadge(
  element: Element,
  fieldType: string,
  method?: string,
): void {
  ensureStyles();

  const rect = element.getBoundingClientRect();
  if (rect.width === 0 && rect.height === 0) return;

  const badge = document.createElement("div");
  badge.setAttribute(BADGE_ATTR, "");
  badge.style.background = TYPE_COLOR[fieldType] ?? "#64748b";

  const icon = method ? (METHOD_ICON[method] ?? "") : "";
  badge.textContent = icon ? `${icon} ${fieldType}` : fieldType;

  // Anchor: just above the top-right corner of the field
  badge.style.top = `${Math.max(0, rect.top - 20)}px`;
  badge.style.left = `${rect.right}px`;
  badge.style.transform = "translateX(-100%)";

  document.body.appendChild(badge);

  // Animate in on next frame
  requestAnimationFrame(() => {
    badge.style.opacity = "1";
  });

  // Auto-fade and remove
  setTimeout(() => {
    badge.style.transition = "opacity 0.3s ease";
    badge.style.opacity = "0";
    setTimeout(() => badge.remove(), 320);
  }, BADGE_LIFETIME_MS);
}

Dependencies (Outgoing)

graph LR showDetectionBadge["showDetectionBadge"] ensureStyles["ensureStyles"] showDetectionBadge -->|calls| ensureStyles style showDetectionBadge fill:#dbeafe,stroke:#2563eb,stroke-width:2px click showDetectionBadge "d8265190c1104e79.html" click ensureStyles "9133434bbe64c3e3.html"
TargetType
ensureStyles calls

No incoming dependencies.