Skip to content

Commit

Permalink
fix: Prevent scrolling behavior in CommentInput component on mobile d…
Browse files Browse the repository at this point in the history
…evices
  • Loading branch information
TimDaub committed Feb 21, 2025
1 parent 1948ec0 commit 8d5c415
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/web/src/CommentInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,21 @@ const MobileComposer = ({
}, []);
return (
<div
onTouchMove={(e) => e.preventDefault()}
onTouchMove={(e) => { if (!e.target.closest("textarea")) { e.preventDefault(); } }}
style={{
position: "fixed",
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: "white",
zIndex: 1000,
display: "flex",
flexDirection: "column",
width: "100%",
overflow: "hidden",
touchAction: "none",
overscrollBehavior: "none",
height: viewportHeight,
}}
>
<div
Expand Down Expand Up @@ -275,29 +276,36 @@ const CommentInput = (props) => {

function preventScroll(e) {
if (!showMobileComposer) return;

// Stop propagation to prevent underlying page from scrolling.
e.stopPropagation();

if (e.target.tagName === "TEXTAREA") {
const textarea = e.target;

// For touchmove events inside a textarea (e.g. selection handles), allow native behavior.
if (e.type === "touchmove" && e.target.closest("textarea")) {
return;
}

if (e.target.closest("textarea")) {
const textarea = e.target.closest("textarea");
// Prevent scrolling if the textarea is empty.
if (textarea.value.trim() === "") {
e.preventDefault();
return;
}
const isScrollable = textarea.scrollHeight > textarea.clientHeight;
const isAtTop = textarea.scrollTop === 0;
const isAtBottom =
textarea.scrollTop + textarea.clientHeight === textarea.scrollHeight;

// Prevent scroll if content fits or we're at the bounds.
if (
!isScrollable ||
(isAtTop && e.deltaY < 0) ||
(isAtBottom && e.deltaY > 0)
) {
e.preventDefault();
if (e.type === "wheel") {
const isScrollable = textarea.scrollHeight > textarea.clientHeight;
const isAtTop = textarea.scrollTop === 0;
const isAtBottom =
textarea.scrollTop + textarea.clientHeight === textarea.scrollHeight;

// Prevent scroll if content fits or we're at the bounds.
if (
!isScrollable ||
(isAtTop && e.deltaY < 0) ||
(isAtBottom && e.deltaY > 0)
) {
e.preventDefault();
}
}
} else {
e.preventDefault();
Expand Down

0 comments on commit 8d5c415

Please # to comment.