Skip to content
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

Added enter to insert new line on mobile instead of default. #1475

Merged
merged 8 commits into from
Sep 27, 2024
26 changes: 18 additions & 8 deletions src/lib/components/chat/ChatInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@
function handleKeydown(event: KeyboardEvent) {
// submit on enter
if (event.key === "Enter" && !event.shiftKey && !isCompositionOn) {
event.preventDefault();
// blur to close keyboard on mobile
textareaElement.blur();
// refocus so that user on desktop can start typing without needing to reclick on textarea
if (isDesktop(window)) {
textareaElement.focus();
// using isDesktop to check if the user is on a mobile device
if (!isDesktop(window)) {
event.preventDefault(); // prevent the default behavior of the enter key
// insert a newline at the cursor position
const start = textareaElement.selectionStart;
const end = textareaElement.selectionEnd;
value = value.substring(0, start) + "\n" + value.substring(end);
textareaElement.selectionStart = textareaElement.selectionEnd = start + 1;
} else {
event.preventDefault();
// blur to close keyboard on mobile
textareaElement.blur();
// refocus so that user on desktop can start typing without needing to reclick on textarea
if (isDesktop(window)) {
textareaElement.focus();
natecard marked this conversation as resolved.
Show resolved Hide resolved
}
dispatch("submit"); // use a custom event instead of `event.target.form.requestSubmit()` as it does not work on Safari 14
}
dispatch("submit"); // use a custom event instead of `event.target.form.requestSubmit()` as it does not work on Safari 14
}
}

Expand All @@ -44,7 +54,7 @@
style="min-height: {minHeight}; max-height: {maxHeight}">{(value || " ") + "\n"}</pre>

<textarea
enterkeyhint="send"
enterkeyhint={!isDesktop(window) ? "enter" : "send"}
tabindex="0"
rows="1"
class="scrollbar-custom absolute top-0 m-0 h-full w-full resize-none scroll-p-3 overflow-x-hidden overflow-y-scroll border-0 bg-transparent p-3 outline-none focus:ring-0 focus-visible:ring-0"
Expand Down