Skip to content

[docs] Manage focus on Open in Chat button click - #48942

Open
silviuaavram wants to merge 1 commit into
mui:masterfrom
silviuaavram:docs/fix-lost-focus-on-chat-button-click
Open

[docs] Manage focus on Open in Chat button click#48942
silviuaavram wants to merge 1 commit into
mui:masterfrom
silviuaavram:docs/fix-lost-focus-on-chat-button-click

Conversation

@silviuaavram

@silviuaavram silviuaavram commented Aug 10, 2026

Copy link
Copy Markdown
Member

When clicking Open in Chat button in the docs, the focus is lost to the body. Restore the focus to the button once the button is not loading anymore, and if the user did not move focus elsewhere.

Also, there is no reason to disable the button while showing the error toast. Button should be enabled as soon as possible, when the request returns.

@silviuaavram
silviuaavram requested review from ZeeshanTamboli, brijeshb42, sai6855 and siriwatknp and a lite review from Copilot August 10, 2026 09:02
@silviuaavram silviuaavram added the docs Improvements or additions to the documentation. label Aug 10, 2026
@code-infra-dashboard

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-48942--material-ui.netlify.app/
QR code for https://deploy-preview-48942--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

Copilot AI left a comment

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.

Pull request overview

This PR improves the docs “Edit in Chat” (Open in MUI Chat) experience by addressing focus loss after clicking the button, and by ensuring the button is re-enabled as soon as the request completes (even when an error toast is shown).

Changes:

  • Add logic to restore focus to the “Edit in Chat” button after the loading state completes.
  • Stop disabling the button based solely on the presence of an error toast.
  • Add unit tests covering focus restoration on success, failure, and when the user moves focus elsewhere.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages-internal/core-docs/src/Demo/OpenInMUIChatButton.tsx Adds focus-management logic tied to the loading lifecycle and adjusts ref handling.
packages-internal/core-docs/src/Demo/OpenInMUIChatButton.test.tsx Introduces tests verifying focus restoration behavior across success/failure and user-driven focus changes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +69 to +77
beforeEach(() => {
mocks.openSandbox.mockReset();
vi.stubEnv('MUI_CHAT_API_BASE_URL', 'https://chat.example.com');
vi.stubEnv('MUI_CHAT_SCOPES', 'material-ui');
});

afterEach(() => {
vi.unstubAllEnvs();
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We use vi functions in our tests, we should be fine with these as well.

Comment on lines +103 to +122
const rainbowButtonRef = React.useRef<HTMLButtonElement | null>(null);
const handleRef = useForkRef<HTMLButtonElement>(ref, rainbowButtonRef);
const wasLoadingRef = React.useRef(false);

React.useEffect(() => {
if (wasLoadingRef.current && !loading) {
const rainbowButton = rainbowButtonRef.current;
const document = ownerDocument(rainbowButton);
const activeElement = getActiveElement(document);

if (
activeElement === document.body ||
activeElement === null ||
activeElement === document.documentElement
) {
rainbowButtonRef.current?.focus();
}
}
wasLoadingRef.current = loading;
}, [loading]);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

should be fine, if the user uses the mouse to interact, they don't care about focus being management at that point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to the documentation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants