Created
June 19, 2021 01:32
-
-
Save anied/c896720abe59e128cfa1b25ac4d2a956 to your computer and use it in GitHub Desktop.
Inaccessible Tabbed Container Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #tabbed-container { | |
| border: 2px solid black; | |
| width: 90vw; | |
| height: 200px; | |
| display: grid; | |
| grid-template-rows: 1.125rem auto; | |
| grid-template-columns: auto; | |
| grid-template-areas: "tabs-container", "panes-container"; | |
| } | |
| #tabs-container { | |
| border-bottom: 1px solid gray; | |
| display: flex; | |
| } | |
| .tab { | |
| min-width: 80px; | |
| border-right: 1px solid gray; | |
| text-align: center; | |
| cursor: pointer; | |
| } | |
| .tab.active { | |
| font-weight: bold; | |
| } | |
| .tab:hover { | |
| background-color: lightcyan; | |
| } | |
| #panes-container { | |
| font-size: 1rem; | |
| position: relative; | |
| } | |
| .pane { | |
| background-color: lightyellow; | |
| position: absolute; | |
| top: 0; | |
| bottom: 0; | |
| left: 0; | |
| right: 0; | |
| z-index: 0; | |
| } | |
| .pane.active { | |
| z-index: 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="tabbed-container"> | |
| <div id="tabs-container"> | |
| <div class="tab active" data-tab="1">Tab #1</div> | |
| <div class="tab" data-tab="2">Tab #2</div> | |
| <div class="tab" data-tab="3">Tab #3</div> | |
| <div class="tab" data-tab="4">Tab #4</div> | |
| </div> | |
| <div id="panes-container"> | |
| <div class="pane active" data-pane="1"> | |
| Pane #1 Lorem Ipsum blah blah blah | |
| </div> | |
| <div class="pane" data-pane="2"> | |
| Pane #2 Lorem Ipsum blah blah blah | |
| <a href="https://www.alexnied.com">website</a> | |
| </div> | |
| <div class="pane" data-pane="3"> | |
| Pane #3 Lorem Ipsum blah blah blah | |
| </div> | |
| <div class="pane" data-pane="4"> | |
| Pane #4 Lorem Ipsum blah blah blah | |
| </div> | |
| </div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const tabsContainer = document.querySelector("#tabs-container"); | |
| tabsContainer.addEventListener("click", (e) => { | |
| if (e.target && e.target.matches(".tab")) { | |
| const tabs = document.querySelectorAll(".tab"); | |
| const panes = document.querySelectorAll(".pane"); | |
| tabs.forEach((tab) => tab.classList.remove("active")); | |
| panes.forEach((pane) => pane.classList.remove("active")); | |
| const newActiveNumber = e.target.dataset.tab; | |
| const newActiveSet = document.querySelectorAll( | |
| `[data-tab="${newActiveNumber}"], [data-pane="${newActiveNumber}"]` | |
| ); | |
| newActiveSet.forEach((item) => item.classList.add("active")); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment