-
Notifications
You must be signed in to change notification settings - Fork 853
fix(web): prevent Enter from sending message during IME composition on Safari #1815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -954,6 +954,11 @@ export const PromptInputTextarea = forwardRef< | |
| const controller = useOptionalPromptInputController(); | ||
| const attachments = usePromptInputAttachments(); | ||
| const [isComposing, setIsComposing] = useState(false); | ||
| // Track when compositionend last fired. Safari (WebKit bug #165004) fires | ||
| // compositionend *before* the Enter keydown, so both isComposing and | ||
| // nativeEvent.isComposing are false by the time we check. A short cooldown | ||
| // after compositionend prevents that Enter from triggering a send. | ||
| const compositionEndTimeRef = useRef(0); | ||
|
|
||
| const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = (e) => { | ||
| onKeyDown?.(e); | ||
|
|
@@ -962,7 +967,11 @@ export const PromptInputTextarea = forwardRef< | |
| } | ||
|
|
||
| if (e.key === "Enter") { | ||
| if (isComposing || e.nativeEvent.isComposing) { | ||
| if ( | ||
| isComposing || | ||
| e.nativeEvent.isComposing || | ||
| Date.now() - compositionEndTimeRef.current < 100 | ||
| ) { | ||
|
Comment on lines
+970
to
+974
|
||
| return; | ||
| } | ||
| if (e.shiftKey) { | ||
|
|
@@ -1044,7 +1053,10 @@ export const PromptInputTextarea = forwardRef< | |
| autoComplete="off" | ||
| name="message" | ||
| onBlur={() => setIsComposing(false)} | ||
| onCompositionEnd={() => setIsComposing(false)} | ||
| onCompositionEnd={() => { | ||
| setIsComposing(false); | ||
| compositionEndTimeRef.current = Date.now(); | ||
| }} | ||
|
Comment on lines
1055
to
+1059
|
||
| onCompositionStart={() => setIsComposing(true)} | ||
| onKeyDown={handleKeyDown} | ||
| onPaste={handlePaste} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
Date.now() - compositionEndTimeRef.current < 100guard inhandleKeyDownruns on all browsers, not just Safari. After anycompositionend(for example, IME text committed via Space/candidate click), a user who presses Enter quickly to send will hit this early return beforee.preventDefault(), so the textarea inserts a newline instead of submitting. This creates a cross-browser regression for fast IME workflows; the cooldown should be limited to the Safari-specific path (or suppress default Enter behavior when blocking submit).Useful? React with 👍 / 👎.