Resize Observer는 뷰포트가 아니라 특정 요소의 크기를 관찰합니다. 부모 패널의 크기가 바뀌는 컴포넌트도 자신의 실제 공간에 맞춰 반응할 수 있습니다.
데모의 패널 폭이 자동으로 변하고 측정값이 따라갑니다. 관찰 콜백에서 즉시 다시 크기를 바꾸면 반복 알림이 생길 수 있으므로 갱신 흐름을 설계해야 합니다.
언제 쓰나
차트나 캔버스를 부모 컨테이너 크기에 맞출 때 씁니다.
요소의 크기가 바뀌면 새 크기를 콜백으로 받습니다.
<div class="v"><header>RESIZE OBSERVER <span id="width">— px</span></header><div class="stage size"><div class="resizable" id="box"><div class="bars"><i></i><i></i><i></i></div><span id="mode">wide layout</span></div><div class="ruler"></div></div><div class="muted">container width → component layout</div></div>.v{width:min(94vw,760px);height:min(88vh,320px);padding:clamp(9px,2.4vmin,18px);border:1px solid var(--line);border-radius:12px;background:var(--surface);font:500 clamp(12px,2.4vmin,16px)/1.3 var(--font-sans,system-ui,sans-serif);display:flex;flex-direction:column;gap:clamp(5px,1.4vmin,10px);overflow:hidden}.v header{display:flex;align-items:center;justify-content:space-between;gap:8px;color:var(--accent);font-weight:700}.v header span{color:var(--muted);font-weight:500}.v .stage{flex:1;min-height:0;position:relative}.v .muted{color:var(--muted)}.v .active{background:color-mix(in srgb,var(--accent) 18%,var(--surface));border-color:var(--accent)!important;color:var(--fg)}.v button{font:inherit;color:var(--fg);background:var(--bg);border:1px solid var(--line);border-radius:7px;padding:5px 8px;cursor:pointer}.size{display:flex;align-items:center;justify-content:center;flex-direction:column;gap:5px}.resizable{width:75%;height:68%;border:2px solid var(--accent);border-radius:8px;background:var(--bg);display:flex;align-items:center;justify-content:center;gap:8px;animation:resize 3s ease-in-out infinite alternate}.bars{display:flex;align-items:end;gap:4px;height:35px}.bars i{display:block;width:10px;background:var(--accent);height:50%}.bars i:nth-child(2){height:100%}.bars i:nth-child(3){height:70%}.ruler{width:75%;border-top:1px dashed var(--muted)}@keyframes resize{to{width:42%}}@media(max-width:420px){.resizable{width:82%}@keyframes resize{to{width:52%}}}const box=document.getElementById("box");new ResizeObserver(([entry])=>{const w=Math.round(entry.contentRect.width);document.getElementById("width").textContent=w+" px";document.getElementById("mode").textContent=w<150?"compact":"wide layout"}).observe(box)Resize Observer는 뷰포트가 아니라 특정 요소의 크기를 관찰합니다. 부모 패널의 크기가 바뀌는 컴포넌트도 자신의 실제 공간에 맞춰 반응할 수 있습니다.
데모의 패널 폭이 자동으로 변하고 측정값이 따라갑니다. 관찰 콜백에서 즉시 다시 크기를 바꾸면 반복 알림이 생길 수 있으므로 갱신 흐름을 설계해야 합니다.
차트나 캔버스를 부모 컨테이너 크기에 맞출 때 씁니다.