Skip to content

Commit

Permalink
fix: mobile nav on ios could scroll its background
Browse files Browse the repository at this point in the history
  • Loading branch information
olaszakos committed Jan 4, 2024
1 parent c1197a5 commit ba376e1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/components/Common/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PageSearchResult } from "@site/search/src/declarations/search/search.di
import { createFocusTrap } from "focus-trap";
import Link from "@docusaurus/Link";
import { trackEvent } from "@site/src/utils/matomo";
import useLockBodyScroll from "../../../utils/use-lock-body-scroll";

let initialTerm = "";
let initialResults: PageSearchResult[] | null = null;
Expand All @@ -26,6 +27,8 @@ const Search: FC<{ onClose: () => void }> = ({ onClose }) => {
>({});
const [loading, setLoading] = useState(false);

useLockBodyScroll(dialogRef);

useEffect(() => {
actorRefPromise.current = import("./actor").then(({ createActor }) => {
console.log("search module loaded");
Expand All @@ -52,9 +55,7 @@ const Search: FC<{ onClose: () => void }> = ({ onClose }) => {

window.addEventListener("keydown", onKeydown);

document.body.style.overflow = "hidden";
return () => {
document.body.style.overflow = "unset";
window.removeEventListener("keydown", onKeydown);
trap.deactivate();
};
Expand Down Expand Up @@ -104,7 +105,15 @@ const Search: FC<{ onClose: () => void }> = ({ onClose }) => {
return (
<>
<motion.div
className="fixed inset-0 backdrop-blur-lg z-[3001] overflow-auto"
className="fixed h-screen inset-0 backdrop-blur-lg z-[3001] touch-none"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={onClose}
aria-modal="true"
></motion.div>
<motion.div
className="fixed max-h-screen top-0 left-0 right-0 z-[3002] overflow-auto"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
Expand All @@ -113,7 +122,7 @@ const Search: FC<{ onClose: () => void }> = ({ onClose }) => {
aria-modal="true"
>
<div
className="md:container-10 backdrop-blur-0"
className="md:container-10 backdrop-blur-0 touch-auto"
aria-label="Search the website"
>
<div
Expand Down
1 change: 0 additions & 1 deletion src/theme/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export default function Layout(props) {
</ErrorBoundary>
</div>
{!noFooter && <Footer editPath={editPath} />}
<div id="search-overlay-portal"></div>
</LayoutProvider>
);
}
23 changes: 23 additions & 0 deletions src/utils/use-lock-body-scroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect } from "react";

const useLockBodyScroll = (modalRef: React.RefObject<HTMLDivElement>) => {
useEffect(() => {
// Get original body overflow
const originalStyle = window.getComputedStyle(document.body).overflow;
const originalTouchAction = window.getComputedStyle(
document.body
).touchAction;

// Prevent scrolling on mount
document.body.style.overflow = "hidden";
document.body.style.touchAction = "none";

// Re-enable scrolling when component unmounts
return () => {
document.body.style.overflow = originalStyle;
document.body.style.touchAction = originalTouchAction;
};
}, [modalRef]); // Empty array ensures effect is only run on mount and unmount
};

export default useLockBodyScroll;

0 comments on commit ba376e1

Please sign in to comment.