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

Prevent telegram swipe down closing #14

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@
-webkit-text-size-adjust: 100%;
}

.fixed-container {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
}

.scrollable-content {
height: 100%;
overflow-y: scroll;
transform: translate3d(0, 0, 0);
-webkit-overflow-scrolling: touch;

padding: 0 16px 0 16px;
}

body {
margin: 0;
display: flex;
Expand Down
77 changes: 77 additions & 0 deletions src/lib/telegram/preventTelegramSwipeDownClosing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// @ts-nocheck
import React, { ReactNode, useEffect } from "react";

function throttle(func, limit) {
let inThrottle;
return function () {
// eslint-disable-next-line
const args = arguments;
// eslint-disable-next-line
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}

type Props = {
condition: boolean;
children: ReactNode;
withScroll: boolean;
};

export const PreventTelegramSwipeDownClosing = (props: Props) => {
const { condition, children, withScroll } = props;

useEffect(() => {
if (!condition) {
return;
}
const scrollableElement = document.querySelector(".scrollable-content");

const onTouchMove = throttle((e) => {
if (withScroll) {
requestAnimationFrame(() => {
e.preventDefault();
});
} else {
e.preventDefault();
}
}, 100);

scrollableElement.addEventListener("touchmove", onTouchMove, false);

return () => {
scrollableElement.removeEventListener("touchMove", onTouchMove, false);
};
// eslint-disable-next-line
}, []);

if (!condition) {
return children;
}

return (
<div className="fixed-container">
<div className="scrollable-content">{children}</div>
</div>
);
};

const isMobileIos = /iPad|iPhone|iPod/.test(navigator.userAgent);

export const PreventTelegramSwipeDownClosingIos = (props: {
children: ReactNode;
withScroll: boolean;
}) => {
return (
<PreventTelegramSwipeDownClosing
condition={isMobileIos}
withScroll={props.withScroll}
>
{props.children}
</PreventTelegramSwipeDownClosing>
);
};
9 changes: 6 additions & 3 deletions src/screens/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UserSettingsStoreProvider } from "../store/user-settings-store-context.
import { UserSettingsMain } from "./user-settings/user-settings-main.tsx";
import { deckListStore } from "../store/deck-list-store.ts";
import { FullScreenLoader } from "./deck-list/full-screen-loader.tsx";
import { PreventTelegramSwipeDownClosingIos } from "../lib/telegram/preventTelegramSwipeDownClosing.tsx";

export const App = observer(() => {
if (deckListStore.isSharedDeckLoading || deckListStore.isDeckRemoving) {
Expand All @@ -34,9 +35,11 @@ export const App = observer(() => {
)}
{screenStore.screen.type === "cardQuickAddForm" && <QuickAddCardForm />}
{screenStore.screen.type === "userSettings" && (
<UserSettingsStoreProvider>
<UserSettingsMain />
</UserSettingsStoreProvider>
<PreventTelegramSwipeDownClosingIos withScroll={false}>
<UserSettingsStoreProvider>
<UserSettingsMain />
</UserSettingsStoreProvider>
</PreventTelegramSwipeDownClosingIos>
)}
</div>
);
Expand Down
13 changes: 11 additions & 2 deletions src/screens/deck-review/deck-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import { Review } from "./review.tsx";
import { DeckPreview } from "./deck-preview.tsx";
import { useReviewStore } from "../../store/review-store-context.tsx";
import { DeckFinished } from "./deck-finished.tsx";
import { PreventTelegramSwipeDownClosingIos } from "../../lib/telegram/preventTelegramSwipeDownClosing.tsx";

export const DeckScreen = observer(() => {
const reviewStore = useReviewStore();

if (reviewStore.isFinished) {
return <DeckFinished />;
return (
<PreventTelegramSwipeDownClosingIos withScroll={false}>
<DeckFinished />
</PreventTelegramSwipeDownClosingIos>
);
} else if (reviewStore.currentCardId) {
return <Review />;
return (
<PreventTelegramSwipeDownClosingIos withScroll={false}>
<Review />
</PreventTelegramSwipeDownClosingIos>
);
}
return <DeckPreview />;
});