Repository navigation
fix(Comments): Mark as resolved and Reopen hand focus to the replacing control in the comment header #14155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(Comments): Mark as resolved and Reopen hand focus to the replacing control in the comment header #14155
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [chore] Internal: the focus hand-off Core runs when it replaces one of a control's elements ignores an element while it is inside an inert region, such as the copy OverflowList renders to measure its items, and Core's internal `@astryxdesign/core/internal/touch` entry re-exports it for Astryx's own packages, so Lab's Comments carries focus from Mark as resolved to Reopen and back. It is not part of the public API. Apps see no change. | ||
|
|
||
| @ernestt | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,10 @@ | |
| * focus, and only if focus has fallen to the page: React also detaches and | ||
| * re-attaches the same node (for example when an <Activity> boundary hides and | ||
| * reveals it), and neither case may pull focus back after the person has moved | ||
| * elsewhere. | ||
| * elsewhere. An element inside an inert region, such as the copy OverflowList | ||
| * renders to measure its items, can hold no focus, so it neither leaves nor | ||
| * takes a handoff. Inertness is read when a handoff is recorded or taken, not | ||
| * when the node attaches: a region can stop being inert later. | ||
|
astracat-bot[bot] marked this conversation as resolved.
Resolved
|
||
| */ | ||
|
|
||
| import {useCallback, useEffect, useRef, type Ref} from 'react'; | ||
|
|
@@ -33,6 +36,10 @@ function isNativeTarget(value: unknown): value is HTMLElement { | |
| ); | ||
| } | ||
|
|
||
| function isInertCopy(value: unknown): boolean { | ||
| return isHTMLElement(value) && value.closest('[inert]') != null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Miss: an open modal dialog escapes its ancestor's const inertRoot = value.closest('[inert]');
const dialog = value.closest('dialog[open]');
return inertRoot != null && !(dialog != null && isModal(dialog) && inertRoot.contains(dialog));with |
||
| } | ||
|
|
||
| function wasHidden(node: HTMLElement): boolean { | ||
| // A hidden Activity can commit another change before microtasks run. | ||
| for ( | ||
|
|
@@ -71,33 +78,38 @@ export function useFocusAcrossSwap<T extends HTMLElement>( | |
| if (node === null) { | ||
| return undefined; | ||
| } | ||
| const outgoing = focusedNodeRef.current; | ||
| focusedNodeRef.current = null; | ||
| const resolved = getFocusTarget ? getFocusTarget(node) : node; | ||
| const target = isNativeTarget(resolved) ? resolved : null; | ||
| if ( | ||
| target?.isConnected && | ||
| outgoing !== null && | ||
| outgoing.target !== target && | ||
| !outgoing.target.isConnected && | ||
| !wasHidden(outgoing.source) && | ||
| isFocusUnclaimed(target) | ||
| ) { | ||
| target.focus({preventScroll: true}); | ||
| if (!isInertCopy(node)) { | ||
| const outgoing = focusedNodeRef.current; | ||
| focusedNodeRef.current = null; | ||
| if ( | ||
| target?.isConnected && | ||
| outgoing !== null && | ||
| outgoing.target !== target && | ||
| !outgoing.target.isConnected && | ||
| !wasHidden(outgoing.source) && | ||
| isFocusUnclaimed(target) | ||
| ) { | ||
| target.focus({preventScroll: true}); | ||
| } | ||
| } | ||
| const release = mergeRefs<T>(ref)(node); | ||
| return () => { | ||
| const outgoing = | ||
| target && ownsFocus(target) ? {target, source: node} : null; | ||
| focusedNodeRef.current = outgoing; | ||
| if (outgoing) { | ||
| // Descendant refs can detach without disconnecting this hook's Effect. | ||
| // Expire the handoff after this commit, but leave newer handoffs intact. | ||
| queueMicrotask(() => { | ||
| if (focusedNodeRef.current === outgoing) { | ||
| focusedNodeRef.current = null; | ||
| } | ||
| }); | ||
| if (!isInertCopy(node)) { | ||
| const outgoing = | ||
| target && ownsFocus(target) ? {target, source: node} : null; | ||
| focusedNodeRef.current = outgoing; | ||
| if (outgoing) { | ||
| // Descendant refs can detach without disconnecting this hook's | ||
| // Effect. Expire the handoff after this commit, but leave newer | ||
| // handoffs intact. | ||
| queueMicrotask(() => { | ||
| if (focusedNodeRef.current === outgoing) { | ||
| focusedNodeRef.current = null; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| release?.(); | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,13 @@ | |
| * @position Internal parts of a comment row in the Comments family | ||
| */ | ||
|
|
||
| import {useCallback, useRef, useState, type ReactNode} from 'react'; | ||
| import { | ||
| useCallback, | ||
| useRef, | ||
| useState, | ||
| type ReactNode, | ||
| type RefCallback, | ||
| } from 'react'; | ||
| import {flushSync} from 'react-dom'; | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| import {Button} from '@astryxdesign/core/Button'; | ||
|
|
@@ -57,7 +63,12 @@ const styles = stylex.create({ | |
| export type CommentActionId = 'resolve' | 'reopen' | 'react' | 'reply' | 'link'; | ||
|
|
||
| /** Mark as resolved on an open thread, Reopen on a resolved one. */ | ||
| export type ResolveAction = {kind: 'resolve' | 'reopen'; onClick: () => void}; | ||
| export type ResolveAction = { | ||
| kind: 'resolve' | 'reopen'; | ||
| onClick: () => void; | ||
| /** Carries focus to the control that replaces a pressed one. */ | ||
| controlRef?: RefCallback<HTMLElement>; | ||
| }; | ||
|
|
||
| /** One of a comment's action buttons, as it shows. OverflowList renders a | ||
| * hidden copy of every item to measure it; only the copy in the visible list | ||
|
|
@@ -401,6 +412,7 @@ export function CommentActions({ | |
| {resolveAction?.kind === 'resolve' ? ( | ||
| <IconButton | ||
| key="resolve" | ||
| ref={resolveAction.controlRef} | ||
| label={resolveLabel} | ||
| tooltip={resolveLabel} | ||
| variant="ghost" | ||
|
|
@@ -413,6 +425,7 @@ export function CommentActions({ | |
| ) : resolveAction?.kind === 'reopen' ? ( | ||
| <Button | ||
| key="reopen" | ||
| ref={resolveAction.controlRef} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Miss: this ref hands focus to Reopen, but Reopen (75 px) is wider than the Mark as resolved icon (28 px). OverflowList has no minimum and remeasures when the key changes, so in a narrow header Reopen moves into the more menu one commit later and focus falls to the page body (reproduced locally in the Margin Comment story). The more-menu trigger already keeps focus when the action is picked from the menu, so it is a natural target for that case. |
||
| label={reopenLabel} | ||
| variant="ghost" | ||
| size="sm" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.