Skip to content

useAccessibilityHandler: withAutoFocus가 비포커스 요소에서 동작하지 않음 #70

Open

Description

@theo-watcha

요약

useAccessibilityHandler의 options.withAutoFocus가 target에 focus()만 합니다. <div> 처럼 기본 포커스 불가 요소면 focus()가 무시되고, 요소에 붙은 keydown도 받지 못합니다.

호출부가 tabIndex를 직접 넣어야 포커스·Esc가 동작합니다.

재현

const rootRef = useRef<HTMLDivElement>(null);

useAccessibilityHandler({
  target: rootRef,
  options: { withAutoFocus: true },
  handler: (event) => {
    if (event.key === "Escape") {
      onClose();
    }
  },
});

return <div ref={rootRef}>{/* dialog */}</div>;
  1. 다이얼로그 등 비포커스 div를 target으로 넘긴다.
  2. withAutoFocus: true로 연다.
  3. 열린 뒤 Esc를 누른다.

실제 동작

  • target.current.focus()가 호출되지만 포커스가 안 옮겨진다.
  • Esc가 핸들러까지 안 올라간다.

기대 동작

withAutoFocus: true이면 호출부가 tabIndex를 안 넣어도 target에 포커스가 가고, keydown이 동작한다.

탭 순서에 넣는 건 아니므로 tabIndex={0}이 아니라 tabindex="-1"이 맞다.

현재 구현

0.1.4, 1.1.7 모두 같습니다.

if (enable && target.current) {
  target.current.addEventListener("keydown", stableHandler);
  if (options?.withAutoFocus ?? false) target.current.focus();
}

타입 주석에도 tabindex 설명이 없고, README 예시는 호출부가 tabIndex={0}을 넣도록 하고 있습니다.

제안

withAutoFocus일 때 tabindex가 없으면 -1을 넣고 focus() 한다.

if (withAutoFocus && target.current) {
  if (!target.current.getAttribute("tabindex")) {
    target.current.setAttribute("tabindex", "-1");
  }
  target.current.focus();
}

호출부가 이미 tabIndex를 준 경우는 그대로 두면 됩니다.

아니면 withAutoFocus 계약(div 등에서는 tabIndex가 필요함)을 README/타입에 명시해 주세요.

Activity

  1. theo-watcha commented on Aug 26, 2026

    @theo-watcha
    MemberAuthor

    프롭스 동작에 따라 기본값이 적절히 동작하는지 추가 검토하기

  2. self-assigned this
    on Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions