See the demo.
Synopsis
<div class="curvy-tabs-container">
<div style="background-color:lightblue" name="Tab A">
Content for Tab A goes here.
</div>
<div style="background-color:lightgreen" name="Tab B">
Content for Tab B goes here.
</div>
</div>
As npm module:
var CurvyTabs = require('curvy-tab');
From CDN:
<script src="https://joneit.github.io/curvy-tabs/2.1.1/curvy-tabs.min.js">
The following instantiates the controller object, collecting all the content divs into a sub-div, and adds the tab bar (a canvas
element) above it:
var container = document.querySelector('.curvy-tabs-container');
var tabBar = new CurvyTabs(container);
tabBar.paint();
The tabs are named after the content element names.
API
CurvyTabs.version
shared property (aka constructor.version
property)
Contains the version string 2.1.0
(major.minor.patch with no leading v
).
container
property
An element (div.curvy-tabs-container
in the above) that contains this instance’s tabs.
contents
property
A <div>...</div>
element, programmatically created by the constructor to group all tabs’ content elements (the container’s children).
getTab
method
Convenience method to get a content element by index or name.
For example, to get a reference to the 2nd tab’s content element:
var tab = tabBar.getTab(1);
var tab = tabBar.getTab('Tab B');
selected
property and select
method
The first tab is selected by default.
To select an alternative default tab on instantiation:
var tabBar = new CurvyTabs(container, tabB);
To programmatically specify some other tab, set selected
to a specific content element:
var tabB = tabBar.contents.children[1];
var tabB = tabBar.contents.querySelector('[name="Tab B"]');
tabBar.selected = tabB;
Or, use the select
convenience method for setting selected
property by tab index or name:
tabBar.select(1);
tabBar.select('Tab B');
Tab clear
method
To "clear" (removes all child elements from) the 2nd tab’s content element:
tabBar.clear(1);
tabBar.clear('Tab B');
Tab hide/show by index or name
To hide the 2nd tab, set 2nd content element’s display
style:
- Declaratively, before instantiation: Use CSS (affects all instances):
<style>
.curvy-tabs-content > div > *:nth-child(1) { display: none }
.curvy-tabs-content > div > *[name="Tab B"] { display: none }
</style>
- _Programmatically, after instantiation, set the
display
style property of the content div:
tabBar.contents.querySelector(1).style.display = 'none';
tabBar.contents.querySelector('[name="Tab B"]').style.display = 'none';
tabBar.paint();
Or, use the toggle
or hide
& show
convenience methods to set a tab’s display
property (by index or name) and then call paint
for you:
var indexOrName = 1;
var indexOrName = 'Tab B';
tabBar.hide(indexOrName);
tabBar.toggle(indexOrName, false);
tabBar.show(indexOrName);
tabBar.toggle(indexOrName, true);
tabBar.toggle(indexOrName);
Tab curviness
property
To change the curviness of the tab outlines:
tabBar.curviness = 0;
tabBar.curviness = 0.5;
tabBar.curviness = 1;
Tab minWidth
property
Tabs are sized proportional to their labels. To make all tabs the same width:
tabBar.minWidth = 100; // Tabs whose text exceeds 100 pixels are widened to accommodate
Tab font
property
To change the tab font:
tabBar.font = '12pt cursive';
To set the size (i.e., height) of the tabs (for example to accommodate outsized fonts):
- Before instantiation: Reset the default tab size (initially 29 pixels):
CurvyTabs.size = 40;
- After instantiation:
tabBar.size = 40;
Container width
and height
property
The container must have a width and height. The default is 500 × 500 pixels.
css
method
The tab bar’s background color, border color, and border width affect both the tab bar and content area and can be set as follows:
- Declaratively, before instantiation: Use CSS (affects all instances):
<style>
.curvy-tabs-container > div {
border: 2x solid red;
background-color: yellow;
}
</style>
- Programmatically, after instantiation: Such styles can be set programmatically using the
css
method (works like jQuery's css
method):
tabBar.css('borderColor', 'red');
tabBar.css('borderColor');
tabBar.css({ borderColor: 'yellow', backgroundColor: 'red' });
tabBar.css(['borderColor', 'backgroundColor']);
(Note that the tab bar’s background color is only visible through transparent tabs; there is no point in setting this if all your tabs have defined colors.)
Tab contentCss
method
To set styles on all the content divs at once:
Event Handlers
tabBar.onclick
If defined as a function, this event handler will be fired on every click of any tab. The event object contains content
(a reference to the content element to be displayed, whose name
attribute is used as the tab label), left
(horizontal pixel location of left edge of tab, and width
(width of tab). For example:
tabBar.onclick = function(event) {
console.log('tab clicked:', event.content.getAttribute('name'));
};
event.preventDefault()
Calling event.preventDefault()
from this handler will prevent the clicked tab from being selected. Therefore, this is a way of disabling all tabs.
tab.onclick
In addition, each tab may also define its own event handler, fired only when that tab is clicked on. For example:
var content = this.tabBar.container.querySelector('.curvy-tabs-content');
tabBar.tabs.get(content).onclick = function(event) { ... };
event.preventDefault()
As above, calling event.preventDefault()
from within will prevent the tab from being selected. This is a way of disabling just this specific tab.
event.stopPropagation()
The event will be propagated to the tabBar.onclick
handler (if defined) unless you call event.stopPropagation()
from within.
Version History
1.0.0
— Initial version2.0.0
- Cleaner DOM structure
height
is now size
containerHeight
is now height
- Add
contents
, contentDivs
, css, and
contentCss`
2.0.1
- Bump version numbers in README.md
2.1.0
- Add & document
getTab
, select
, and clear
convenience methods - Tab visibility now respects content div's
display
prop - Document how to hide a tab by setting content div's
display
prop - Add & document
hide
, show
, and toggle
convenience methods
2.1.1
- Bump version numbers in README.md (again) (doh!)
- Add
CurvyTabs.version
shared property - Adjust build-and-push.sh to keep previous versions