[fix] site: 모달·갤러리·소식 스크롤 잠금을 훅 하나로 통합 — 복사된 스크롤 버그 재발 방지
모달 스크롤 버그를 Modal.tsx 한 곳만 고쳤더니 같은 잠금·복귀 코드가 그대로 복사돼 있던 GallerySection·EventSection 에는 버그가 그대로 남아 있었다(2026-09-21 실측). 복사한 코드는 복사한 곳마다 따로 고쳐야 하므로 훅 하나로 뺀다. - lib/ui/use-scroll-lock.ts: 스크롤 잠금·복귀 로직 신규 — scroll-behavior:auto 강제 포함 - lib/ui/Modal.tsx, sections/GallerySection.tsx, sections/items/EventSection.tsx: 중복 잠금 코드 제거, useScrollLock() 호출로 교체 - lib/ui/index.ts: useScrollLock export 추가 tsc 통과, vitest 100 passed(무관한 날씨 조건 테스트 2건은 이 변경 전부터 실패 중) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4260e20a70
commit
b386982f34
5 files changed
+52
-56
No files matched your search
@@ -2,7 +2,7 @@ import {useCallback, useEffect, useState} from 'react';
|
||||
import {ChevronLeft, ChevronRight, X} from 'lucide-react';
|
||||
import {useSite} from '@site/lib/site-context';
|
||||
import {galleryImages} from '@site/lib/derive';
|
||||
import {Carousel, CarouselSlide, Section} from '@site/lib/ui';
|
||||
import {Carousel, CarouselSlide, Section, useScrollLock} from '@site/lib/ui';
|
||||
|
||||
/**
|
||||
* 사진 갤러리.
|
||||
@@ -29,6 +29,8 @@ export function GallerySection() {
|
||||
|
||||
// 크게 보기 중에는 Esc 로 닫고 좌우 키로 넘긴다. 뒤 배경은 스크롤을 잠근다 —
|
||||
// 잠그지 않으면 모달 위에서 휠을 굴렸을 때 뒤 페이지가 밀린다.
|
||||
useScrollLock(openIndex !== null);
|
||||
|
||||
useEffect(() => {
|
||||
if (openIndex === null) return;
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
@@ -36,25 +38,8 @@ export function GallerySection() {
|
||||
if (event.key === 'ArrowLeft') step(-1);
|
||||
if (event.key === 'ArrowRight') step(1);
|
||||
};
|
||||
const scrollY = window.scrollY;
|
||||
const body = document.body;
|
||||
const prevPosition = body.style.position;
|
||||
const prevTop = body.style.top;
|
||||
const prevWidth = body.style.width;
|
||||
const prevOverflow = body.style.overflow;
|
||||
body.style.position = 'fixed';
|
||||
body.style.top = `-${scrollY}px`;
|
||||
body.style.width = '100%';
|
||||
body.style.overflow = 'hidden';
|
||||
window.addEventListener('keydown', onKey);
|
||||
return () => {
|
||||
body.style.position = prevPosition;
|
||||
body.style.top = prevTop;
|
||||
body.style.width = prevWidth;
|
||||
body.style.overflow = prevOverflow;
|
||||
window.scrollTo(0, scrollY);
|
||||
window.removeEventListener('keydown', onKey);
|
||||
};
|
||||
return () => window.removeEventListener('keydown', onKey);
|
||||
}, [openIndex, close, step]);
|
||||
|
||||
if (images.length === 0) return null;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* 카드가 무엇을 흉내 내는지 한눈에 보여야 목록이 아니라 물건으로 읽힌다.
|
||||
*/
|
||||
import {useCallback, useEffect, useState} from 'react';
|
||||
import {ShowMore} from '@site/lib/ui';
|
||||
import {ShowMore, useScrollLock} from '@site/lib/ui';
|
||||
import {X} from 'lucide-react';
|
||||
import type {EventItem} from '@o2o/shared';
|
||||
import {useSite} from '@site/lib/site-context';
|
||||
@@ -90,30 +90,15 @@ export function EventSection() {
|
||||
const close = useCallback(() => setOpen(null), []);
|
||||
|
||||
// 창이 열려 있는 동안 Esc 로 닫고 뒤 배경 스크롤을 잠근다 — 안 잠그면 창 위에서 뒤가 밀린다.
|
||||
useScrollLock(open !== null);
|
||||
|
||||
useEffect(() => {
|
||||
if (open === null) return;
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') close();
|
||||
};
|
||||
const scrollY = window.scrollY;
|
||||
const body = document.body;
|
||||
const prevPosition = body.style.position;
|
||||
const prevTop = body.style.top;
|
||||
const prevWidth = body.style.width;
|
||||
const prevOverflow = body.style.overflow;
|
||||
body.style.position = 'fixed';
|
||||
body.style.top = `-${scrollY}px`;
|
||||
body.style.width = '100%';
|
||||
body.style.overflow = 'hidden';
|
||||
window.addEventListener('keydown', onKey);
|
||||
return () => {
|
||||
body.style.position = prevPosition;
|
||||
body.style.top = prevTop;
|
||||
body.style.width = prevWidth;
|
||||
body.style.overflow = prevOverflow;
|
||||
window.scrollTo(0, scrollY);
|
||||
window.removeEventListener('keydown', onKey);
|
||||
};
|
||||
return () => window.removeEventListener('keydown', onKey);
|
||||
}, [open, close]);
|
||||
|
||||
if (parsed.items.length === 0) return null;
|
||||
|
||||
Reference in new issue
Block a user