-
-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: prevent background scroll when roadmap modal is open #4710
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,17 @@ interface IModalProps { | |
| export default function Modal({ title, children, onModalClose = () => {} }: IModalProps) { | ||
| const modalRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| // Focus the modal when it mounts | ||
| // Focus the modal when it mounts and lock body scroll | ||
| useEffect(() => { | ||
| if (modalRef.current) modalRef.current.focus(); | ||
|
|
||
| // Prevent background scroll when modal is open | ||
| document.body.style.overflow = 'hidden'; | ||
|
|
||
| // Cleanup: restore scroll when modal is closed | ||
| return () => { | ||
| document.body.style.overflow = ''; | ||
| }; | ||
| }, []); | ||
|
Comment on lines
+18
to
29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, examine the Modal component file structure
find . -type f -name "Modal.tsx" | head -5Repository: asyncapi/website Length of output: 82 🏁 Script executed: # Check for all references to body.style.overflow in the codebase
rg "body\.style\.overflow|document\.body\.style\.overflow" --type tsx --type ts -nRepository: asyncapi/website Length of output: 87 🏁 Script executed: # Search for Modal usage patterns to understand stacking scenarios
rg "Modal|<Modal" --type tsx --type ts -B 2 -A 2 | head -100Repository: asyncapi/website Length of output: 87 🏁 Script executed: # Check for overscroll-behavior in CSS or Tailwind configs
rg "overscroll|scroll-contain" --type css --type tsx -nRepository: asyncapi/website Length of output: 87 🏁 Script executed: # Look at the complete Modal component to understand context
cat -n components/Modal.tsx 2>/dev/null || find . -path "*/components/Modal.tsx" -exec cat -n {} \;Repository: asyncapi/website Length of output: 3256 🏁 Script executed: # Search for Modal imports and usage
grep -r "Modal" --include="*.tsx" --include="*.ts" --include="*.jsx" --include="*.js" | grep -i "import\|<Modal" | head -20Repository: asyncapi/website Length of output: 326 🏁 Script executed: # Search for body.style.overflow without file type restriction
grep -r "body\.style\.overflow\|document\.body\.style\.overflow" . 2>/dev/null | grep -v node_modulesRepository: asyncapi/website Length of output: 191 🏁 Script executed: # Check if there are multiple Modal instances rendered simultaneously anywhere
grep -r "<Modal" --include="*.tsx" --include="*.jsx" -B 5 -A 5 | head -60Repository: asyncapi/website Length of output: 721 🏁 Script executed: # Look for any overflow manipulation in CSS or global styles
find . -name "*.css" -o -name "tailwind.config.*" | xargs grep -l "overflow\|overscroll" 2>/dev/null | head -10Repository: asyncapi/website Length of output: 101 🏁 Script executed: # Check globals.css for any overflow or body styles
cat -n ./styles/globals.css | head -100Repository: asyncapi/website Length of output: 2501 🏁 Script executed: # Check tailwind.config.ts for overscroll or scroll-related settings
cat -n ./tailwind.config.tsRepository: asyncapi/website Length of output: 7484 Restore previous The current cleanup sets export default function Modal({ title, children, onModalClose = () => {} }: IModalProps) {
const modalRef = useRef<HTMLDivElement>(null);
+ const prevBodyOverflowRef = useRef<string>('');
// Focus the modal when it mounts and lock body scroll
useEffect(() => {
if (modalRef.current) modalRef.current.focus();
// Prevent background scroll when modal is open
- document.body.style.overflow = 'hidden';
+ prevBodyOverflowRef.current = document.body.style.overflow;
+ document.body.style.overflow = 'hidden';
// Cleanup: restore scroll when modal is closed
return () => {
- document.body.style.overflow = '';
+ document.body.style.overflow = prevBodyOverflowRef.current;
};
}, []);The optional
🤖 Prompt for AI Agents |
||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation doesn't handle the case where multiple Modal instances might be mounted simultaneously. If two modals are open and the first one closes, it will restore body scroll even though the second modal is still open. Consider implementing a reference counting mechanism or using a shared state manager to track the number of open modals, and only restore overflow when the count reaches zero.