Skip to content

Commit

Permalink
add option to use history
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic Oliver committed Oct 16, 2015
1 parent 1002bd6 commit abd8552
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
23 changes: 15 additions & 8 deletions dist/js/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default class Tabs {
titleSelector: $('.tab-title'),
activeClass: 'active',
afterSetup: () => {},
afterChange: () => {}
afterChange: () => {},
tabHistory: true
}, options);

this.$el = this.options.moduleSelector;
Expand Down Expand Up @@ -52,8 +53,19 @@ export default class Tabs {
this.$el.find('a').on('click', (e) => {
e.preventDefault();
const $target = $(e.currentTarget);
const tabId = $target.attr('href');
this._toggleClasses($target.parent());
this.displayTabContent($target.attr('href'));
this.displayTabContent(tabId);

if (history.pushState) {
if (this.options.tabHistory) {
history.pushState({}, tabId, tabId);
} else {
history.replaceState({}, tabId, tabId);
}
} else {
window.location.hash = tabId;
}
});

$(window).on('hashchange', (e) => {
Expand All @@ -74,13 +86,8 @@ export default class Tabs {

// Update the tab content "active" state (showing the tab).
displayTabContent(tabId) {
if (history.pushState) {
history.pushState(null, null, tabId);
} else {
window.location.hash = tabId;
}

this._toggleClasses($(tabId));

this._updateTabNav(tabId);

this.options.afterChange(tabId);
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Callback function called once the tabs have been initialized. Gets passed the id
#### `afterChange`
Callback function called when a tab is clicked. Gets passed the id of the clicked tab.

#### `tabHistory`
Leverage the history API for tab chages. Defaults to `false`.



### Some sample markup
Expand Down

0 comments on commit abd8552

Please sign in to comment.