useDelegateFocus.ts 704 B

1234567891011121314151617181920212223242526
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. export const useDelegateFocus = (containerId: string, firstChildId: string) => {
  3. const delegateFocus = (event: FocusEvent) => {
  4. const containerElement: Maybe<HTMLElement> = document.querySelector(
  5. `#${containerId}`,
  6. )
  7. const firstChildElement: Maybe<HTMLElement> = document.querySelector(
  8. `#${firstChildId}`,
  9. )
  10. // Check if the element that just lost focus is another child element of the container.
  11. if (
  12. event.relatedTarget &&
  13. containerElement?.contains(event.relatedTarget as Node)
  14. )
  15. return
  16. firstChildElement?.focus()
  17. }
  18. return {
  19. delegateFocus,
  20. }
  21. }