Tabs (Using JavaScript)

이 μ˜ˆμ œμ—μ„œλŠ” htmxλ₯Ό μ‚¬μš©ν•˜μ—¬ νƒ­ λ‚΄μš©μ„ λ‘œλ“œν•˜κ³ , μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ β€œν™œμ„±β€ 탭을 μ„ νƒν•˜λŠ” 방법을 λ³΄μ—¬μ€λ‹ˆλ‹€. 이λ₯Ό 톡해 νƒ­ HTML을 λ‹€μ‹œ λ Œλ”λ§ν•˜λŠ” μž‘μ—…μ˜ 일뢀λ₯Ό μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ„œλ²„μ—μ„œ ν΄λΌμ΄μ–ΈνŠΈμ˜ λΈŒλΌμš°μ €λ‘œ μ˜€ν”„λ‘œλ“œν•˜μ—¬ 쀑볡을 쀄일 수 μžˆμŠ΅λ‹ˆλ‹€.

Hypertext As The Engine Of Application State 원칙을 λ”°λ₯΄λŠ” 더 정톡적인 μ ‘κ·Ό 방식을 κ³ λ €ν•΄ λ³Ό μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

#예제 μ½”λ“œ

μ•„λž˜ HTML은 νƒ­ λͺ©λ‘μ„ ν‘œμ‹œν•˜λ©°, htmxλ₯Ό μΆ”κ°€ν•˜μ—¬ μ„œλ²„μ—μ„œ 각 νƒ­ νŒ¨λ„μ„ λ™μ μœΌλ‘œ λ‘œλ“œν•©λ‹ˆλ‹€. κ°„λ‹¨ν•œ JavaScript 이벀트 ν•Έλ“€λŸ¬λŠ” take ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ μ½˜ν…μΈ κ°€ DOM에 μŠ€μ™‘λ  λ•Œ μ„ νƒλœ 탭을 μ „ν™˜ν•©λ‹ˆλ‹€.


<div id="tabs" hx-target="#tab-contents" role="tablist"
     hx-on:htmx-after-on-load="let currentTab = document.querySelector('[aria-selected=true]');
                               currentTab.setAttribute('aria-selected', 'false')
                               currentTab.classList.remove('selected')
                               let newTab = event.target
                               newTab.setAttribute('aria-selected', 'true')
                               newTab.classList.add('selected')">
    <button role="tab" aria-controls="tab-contents" aria-selected="true" hx-get="/tab1" class="selected">Tab 1</button>
    <button role="tab" aria-controls="tab-contents" aria-selected="false" hx-get="/tab2">Tab 2</button>
    <button role="tab" aria-controls="tab-contents" aria-selected="false" hx-get="/tab3">Tab 3</button>
</div>

<div id="tab-contents" role="tabpanel" hx-get="/tab1" hx-trigger="load"></div>
Server Requests ↑ Show

πŸ”—Demo