Skip to content
Draft
5 changes: 5 additions & 0 deletions .changeset/comments-more-menu-resolve-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astryxdesign/lab': patch
---

[fix] In a narrow comment, Mark as resolved and Reopen keep focus off the page. When the focused button is replaced by an action the header has no room for, focus moves to the comment's more menu button, and the menu stays closed. When one picked from the more menu is replaced by a header button, that button takes focus; if the menu still holds other actions, focus returns to the more menu button.
115 changes: 104 additions & 11 deletions packages/lab/src/Comments/CommentActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @file CommentActions.tsx
* @input Uses React, React DOM flushSync, StyleX, Core's internal
* useClipboardCopy, IconButton,
* useClipboardCopy and isFocusLost, IconButton,
* Button, Tooltip, ToggleButton, OverflowList, DropdownMenu, Popover, Text
* @output Exports CommentActions (a comment's header actions), ReactionSummary
* (its reaction toggles), and the action-lookup helpers rows use for focus
Expand All @@ -30,6 +30,7 @@ import {
import {Icon} from '@astryxdesign/core/Icon';
import {useClipboardCopy} from '@astryxdesign/core/internal/clipboard-copy';
import {IconButton} from '@astryxdesign/core/IconButton';
import {isFocusLost} from '@astryxdesign/core/internal/touch';
import {HStack, VStack} from '@astryxdesign/core/Layout';
import {OverflowList, type OverflowItem} from '@astryxdesign/core/OverflowList';
import {Popover} from '@astryxdesign/core/Popover';
Expand Down Expand Up @@ -84,6 +85,13 @@ export function visibleCommentAction(
);
}

/** Whether focus is on `node` or inside it, in its own document or shadow root. */
function holdsFocus(node: HTMLElement): boolean {
const root = node.getRootNode() as Document | ShadowRoot;
const active = root.activeElement ?? null;
return active != null && node.contains(active);
}

export function commentMoreMenuTrigger(
root: HTMLElement | null,
): HTMLElement | null {
Expand Down Expand Up @@ -281,6 +289,31 @@ export function CommentActions({
const [isPickerOpen, setIsPickerOpen] = useState(false);
const [collapsed, setCollapsed] = useState(NO_COLLAPSED_ACTIONS);
const clusterRef = useRef<HTMLElement | null>(null);
// True from the commit that removed a focused Mark as resolved or Reopen
// until the end of that commit's work.
const resolveFocusDroppedRef = useRef(false);
// True from that overflow report until the more menu's trigger takes focus,
// at most until the end of that commit's work.
const isMoreFocusPendingRef = useRef(false);
const resolveControlRef = resolveAction?.controlRef;
const trackResolveControl = useCallback(
(node: HTMLElement | null) => {
const release = resolveControlRef?.(node);
if (node == null) {
return undefined;
}
return () => {
if (holdsFocus(node)) {
resolveFocusDroppedRef.current = true;
queueMicrotask(() => {
resolveFocusDroppedRef.current = false;
});
}
release?.();
};
},
[resolveControlRef],
);
const href = `#${commentAnchorId(comment.id)}`;
const linkLabel = isLinkCopyFailed
? linkCopyFailedLabel
Expand Down Expand Up @@ -318,15 +351,74 @@ export function CommentActions({
)?.focus();
};
const changeOverflow = useCallback((items: OverflowItem[]) => {
setCollapsed(
new Set(
items.flatMap(item => {
const action = commentActionOf(item);
return action == null ? [] : [action];
}),
),
const next = new Set(
items.flatMap(item => {
const action = commentActionOf(item);
return action == null ? [] : [action];
}),
);
setCollapsed(next);
// A Mark as resolved or Reopen that had focus was replaced by its other
// action, and the header had no room for it. The more menu now holds it,
// so its trigger takes focus (the docs promise focus never drops to the
// page). The cluster's ref, which attaches after this list's layout, moves
// the focus.
if (
resolveFocusDroppedRef.current &&
(next.has('resolve') || next.has('reopen'))
) {
resolveFocusDroppedRef.current = false;
isMoreFocusPendingRef.current = true;
queueMicrotask(() => {
isMoreFocusPendingRef.current = false;
});
}
}, []);
const takeMoreFocus = useCallback((root: HTMLElement | null) => {
if (!isMoreFocusPendingRef.current || root == null) {
return;
}
const trigger = commentMoreMenuTrigger(root);
if (trigger == null) {
return;
}
isMoreFocusPendingRef.current = false;
if (isFocusLost(null, root.ownerDocument)) {
trigger.focus({preventScroll: true});
}
}, []);
// The more menu takes part in the resolve control's focus swap: when an
// action picked from it is replaced by a header button and the emptied
// menu goes, that button takes focus.
Comment on lines +390 to +392

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (comment style): one 32-word sentence with a passive clause. Suggested: "The more menu joins the resolve control's focus swap. When a menu pick empties the menu, the header button that replaces the pick takes focus."

const attachMoreMenu = useCallback(
(element: HTMLElement | null) => {
const release = resolveControlRef?.(element);
if (element == null) {
return release;
}
takeMoreFocus(clusterRef.current ?? element);
return () => {
const heldFocus = holdsFocus(element);
release?.();
if (heldFocus) {
// The header can show the replacing action one commit before the
// emptied menu goes, so no node attaches after it. The visible
// replacing control then takes focus.
queueMicrotask(() => {
const root = clusterRef.current;
if (root == null || !isFocusLost(null, root.ownerDocument)) {
return;
}
(
visibleCommentAction(root, 'resolve') ??
visibleCommentAction(root, 'reopen')
)?.focus({preventScroll: true});
});
}
};
},
[resolveControlRef, takeMoreFocus],
);

const collapsedItems: DropdownMenuOption[] = [];
if (resolveAction != null && collapsed.has(resolveAction.kind)) {
Expand Down Expand Up @@ -397,6 +489,7 @@ export function CommentActions({
ref={(element: HTMLElement | null) => {
clusterRef.current = element;
anchorRef?.(element);
takeMoreFocus(element);
}}
gap={0.5}
align="center"
Expand All @@ -412,7 +505,7 @@ export function CommentActions({
{resolveAction?.kind === 'resolve' ? (
<IconButton
key="resolve"
ref={resolveAction.controlRef}
ref={trackResolveControl}
label={resolveLabel}
tooltip={resolveLabel}
variant="ghost"
Expand All @@ -425,7 +518,7 @@ export function CommentActions({
) : resolveAction?.kind === 'reopen' ? (
<Button
key="reopen"
ref={resolveAction.controlRef}
ref={trackResolveControl}
label={reopenLabel}
variant="ghost"
size="sm"
Expand Down Expand Up @@ -490,7 +583,7 @@ export function CommentActions({
</OverflowList>
{additionalActions}
{items.length > 0 && (
<HStack data-comment-more-menu="true">
<HStack ref={attachMoreMenu} data-comment-more-menu="true">
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
<DropdownMenu
items={items}
menuWidth={MORE_MENU_MIN_WIDTH}
Expand Down
13 changes: 12 additions & 1 deletion packages/lab/src/Comments/CommentRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,22 @@ const styles = stylex.create({
// The header actions reveal instantly, with no fade or dwell.
const INSTANT_REVEAL_STYLE = {transition: 'none'} as const;

// The resolve control's focus swap includes the comment's more menu, whose
// focus target is its trigger button.
function resolveFocusTarget(node: HTMLElement): HTMLElement | null {
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
return node.matches('[data-comment-more-menu]')
? node.querySelector<HTMLElement>('button')
: node;
}

/**
* A thread surface (a basic Card or Section) made selectable with
* `useClickableContainer`: surface clicks toggle selection while inner
* controls keep their own clicks, and a visually hidden toggle button carries
* the accessible name and pressed state. Selection paints the container's own
* muted fill, so no selectable component is restyled.
*/

function ThreadSurface({
children,
dividers,
Expand Down Expand Up @@ -389,7 +398,9 @@ export function CommentRow({
const isMuted = isResolved || isParentThreadResolved;
// The host's update replaces Mark as resolved with Reopen, and back.
// The replacement takes focus if the pressed control had it.
const resolveControlRef = useFocusAcrossSwap<HTMLElement>();
const resolveControlRef = useFocusAcrossSwap<HTMLElement>(undefined, {
getFocusTarget: resolveFocusTarget,
});
const resolveAction: ResolveAction | undefined =
thread == null || thread.canResolve === false || !canMutate
? undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/lab/src/Comments/Comments.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export const docs = {
category: 'Keyboard',
criterion: '2.4.3 Focus Order',
description:
'Focus never drops to the page. After a delete it moves to the next thread, or to the thread when a reply was deleted. Load earlier comments keeps focus on itself, or on the Retry that replaces it when loading fails. A thread selected when the panel mounts opens without taking focus.',
'Focus never drops to the page. Mark as resolved and Reopen hand focus to each other. When the header has no room for the one that replaces it, focus goes to the more menu button that holds it. When one picked from the more menu is replaced by a header button, that button takes focus; if the menu still holds other actions, focus returns to the more menu button. After a delete it moves to the next thread, or to the thread when a reply was deleted. Load earlier comments keeps focus on itself, or on the Retry that replaces it when loading fails. A thread selected when the panel mounts opens without taking focus.',
},
{
name: 'Busy actions',
Expand Down
Loading