zammad/app/frontend/shared/composables/useDelegateFocus.ts
2026-01-02 15:41:09 +02:00

18 lines
654 B
TypeScript

// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/
export const useDelegateFocus = (containerId: string, firstChildId: string) => {
const delegateFocus = (event: FocusEvent) => {
const containerElement: Maybe<HTMLElement> = document.querySelector(`#${containerId}`)
const firstChildElement: Maybe<HTMLElement> = document.querySelector(`#${firstChildId}`)
// Check if the element that just lost focus is another child element of the container.
if (event.relatedTarget && containerElement?.contains(event.relatedTarget as Node)) return
firstChildElement?.focus()
}
return {
delegateFocus,
}
}