09Lightbox That Instantly Reflects Reduced Motion
When reduced motion is requested, every expand, swipe, and fling movement is skipped, and the image and backdrop simply appear and disappear instantly like a switch.
Best forAny image lightbox that needs accessibility support
matchMediaprefers-reduced-motionchange event

<main class="stage">
<div class="scene is-demo" id="scene">
<div class="switchRow">
<button class="switch" id="switchBtn" type="button" role="switch" aria-checked="false" aria-label="Reduced-motion simulation">
<span class="switchKnob" id="switchKnob"></span>
</button>
<p class="switchLabel" id="switchLabel">Motion normal</p>
</div>
<ul class="grid" id="grid">
<li><button class="thumb" id="thumb0" type="button" aria-haspopup="dialog"><span class="thumbImg"></span></button></li>
</ul>
<div class="backdrop" id="backdrop"></div>
<div class="lightbox" id="lightbox" role="dialog" aria-modal="true" aria-hidden="true" aria-label="Reduced motion comparison">
<span class="fullImg" id="fullImg"></span>
<p class="modeNote" id="modeNote">Opened with normal motion</p>
<button class="closeBtn" id="closeBtn" type="button">Close</button>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 변수 3개
$duration: $dur-base;
$easing: $ease-out;
$color: $subject-lilac;
// 계산 겹: index.html 은 실제 `matchMedia('(prefers-reduced-motion: reduce)')` 결과와 화면의
// 시뮬레이션 스위치를 OR 로 합친 effectiveReduce() 하나로 열기/닫기 트윈 실행 여부를 결정한다 —
// 실제 설정이 바뀌는 change 이벤트와 스위치 클릭 둘 다 같은 함수로 수렴한다.
@include stage($stage-ink);
.scene {
position: relative;
width: 320px;
height: 180px;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
background: linear-gradient(160deg, #232a3a, $stage-ink 85%);
}
.switchRow {
position: absolute;
top: $sp-2;
left: $sp-2;
display: flex;
align-items: center;
gap: $sp-2;
z-index: 2;
}
.switch {
appearance: none;
border: none;
width: 36px;
height: 20px;
border-radius: $r-pill;
background: rgba($subject-cream, .28);
padding: $sp-1;
cursor: pointer;
position: relative;
&.is-on { background: $color; }
}
.switchKnob {
display: block;
width: 12px;
height: 12px;
border-radius: 50%;
background: $subject-cream;
transform: translateX(0);
transition: transform $dur-quick $ease-out;
.switch.is-on & { transform: translateX(16px); }
}
.switchLabel {
margin: 0;
font-size: 9px;
font-weight: 700;
color: $subject-cream;
}
.grid {
position: absolute;
inset: 0;
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
.thumb {
appearance: none;
border: none;
background: none;
padding: 0;
cursor: pointer;
&:focus-visible .thumbImg { outline: 2px solid $color; outline-offset: 2px; }
}
.thumbImg {
display: block;
width: 72px;
height: 72px;
border-radius: $r-sm;
box-shadow: $shadow-press;
background: linear-gradient(135deg, $subject-lilac, $subject-blue);
}
.backdrop {
position: absolute;
inset: 0;
background: $subject-ink;
opacity: 0;
visibility: hidden;
pointer-events: none;
}
.lightbox {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: $sp-2;
visibility: hidden;
}
.fullImg {
display: block;
width: 190px;
height: 110px;
border-radius: $r-card;
background: linear-gradient(135deg, $subject-lilac, $subject-blue 55%, $subject-sky);
}
.modeNote {
margin: 0;
font-size: 10px;
font-weight: 700;
color: $subject-cream;
background: rgba(#000, .38);
padding: $sp-1 $sp-3;
border-radius: $r-pill;
}
.closeBtn {
position: absolute;
top: $sp-2;
right: $sp-2;
appearance: none;
border: none;
border-radius: $r-pill;
padding: $sp-1 $sp-3;
font-size: 11px;
font-weight: 600;
color: $subject-ink;
background: rgba($subject-cream, .92);
cursor: pointer;
&:focus-visible { outline: 2px solid $color; outline-offset: 2px; }
}
@media (max-width: 340px) {
.scene { width: 220px; height: 124px; }
.fullImg { width: 140px; height: 84px; }
.switchLabel { font-size: 8px; }
}
@media (prefers-reduced-motion: reduce) {
// 실사용 경로 적용 — 열기/닫기 트윈은 index.html 의 effectiveReduce() 가 실제 매체쿼리를 읽어
// reduce 시 animate() 를 건너뛰고 즉시 최종 상태로 처리한다. 스위치 노브의 슬라이드도 여기서 끈다.
.switchKnob { transition: none; }
.closeBtn, .thumb { transition: none; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.scene {
position: relative;
width: 320px;
height: 180px;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
background: linear-gradient(160deg, #232a3a, #17141a 85%);
}
.switchRow {
position: absolute;
top: 8px;
left: 8px;
display: flex;
align-items: center;
gap: 8px;
z-index: 2;
}
.switch {
appearance: none;
border: none;
width: 36px;
height: 20px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.28);
padding: 4px;
cursor: pointer;
position: relative;
}
.switch.is-on {
background: #b9a0ff;
}
.switchKnob {
display: block;
width: 12px;
height: 12px;
border-radius: 50%;
background: #fff7e6;
transform: translateX(0);
transition: transform 220ms ease-out;
}
.switch.is-on .switchKnob {
transform: translateX(16px);
}
.switchLabel {
margin: 0;
font-size: 9px;
font-weight: 700;
color: #fff7e6;
}
.grid {
position: absolute;
inset: 0;
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
.thumb {
appearance: none;
border: none;
background: none;
padding: 0;
cursor: pointer;
}
.thumb:focus-visible .thumbImg {
outline: 2px solid #b9a0ff;
outline-offset: 2px;
}
.thumbImg {
display: block;
width: 72px;
height: 72px;
border-radius: 8px;
box-shadow: 0 6px 12px rgba(23, 20, 26, 0.32);
background: linear-gradient(135deg, #b9a0ff, #2f6df6);
}
.backdrop {
position: absolute;
inset: 0;
background: #17141a;
opacity: 0;
visibility: hidden;
pointer-events: none;
}
.lightbox {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
visibility: hidden;
}
.fullImg {
display: block;
width: 190px;
height: 110px;
border-radius: 16px;
background: linear-gradient(135deg, #b9a0ff, #2f6df6 55%, #6ec8ff);
}
.modeNote {
margin: 0;
font-size: 10px;
font-weight: 700;
color: #fff7e6;
background: rgba(0, 0, 0, 0.38);
padding: 4px 12px;
border-radius: 999px;
}
.closeBtn {
position: absolute;
top: 8px;
right: 8px;
appearance: none;
border: none;
border-radius: 999px;
padding: 4px 12px;
font-size: 11px;
font-weight: 600;
color: #17141a;
background: rgba(255, 247, 230, 0.92);
cursor: pointer;
}
.closeBtn:focus-visible {
outline: 2px solid #b9a0ff;
outline-offset: 2px;
}
@media (max-width: 340px) {
.scene {
width: 220px;
height: 124px;
}
.fullImg {
width: 140px;
height: 84px;
}
.switchLabel {
font-size: 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.switchKnob {
transition: none;
}
.closeBtn, .thumb {
transition: none;
}
}(function () {
var OPEN_MS = 420;
var CLOSE_MS = 320;
var HOLD_MS = 380;
var LOOP_MS = 2000;
var scene = document.getElementById('scene');
var backdrop = document.getElementById('backdrop');
var lightbox = document.getElementById('lightbox');
var fullImg = document.getElementById('fullImg');
var modeNote = document.getElementById('modeNote');
var closeBtn = document.getElementById('closeBtn');
var thumb = document.getElementById('thumb0');
var switchBtn = document.getElementById('switchBtn');
var switchLabel = document.getElementById('switchLabel');
// 실제 OS/브라우저 설정 — 이 값이 true 면 시뮬레이션 스위치와 무관하게 항상 즉시 전환한다
var mql = window.matchMedia('(prefers-reduced-motion: reduce)');
var realReduce = mql.matches;
// 시연용 시뮬레이션 스위치 — 실제 설정이 "보통"이어도 이 편의 라이트박스만 강제로 즉시 전환해 차이를 비교한다
var simulateReduce = false;
function effectiveReduce() { return realReduce || simulateReduce; }
var open = false;
var timers = [];
function clamp(v, lo, hi) { return Math.min(hi, Math.max(lo, v)); }
function easeOutCubic(t) { return 1 - Math.pow(1 - t, 3); }
function animate(duration, onFrame, onDone) {
if (effectiveReduce()) { onFrame(1); if (onDone) onDone(); return; }
var start = performance.now();
function frame(now) {
var p = clamp((now - start) / duration, 0, 1);
onFrame(easeOutCubic(p));
if (p < 1) requestAnimationFrame(frame);
else if (onDone) onDone();
}
requestAnimationFrame(frame);
}
function updateSwitchUI() {
switchBtn.setAttribute('aria-checked', simulateReduce ? 'true' : 'false');
switchBtn.classList.toggle('is-on', simulateReduce);
if (realReduce) {
switchLabel.textContent = 'Actual setting: reduced motion on';
} else {
switchLabel.textContent = simulateReduce ? 'Reduced-motion simulation on' : 'Motion normal';
}
}
function setSimulate(on) {
simulateReduce = on;
updateSwitchUI();
}
function openFrom() {
if (open) return;
open = true;
var reduceNow = effectiveReduce();
modeNote.textContent = reduceNow ? 'Switched instantly (reduced motion)' : 'Opened with normal motion';
fullImg.style.transform = 'scale(.82)';
fullImg.style.opacity = '0';
lightbox.style.visibility = 'visible';
lightbox.setAttribute('aria-hidden', 'false');
backdrop.style.visibility = 'visible';
animate(OPEN_MS, function (e) {
fullImg.style.transform = 'scale(' + (0.82 + 0.18 * e).toFixed(4) + ')';
fullImg.style.opacity = e.toFixed(3);
backdrop.style.opacity = (0.74 * e).toFixed(3);
}, function () {
closeBtn.focus({ preventScroll: true });
});
}
function closeLightbox() {
if (!open) return;
animate(CLOSE_MS, function (e) {
fullImg.style.opacity = (1 - e).toFixed(3);
backdrop.style.opacity = (0.74 * (1 - e)).toFixed(3);
}, function () {
lightbox.style.visibility = 'hidden';
lightbox.setAttribute('aria-hidden', 'true');
backdrop.style.visibility = 'hidden';
open = false;
thumb.focus({ preventScroll: true });
});
}
function human() {
if (scene.classList.contains('is-demo')) {
scene.classList.remove('is-demo');
timers.forEach(function (id) { clearTimeout(id); clearInterval(id); });
timers = [];
}
}
thumb.addEventListener('click', function () { human(); openFrom(); });
closeBtn.addEventListener('click', function () { human(); closeLightbox(); });
backdrop.addEventListener('click', function () { human(); closeLightbox(); });
switchBtn.addEventListener('click', function () { human(); setSimulate(!simulateReduce); });
document.addEventListener('keydown', function (e) {
if (open && e.key === 'Escape') { human(); closeLightbox(); }
});
// 실제 설정이 바뀌면(운영체제·브라우저 쪽) 즉시 반영한다 — 시뮬레이션과 별개의 진짜 경로
function onRealChange(e) {
realReduce = e.matches;
updateSwitchUI();
}
if (mql.addEventListener) mql.addEventListener('change', onRealChange);
else if (mql.addListener) mql.addListener(onRealChange);
function cycle() {
setSimulate(false);
openFrom();
timers.push(setTimeout(closeLightbox, OPEN_MS + HOLD_MS));
timers.push(setTimeout(function () {
setSimulate(true);
openFrom();
}, OPEN_MS + HOLD_MS + CLOSE_MS + 160));
timers.push(setTimeout(closeLightbox, OPEN_MS + HOLD_MS + CLOSE_MS + 160 + HOLD_MS));
}
updateSwitchUI();
if (!realReduce) {
cycle();
timers.push(setInterval(function () {
if (scene.classList.contains('is-demo')) cycle();
}, LOOP_MS));
} else {
// 실제 설정이 이미 줄이기라면 루프를 돌리지 않고 정적 상태로 둔다 — 실사용 경로 그대로
modeNote.textContent = 'Switched instantly (reduced motion)';
}
})();import { useEffect, useRef } from 'react';
import styles from './ReducedMotionInstant.module.scss';
const OPEN_MS = 420;
const CLOSE_MS = 320;
const HOLD_MS = 380;
const LOOP_MS = 2000;
export default function ReducedMotionInstant() {
const sceneRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const scene0 = sceneRef.current;
if (!scene0) return undefined;
const backdrop0 = scene0.querySelector<HTMLDivElement>('.' + styles.backdrop);
const lightbox0 = scene0.querySelector<HTMLDivElement>('.' + styles.lightbox);
const fullImg0 = scene0.querySelector<HTMLSpanElement>('.' + styles.fullImg);
const modeNote0 = scene0.querySelector<HTMLParagraphElement>('.' + styles.modeNote);
const closeBtn0 = scene0.querySelector<HTMLButtonElement>('.' + styles.closeBtn);
const thumb0 = scene0.querySelector<HTMLButtonElement>('.' + styles.thumb);
const switchBtn0 = scene0.querySelector<HTMLButtonElement>('.' + styles.switch);
const switchLabel0 = scene0.querySelector<HTMLParagraphElement>('.' + styles.switchLabel);
if (!backdrop0 || !lightbox0 || !fullImg0 || !modeNote0 || !closeBtn0 || !thumb0 || !switchBtn0 || !switchLabel0) return undefined;
const scene = scene0;
const backdrop = backdrop0;
const lightbox = lightbox0;
const fullImg = fullImg0;
const modeNote = modeNote0;
const closeBtn = closeBtn0;
const thumb = thumb0;
const switchBtn = switchBtn0;
const switchLabel = switchLabel0;
// 실제 OS/브라우저 설정 — 이 값이 true 면 시뮬레이션 스위치와 무관하게 항상 즉시 전환한다
const mql = window.matchMedia('(prefers-reduced-motion: reduce)');
let realReduce = mql.matches;
// 시연용 시뮬레이션 스위치 — 실제 설정이 "보통"이어도 이 편의 라이트박스만 강제로 즉시 전환해 차이를 비교한다
let simulateReduce = false;
function effectiveReduce() { return realReduce || simulateReduce; }
let open = false;
let timers: number[] = [];
function clamp(v: number, lo: number, hi: number) { return Math.min(hi, Math.max(lo, v)); }
function easeOutCubic(t: number) { return 1 - Math.pow(1 - t, 3); }
function animate(duration: number, onFrame: (e: number) => void, onDone?: () => void) {
if (effectiveReduce()) { onFrame(1); if (onDone) onDone(); return; }
const start = performance.now();
function frame(now: number) {
const p = clamp((now - start) / duration, 0, 1);
onFrame(easeOutCubic(p));
if (p < 1) requestAnimationFrame(frame);
else if (onDone) onDone();
}
requestAnimationFrame(frame);
}
function updateSwitchUI() {
switchBtn.setAttribute('aria-checked', simulateReduce ? 'true' : 'false');
switchBtn.classList.toggle(styles['is-on'], simulateReduce);
if (realReduce) {
switchLabel.textContent = '실제 설정: 모션 줄이기 켜짐';
} else {
switchLabel.textContent = simulateReduce ? '모션 줄이기 시뮬레이션 켜짐' : '모션 보통';
}
}
function setSimulate(on: boolean) {
simulateReduce = on;
updateSwitchUI();
}
function openFrom() {
if (open) return;
open = true;
const reduceNow = effectiveReduce();
modeNote.textContent = reduceNow ? '즉시 전환 (모션 줄이기)' : '보통 모션으로 열림';
fullImg.style.transform = 'scale(.82)';
fullImg.style.opacity = '0';
lightbox.style.visibility = 'visible';
lightbox.setAttribute('aria-hidden', 'false');
backdrop.style.visibility = 'visible';
animate(OPEN_MS, (e) => {
fullImg.style.transform = 'scale(' + (0.82 + 0.18 * e).toFixed(4) + ')';
fullImg.style.opacity = e.toFixed(3);
backdrop.style.opacity = (0.74 * e).toFixed(3);
}, () => {
closeBtn.focus({ preventScroll: true });
});
}
function closeLightbox() {
if (!open) return;
animate(CLOSE_MS, (e) => {
fullImg.style.opacity = (1 - e).toFixed(3);
backdrop.style.opacity = (0.74 * (1 - e)).toFixed(3);
}, () => {
lightbox.style.visibility = 'hidden';
lightbox.setAttribute('aria-hidden', 'true');
backdrop.style.visibility = 'hidden';
open = false;
thumb.focus({ preventScroll: true });
});
}
function human() {
if (scene.classList.contains(styles['is-demo'])) {
scene.classList.remove(styles['is-demo']);
timers.forEach((id) => { window.clearTimeout(id); window.clearInterval(id); });
timers = [];
}
}
function onThumbClick() { human(); openFrom(); }
function onCloseClick() { human(); closeLightbox(); }
function onBackdropClick() { human(); closeLightbox(); }
function onSwitchClick() { human(); setSimulate(!simulateReduce); }
function onKeydown(e: KeyboardEvent) {
if (open && e.key === 'Escape') { human(); closeLightbox(); }
}
thumb.addEventListener('click', onThumbClick);
closeBtn.addEventListener('click', onCloseClick);
backdrop.addEventListener('click', onBackdropClick);
switchBtn.addEventListener('click', onSwitchClick);
document.addEventListener('keydown', onKeydown);
// 실제 설정이 바뀌면(운영체제·브라우저 쪽) 즉시 반영한다 — 시뮬레이션과 별개의 진짜 경로
function onRealChange(e: MediaQueryListEvent) {
realReduce = e.matches;
updateSwitchUI();
}
if (mql.addEventListener) mql.addEventListener('change', onRealChange);
function cycle() {
setSimulate(false);
openFrom();
timers.push(window.setTimeout(closeLightbox, OPEN_MS + HOLD_MS));
timers.push(window.setTimeout(() => {
setSimulate(true);
openFrom();
}, OPEN_MS + HOLD_MS + CLOSE_MS + 160));
timers.push(window.setTimeout(closeLightbox, OPEN_MS + HOLD_MS + CLOSE_MS + 160 + HOLD_MS));
}
updateSwitchUI();
if (!realReduce) {
cycle();
timers.push(window.setInterval(() => {
if (scene.classList.contains(styles['is-demo'])) cycle();
}, LOOP_MS));
} else {
// 실제 설정이 이미 줄이기라면 루프를 돌리지 않고 정적 상태로 둔다 — 실사용 경로 그대로
modeNote.textContent = '즉시 전환 (모션 줄이기)';
}
return () => {
timers.forEach((id) => { window.clearTimeout(id); window.clearInterval(id); });
thumb.removeEventListener('click', onThumbClick);
closeBtn.removeEventListener('click', onCloseClick);
backdrop.removeEventListener('click', onBackdropClick);
switchBtn.removeEventListener('click', onSwitchClick);
document.removeEventListener('keydown', onKeydown);
if (mql.removeEventListener) mql.removeEventListener('change', onRealChange);
};
}, []);
return (
<div className={`${styles.scene} ${styles['is-demo']}`} ref={sceneRef}>
<div className={styles.switchRow}>
<button className={styles.switch} type="button" role="switch" aria-checked="false" aria-label="모션 줄이기 시뮬레이션">
<span className={styles.switchKnob} />
</button>
<p className={styles.switchLabel}>모션 보통</p>
</div>
<ul className={styles.grid}>
<li><button className={styles.thumb} type="button" aria-haspopup="dialog"><span className={styles.thumbImg} /></button></li>
</ul>
<div className={styles.backdrop} />
<div className={styles.lightbox} role="dialog" aria-modal="true" aria-hidden="true" aria-label="모션 줄이기 비교">
<span className={styles.fullImg} />
<p className={styles.modeNote}>보통 모션으로 열림</p>
<button className={styles.closeBtn} type="button">닫기</button>
</div>
</div>
);
}09 Lightbox That Instantly Reflects Reduced Motion — Measured area 22.44% · moving frames 17/23Original by GODRICH · SCSS and React ship in the zip · CSS Lightbox Gallery Animation — 9 Transitions
The post this demo lives in
CSS Lightbox Gallery Animation — 9 TransitionsAmong nine css lightbox gallery ideas, one grows from the clicked thumbnail's spot, one times a downward flick to decide See all demos in this post
If you like this effect - similar demos
Transitions09 Reduced-motion instant toggle — with reduced motion on, every transition from the prior eight bars switches straight to its target valueCSS Bottom Navigation Bar: 9 Transition Patterns
Transitions09 Safeguard That Detects a Reduced-Motion Value and Turns the Transition OffView Transitions API: 9 Patterns by One Value
Transitions06 Empty-state crossfade — a message that fades out the instant the item count value leaves zeroCart Drawer UI Patterns: 9 Motion Techniques