Skip to content

Commit b3fb7f5

Browse files
authored
fix(sidepanel): autofocus prompt input on open for desktop only (#2857)
Focus the prompt textarea when the sidepanel opens, skipping mobile to avoid triggering the virtual keyboard which disrupts the layout. Blur the input when the sidepanel closes to remove lingering focus.
1 parent fbb975b commit b3fb7f5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

packages/docsearch-react/src/Sidepanel/Sidepanel.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AlgoliaLogo, type AlgoliaLogoTranslations } from '../AlgoliaLogo';
66
import type { DocSearchSidepanelProps, SidepanelSearchParameters } from '../Sidepanel';
77
import type { StoredAskAiState, SuggestedQuestionHit } from '../types';
88
import { useAskAi } from '../useAskAi';
9+
import { useIsMobile } from '../useIsMobile';
910
import { useSearchClient } from '../useSearchClient';
1011
import { useSuggestedQuestions } from '../useSuggestedQuestions';
1112
import { buildDummyAskAiHit } from '../utils/ai';
@@ -159,6 +160,7 @@ function SidepanelInner(
159160
const [stoppedStreaming, setStoppedStreaming] = React.useState(false);
160161
const sidepanelContainerRef = React.useRef<HTMLDivElement>(null);
161162
const promptInputRef = React.useRef<HTMLTextAreaElement>(null);
163+
const isMobile = useIsMobile();
162164

163165
const expectedWidth = useSidepanelWidth({
164166
isExpanded,
@@ -342,11 +344,19 @@ function SidepanelInner(
342344
}
343345
}, [initialMessage, sendMessage, conversations, handleSelectConversation, setMessages]);
344346

345-
// eslint-disable-next-line no-warning-comments
346-
// FIX: Renable autofocus on open once mobile focus issue is solved
347-
// React.useEffect(() => {
348-
// promptInputRef.current?.focus();
349-
// }, [isOpen]);
347+
// Autofocus the prompt input when the sidepanel opens and blur it when
348+
// it closes. Disabled on mobile because focusing the textarea triggers the
349+
// virtual keyboard which disrupts the layout — this is a known issue that
350+
// has not been resolved yet.
351+
React.useEffect(() => {
352+
if (isOpen && !isMobile) {
353+
promptInputRef.current?.focus();
354+
}
355+
356+
if (!isOpen) {
357+
promptInputRef.current?.blur();
358+
}
359+
}, [isOpen, isMobile]);
350360

351361
return (
352362
<div

0 commit comments

Comments
 (0)