Skip to content

Commit 139b742

Browse files
committed
feat: Add autoFocus to chat input
1 parent d29f958 commit 139b742

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/layout/components/ai-bot/ai-chat-modal.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ export function AiChatModal({ isOpen, onClose }: AiChatModalProps) {
8080
};
8181

8282
useEffect(() => {
83+
const handleKeyDown = (event: KeyboardEvent) => {
84+
if (event.key === 'Escape') {
85+
onClose();
86+
}
87+
};
88+
window.addEventListener('keydown', handleKeyDown);
8389
const callback = (mutationList: MutationRecord[]) => {
8490
for (const mutation of mutationList) {
8591
if (mutation.type === 'childList') {
@@ -96,7 +102,10 @@ export function AiChatModal({ isOpen, onClose }: AiChatModalProps) {
96102
observer.observe(chatContainerRef.current, MUTATION_OBSERVER_CONFIGS);
97103
}
98104

99-
return () => observer.disconnect();
105+
return () => {
106+
observer.disconnect();
107+
window.removeEventListener('keydown', handleKeyDown);
108+
}
100109
}, []);
101110

102111
return (
@@ -166,9 +175,10 @@ export function AiChatModal({ isOpen, onClose }: AiChatModalProps) {
166175
>
167176
<TextInput
168177
required
169-
id="text"
178+
autoFocus
170179
type="text"
171180
tabIndex={1}
181+
id="ai-chat-input"
172182
className="w-full h-12"
173183
error={errors.text?.message}
174184
placeholder="Ask About Ramin From AI"

src/shared/components/text-input/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { clsx } from 'clsx';
66

77
interface TextInputProps {
88
id?: string;
9+
name?: string;
910
value?: string;
1011
label?: string;
1112
tabIndex?: number;
1213
required?: boolean;
1314
className?: string;
15+
autoFocus?: boolean;
1416
placeholder?: string;
1517
error?: string | null;
1618
containerClassName?: string;
@@ -19,6 +21,7 @@ interface TextInputProps {
1921
}
2022

2123
export function TextInput({
24+
name,
2225
label,
2326
value,
2427
error,
@@ -29,6 +32,7 @@ export function TextInput({
2932
type = 'text',
3033
required = false,
3134
id = 'text-input',
35+
autoFocus = false,
3236
containerClassName,
3337
...rest
3438
}: TextInputProps) {
@@ -48,9 +52,11 @@ export function TextInput({
4852
id={id}
4953
type={type}
5054
value={value}
55+
name={name ?? id}
5156
tabIndex={tabIndex}
5257
required={required}
5358
onChange={onChange}
59+
autoFocus={autoFocus}
5460
placeholder={placeholder}
5561
className={clsx(INPUT_CLASSES, {
5662
'border-red-500': !!error

0 commit comments

Comments
 (0)