diff --git a/src/presentation/design-system/primitives/escape-layer.ts b/src/presentation/design-system/primitives/escape-layer.ts new file mode 100644 index 00000000..ada44548 --- /dev/null +++ b/src/presentation/design-system/primitives/escape-layer.ts @@ -0,0 +1,15 @@ +/** + * 1112/05-11. Whether an Escape is for a layer drawn over the page (a + * Popover or a shown Tooltip) whose root is `root`. Not when it was pressed + * inside a dialog that does not hold the layer: that Escape is the dialog's. + * An Escape from the layer, from a dialog around it, or from no element + * (focus on the body, as after a click in WebKit) is the layer's. + */ +export function escapeIsForLayer(event: KeyboardEvent, root: Element | null): boolean { + const target = event.target; + if (root === null || !(target instanceof Element) || root.contains(target)) { + return true; + } + const dialog = target.closest("dialog"); + return dialog === null || dialog.contains(root); +} diff --git a/src/presentation/design-system/primitives/popover.tsx b/src/presentation/design-system/primitives/popover.tsx index 27ec21d7..6b9e8490 100644 --- a/src/presentation/design-system/primitives/popover.tsx +++ b/src/presentation/design-system/primitives/popover.tsx @@ -1,6 +1,7 @@ import { useEffect, useId, useRef, useState } from "react"; import { Button } from "./button.tsx"; +import { escapeIsForLayer } from "./escape-layer.ts"; export type PopoverProps = Readonly<{ triggerLabel: string; @@ -26,9 +27,14 @@ export function Popover({ triggerLabel, children }: PopoverProps) { } } // Capture phase, and the default prevented: the Escape closes this - // layer only, not a Dialog around it (1112/05-11). + // layer only, not a Dialog around it (1112/05-11). An Escape pressed in + // a Dialog this popover is not in is the Dialog's. function closeOnEscape(event: KeyboardEvent) { - if (event.key === "Escape" && !event.defaultPrevented) { + if ( + event.key === "Escape" && + !event.defaultPrevented && + escapeIsForLayer(event, rootRef.current) + ) { event.preventDefault(); setOpen(false); triggerRef.current?.focus(); diff --git a/src/presentation/design-system/primitives/tooltip.tsx b/src/presentation/design-system/primitives/tooltip.tsx index 331621bb..5ac9869a 100644 --- a/src/presentation/design-system/primitives/tooltip.tsx +++ b/src/presentation/design-system/primitives/tooltip.tsx @@ -1,4 +1,6 @@ -import { cloneElement, useEffect, useId, useState } from "react"; +import { cloneElement, useEffect, useId, useRef, useState } from "react"; + +import { escapeIsForLayer } from "./escape-layer.ts"; export type TooltipProps = Readonly<{ content: string; @@ -15,15 +17,22 @@ export function Tooltip({ content, children }: TooltipProps) { const [dismissed, setDismissed] = useState(false); const [hovered, setHovered] = useState(false); const [focused, setFocused] = useState(false); + const rootRef = useRef(null); const shown = (hovered || focused) && !dismissed; useEffect(() => { if (!shown) return; // Document level: a hovered trigger does not have keyboard focus. Capture // phase, and the default prevented: this Escape hides the tooltip only, - // not a Dialog around it (1112/05-11). A tooltip that is not shown leaves - // the Escape alone. + // not a Dialog around it (1112/05-11). A tooltip that is not shown, or + // one outside the Dialog the Escape was pressed in, leaves it alone. function dismissOnEscape(event: KeyboardEvent) { - if (event.key !== "Escape" || event.defaultPrevented) return; + if ( + event.key !== "Escape" || + event.defaultPrevented || + !escapeIsForLayer(event, rootRef.current) + ) { + return; + } event.preventDefault(); setDismissed(true); } @@ -36,6 +45,7 @@ export function Tooltip({ content, children }: TooltipProps) { return ( { if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false); }} diff --git a/tests/component/design-system-a11y.test.tsx b/tests/component/design-system-a11y.test.tsx index bf743b1f..b43911cb 100644 --- a/tests/component/design-system-a11y.test.tsx +++ b/tests/component/design-system-a11y.test.tsx @@ -105,6 +105,64 @@ describe("Escape inside a Dialog", () => { expect(onClose).not.toHaveBeenCalled(); }); + // round9 confirm review. A popover or tooltip left open outside a Dialog + // does not take the Dialog's Escape: the Escape belongs to the Dialog it + // was pressed in. + it.each([ + [ + "a Popover", + ( + + + + ), + (user: ReturnType) => user.click(screen.getByRole("button", { name: "설명 열기" })), + ], + [ + "a Tooltip", + ( + + + + ), + (user: ReturnType) => user.hover(screen.getByRole("button", { name: "도움말" })), + ], + ])("closes the Dialog when %s outside it is open", async (_label, layer, openLayer) => { + const user = userEvent.setup(); + const onClose = vi.fn(); + render( + <> + {layer} + + + + , + ); + await openLayer(user); + screen.getByRole("button", { name: "안쪽 버튼" }).focus(); + + await user.keyboard("{Escape}"); + + expect(onClose).toHaveBeenCalledTimes(1); + }); + + // WebKit does not focus a clicked button: an Escape with focus on the body + // still closes the open Popover. + it("closes an open Popover on an Escape from the body", async () => { + const user = userEvent.setup(); + render( + + + , + ); + await user.click(screen.getByRole("button", { name: "설명 열기" })); + (document.activeElement as HTMLElement | null)?.blur(); + + await user.keyboard("{Escape}"); + + expect(screen.queryByRole("button", { name: "내부 작업" })).not.toBeInTheDocument(); + }); + it("closes the Dialog when no inner layer is open", async () => { const user = userEvent.setup(); const onClose = vi.fn();