Skip to content

Commit

Permalink
Merge pull request #2 from stargate/add-tab-blocks
Browse files Browse the repository at this point in the history
add tabs codeblock ui
  • Loading branch information
polandll authored Oct 30, 2020
2 parents abba354 + 5fe7a86 commit f19f2d9
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
@import "highlight.css";
@import "print.css";
@import "typeface-nunito.css";
@import "tabs.css";
72 changes: 72 additions & 0 deletions src/css/tabs.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
.tabset {
margin-bottom: 20px;
margin-top: 20px;
}

.tabs ul {
display: flex;
flex-wrap: wrap;
list-style: none;
margin: 0 -0.25rem 0 0;
padding: 0;
}

.tabs li {
align-items: center;
border-bottom: 0;
border: 1px solid #808080;
cursor: pointer;
display: flex;
font-weight: bold;
height: 2.5rem;
line-height: 1;
margin-right: 0.25rem;
padding: 0 1.5rem;
position: relative;
}

.tabs.ulist li {
margin-bottom: 0;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}

.tabs li + li {
margin-top: 0;
}

.tabset.is-loading .tabs li:not(:first-child),
.tabset:not(.is-loading) .tabs li:not(.is-active) {
background-color: #fafafa;
color: #8e8e8e;
font-weight: normal;
}

.tabset.is-loading .tabs li:first-child::after,
.tabs li.is-active::after {
background-color: white;
content: "";
display: block;
height: 3px; /* Chrome doesn't always paint the line accurately, so add a little extra */
position: absolute;
bottom: -1.5px;
left: 0;
right: 0;
}

.tabset > .content {
border: 1px solid gray;
padding: 1.25rem;
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
border-top-right-radius: 3px;
}

.tabset.is-loading .tab-pane:not(:first-child),
.tabset:not(.is-loading) .tab-pane:not(.is-active) {
display: none;
}

.tab-pane > :first-child {
margin-top: 0;
}
50 changes: 50 additions & 0 deletions src/js/06-tabs-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
;(function () {
'use strict'

var hash = window.location.hash
find('.tabset').forEach(function (tabset) {
var active
var tabs = tabset.querySelector('.tabs')
if (tabs) {
var first
find('li', tabs).forEach(function (tab, idx) {
var id = (tab.querySelector('a[id]') || tab).id
if (!id) return
var pane = getPane(id, tabset)
if (!idx) first = { tab: tab, pane: pane }
if (!active && hash === '#' + id && (active = true)) {
tab.classList.add('is-active')
if (pane) pane.classList.add('is-active')
} else if (!idx) {
tab.classList.remove('is-active')
if (pane) pane.classList.remove('is-active')
}
tab.addEventListener('click', activateTab.bind({ tabset: tabset, tab: tab, pane: pane }))
})
if (!active && first) {
first.tab.classList.add('is-active')
if (first.pane) first.pane.classList.add('is-active')
}
}
tabset.classList.remove('is-loading')
})

function activateTab (e) {
var tab = this.tab
var pane = this.pane
find('.tabs li, .tab-pane', this.tabset).forEach(function (it) {
it === tab || it === pane ? it.classList.add('is-active') : it.classList.remove('is-active')
})
e.preventDefault()
}

function find (selector, from) {
return Array.prototype.slice.call((from || document).querySelectorAll(selector))
}

function getPane (id, tabset) {
return find('.tab-pane', tabset).find(function (it) {
return it.getAttribute('aria-labelledby') === id
})
}
})()

0 comments on commit f19f2d9

Please sign in to comment.