This commit is contained in:
@@ -42,7 +42,7 @@
|
||||
div(tab="[tab title]")
|
||||
div(tab="[tab title]")
|
||||
</pre>
|
||||
<pre class="language-css" tab="css">.tab-group,tabset{margin:2rem 0 1rem 0}.tab-group>ul,tabset>ul{display:-webkit-box;display:-ms-flexbox;display:flex;margin:0;padding:0}.tab-group>ul li.separator,tabset>ul li.separator{border-bottom:1px solid #7f7f7f;border-left:1px solid #7f7f7f;display:inline-block;margin:0.45rem 0 0 0;width:100%}.tab-group .tab-hidden,tabset .tab-hidden{display:none}.tab-group [role=tab],tabset [role=tab]{background-color:#FFF;border-left:1px solid #7f7f7f;border-radius:0.5rem 0.5rem 0 0;border-top:1px solid #7f7f7f;color:#000;cursor:pointer;display:inline;margin:0;padding:1rem 1.5rem 0.14rem 1.5rem;z-index:2}.tab-group [role=tab]:last-of-type,tabset [role=tab]:last-of-type{border-right:1px solid #7f7f7f}.tab-group [role=tab]:not(.selected),tabset [role=tab]:not(.selected){background-color:#f0f0f0;border-bottom:1px solid #7f7f7f;color:#000}.tab-group [role=tab] span,tabset [role=tab] span{display:block;margin:0 0 0.5rem 0}.tab-group [role=tabpanel],tabset [role=tabpanel]{background-color:#FFF;border:1px solid #7f7f7f;border-top:#7f7f7f solid 1px;padding:1rem;z-index:1}.tab-group [role=tabpanel]:not(.open),tabset [role=tabpanel]:not(.open){display:none}</pre>
|
||||
<pre class="language-css" tab="css">@media (min-width:450px){.tab-group,tabset{margin:2rem 0 1rem 0}}.tab-group>ul,tabset>ul{display:-webkit-box;display:-ms-flexbox;display:flex;margin:0;padding:0}.tab-group>ul li.separator,tabset>ul li.separator{border-bottom:1px solid #7f7f7f;border-left:1px solid #7f7f7f;display:inline-block;margin:0.45rem 0 0 0;width:100%}.tab-group .tab-hidden,tabset .tab-hidden{display:none}.tab-group [role=tab],tabset [role=tab]{background-color:#FFF;border-left:1px solid #7f7f7f;border-top:1px solid #7f7f7f;border-radius:0.5rem 0.5rem 0 0;cursor:pointer;margin:0;display:inline;padding:1rem 1.5rem 0.14rem 1.5rem;z-index:2}.tab-group [role=tab]:last-of-type,tabset [role=tab]:last-of-type{border-right:1px solid #7f7f7f}.tab-group [role=tab]:not(.selected),tabset [role=tab]:not(.selected){background-color:#f0f0f0;border-bottom:1px solid #7f7f7f}.tab-group [role=tab] span,tabset [role=tab] span{display:block;margin:0 0 0.5rem 0}.tab-group [role=tabpanel],tabset [role=tabpanel]{background-color:#FFF;border:1px solid #7f7f7f;border-top:none;padding:1rem;z-index:1}@media (min-width:450px){.tab-group [role=tabpanel][open] summary,tabset [role=tabpanel][open] summary{display:none}}@media (min-width:450px){.tab-group [role=tabpanel]:not(.open),tabset [role=tabpanel]:not(.open){display:none}}@media (max-width:450px){.tab-group:has(details) [role=tablist],tabset:has(details) [role=tablist]{display:none}.tab-group:has(details) [role=tabpanel],tabset:has(details) [role=tabpanel]{border:none}}</pre>
|
||||
<div tab="scss">
|
||||
<pre class="language-sass">@import "scss/core/tabs/_tabs";
|
||||
@include tabs{
|
||||
@@ -52,23 +52,23 @@
|
||||
<pre class="language-sass">// DS2 core (c) 2024 Alexander McIlwraith
|
||||
// Licensed under CC BY-SA 4.0
|
||||
|
||||
|
||||
$tab-border: #7f7f7f !default;
|
||||
$tab-panel-background-color: #FFF !default;
|
||||
$tab-panel-top-border: #7f7f7f !default;
|
||||
$tab-panel-top-border-width: 1px !default;
|
||||
$tab-selected: #FFF !default;
|
||||
$tab-selected-text: #000 !default;
|
||||
$tab-notselected: #f0f0f0 !default;
|
||||
$tab-notselected-text: #000 !default;
|
||||
$tab-break: 450px !default;
|
||||
|
||||
@mixin tabs {
|
||||
tabset, .tab-group {
|
||||
margin: 2rem 0 1rem 0;
|
||||
@media (min-width: $tab-break) {
|
||||
margin: 2rem 0 1rem 0;
|
||||
}
|
||||
> ul {
|
||||
display: flex;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
|
||||
|
||||
li.separator {
|
||||
border-bottom: 1px solid $tab-border;
|
||||
border-left: 1px solid $tab-border;
|
||||
@@ -78,6 +78,8 @@ $tab-notselected-text: #000 !default;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
.tab-hidden {
|
||||
display: none;
|
||||
}
|
||||
@@ -85,12 +87,11 @@ $tab-notselected-text: #000 !default;
|
||||
[role="tab"] {
|
||||
background-color: $tab-selected;
|
||||
border-left: 1px solid $tab-border;
|
||||
border-radius: .5rem .5rem 0 0;
|
||||
border-top: 1px solid $tab-border;
|
||||
color: $tab-selected-text;
|
||||
border-radius: .5rem .5rem 0 0;
|
||||
cursor:pointer;
|
||||
display: inline;
|
||||
margin: 0;
|
||||
display: inline;
|
||||
padding: 1rem 1.5rem .14rem 1.5rem;
|
||||
z-index: 2;
|
||||
|
||||
@@ -101,7 +102,6 @@ $tab-notselected-text: #000 !default;
|
||||
&:not(.selected) {
|
||||
background-color: $tab-notselected;
|
||||
border-bottom: 1px solid $tab-border;
|
||||
color: $tab-notselected-text;
|
||||
}
|
||||
|
||||
span {
|
||||
@@ -111,17 +111,41 @@ $tab-notselected-text: #000 !default;
|
||||
|
||||
}
|
||||
[role="tabpanel"] {
|
||||
background-color: $tab-panel-background-color;
|
||||
background-color: $tab-selected;
|
||||
border: 1px solid $tab-border;
|
||||
border-top: $tab-panel-top-border solid $tab-panel-top-border-width;
|
||||
border-top: none;
|
||||
padding: 1rem;
|
||||
z-index: 1;
|
||||
|
||||
&:not(.open) {
|
||||
display: none;
|
||||
@media (min-width: $tab-break) {
|
||||
&[open] summary {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: $tab-break) {
|
||||
&:not(.open) {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
@content;
|
||||
|
||||
}
|
||||
|
||||
@media (max-width: $tab-break) {
|
||||
&:has(details) {
|
||||
[role="tablist"] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
[role="tabpanel"] {
|
||||
border: none;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}</pre>
|
||||
</div>
|
||||
@@ -160,28 +184,53 @@ const waitForElement = (selector) => {
|
||||
});
|
||||
}
|
||||
|
||||
let tabsetCount = 0;
|
||||
|
||||
export function init(container = document, spacer = true, args = {}) {
|
||||
|
||||
container.querySelectorAll(".tab-group, tabset").forEach(tabGroup => {
|
||||
|
||||
if (tabGroup.querySelector("[role=tablist]") === null) {
|
||||
if (tabGroup.getAttribute("id") == null) {
|
||||
tabGroup.setAttribute("id", "tab-group-" + tabsetCount);
|
||||
tabsetCount++;
|
||||
}
|
||||
|
||||
|
||||
const tabgroup = tabGroup.getAttribute("id");
|
||||
let tablist = "";
|
||||
|
||||
Array.from(tabGroup.children).forEach(child => {
|
||||
const tab = child.getAttribute("tab") || child.getAttribute("data-tab");
|
||||
|
||||
// is details?
|
||||
let dtls = child.nodeName == "DETAILS" ? true : false;
|
||||
|
||||
// get the tab text
|
||||
let tab = dtls ? child.querySelector("summary").innerHTML : child.getAttribute("tab") || child.getAttribute("data-tab");
|
||||
|
||||
// if the tab text is not blank
|
||||
if (tab !== null) {
|
||||
const tabID = tab.replace(/\W+/g, "-").toLowerCase();
|
||||
const tabPanel = document.createElement('div');
|
||||
|
||||
|
||||
// define the tab panel content
|
||||
let tabPanel = null;
|
||||
if (dtls) {
|
||||
tabPanel = child;
|
||||
tabPanel.setAttribute("open", "");
|
||||
} else {
|
||||
tabPanel = document.createElement('div');
|
||||
tabPanel.appendChild(child.cloneNode(true));
|
||||
}
|
||||
|
||||
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>`;
|
||||
let cls = tablist === "" ? "class='selected'" : "";
|
||||
tablist += `<li tabindex="0" role="tab" ${cls} id="tab-${tabgroup}-${tabID}"><span>${tab}</span></li>`;
|
||||
} else {
|
||||
child.classList.add("tab-hidden");
|
||||
}
|
||||
@@ -193,25 +242,32 @@ export function init(container = document, spacer = true, args = {}) {
|
||||
tabGroup.insertBefore(ul, tabGroup.firstChild);
|
||||
|
||||
tabGroup.querySelectorAll('[role="tab"]').forEach(tab => {
|
||||
tab.addEventListener("click", (e) => {
|
||||
if (e.altKey && typeof args.altModifier == "function") {
|
||||
args.altModifier(tab);
|
||||
} else if (e.shiftKey && typeof args.shiftModifier == "function") {
|
||||
args.shiftModifier(tab);
|
||||
} else if (e.metaKey && typeof args.metaModifier == "function") {
|
||||
args.metaModifier(tab);
|
||||
} else {
|
||||
const siblings = Array.from(tab.parentNode.children);
|
||||
siblings.forEach(sibling => sibling.classList.remove("selected"));
|
||||
tab.classList.add("selected");
|
||||
tab.addEventListener("click", (evt) => {
|
||||
// if (evt.altKey && typeof args.altModifier == "function") {
|
||||
// args.altModifier(tab, evt);
|
||||
// } else if (evt.shiftKey && typeof args.shiftModifier == "function") {
|
||||
// args.shiftModifier(tab, evt);
|
||||
// } else if (evt.metaKey && typeof args.metaModifier == "function") {
|
||||
// args.metaModifier(tab, evt);
|
||||
// } else {
|
||||
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");
|
||||
}
|
||||
let urlPath = window.location;
|
||||
urlPath.hash = evt.currentTarget.getAttribute("id");
|
||||
window.history.pushState({"pageTitle": window.title + " : " + evt.currentTarget.innerHTML},"", urlPath.toString());
|
||||
// window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
|
||||
|
||||
|
||||
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");
|
||||
//}
|
||||
});
|
||||
|
||||
|
||||
@@ -230,11 +286,18 @@ export function init(container = document, spacer = true, args = {}) {
|
||||
el.focus();
|
||||
el.dispatchEvent(click);
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
</pre>
|
||||
|
||||
addEventListener("hashchange", (evt) => {
|
||||
document.querySelector(window.location.hash).dispatchEvent(click);
|
||||
|
||||
// window.location.hash
|
||||
// console.log(window.location.hash)
|
||||
})
|
||||
|
||||
|
||||
}</pre>
|
||||
</div>
|
||||
</tabset>
|
||||
</body>
|
||||
|
Reference in New Issue
Block a user