Declarative focusgroup navigation

선언적 화살표 키 탐색

An HTML attribute declares arrow-key movement across toolbar items.

···
html
<div class="v focusdemo"><header>FOCUSGROUP <span id="focusLabel">Bold</span></header><div class="stage focus-stage"><div class="toolbar" focusgroup="toolbar wrap" aria-label="Text formatting"><button id="f0">Bold</button><button id="f1">Italic</button><button id="f2">Underline</button></div><div class="focus-path"><span>Tab enters group</span><b>←</b><b>→</b><span>arrows move focus</span></div></div><div class="muted">one toolbar · three focus stops</div></div>
css
.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}.focus-stage{display:flex;align-items:center;justify-content:center;flex-direction:column;gap:15px}.toolbar{display:flex;gap:5px;padding:7px;border:1px solid var(--line);border-radius:9px;background:var(--bg)}.toolbar button{font-size:clamp(12px,2.4vmin,14px);padding:7px;min-width:58px}.toolbar button:focus,.toolbar button.on{outline:2px solid var(--accent);outline-offset:2px;color:var(--accent)}.focus-path{display:flex;align-items:center;gap:7px;color:var(--muted);font-size:clamp(12px,2.2vmin,13px)}.focus-path b{color:var(--accent);font-size:17px}
js
const toolbar=document.querySelector(".toolbar"),buttons=[...toolbar.querySelectorAll("button")];let i=0;function show(n){i=n;buttons.forEach((b,j)=>b.classList.toggle("on",j===i));buttons[i].focus();document.getElementById("focusLabel").textContent=buttons[i].textContent}show(0);let timer=setInterval(()=>show((i+1)%buttons.length),850);toolbar.addEventListener("pointerdown",()=>{clearInterval(timer);timer=0});toolbar.addEventListener("focusin",e=>{if(!timer){i=buttons.indexOf(e.target);buttons.forEach((b,j)=>b.classList.toggle("on",j===i));document.getElementById("focusLabel").textContent=e.target.textContent}});toolbar.addEventListener("keydown",e=>{if(e.key!=="ArrowRight"&&e.key!=="ArrowLeft")return;clearInterval(timer);timer=0;if(!("focusgroup" in toolbar)){e.preventDefault();show((buttons.indexOf(document.activeElement)+(e.key==="ArrowRight"?1:buttons.length-1))%buttons.length)}})

The `focusgroup` attribute lets the browser handle arrow-key navigation in composite controls such as toolbars and menus. It reduces the need for custom keydown handlers that locate the next item.

The demo automatically moves focus between buttons; on the detail view you can try the arrow keys. It arrived in Chrome in 2026, so unsupported browsers need a keyboard fallback.

When to use

Consider it for keyboard navigation across a toolbar of related controls.

Open as page ↗