Components Disclosure
Tabs
Switch between related panels while keeping your place in the page.
Field Notes No. 04
An independent print journal about materials, processes, and the people who work with them.
The print edition
64 pages, saddle stitched, printed on an uncoated stock. Finished size 148 × 210 mm.
Contributors
Edited by the Field Studio team, with drawings from its spring archive.
Usage
Each item supplies Label, Title, and Description. The first panel opens initially. Use Left, Right, Home, or End while a tab has focus. Tab moves into the active panel. Without the module, fragment links navigate the visible sections.
Register tabs with Head and pass its properties to Render. Go setup guide
Go usage
err := mono.UI.Render(w, "tabs", mono.Props{
"Items": []any{
map[string]any{
"Description": "An independent print journal about materials, processes, and the people who work with them.",
"Label": "Overview",
"Title": "Field Notes No. 04",
},
map[string]any{
"Description": "64 pages, saddle stitched, printed on an uncoated stock. Finished size 148 × 210 mm.",
"Label": "Details",
"Title": "The print edition",
},
map[string]any{
"Description": "Edited by the Field Studio team, with drawings from its spring archive.",
"Label": "Credits",
"Title": "Contributors",
},
},
"Label": "Issue details",
})Rendered HTML
<section id="demo-tabs" data-mono="tabs" class="mu-stack">
<div class="mu-row" data-tab-list aria-label="Issue details">
<a id="demo-tabs-tab-0" href="#demo-tabs-panel-0" data-tab>Overview</a><a id="demo-tabs-tab-1" href="#demo-tabs-panel-1" data-tab>Details</a><a id="demo-tabs-tab-2" href="#demo-tabs-panel-2" data-tab>Credits</a>
</div>
<section id="demo-tabs-panel-0" class="mu-card" data-tab-panel>
<h3 class="mu-title">Field Notes No. 04</h3>
<p class="mu-muted">An independent print journal about materials, processes, and the people who work with them.</p>
</section><section id="demo-tabs-panel-1" class="mu-card" data-tab-panel>
<h3 class="mu-title">The print edition</h3>
<p class="mu-muted">64 pages, saddle stitched, printed on an uncoated stock. Finished size 148 × 210 mm.</p>
</section><section id="demo-tabs-panel-2" class="mu-card" data-tab-panel>
<h3 class="mu-title">Contributors</h3>
<p class="mu-muted">Edited by the Field Studio team, with drawings from its spring archive.</p>
</section>
</section>Go template
<section id="{{.ID}}" data-mono="tabs" class="mu-stack">
<div class="mu-row" data-tab-list aria-label="{{.Label}}">
{{range $i, $item := .Items}}<a id="{{$.ID}}-tab-{{$i}}" href="#{{$.ID}}-panel-{{$i}}" data-tab>{{$item.Label}}</a>{{end}}
</div>
{{range $i, $item := .Items}}<section id="{{$.ID}}-panel-{{$i}}" class="mu-card" data-tab-panel>
<h3 class="mu-title">{{$item.Title}}</h3>
<p class="mu-muted">{{$item.Description}}</p>
</section>{{end}}
</section>Component CSS
[data-mono="tabs"] { width: min(100%, 28rem); }
[data-mono="tabs"] [data-tab-list] { gap: .2rem; background: #ededed; padding: .25rem; border-radius: .5rem; width: fit-content; }
[data-mono="tabs"] [data-tab] { padding: .42rem .72rem; font-size: .88rem; color: #444; border-radius: .3rem; text-decoration: none; }
[data-mono="tabs"] [aria-selected="true"] { background: white; color: #141414; box-shadow: 0 1px 3px #0002; font-weight: 650; }
[data-mono="tabs"] [data-tab-panel] p { margin: .6rem 0 0; }JavaScript module
export function mount(root) {
const list = root.querySelector('[data-tab-list]');
const tabs = [...list.querySelectorAll('[data-tab]')];
const panels = [...root.querySelectorAll('[data-tab-panel]')];
if (!tabs.length) return;
list.setAttribute('role', 'tablist');
function select(index) {
tabs.forEach((tab, i) => {
tab.tabIndex = i === index ? 0 : -1;
tab.setAttribute('aria-selected', String(i === index));
panels[i].hidden = i !== index;
});
}
tabs.forEach((tab, i) => {
const panel = panels[i];
tab.setAttribute('role', 'tab');
tab.setAttribute('aria-controls', panel.id);
panel.setAttribute('role', 'tabpanel');
panel.setAttribute('aria-labelledby', tab.id);
panel.tabIndex = 0;
tab.addEventListener('click', event => {
event.preventDefault();
select(i);
});
tab.addEventListener('keydown', event => {
const rtl = getComputedStyle(list).direction === 'rtl';
const forward = rtl ? 'ArrowLeft' : 'ArrowRight';
const back = rtl ? 'ArrowRight' : 'ArrowLeft';
let target = i;
if (event.key === forward) target = (i + 1) % tabs.length;
else if (event.key === back) target = (i - 1 + tabs.length) % tabs.length;
else if (event.key === 'Home') target = 0;
else if (event.key === 'End') target = tabs.length - 1;
else if (event.key !== ' ') return;
event.preventDefault();
select(target);
tabs[target].focus();
});
});
const linked = panels.findIndex(panel => '#' + panel.id === location.hash);
select(Math.max(0, linked));
}Use the HTML in a static site
Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.
Shared CSS Component CSS Ready-to-use module
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./tabs.css">
<script type="module" src="./tabs.js"></script>Copy into your project
Run this command from the Mono checkout. Set -out to the component directory in your application.
go run ./cmd/mono-ui add -out ./components tabsExample properties (JSON)
{
"Items": [
{
"Description": "An independent print journal about materials, processes, and the people who work with them.",
"Label": "Overview",
"Title": "Field Notes No. 04"
},
{
"Description": "64 pages, saddle stitched, printed on an uncoated stock. Finished size 148 × 210 mm.",
"Label": "Details",
"Title": "The print edition"
},
{
"Description": "Edited by the Field Studio team, with drawings from its spring archive.",
"Label": "Credits",
"Title": "Contributors"
}
],
"Label": "Issue details"
}