code.asciidoc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // get all of the sibling tabs
  135. const buttons = Array.apply(null, tabList.querySelectorAll('button'));
  136. // loop over the siblings to discover which index thje clicked one was
  137. const { index } = buttons.reduce(({ found, index }, button) => {
  138. if (!found && buttons[index] === e.target) {
  139. return { found: true, index };
  140. } else if (!found) {
  141. return { found, index: index + 1 };
  142. } else {
  143. return { found, index };
  144. }
  145. }, { found: false, index: 0 });
  146. // get the tab container
  147. const container = tabList.parentNode;
  148. // read the data-tab-group value from the container, e.g. "os"
  149. const { tabGroup } = container.dataset;
  150. // get a list of all the tab groups that match this value on the page
  151. const groups = document.querySelectorAll('[data-tab-group=' + tabGroup + ']');
  152. // for each of the found tab groups, find the tab button at the previously discovered index and select it for each group
  153. groups.forEach((group) => {
  154. const target = group.querySelectorAll('button')[index];
  155. setActiveTab(target);
  156. });
  157. }
  158. </script>
  159. ++++