Title: React Quill loses focus after one keypress in deployed application
I'm having an issue with my React Quill editor losing focus after just one keypress when deployed. Below is the code for the page:
"use client";
import Image from "next/image";
import styles from "./writePage.module.css";
import { useEffect, useState } from "react";
import "react-quill/dist/quill.bubble.css"
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import {
getStorage,
ref,
uploadBytesResumable,
getDownloadURL,
} from "firebase/storage";
import { app } from "@/utils/firebase";
import dynamic from "next/dynamic"
const WritePage = () => {
const { status } = useSession();
const ReactQuill = dynamic(()=> import('react-quill'),{ssr: false});
const router = useRouter();
const [open, setOpen] = useState(false);
const [file, setFile] = useState(null);
const [media, setMedia] = useState("");
const [value, setValue] = useState("");
const [title, setTitle] = useState("");
const [catSlug, setCatSlug] = useState("");
useEffect(() => {
const storage = getStorage(app);
const upload = () => {
const name = new Date().getTime() + file.name;
const storageRef = ref(storage, name);
const uploadTask = uploadBytesResumable(storageRef, file);
uploadTask.on(
"state_changed",
(snapshot) => {
const progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log("Upload is " + progress + "% done");
switch (snapshot.state) {
case "paused":
console.log("Upload is paused");
break;
case "running":
console.log("Upload is running");
break;
}
},
(error) => {},
() => {
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
setMedia(downloadURL);
});
}
);
};
file && upload();
}, [file]);
if (status === "loading") {
return <div className={styles.loading}>Loading...</div>;
}
if (status === "unauthenticated") {
router.push("/");
}
const slugify = (str) =>
str
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, "")
.replace(/[\s_-]+/g, "-")
.replace(/^-+|-+$/g, "");
const handleSubmit = async () => {
const res = await fetch("/api/posts", {
method: "POST",
body: JSON.stringify({
title,
desc: value,
img: media,
slug: slugify(title),
catSlug: catSlug || "style", //If not selected, choose the general category
}),
});
if (res.status === 200) {
const data = await res.json();
router.push(`/posts/${data.slug}`);
}
};
return (
<div className={styles.container}>
<input
type="text"
placeholder="Title"
className={styles.input}
onChange={(e) => setTitle(e.target.value)}
/>
<select className={styles.select} onChange={(e) => setCatSlug(e.target.value)}>
<option value="style">style</option>
<option value="fashion">fashion</option>
<option value="food">food</option>
<option value="culture">culture</option>
<option value="travel">travel</option>
<option value="coding">coding</option>
</select>
<div className={styles.editor}>
<button className={styles.button} onClick={() => setOpen(!open)}>
<Image src="/plus.png" alt="" width={16} height={16} />
</button>
{open && (
<div className={styles.add}>
<input
type="file"
id="image"
onChange={(e) => setFile(e.target.files[0])}
style={{ display: "none" }}
/>
<button className={styles.addButton}>
<label htmlFor="image">
<Image src="/image.png" alt="" width={16} height={16} />
</label>
</button>
</div>
)}
<ReactQuill
className={styles.textArea}
theme="bubble"
value={value}
onChange={setValue}
placeholder="Tell your story..."
/>
</div>
<button className={styles.publish} onClick={handleSubmit}>
Publish
</button>
</div>
);
};
export default WritePage;
I have tried dynamically importing React Quill using next/dynamic to ensure it's not causing the issue, but the problem persists.
The behavior is consistent in both development and production environments. In development, the editor works as expected, but when deployed, it loses focus after typing just one character.
I've also checked for any errors in the console, but there are no errors related to the focus issue.
Any insights or suggestions on how to troubleshoot and fix this issue would be greatly appreciated. Thank you!