Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

fix(shortcuts): detect a focused field through a shadow root

Pouzor9bab0e6ad73f
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 5ef0d30..da23d07 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -6,6 +6,7 @@ import { groupIntoZones, liftEdgesToTopLevel, type AutoLayoutMode } from '@/util
import { serializeNode, serializeEdge, deserializeApiCanvas, migrateClusterHandles, type ApiNode, type ApiEdge } from '@/utils/canvasSerializer'
import { generateUUID } from '@/utils/uuid'
import { getCenteredPosition } from '@/utils/viewportCenter'
+import { isTypingTarget } from '@/utils/keyboard'
import { resolveVirtualEdgeParent } from '@/utils/virtualEdgeParent'
import { getDesignIdFromUrl, setDesignIdInUrl } from '@/utils/designUrl'
import { useDocsUrlSync } from '@/hooks/useDocsUrlSync'
@@ -539,8 +540,7 @@ export default function App() {
const handler = (e: KeyboardEvent) => {
const ctrl = e.ctrlKey || e.metaKey
// Ignore shortcuts when typing in an input/textarea
- const tag = (e.target as HTMLElement).tagName
- const isInput = tag === 'INPUT' || tag === 'TEXTAREA' || (e.target as HTMLElement).isContentEditable
+ const isInput = isTypingTarget(e)
if (ctrl && e.key === 's') { e.preventDefault(); handleSaveRef.current(); return }
// Undo belongs to whatever is being typed in — the document editor keeps
diff --git a/frontend/src/components/canvas/CanvasContainer.tsx b/frontend/src/components/canvas/CanvasContainer.tsx
index 6af740a..ef0567e 100644
--- a/frontend/src/components/canvas/CanvasContainer.tsx
+++ b/frontend/src/components/canvas/CanvasContainer.tsx
@@ -26,6 +26,7 @@ import { AlignmentGuides } from './AlignmentGuides'
import { FloorMapLayer } from './FloorMapLayer'
import { useAlignmentGuides } from '@/hooks/useAlignmentGuides'
import { setViewportCenterProjector } from '@/utils/viewportCenter'
+import { isTypingTarget } from '@/utils/keyboard'
import type { NodeData, EdgeData } from '@/types'
interface CanvasContainerProps {
@@ -82,9 +83,7 @@ export function CanvasContainer({ onConnect: onConnectProp, onEdgeDoubleClick, o
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (!(e.ctrlKey || e.metaKey)) return
- const el = e.target as HTMLElement
- const isInput = el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.isContentEditable
- if (isInput) return
+ if (isTypingTarget(e)) return
if (e.key === 'c') {
copySelectedNodes()
} else if (e.key === 'v') {
diff --git a/frontend/src/components/modals/DeviceInventoryModal.tsx b/frontend/src/components/modals/DeviceInventoryModal.tsx
index b31cfec..041964a 100644
--- a/frontend/src/components/modals/DeviceInventoryModal.tsx
+++ b/frontend/src/components/modals/DeviceInventoryModal.tsx
@@ -21,6 +21,7 @@ import { formatRelative, formatTimestamp } from '@/utils/timeFormat'
import { getCenteredPosition } from '@/utils/viewportCenter'
import { sourceBuckets, orderedSources, isRackDevice, SOURCE_META, type SourceBucket } from '@/utils/deviceSources'
import { isRackable } from '@/utils/rackable'
+import { isTypingTarget } from '@/utils/keyboard'
import { ProxmoxApproveModal, type ProxmoxApproveChoice } from '@/components/modals/ProxmoxApproveModal'
import { MergeDevicesModal } from '@/components/modals/MergeDevicesModal'
import { layoutProxmoxContainers, measureProxmoxContainers } from '@/utils/proxmoxContainerLayout'
@@ -640,8 +641,7 @@ export function DeviceInventoryModal({ open, onClose, highlightId, initialStatus
useEffect(() => {
if (!open) return
const handler = (e: KeyboardEvent) => {
- const target = e.target as HTMLElement | null
- const inField = target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT')
+ const inField = isTypingTarget(e)
if (e.key === 'Escape') {
if (selectMode && selectedIds.size > 0) { e.preventDefault(); setSelectedIds(new Set()) }
return
diff --git a/frontend/src/rack/components/PortPositionEditor.tsx b/frontend/src/rack/components/PortPositionEditor.tsx
index 86569c4..785d23d 100644
--- a/frontend/src/rack/components/PortPositionEditor.tsx
+++ b/frontend/src/rack/components/PortPositionEditor.tsx
@@ -13,6 +13,7 @@ import { INNER_WIDTH_PX, U_PX } from '../layout'
import { clampPort, snapThreshold, snapToPeers } from '../portLayout'
import { useRackPalette } from '../rackTheme'
import { RACK_COLUMNS, type DeviceStatus, type Port } from '@/types'
+import { isTypingTarget } from '@/utils/keyboard'
/** Default width of a full-width (12-column) plate in the editor. */
const EDITOR_FULL_WIDTH = 660
@@ -161,11 +162,7 @@ export function PortPositionEditor({
const onKeyDown = (e: KeyboardEvent) => {
const step = ARROW_STEPS[e.key]
if (!step) return
- const target = e.target as HTMLElement | null
- const tag = target?.tagName
- if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || target?.isContentEditable) {
- return
- }
+ if (isTypingTarget(e)) return
e.preventDefault()
nudge(selectedPortId, step[0], step[1])
}
diff --git a/frontend/src/rack/components/RackCanvas.tsx b/frontend/src/rack/components/RackCanvas.tsx
index 3c6cca5..cf1c4a0 100644
--- a/frontend/src/rack/components/RackCanvas.tsx
+++ b/frontend/src/rack/components/RackCanvas.tsx
@@ -19,6 +19,7 @@ import {
type Viewport,
} from '@xyflow/react'
import { Plus } from 'lucide-react'
+import { isTypingTarget } from '@/utils/keyboard'
import { rackHeight, rackWidth } from '../layout'
import { useRackStore } from '../store'
import { useRackPalette } from '../rackTheme'
@@ -101,9 +102,7 @@ function RackCanvasInner() {
useEffect(() => {
if (!selectedCableId && !cableDraft) return
const handler = (e: KeyboardEvent) => {
- const target = e.target as HTMLElement | null
- const tag = target?.tagName
- if (tag === 'INPUT' || tag === 'TEXTAREA' || target?.isContentEditable) return
+ if (isTypingTarget(e)) return
if (selectedCableId && (e.key === 'Delete' || e.key === 'Backspace')) {
e.preventDefault()
removeSelectedCable()
diff --git a/frontend/src/utils/__tests__/keyboard.test.ts b/frontend/src/utils/__tests__/keyboard.test.ts
new file mode 100644
index 0000000..0c7b43e
--- /dev/null
+++ b/frontend/src/utils/__tests__/keyboard.test.ts
@@ -0,0 +1,70 @@
+import { describe, it, expect, afterEach } from 'vitest'
+import { isTypingTarget } from '../keyboard'
+
+// Dispatches a keydown from `el` and returns what a window-level shortcut
+// handler would conclude about it.
+function typingFrom(el: Element): boolean {
+ let result = false
+ const handler = (e: KeyboardEvent) => { result = isTypingTarget(e) }
+ window.addEventListener('keydown', handler)
+ el.dispatchEvent(new KeyboardEvent('keydown', { key: 's', bubbles: true, composed: true }))
+ window.removeEventListener('keydown', handler)
+ return result
+}
+
+function mount<K extends keyof HTMLElementTagNameMap>(tag: K, parent: Node = document.body) {
+ const el = document.createElement(tag)
+ parent.appendChild(el)
+ return el
+}
+
+describe('isTypingTarget', () => {
+ afterEach(() => { document.body.innerHTML = '' })
+
+ it.each(['input', 'textarea', 'select'] as const)('is true for a <%s>', (tag) => {
+ expect(typingFrom(mount(tag))).toBe(true)
+ })
+
+ it('is true for a contentEditable element', () => {
+ const el = mount('div')
+ // jsdom does not implement isContentEditable
+ Object.defineProperty(el, 'isContentEditable', { value: true })
+ expect(typingFrom(el)).toBe(true)
+ })
+
+ it('is false for a non-field element', () => {
+ expect(typingFrom(mount('div'))).toBe(false)
+ expect(typingFrom(mount('button'))).toBe(false)
+ })
+
+ it('is false when the event is dispatched on window itself', () => {
+ let result = true
+ const handler = (e: KeyboardEvent) => { result = isTypingTarget(e) }
+ window.addEventListener('keydown', handler)
+ window.dispatchEvent(new KeyboardEvent('keydown', { key: 's' }))
+ window.removeEventListener('keydown', handler)
+ expect(result).toBe(false)
+ })
+
+ // Regression for #546: inside a shadow root (the Home Assistant panel) the
+ // window listener sees `target` retargeted to the host, not the input.
+ it('is true for an input inside a shadow root', () => {
+ const host = mount('div')
+ const shadow = host.attachShadow({ mode: 'open' })
+ const input = mount('input', shadow)
+
+ let seenTarget: EventTarget | null = null
+ const spy = (e: KeyboardEvent) => { seenTarget = e.target }
+ window.addEventListener('keydown', spy)
+ const result = typingFrom(input)
+ window.removeEventListener('keydown', spy)
+
+ expect(seenTarget).toBe(host)
+ expect(result).toBe(true)
+ })
+
+ it('is false for a non-field element inside a shadow root', () => {
+ const shadow = mount('div').attachShadow({ mode: 'open' })
+ expect(typingFrom(mount('div', shadow))).toBe(false)
+ })
+})
diff --git a/frontend/src/utils/keyboard.ts b/frontend/src/utils/keyboard.ts
new file mode 100644
index 0000000..98faa61
--- /dev/null
+++ b/frontend/src/utils/keyboard.ts
@@ -0,0 +1,15 @@
+const TYPING_TAGS = new Set(['INPUT', 'TEXTAREA', 'SELECT'])
+
+/**
+ * True when a key event comes from a field the user is typing in, so a global
+ * shortcut must leave the key alone.
+ *
+ * Reads `composedPath()[0]` rather than `target`: a listener on `window` sees
+ * `target` retargeted to the shadow host when the app is mounted in a shadow
+ * root (the Home Assistant panel), which made every `tagName` guard miss.
+ */
+export function isTypingTarget(e: KeyboardEvent): boolean {
+ const el = (e.composedPath()[0] ?? e.target) as HTMLElement | null
+ if (!el || typeof el.tagName !== 'string') return false
+ return TYPING_TAGS.has(el.tagName) || el.isContentEditable === true
+}