Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/lab/src/Comments/CommentRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ function ThreadSurface({
const containerRef = useRef<HTMLDivElement | null>(null);
const controlRef = useRef<HTMLButtonElement | null>(null);
const [isFocused, setIsFocused] = useState(false);
// A focused toggle that unmounts fires no blur, so its focus state clears
// when it detaches. Otherwise a remounted toggle shows the ring while focus
// is on the page.
const attachControl = useCallback((node: HTMLButtonElement | null) => {
controlRef.current = node;
if (node == null) {
setIsFocused(false);
}
}, []);
const toggle = useCallback(
(event?: {detail?: number}) =>
onChange(!isSelected, activationSourceOf(event)),
Expand All @@ -226,7 +235,7 @@ function ThreadSurface({
const control = isSelectable ? (
<VisuallyHidden>
<button
ref={controlRef}
ref={attachControl}
type="button"
aria-label={label}
aria-pressed={isSelected}
Expand All @@ -237,10 +246,12 @@ function ThreadSurface({
</VisuallyHidden>
) : null;
const interactionProps = isSelectable ? {onClick, onMouseUp} : {};
// The focus ring comes last. The forced-colors selection outline unsets
// the outline outside forced colors, so it must not override the ring.
const sharedXstyle = [
isSelectable && styles.surface,
isSelectable && isFocused && styles.surfaceFocused,
isFilled && styles.selectedForcedColors,
isSelectable && isFocused && styles.surfaceFocused,

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.

Miss: isFocused only changes in the toggle's onFocus and onBlur (lines 234-235), and React runs no onBlur when it removes the focused toggle, which happens while the thread cannot be selected (for example while the host's isSubmitting is true). With this style now last, a selected or highlighted thread keeps the ring after the toggle comes back, though focus is on the page body; in forced colors the ring also replaces the selection outline. Reproduced here in Chromium 158: Tab to a selected thread's toggle, set isSubmitting to true and then false, and the surface still computes a 2px solid outline at offset 3px with document.activeElement on the body (the base keeps no outline there, and the inset Highlight outline in forced colors).

Clearing the state when the toggle detaches fixes it, and a test for that round trip would pin it:

const setControl = useCallback((node: HTMLButtonElement | null) => {
  controlRef.current = node;
  // React runs no onBlur for a focused node it removes.
  if (node == null) {
    setIsFocused(false);
  }
}, []);

];

if (surface === 'section') {
Expand Down
78 changes: 78 additions & 0 deletions packages/lab/src/Comments/CommentThread.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ vi.mock('../EmojiPicker/emojiData', () => ({
],
}));

import * as stylex from '@stylexjs/stylex';
import {Popover} from '@astryxdesign/core/Popover';
import {focusVars} from '@astryxdesign/core/theme/tokens.stylex';
import {CommentThread, type CommentThreadProps} from './CommentThread';
import {
unsavedDraftOf,
Expand Down Expand Up @@ -210,6 +212,82 @@ describe('CommentThread', () => {
expect(toggle).toHaveFocus();
});

it.each([
['selected', {isSelected: true}, THREAD],
['highlighted', {}, {...THREAD, isHighlighted: true}],
] as const)(
'shows the focus ring on a %s thread that has keyboard focus',
async (_state, props, comment) => {
const user = userEvent.setup();
const focusRing = stylex.create({
ring: {
outlineStyle: focusVars['--focus-outline-style'],
outlineWidth: focusVars['--focus-outline-width'],
},
});
const ringAtoms = stylex
.props(focusRing.ring)
.className!.split(' ')
.filter(name => name.includes('x') && !name.includes('__'));
expect(ringAtoms.length).toBeGreaterThan(0);
render(
<CommentThread
comment={comment}
currentUser={VIEWER}
onSubmit={vi.fn()}
{...props}
/>,
);
await settle();
const toggle = screen.getByRole('button', {name: 'Thread by Maya Chen'});
await user.tab();
expect(toggle).toHaveFocus();
const surface = toggle.closest('[data-thread-surface]');
for (const name of ringAtoms) {
expect(surface).toHaveClass(name);
}
},
);

it('drops the focus ring when a focused toggle unmounts and comes back', async () => {
const user = userEvent.setup();
const focusRing = stylex.create({
ring: {
outlineStyle: focusVars['--focus-outline-style'],
outlineWidth: focusVars['--focus-outline-width'],
},
});
const ringAtoms = stylex
.props(focusRing.ring)
.className!.split(' ')
.filter(name => name.includes('x') && !name.includes('__'));
const thread = (isSubmitting: boolean) => (
<CommentThread
comment={THREAD}
currentUser={VIEWER}
onSubmit={vi.fn()}
isSelected
isSubmitting={isSubmitting}
/>
);
const {rerender, container} = render(thread(false));
await settle();
await user.tab();
expect(
screen.getByRole('button', {name: 'Thread by Maya Chen'}),
).toHaveFocus();
rerender(thread(true));
await settle();
rerender(thread(false));
await settle();
const toggle = screen.getByRole('button', {name: 'Thread by Maya Chen'});
expect(toggle).not.toHaveFocus();
const surface = container.querySelector('[data-thread-surface]');
for (const name of ringAtoms) {
expect(surface).not.toHaveClass(name);
}
});

it('forwards the reply draft lifecycle through a standalone thread', async () => {
const onComposerDraftChange = vi.fn();
const onSubmit = vi.fn().mockResolvedValue(undefined);
Expand Down