|
| 1 | +// Defining styles and script here for simplicity. |
| 2 | +++++ |
| 3 | +<style> |
| 4 | +.tabs { |
| 5 | + width: 100%; |
| 6 | +} |
| 7 | +[role="tablist"] { |
| 8 | + margin: 0 0 -0.1em; |
| 9 | + overflow: visible; |
| 10 | +} |
| 11 | +[role="tab"] { |
| 12 | + position: relative; |
| 13 | + padding: 0.3em 0.5em 0.4em; |
| 14 | + border: 1px solid hsl(219, 1%, 72%); |
| 15 | + border-radius: 0.2em 0.2em 0 0; |
| 16 | + overflow: visible; |
| 17 | + font-family: inherit; |
| 18 | + font-size: inherit; |
| 19 | + background: hsl(220, 20%, 94%); |
| 20 | +} |
| 21 | +[role="tab"]:hover::before, |
| 22 | +[role="tab"]:focus::before, |
| 23 | +[role="tab"][aria-selected="true"]::before { |
| 24 | + position: absolute; |
| 25 | + bottom: 100%; |
| 26 | + right: -1px; |
| 27 | + left: -1px; |
| 28 | + border-radius: 0.2em 0.2em 0 0; |
| 29 | + border-top: 3px solid hsl(219, 1%, 72%); |
| 30 | + content: ''; |
| 31 | +} |
| 32 | +[role="tab"][aria-selected="true"] { |
| 33 | + border-radius: 0; |
| 34 | + background: hsl(220, 43%, 99%); |
| 35 | + outline: 0; |
| 36 | +} |
| 37 | +[role="tab"][aria-selected="true"]:not(:focus):not(:hover)::before { |
| 38 | + border-top: 5px solid hsl(218, 96%, 48%); |
| 39 | +} |
| 40 | +[role="tab"][aria-selected="true"]::after { |
| 41 | + position: absolute; |
| 42 | + z-index: 3; |
| 43 | + bottom: -1px; |
| 44 | + right: 0; |
| 45 | + left: 0; |
| 46 | + height: 0.3em; |
| 47 | + background: hsl(220, 43%, 99%); |
| 48 | + box-shadow: none; |
| 49 | + content: ''; |
| 50 | +} |
| 51 | +[role="tab"]:hover, |
| 52 | +[role="tab"]:focus, |
| 53 | +[role="tab"]:active { |
| 54 | + outline: 0; |
| 55 | + border-radius: 0; |
| 56 | + color: inherit; |
| 57 | +} |
| 58 | +[role="tab"]:hover::before, |
| 59 | +[role="tab"]:focus::before { |
| 60 | + border-color: hsl(218, 96%, 48%); |
| 61 | +} |
| 62 | +[role="tabpanel"] { |
| 63 | + position: relative; |
| 64 | + z-index: 2; |
| 65 | + padding: 1em; |
| 66 | + border: 1px solid hsl(219, 1%, 72%); |
| 67 | + border-radius: 0 0.2em 0.2em 0.2em; |
| 68 | + box-shadow: 0 0 0.2em hsl(219, 1%, 72%); |
| 69 | + background: hsl(220, 43%, 99%); |
| 70 | + margin-bottom: 1em; |
| 71 | +} |
| 72 | +[role="tabpanel"] p { |
| 73 | + margin: 0; |
| 74 | +} |
| 75 | +[role="tabpanel"] * + p { |
| 76 | + margin-top: 1em; |
| 77 | +} |
| 78 | +</style> |
| 79 | +<script> |
| 80 | +window.addEventListener("DOMContentLoaded", () => { |
| 81 | + const tabs = document.querySelectorAll('[role="tab"]'); |
| 82 | + const tabList = document.querySelector('[role="tablist"]'); |
| 83 | + // Add a click event handler to each tab |
| 84 | + tabs.forEach(tab => { |
| 85 | + tab.addEventListener("click", changeTabs); |
| 86 | + }); |
| 87 | + // Enable arrow navigation between tabs in the tab list |
| 88 | + let tabFocus = 0; |
| 89 | + tabList.addEventListener("keydown", e => { |
| 90 | + // Move right |
| 91 | + if (e.keyCode === 39 || e.keyCode === 37) { |
| 92 | + tabs[tabFocus].setAttribute("tabindex", -1); |
| 93 | + if (e.keyCode === 39) { |
| 94 | + tabFocus++; |
| 95 | + // If we're at the end, go to the start |
| 96 | + if (tabFocus >= tabs.length) { |
| 97 | + tabFocus = 0; |
| 98 | + } |
| 99 | + // Move left |
| 100 | + } else if (e.keyCode === 37) { |
| 101 | + tabFocus--; |
| 102 | + // If we're at the start, move to the end |
| 103 | + if (tabFocus < 0) { |
| 104 | + tabFocus = tabs.length - 1; |
| 105 | + } |
| 106 | + } |
| 107 | + tabs[tabFocus].setAttribute("tabindex", 0); |
| 108 | + tabs[tabFocus].focus(); |
| 109 | + } |
| 110 | + }); |
| 111 | +}); |
| 112 | +function setActiveTab(target) { |
| 113 | + const parent = target.parentNode; |
| 114 | + const grandparent = parent.parentNode; |
| 115 | + // console.log(grandparent); |
| 116 | + // Remove all current selected tabs |
| 117 | + parent |
| 118 | + .querySelectorAll('[aria-selected="true"]') |
| 119 | + .forEach(t => t.setAttribute("aria-selected", false)); |
| 120 | + // Set this tab as selected |
| 121 | + target.setAttribute("aria-selected", true); |
| 122 | + // Hide all tab panels |
| 123 | + grandparent |
| 124 | + .querySelectorAll('[role="tabpanel"]') |
| 125 | + .forEach(p => p.setAttribute("hidden", true)); |
| 126 | + // Show the selected panel |
| 127 | + grandparent.parentNode |
| 128 | + .querySelector(`#${target.getAttribute("aria-controls")}`) |
| 129 | + .removeAttribute("hidden"); |
| 130 | +} |
| 131 | +function changeTabs(e) { |
| 132 | + // get the containing list of the tab that was just clicked |
| 133 | + const tabList = e.target.parentNode; |
| 134 | + |
| 135 | + // get all of the sibling tabs |
| 136 | + const buttons = Array.apply(null, tabList.querySelectorAll('button')); |
| 137 | + |
| 138 | + // loop over the siblings to discover which index thje clicked one was |
| 139 | + const { index } = buttons.reduce(({ found, index }, button) => { |
| 140 | + if (!found && buttons[index] === e.target) { |
| 141 | + return { found: true, index }; |
| 142 | + } else if (!found) { |
| 143 | + return { found, index: index + 1 }; |
| 144 | + } else { |
| 145 | + return { found, index }; |
| 146 | + } |
| 147 | + }, { found: false, index: 0 }); |
| 148 | + |
| 149 | + // get the tab container |
| 150 | + const container = tabList.parentNode; |
| 151 | + // read the data-tab-group value from the container, e.g. "os" |
| 152 | + const { tabGroup } = container.dataset; |
| 153 | + // get a list of all the tab groups that match this value on the page |
| 154 | + const groups = document.querySelectorAll('[data-tab-group=' + tabGroup + ']'); |
| 155 | + |
| 156 | + // for each of the found tab groups, find the tab button at the previously discovered index and select it for each group |
| 157 | + groups.forEach((group) => { |
| 158 | + const target = group.querySelectorAll('button')[index]; |
| 159 | + setActiveTab(target); |
| 160 | + }); |
| 161 | +} |
| 162 | +</script> |
| 163 | +++++ |
0 commit comments