@@ -92,6 +92,8 @@ export type MessageComposerProps = {
9292 replyContext ?: ReactNode ;
9393 mediaTimeSeconds ?: number ;
9494 clearMediaTime ?( ) : void ;
95+ /** Focus once when this conversation mounts, not when overlays close. */
96+ autoFocus ?: boolean ;
9597 focusRequest ?: number ;
9698 hideMediaTimeIndicator ?: boolean ;
9799 disabled ?: boolean ;
@@ -138,6 +140,7 @@ function Composer({
138140 replyContext,
139141 mediaTimeSeconds,
140142 clearMediaTime,
143+ autoFocus = false ,
141144 focusRequest,
142145 hideMediaTimeIndicator = false ,
143146 disabled : requestedDisabled = false ,
@@ -202,6 +205,27 @@ function Composer({
202205 const valueRef = useRef ( value ) ;
203206 const caret = useRef < number | undefined > ( undefined ) ;
204207 const input = useRef < ComposerInputElement > ( null ) ;
208+ const focusOnMount = useRef (
209+ autoFocus && ! disabled && typeof document !== "undefined"
210+ ? document . activeElement
211+ : undefined ,
212+ ) ;
213+ useEffect ( ( ) => {
214+ // A navigation/dialog owner may restore focus during this commit. Let that
215+ // explicit handoff win over the conversation's default initial focus.
216+ const previous = focusOnMount . current ;
217+ if (
218+ previous &&
219+ ( previous === document . activeElement ||
220+ ( ! previous . isConnected && document . activeElement === document . body ) )
221+ ) {
222+ const editor = input . current ;
223+ if ( ! editor ) return ;
224+ const end = editor . value . length ;
225+ editor . setSelectionRange ( end , end ) ;
226+ editor . focus ( ) ;
227+ }
228+ } , [ ] ) ;
205229 const nonmembers = useNonmemberMentions ( session , channelId , ( ) =>
206230 input . current ?. focus ( ) ,
207231 ) ;
0 commit comments