11class Tabs {
2- constructor ( $module , namespace , responsive , historyEnabled ) {
2+ constructor ( $module ) {
33 this . $module = $module
4- this . namespace = namespace
5- this . responsive = responsive
6- this . historyEnabled = historyEnabled
7- this . $tabs = $module . querySelectorAll ( `.${ this . namespace } __tab` )
4+ this . $tabs = $module . querySelectorAll ( '.nhsuk-tabs__tab' )
85
96 this . keys = {
107 down : 40 ,
118 left : 37 ,
129 right : 39 ,
1310 up : 38
1411 }
15- this . jsHiddenClass = ` ${ this . namespace } __panel-- hidden`
12+ this . jsHiddenClass = 'nhsuk-tabs__panel-- hidden'
1613
1714 this . showEvent = new CustomEvent ( 'tab.show' )
1815 this . hideEvent = new CustomEvent ( 'tab.hide' )
1916
20- if ( typeof window . matchMedia === 'function' && this . responsive ) {
17+ if ( typeof window . matchMedia === 'function' ) {
2118 this . setupResponsiveChecks ( )
2219 } else {
2320 this . setup ( )
@@ -58,10 +55,8 @@ class Tabs {
5855 setup ( ) {
5956 const { $module } = this
6057 const { $tabs } = this
61- const $tabList = $module . querySelector ( `.${ this . namespace } __list` )
62- const $tabListItems = $module . querySelectorAll (
63- `.${ this . namespace } __list-item`
64- )
58+ const $tabList = $module . querySelector ( '.nhsuk-tabs__list' )
59+ const $tabListItems = $module . querySelectorAll ( '.nhsuk-tabs__list-item' )
6560
6661 if ( ! $tabs || ! $tabList || ! $tabListItems ) {
6762 return
@@ -96,19 +91,15 @@ class Tabs {
9691 this . showTab ( $activeTab )
9792
9893 // Handle hashchange events
99- if ( this . historyEnabled ) {
100- $module . boundOnHashChange = this . onHashChange . bind ( this )
101- window . addEventListener ( 'hashchange' , $module . boundOnHashChange , true )
102- }
94+ $module . boundOnHashChange = this . onHashChange . bind ( this )
95+ window . addEventListener ( 'hashchange' , $module . boundOnHashChange , true )
10396 }
10497
10598 teardown ( ) {
10699 const { $module } = this
107100 const { $tabs } = this
108- const $tabList = $module . querySelector ( `.${ this . namespace } __list` )
109- const $tabListItems = $module . querySelectorAll (
110- `.${ this . namespace } __list-item`
111- )
101+ const $tabList = $module . querySelector ( '.nhsuk-tabs__list' )
102+ const $tabListItems = $module . querySelectorAll ( '.nhsuk-tabs__list-item' )
112103
113104 if ( ! $tabs || ! $tabList || ! $tabListItems ) {
114105 return
@@ -129,10 +120,8 @@ class Tabs {
129120 this . unsetAttributes ( $tab )
130121 } )
131122
132- if ( this . historyEnabled ) {
133- // Remove hashchange event handler
134- window . removeEventListener ( 'hashchange' , $module . boundOnHashChange , true )
135- }
123+ // Remove hashchange event handler
124+ window . removeEventListener ( 'hashchange' , $module . boundOnHashChange , true )
136125 }
137126
138127 onHashChange ( ) {
@@ -167,7 +156,7 @@ class Tabs {
167156 }
168157
169158 getTab ( hash ) {
170- return this . $module . querySelector ( `.${ this . namespace } __tab [href="${ hash } "]` )
159+ return this . $module . querySelector ( `.nhsuk-tabs__tab [href="${ hash } "]` )
171160 }
172161
173162 setAttributes ( $tab ) {
@@ -203,7 +192,7 @@ class Tabs {
203192 }
204193
205194 onTabClick ( e ) {
206- if ( ! e . target . classList . contains ( ` ${ this . namespace } __tab` ) ) {
195+ if ( ! e . target . classList . contains ( 'nhsuk-tabs__tab' ) ) {
207196 e . stopPropagation ( )
208197 e . preventDefault ( )
209198 }
@@ -216,17 +205,15 @@ class Tabs {
216205 }
217206
218207 createHistoryEntry ( $tab ) {
219- if ( this . historyEnabled ) {
220- const $panel = this . getPanel ( $tab )
221-
222- // Save and restore the id
223- // so the page doesn't jump when a user clicks a tab (which changes the hash)
224- const { id } = $panel
225- $panel . id = ''
226- this . changingHash = true
227- window . location . hash = Tabs . getHref ( $tab ) . slice ( 1 )
228- $panel . id = id
229- }
208+ const $panel = this . getPanel ( $tab )
209+
210+ // Save and restore the id
211+ // so the page doesn't jump when a user clicks a tab (which changes the hash)
212+ const { id } = $panel
213+ $panel . id = ''
214+ this . changingHash = true
215+ window . location . hash = Tabs . getHref ( $tab ) . slice ( 1 )
216+ $panel . id = id
230217 }
231218
232219 onTabKeydown ( e ) {
@@ -252,7 +239,7 @@ class Tabs {
252239 let nextTab
253240
254241 if ( nextTabListItem ) {
255- nextTab = nextTabListItem . querySelector ( `. ${ this . namespace } __tab` )
242+ nextTab = nextTabListItem . querySelector ( '.nhsuk-tabs__tab' )
256243 }
257244 if ( nextTab ) {
258245 this . hideTab ( currentTab )
@@ -268,7 +255,7 @@ class Tabs {
268255 let previousTab
269256
270257 if ( previousTabListItem ) {
271- previousTab = previousTabListItem . querySelector ( `. ${ this . namespace } __tab` )
258+ previousTab = previousTabListItem . querySelector ( '.nhsuk-tabs__tab' )
272259 }
273260 if ( previousTab ) {
274261 this . hideTab ( currentTab )
@@ -297,19 +284,19 @@ class Tabs {
297284
298285 unhighlightTab ( $tab ) {
299286 $tab . setAttribute ( 'aria-selected' , 'false' )
300- $tab . parentNode . classList . remove ( ` ${ this . namespace } __list- item--selected` )
287+ $tab . parentNode . classList . remove ( 'nhsuk-tabs__list- item--selected' )
301288 $tab . setAttribute ( 'tabindex' , '-1' )
302289 }
303290
304291 highlightTab ( $tab ) {
305292 $tab . setAttribute ( 'aria-selected' , 'true' )
306- $tab . parentNode . classList . add ( ` ${ this . namespace } __list- item--selected` )
293+ $tab . parentNode . classList . add ( 'nhsuk-tabs__list- item--selected' )
307294 $tab . setAttribute ( 'tabindex' , '0' )
308295 }
309296
310297 getCurrentTab ( ) {
311298 return this . $module . querySelector (
312- `. ${ this . namespace } __list- item--selected .${ this . namespace } __tab`
299+ '.nhsuk-tabs__list- item--selected .nhsuk-tabs__tab'
313300 )
314301 }
315302
@@ -324,20 +311,11 @@ class Tabs {
324311}
325312
326313/**
327- * Main function to invoke tabs. Can be called as follows to alter various features
328- *
329- * Tabs({historyEnabled: false});
330- * Tabs({responsive: false});
331- * Tabs({namespace: 'my-custom-namespace'}); // Alters classes allowing alternative css
314+ * Initialise tabs component
332315 */
333- module . exports = ( {
334- namespace = 'nhsuk-tabs' ,
335- responsive = true ,
336- historyEnabled = true ,
337- scope = document
338- } = { } ) => {
339- const tabs = scope . querySelectorAll ( `[data-module="${ namespace } "]` )
316+ module . exports = ( { scope = document } = { } ) => {
317+ const tabs = scope . querySelectorAll ( '[data-module="nhsuk-tabs"]' )
340318 tabs . forEach ( ( el ) => {
341- new Tabs ( el , namespace , responsive , historyEnabled )
319+ new Tabs ( el )
342320 } )
343321}
0 commit comments