Skip to content

Commit f19f2d9

Browse files
authored
Merge pull request #2 from stargate/add-tab-blocks
add tabs codeblock ui
2 parents abba354 + 5fe7a86 commit f19f2d9

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

src/css/site.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
@import "highlight.css";
1717
@import "print.css";
1818
@import "typeface-nunito.css";
19+
@import "tabs.css";

src/css/tabs.css

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
.tabset {
2+
margin-bottom: 20px;
3+
margin-top: 20px;
4+
}
5+
6+
.tabs ul {
7+
display: flex;
8+
flex-wrap: wrap;
9+
list-style: none;
10+
margin: 0 -0.25rem 0 0;
11+
padding: 0;
12+
}
13+
14+
.tabs li {
15+
align-items: center;
16+
border-bottom: 0;
17+
border: 1px solid #808080;
18+
cursor: pointer;
19+
display: flex;
20+
font-weight: bold;
21+
height: 2.5rem;
22+
line-height: 1;
23+
margin-right: 0.25rem;
24+
padding: 0 1.5rem;
25+
position: relative;
26+
}
27+
28+
.tabs.ulist li {
29+
margin-bottom: 0;
30+
border-top-left-radius: 3px;
31+
border-top-right-radius: 3px;
32+
}
33+
34+
.tabs li + li {
35+
margin-top: 0;
36+
}
37+
38+
.tabset.is-loading .tabs li:not(:first-child),
39+
.tabset:not(.is-loading) .tabs li:not(.is-active) {
40+
background-color: #fafafa;
41+
color: #8e8e8e;
42+
font-weight: normal;
43+
}
44+
45+
.tabset.is-loading .tabs li:first-child::after,
46+
.tabs li.is-active::after {
47+
background-color: white;
48+
content: "";
49+
display: block;
50+
height: 3px; /* Chrome doesn't always paint the line accurately, so add a little extra */
51+
position: absolute;
52+
bottom: -1.5px;
53+
left: 0;
54+
right: 0;
55+
}
56+
57+
.tabset > .content {
58+
border: 1px solid gray;
59+
padding: 1.25rem;
60+
border-bottom-left-radius: 3px;
61+
border-bottom-right-radius: 3px;
62+
border-top-right-radius: 3px;
63+
}
64+
65+
.tabset.is-loading .tab-pane:not(:first-child),
66+
.tabset:not(.is-loading) .tab-pane:not(.is-active) {
67+
display: none;
68+
}
69+
70+
.tab-pane > :first-child {
71+
margin-top: 0;
72+
}

src/js/06-tabs-block.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
;(function () {
2+
'use strict'
3+
4+
var hash = window.location.hash
5+
find('.tabset').forEach(function (tabset) {
6+
var active
7+
var tabs = tabset.querySelector('.tabs')
8+
if (tabs) {
9+
var first
10+
find('li', tabs).forEach(function (tab, idx) {
11+
var id = (tab.querySelector('a[id]') || tab).id
12+
if (!id) return
13+
var pane = getPane(id, tabset)
14+
if (!idx) first = { tab: tab, pane: pane }
15+
if (!active && hash === '#' + id && (active = true)) {
16+
tab.classList.add('is-active')
17+
if (pane) pane.classList.add('is-active')
18+
} else if (!idx) {
19+
tab.classList.remove('is-active')
20+
if (pane) pane.classList.remove('is-active')
21+
}
22+
tab.addEventListener('click', activateTab.bind({ tabset: tabset, tab: tab, pane: pane }))
23+
})
24+
if (!active && first) {
25+
first.tab.classList.add('is-active')
26+
if (first.pane) first.pane.classList.add('is-active')
27+
}
28+
}
29+
tabset.classList.remove('is-loading')
30+
})
31+
32+
function activateTab (e) {
33+
var tab = this.tab
34+
var pane = this.pane
35+
find('.tabs li, .tab-pane', this.tabset).forEach(function (it) {
36+
it === tab || it === pane ? it.classList.add('is-active') : it.classList.remove('is-active')
37+
})
38+
e.preventDefault()
39+
}
40+
41+
function find (selector, from) {
42+
return Array.prototype.slice.call((from || document).querySelectorAll(selector))
43+
}
44+
45+
function getPane (id, tabset) {
46+
return find('.tab-pane', tabset).find(function (it) {
47+
return it.getAttribute('aria-labelledby') === id
48+
})
49+
}
50+
})()

0 commit comments

Comments
 (0)