useGallery - 이미지 갤러리 내비게이션·줌·회전·전체화면을 관리하는 훅
useGallery는 이미지 뷰어의 내비게이션, 전체화면, 툴바 상태를 총괄하는 헤드리스 훅이에요. 이미지별 변형(줌·회전·플립·팬)은 useGalleryItem이 담당해서, 두 훅이 역할을 나눠 이미지 갤러리 전체를 조율해요.
출처: 문서
본문
사용법 (Usage)
import { useGallery } from '@primereact/headless/gallery';
import { useGalleryItem } from '@primereact/headless/gallery/item';
const gallery = useGallery();
const item = useGalleryItem({ gallery });
<div {...gallery.rootProps}>
<div {...gallery.backdropProps}></div>
<button {...gallery.prevProps}></button>
<button {...gallery.nextProps}></button>
<div {...gallery.headerProps}>
<button {...gallery.zoomInProps}></button>
<button {...gallery.zoomOutProps}></button>
<button {...gallery.downloadProps}></button>
</div>
<div {...gallery.contentProps}>
<div {...item.itemProps}></div>
</div>
<div {...gallery.footerProps}></div>
</div>;
useGallery는 이미지 뷰어의 내비게이션, 전체화면, 툴바 상태를 총괄하고, useGalleryItem은 이미지별 변형을 처리해요. 컴포넌트 기반 API는 Primitive 문서를 확인하세요.
기능 (Features)
- 두 훅 분리 (Two-hook split),
useGallery는 내비게이션과 전체화면을,useGalleryItem은 이미지별 줌·회전·플립·팬을 담당해요 - 이전/다음 및 썸네일 내비게이션 (Prev/next and thumbnail navigation), 미리 배선된 버튼 props와 클릭 가능한 미리보기를 위한
thumbnailItemProps(index)를 제공해요 - 이미지 동작 툴바 (Image action toolbar), 줌 인/아웃/토글, 좌·우 회전, X/Y 플립, 다운로드, 전체화면을 위한 펼쳐 쓸 준비가 된 props를 제공해요
- CSS 변수로 변형 (Transform via CSS variables),
useGalleryItem이--scale,--rotation,--flip-x,--flip-y,--position-x,--position-y를 써서 템플릿을 선언적으로 유지해요 - 명령형 컨트롤 (Imperative controls), 커스텀 바인딩을 위한
selectItem,handleNext,handlePrev,toggleFullScreen을 제공해요 - 반응형 상태 (Reactive state),
state.activeIndex,state.isFullscreen,state.zoomed,state.rotated,state.flipped,state.pendingAction을 제공해요
콜백 다루기 (Working with callbacks)
제어되는 활성 인덱스 (Controlled active index)
activeIndex를 onActiveIndexChange와 함께 쓰면 현재 이미지를 URL 파라미터나 썸네일 같은 주변 UI와 동기화할 수 있어요.
const [index, setIndex] = React.useState(0);
const gallery = useGallery({
activeIndex: index,
onActiveIndexChange: (e) => setIndex(e.value)
});
툴바 배선 (Toolbar wiring)
툴바 props는 자체적으로 disabled 상태를 관리해요. zoomInProps는 이미 줌된 상태에서 비활성화되고, zoomOutProps는 줌 해제 상태에서 비활성화돼요.
<button {...gallery.zoomInProps}>Zoom In</button>
<button {...gallery.zoomOutProps}>Zoom Out</button>
<button {...gallery.rotateLeftProps}>Rotate Left</button>
<button {...gallery.rotateRightProps}>Rotate Right</button>
<button {...gallery.flipXProps}>Flip Horizontal</button>
<button {...gallery.downloadProps}>Download</button>
<button {...gallery.fullScreenProps}>Toggle Fullscreen</button>
useGalleryItem로 이미지별 변형 (Per-item transforms with useGalleryItem)
각 항목은 자체 변형 상태를 인스턴스화해요. transform에 CSS 변수를 적용해 조합하면 돼요.
function GalleryItemView({ image, gallery }) {
const item = useGalleryItem({ gallery });
return (
<div
{...item.itemProps}
style={{
...item.itemProps.style,
display: item.state.isActive ? 'flex' : 'none',
transform: 'translate(var(--px-position-x), var(--px-position-y)) scale(var(--px-scale)) rotate(var(--px-rotation)) scaleX(var(--px-flip-x)) scaleY(var(--px-flip-y))'
}}
>
<img src={image} />
</div>
);
}
썸네일 스트립 (Thumbnail strip)
thumbnailItemProps(index)는 클릭 핸들러와 data-active를 붙여 썸네일이 activeIndex와 동기화되게 해 줘요.
{
images.map((src, i) => (
<div key={i} {...gallery.thumbnailItemProps(i)}>
<img src={src} />
</div>
));
}
전체화면·변형 상태에 반응하기 (Reacting to fullscreen and transform state)
반응형 상태를 읽어 문맥에 맞는 UI를 보여주거나, 뷰어가 전체화면일 때 배경 미디어를 일시정지할 수 있어요.
const gallery = useGallery();
{
gallery.state.isFullscreen && <span>Fullscreen Mode</span>;
}
{
gallery.state.zoomed && <span>Zoomed</span>;
}
데이터 속성으로 스타일링 (Styling with data attributes)
props 객체는 각 섹션에 data-scope="gallery"와 data-part를 포함하고, 루트 요소에는 상태 종속 속성이 붙어요.
[data-scope='gallery'][data-fullscreen] {
position: fixed;
inset: 0;
z-index: 9999;
}
[data-scope='gallery'][data-zoomed] {
cursor: zoom-out;
}
[data-scope='gallery'][data-part='item'][data-active='true'] {
display: block;
}
[data-scope='gallery'][data-part='item'][data-active='false'] {
display: none;
}
[data-scope='gallery'][data-part='thumbnailItem'][data-active] {
opacity: 1;
}
API
useGallery
useGalleryItem
접근성 (Accessibility)
화살표 키로 항목을 이동하고, Home/End로 처음/마지막으로 건너뛰며, Escape로 전체화면 보기를 종료해요. 전체 WAI-ARIA 준수 상세는 Primitive 문서를 확인하세요.