Skip to content

[field] Fix autofocus in SSR environments#3871

Merged
mj12albert merged 4 commits into
mui:masterfrom
mj12albert:field-control-autofocus
Jan 29, 2026
Merged

[field] Fix autofocus in SSR environments#3871
mj12albert merged 4 commits into
mui:masterfrom
mj12albert:field-control-autofocus

Conversation

@mj12albert

@mj12albert mj12albert commented Jan 27, 2026

Copy link
Copy Markdown
Member

Demo: https://ytf734-3000.csb.app

Fixes #3172

@mj12albert mj12albert added component: input Changes related to the input component. type: bug It doesn't behave as expected. component: field Changes related to the field component. labels Jan 27, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jan 27, 2026

Copy link
Copy Markdown

commit: 85209bd

@mui-bot

mui-bot commented Jan 27, 2026

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+103B(+0.02%) 🔺+77B(+0.06%)

Details of bundle changes


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

@netlify

netlify Bot commented Jan 27, 2026

Copy link
Copy Markdown

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 85209bd
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/697af6ce980fc50008ae73ee
😎 Deploy Preview https://deploy-preview-3871--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@mj12albert mj12albert marked this pull request as ready for review January 27, 2026 07:44
@greptile-apps

greptile-apps Bot commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

This PR adds SSR-safe autoFocus prop support to FieldControl by detecting when the browser's native autofocus has already focused the input element and synchronizing the field's internal focused state accordingly.

Key changes:

  • Added autoFocus prop handling (defaults to false)
  • Created new inputRef to track the input element
  • Added useIsoLayoutEffect hook that checks if the input is the active element after browser autofocus and syncs fieldState.focused
  • Merged inputRef with existing forwardedRef to enable focus detection
  • Properly uses useIsoLayoutEffect which becomes a noop in SSR environments, preventing errors when document is unavailable
  • Follows existing codebase patterns for activeElement(ownerDocument(...)) usage seen in other components

The implementation correctly handles SSR hydration where the browser's native autofocus occurs before React event handlers are attached.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation follows established patterns in the codebase for handling autofocus and SSR. It uses useIsoLayoutEffect which is SSR-safe (becomes noop when document is undefined), properly handles null cases in the activeElement check, and correctly merges multiple refs. The logic is straightforward and defensive.
  • No files require special attention

Important Files Changed

Filename Overview
packages/react/src/field/control/FieldControl.tsx Adds autofocus support with SSR-safe focus state synchronization using useIsoLayoutEffect and activeElement check

@mj12albert mj12albert force-pushed the field-control-autofocus branch from f6180d5 to 83fd0e7 Compare January 27, 2026 07:57
Comment thread packages/react/src/field/control/FieldControl.tsx Outdated
Comment thread packages/react/src/field/control/FieldControl.tsx Outdated
@mj12albert mj12albert force-pushed the field-control-autofocus branch from 83fd0e7 to 8d2c1f5 Compare January 28, 2026 08:48
@mj12albert mj12albert merged commit 604623a into mui:master Jan 29, 2026
23 checks passed
@mj12albert mj12albert deleted the field-control-autofocus branch January 29, 2026 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: field Changes related to the field component. component: input Changes related to the input component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[field] Focus state inaccurate on initial render when Field.Control has autoFocus

4 participants