Files
ServerKit/frontend/src/hooks/useLockBodyScroll.js
T
Juan DenisandClaude Opus 4.8 9bd6d18f2c refactor(frontend): Phase 5 §8.3/§8.2 — useLockBodyScroll hook + markdown link hardening
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>
2026-06-24 02:24:27 -04:00

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;