mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-05 22:51:51 +09:00
Per docs/plans/FRONTEND_UX_TAILWIND_CLEANUP_PLAN.md Phase 5. - Add hooks/useLockBodyScroll.js: a ref-counted body-scroll lock that composes nested locks correctly (a modal over an already-locked drawer no longer restores scroll prematurely). Replaces the duplicated document.body.style.overflow toggles in DashboardLayout (mobile nav drawer), PreviewDrawer (file preview), and ChatDrawer (mobile AI sheet). - lib/ai/markdown.js: escape quotes in link URLs. The renderer already HTML-escapes all input and only emits allowlisted tags, but `"`/`'` in a crafted URL could break out of the href attribute — now percent-encoded. Verified: npm run build passes; eslint adds no new errors. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
35 lines
988 B
JavaScript
35 lines
988 B
JavaScript
import { useEffect } from 'react';
|
|
|
|
// Lock body scroll while `locked` is true. Ref-counted so nested locks (e.g. a
|
|
// modal opening over an already-locked drawer) compose correctly — the body
|
|
// overflow is only restored once the LAST active lock is released.
|
|
//
|
|
// useLockBodyScroll(isOpen);
|
|
//
|
|
// Replaces the hand-rolled `document.body.style.overflow` toggles that were
|
|
// duplicated across DashboardLayout, PreviewDrawer, and ChatDrawer.
|
|
|
|
let lockCount = 0;
|
|
let originalOverflow = '';
|
|
|
|
export function useLockBodyScroll(locked = true) {
|
|
useEffect(() => {
|
|
if (!locked || typeof document === 'undefined') return undefined;
|
|
|
|
if (lockCount === 0) {
|
|
originalOverflow = document.body.style.overflow;
|
|
document.body.style.overflow = 'hidden';
|
|
}
|
|
lockCount += 1;
|
|
|
|
return () => {
|
|
lockCount -= 1;
|
|
if (lockCount === 0) {
|
|
document.body.style.overflow = originalOverflow;
|
|
}
|
|
};
|
|
}, [locked]);
|
|
}
|
|
|
|
export default useLockBodyScroll;
|