라이트박스
네이티브 대화 상자를 사용해 페이지 위에 이미지를 크게 띄웁니다.
이 페이지의 내용
라이트박스는 페이지 위에 이미지를 크게 띄우는 컴포넌트입니다. 네이티브 <dialog>에 .lightbox 클래스를 적용하세요. 썸네일 버튼에 commandfor와 command="show-modal"을 설정해 라이트박스를 열면 스크립트 없이 브라우저가 최상위 레이어, 포커스 트랩, Esc 키로 닫는 동작을 제공합니다. 대화 상자에는 별도의 표면이 없으며 크기는 이미지와 정확히 같습니다. 뒤쪽 페이지는 어둡고 흐릿하게 처리되어 이미지가 돋보입니다.
라이트박스
<button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal"> <img class="card-media" src="/demo/moonstone.svg" alt> <span class="card-header"> <span class="card-title">Moonstone</span> <span class="card-description">View the picture</span> </span></button><dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9"> <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills"> <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button></dialog>import XIcon from '@master/icons-react/native/16/soft/x'<> <button className="card card-interactive w(240)" type="button" {...{ commandfor: 'moonstone', command: 'show-modal' }}> <img className="card-media" src="/demo/moonstone.svg" alt="" /> <span className="card-header"> <span className="card-title">Moonstone</span> <span className="card-description">View the picture</span> </span> </button> <dialog className="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style={{ '--lightbox-ratio': '16 / 9' } as React.CSSProperties}> <img className="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills" /> <p className="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button className="btn btn-sm btn-square lightbox-close" type="button" {...{ commandfor: 'moonstone', command: 'close' }} aria-label="Close"> <XIcon className="icon" /> </button> </dialog></><script> import XIcon from '@master/icons-svelte/native/16/soft/x'</script><button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal"> <img class="card-media" src="/demo/moonstone.svg" alt> <span class="card-header"> <span class="card-title">Moonstone</span> <span class="card-description">View the picture</span> </span></button><dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9"> <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills"> <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close"> <XIcon class="icon" /> </button></dialog><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal"> <img class="card-media" src="/demo/moonstone.svg" alt> <span class="card-header"> <span class="card-title">Moonstone</span> <span class="card-description">View the picture</span> </span> </button> <dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9"> <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills"> <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close"> <XIcon class="icon" /> </button> </dialog></template>#열기와 닫기
- 썸네일 버튼에
commandfor="id"와command="show-modal"을 함께 지정하면 라이트박스가 열립니다. 닫기 버튼에command="close"를 지정하면 라이트박스가 닫히고 포커스가 썸네일로 돌아갑니다. closedby="any"로 설정하면 바깥을 클릭해 닫을 수 있습니다. 대화 상자의 크기가 이미지와 정확히 같으므로 이미지 바깥을 클릭하면 대화 상자가 닫힙니다. 이 속성을 지원하지 않는 브라우저에서도 Esc 키와 닫기 버튼으로 닫을 수 있습니다.- 호출자 명령을 지원하지 않는 브라우저에서는 클릭 핸들러에서
dialog.showModal()과dialog.close()를 호출하세요. 스타일시트는 동일합니다.
@starting-style과 이산 전환을 사용해 이미지는 약간 작은 크기에서 서서히 나타나며, 닫힐 때는 열릴 때보다 빠르게 사라집니다. 이 기능을 지원하지 않으면 이미지가 즉시 나타나고 사라집니다.
#크기
lightbox-media 클래스는 <img>, <video> 또는 <iframe>에 적용합니다. 표시 크기는 파일의 원본 크기와 무관합니다. --lightbox-ratio에 지정한 가로세로 비율을 유지하면서 너비를 64rem으로 설정하되, 뷰포트의 너비와 높이에 맞춰 사방에 24px의 여백을 확보할 수 있는 크기까지 줄입니다. 별도로 지정하지 않으면 비율은 3 / 2입니다. 이미지, 동영상, 프레임 모두 같은 방식으로 크기가 정해지므로 로드될 때 레이아웃이 움직이지 않습니다.
예제에서 16 / 9로 지정한 것처럼, 대화 상자에서 --lightbox-ratio 값을 이미지의 원래 가로세로 비율로 설정하세요. 지정한 비율과 다른 이미지는 표시 영역에 맞게 잘립니다. --lightbox-width로 최대 너비를, --lightbox-gutter로 가장자리 여백을 설정합니다.
#캡션과 닫기 버튼
lightbox-caption은 이미지 아래에 떠 있는 막대로, 너비가 이미지와 같으며 이미지 배치 시 이 막대가 들어갈 공간을 확보합니다. 캡션은 한두 줄로 작성하세요. 작은 정사각형 버튼에 lightbox-close 클래스를 적용하면 버튼이 이미지 위쪽 모서리에서 안쪽으로 8px 떨어진 곳에 배치됩니다. 버튼 자체가 볼록하고 테두리가 있어 어떤 이미지 위에서도 쉽게 찾을 수 있습니다.
#라이트박스와 대화 상자 중 선택
라이트박스에는 이미지 한 장만 들어가며 사용자가 별도로 결정할 사항은 없습니다. 제목, 텍스트 또는 폼이 포함된 오버레이에는 대화 상자를 사용하세요. 페이지 안에 캡션과 함께 표시하는 이미지에는 그림을 사용하세요.
#접근성
- 대화 상자의 이름은
aria-label로 지정하거나,aria-labelledby로 캡션을 참조하도록 설정하세요. - 큰 이미지에는 내용을 설명하는
alt값을 지정하세요. 버튼의 텍스트 자체가 무엇이 열리는지 알려 준다면 버튼 안에 있는 썸네일의 해당 값은 비워 둘 수 있습니다. - 접근 가능한 이름을 가진 닫기 버튼을 항상 제공하세요. 바깥을 클릭하거나 Esc 키를 눌러 닫을 수 있다는 점을 모든 사용자가 알아차리는 것은 아닙니다.
- 라이트박스가 열리면 포커스가 내부로 이동하고, 닫히면 썸네일로 돌아갑니다. 사용자가 동작 줄이기 설정을 켜면 이미지는 크기 변화 없이 서서히 나타나고 사라집니다.
#클래스
| 클래스 | 설명 |
|---|---|
lightbox | 대화 상자 자체에는 별도의 표면이 없으며, 크기는 이미지와 정확히 같습니다. 뒤쪽 페이지는 어둡고 흐리게 표시됩니다. |
lightbox-media | 이미지, 동영상 또는 프레임입니다. 너비는 64rem이며, 뷰포트가 좁으면 가장자리 여백 24px를 확보할 수 있는 최대 너비로 표시합니다. 가로세로 비율은 --lightbox-ratio를 따릅니다. |
lightbox-caption | 이미지 아래에 떠 있는 막대로, 너비는 이미지와 같습니다. |
lightbox-close | 닫기 버튼은 이미지의 위쪽 모서리에서 안쪽으로 8px 떨어진 곳에 놓입니다. |
#사용자 지정 속성
| 속성 | 기본값 | 설명 |
|---|---|---|
--lightbox-ratio | 3 / 2 | 이미지의 가로세로 비율(너비 ÷ 높이)입니다. 이미지는 이 비율에 맞춰 잘립니다. |
--lightbox-width | 64rem | 이미지의 최대 너비 |
--lightbox-gutter | 1.5rem | 이미지와 뷰포트의 각 가장자리 사이에 확보하는 여백입니다. |
--lightbox-radius | var(--radius-control-lg) | 이미지와 캡션의 모서리 반경입니다. |