ds2-core/public/patterns/layouts/tabs-core/index.html

202 lines
6.1 KiB
HTML
Raw Normal View History

2024-06-13 00:00:00 -04:00
<html>
<head>
<title>Pattern</title>
</head>
<body data-prismjs-copy-timeout="1500">
2024-07-11 21:05:34 -04:00
<h2>What is it</h2>
<h2>When to use it</h2>
<h2>How to use it</h2>
2024-06-13 00:00:00 -04:00
<p>The tabbed user interface enables users to jump to their target section quickly. Tabs present like logically group information on the same page. Information should </p>
<ul>
<li>be logically chunked and ordered</li>
<li>be arallel in nature</li>
<li>show user's context</li>
<li>obvious where they begin and end </li>
</ul>
<p>Users should not need to see content of multiple tabs simultaneously and the user should be able to easily recognise where they are within the content. </p>
2024-07-11 21:05:34 -04:00
<p>The tab module can be initialised by importing a file with the javascript module using import * as tabs from "../pg/patterns/layouts/tabs/_tabs.js"; contains a modularized version of the jQuery code, so that it can be called on demand. It is what is used in the design system so that the JavaScript can be called at run time (after loading content).</p>
<tabset id="tabs">
<pre class="language-html" tab="html">
<div class="tab-group" id="uniqueID">
<div data-tab="[tab title]"></div>
<div data-tab="[tab title]"></div>
</div></pre>
<pre class="language-css" tab="css">tabset, .tab-group {
2024-06-13 00:00:00 -04:00
margin: 2rem 0 1rem 0;
}
2024-07-11 21:05:34 -04:00
tabset > ul, .tab-group > ul {
2024-06-13 00:00:00 -04:00
display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin: 0;
padding: 0;
}
2024-07-11 21:05:34 -04:00
tabset > ul li.separator, .tab-group > ul li.separator {
2024-06-13 00:00:00 -04:00
border-bottom: 1px solid var(--color-grey);
border-left: 1px solid var(--color-grey);
display: inline-block;
margin: 0.45rem 0 0 0;
width: 100%;
}
2024-07-11 21:05:34 -04:00
tabset .tab-hidden, .tab-group .tab-hidden {
2024-06-13 00:00:00 -04:00
display: none;
}
2024-07-11 21:05:34 -04:00
tabset [role=tab], .tab-group [role=tab] {
2024-06-13 00:00:00 -04:00
background-color: var(--color-white);
border-left: 1px solid var(--color-grey);
border-top: 1px solid var(--color-grey);
border-radius: 0.5rem 0.5rem 0 0;
2024-07-11 21:05:34 -04:00
cursor: pointer;
2024-06-13 00:00:00 -04:00
margin: 0;
display: inline;
padding: 1rem 1.5rem 0.14rem 1.5rem;
z-index: 2;
}
2024-07-11 21:05:34 -04:00
tabset [role=tab]:last-of-type, .tab-group [role=tab]:last-of-type {
2024-06-13 00:00:00 -04:00
border-right: 1px solid var(--color-grey);
}
2024-07-11 21:05:34 -04:00
tabset [role=tab]:not(.selected), .tab-group [role=tab]:not(.selected) {
2024-06-13 00:00:00 -04:00
background-color: var(--color-grey-xxl);
border-bottom: 1px solid var(--color-grey);
}
2024-07-11 21:05:34 -04:00
tabset [role=tab] span, .tab-group [role=tab] span {
2024-06-13 00:00:00 -04:00
display: block;
margin: 0 0 0.5rem 0;
}
2024-07-11 21:05:34 -04:00
tabset [role=tabpanel], .tab-group [role=tabpanel] {
2024-06-13 00:00:00 -04:00
background-color: var(--color-white);
border: 1px solid var(--color-grey);
border-top: none;
padding: 1rem;
z-index: 1;
}
2024-07-11 21:05:34 -04:00
tabset [role=tabpanel]:not(.open), .tab-group [role=tabpanel]:not(.open) {
2024-06-13 00:00:00 -04:00
display: none;
}</pre>
2024-07-11 21:05:34 -04:00
<pre class="language-css" tab="scss">//- DS2 core (c) 2024 Alexander McIlwraith
//- Licensed under CC BY-SA 4.0
$tab-border: var(--color-grey) !default;
$tab-selected: var(--color-white) !default;
$tab-notselected: var(--color-grey-xxl) !default;
@mixin tabs {
tabset, .tab-group {
2024-06-13 00:00:00 -04:00
margin: 2rem 0 1rem 0;
> ul {
display: flex;
margin: 0;
padding: 0;
li.separator {
2024-07-11 21:05:34 -04:00
border-bottom: 1px solid $tab-border;
border-left: 1px solid $tab-border;
2024-06-13 00:00:00 -04:00
display: inline-block;
margin: .45rem 0 0 0;
width: 100%;
}
}
.tab-hidden {
display: none;
}
[role="tab"] {
2024-07-11 21:05:34 -04:00
background-color: $tab-selected;
border-left: 1px solid $tab-border;
border-top: 1px solid $tab-border;
2024-06-13 00:00:00 -04:00
border-radius: .5rem .5rem 0 0;
2024-07-11 21:05:34 -04:00
cursor:pointer;
2024-06-13 00:00:00 -04:00
margin: 0;
display: inline;
padding: 1rem 1.5rem .14rem 1.5rem;
z-index: 2;
&:last-of-type {
2024-07-11 21:05:34 -04:00
border-right: 1px solid $tab-border;
2024-06-13 00:00:00 -04:00
}
&:not(.selected) {
2024-07-11 21:05:34 -04:00
background-color: $tab-notselected;
border-bottom: 1px solid $tab-border;
2024-06-13 00:00:00 -04:00
}
span {
display: block;
margin: 0 0 .5rem 0;
}
}
[role="tabpanel"] {
2024-07-11 21:05:34 -04:00
background-color: $tab-selected;
border: 1px solid $tab-border;
2024-06-13 00:00:00 -04:00
border-top: none;
padding: 1rem;
z-index: 1;
&:not(.open) {
display: none;
}
@content;
}
}
}</pre>
2024-07-12 22:19:12 -04:00
<pre class="language-css" tab="js">/* DS2 core (c) 2024 Alexander McIlwraith
import * as tabs from "../pg/patterns/layouts/tabs/_tabs.js";
tabs.init();
*/
2024-07-11 21:05:34 -04:00
2024-07-12 22:19:12 -04:00
export function init(p = document) {
p.querySelectorAll(".tab-group, tabset").forEach(tabGroup => {
2024-07-11 21:05:34 -04:00
if (tabGroup.querySelector("[role=tablist]") === null) {
const tabgroup = tabGroup.getAttribute("id");
let tablist = "";
Array.from(tabGroup.children).forEach(child => {
const tab = child.getAttribute("tab") || child.getAttribute("data-tab");
if (tab !== null) {
const tabID = tab.replace(/\W+/g, "-").toLowerCase();
const tabPanel = document.createElement('div');
tabPanel.id = `tab-panel-${tabgroup}-${tabID}`;
tabPanel.className = tablist === "" ? "open" : "";
tabPanel.setAttribute("role", "tabpanel");
tabPanel.setAttribute("tabindex", "0");
tabPanel.setAttribute("aria-labelledby", `tab-${tabgroup}-${tabID}`);
tabPanel.appendChild(child.cloneNode(true));
child.parentNode.replaceChild(tabPanel, child);
tablist += `<li tabindex="0" role="tab" ${tablist === "" ? "class='selected'" : ""} id="tab-${tabgroup}-${tabID}"><span>${tab}</span></li>`;
} else {
child.classList.add("tab-hidden");
}
});
const ul = document.createElement('ul');
ul.setAttribute("role", "tablist");
ul.innerHTML = `${tablist}<li role="separator" class="separator"></li>`;
tabGroup.insertBefore(ul, tabGroup.firstChild);
tabGroup.querySelectorAll('[role="tab"]').forEach(tab => {
tab.addEventListener("click", () => {
const siblings = Array.from(tab.parentNode.children);
siblings.forEach(sibling => sibling.classList.remove("selected"));
tab.classList.add("selected");
const tabPanels = Array.from(tab.parentNode.parentNode.children)
.filter(child => child.getAttribute("role") === "tabpanel");
tabPanels.forEach(panel => panel.classList.remove("open"));
const tabPanelId = tab.getAttribute("id").replace("tab", "tab-panel");
document.getElementById(tabPanelId).classList.add("open");
});
});
2024-06-13 00:00:00 -04:00
2024-07-11 21:05:34 -04:00
}
});
}
</pre>
</tabset>
2024-06-13 00:00:00 -04:00
</body>
</html>