1
1
class Tabs {
2
- constructor ( $module , namespace , responsive , historyEnabled ) {
2
+ constructor ( $module ) {
3
3
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' )
8
5
9
6
this . keys = {
10
7
down : 40 ,
11
8
left : 37 ,
12
9
right : 39 ,
13
10
up : 38
14
11
}
15
- this . jsHiddenClass = ` ${ this . namespace } __panel-- hidden`
12
+ this . jsHiddenClass = '.nhsuk-tabs__panel-- hidden'
16
13
17
14
this . showEvent = new CustomEvent ( 'tab.show' )
18
15
this . hideEvent = new CustomEvent ( 'tab.hide' )
19
16
20
- if ( typeof window . matchMedia === 'function' && this . responsive ) {
17
+ if ( typeof window . matchMedia === 'function' ) {
21
18
this . setupResponsiveChecks ( )
22
19
} else {
23
20
this . setup ( )
@@ -58,10 +55,8 @@ class Tabs {
58
55
setup ( ) {
59
56
const { $module } = this
60
57
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' )
65
60
66
61
if ( ! $tabs || ! $tabList || ! $tabListItems ) {
67
62
return
@@ -96,19 +91,15 @@ class Tabs {
96
91
this . showTab ( $activeTab )
97
92
98
93
// 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 )
103
96
}
104
97
105
98
teardown ( ) {
106
99
const { $module } = this
107
100
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' )
112
103
113
104
if ( ! $tabs || ! $tabList || ! $tabListItems ) {
114
105
return
@@ -129,10 +120,8 @@ class Tabs {
129
120
this . unsetAttributes ( $tab )
130
121
} )
131
122
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 )
136
125
}
137
126
138
127
onHashChange ( ) {
@@ -167,7 +156,7 @@ class Tabs {
167
156
}
168
157
169
158
getTab ( hash ) {
170
- return this . $module . querySelector ( `.${ this . namespace } __tab [href="${ hash } "]` )
159
+ return this . $module . querySelector ( `.nhsuk-tabs__tab [href="${ hash } "]` )
171
160
}
172
161
173
162
setAttributes ( $tab ) {
@@ -203,7 +192,7 @@ class Tabs {
203
192
}
204
193
205
194
onTabClick ( e ) {
206
- if ( ! e . target . classList . contains ( ` ${ this . namespace } __tab` ) ) {
195
+ if ( ! e . target . classList . contains ( 'nhsuk-tabs__tab' ) ) {
207
196
e . stopPropagation ( )
208
197
e . preventDefault ( )
209
198
}
@@ -216,17 +205,15 @@ class Tabs {
216
205
}
217
206
218
207
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
230
217
}
231
218
232
219
onTabKeydown ( e ) {
@@ -252,7 +239,7 @@ class Tabs {
252
239
let nextTab
253
240
254
241
if ( nextTabListItem ) {
255
- nextTab = nextTabListItem . querySelector ( `. ${ this . namespace } __tab` )
242
+ nextTab = nextTabListItem . querySelector ( '.nhsuk-tabs__tab' )
256
243
}
257
244
if ( nextTab ) {
258
245
this . hideTab ( currentTab )
@@ -268,7 +255,7 @@ class Tabs {
268
255
let previousTab
269
256
270
257
if ( previousTabListItem ) {
271
- previousTab = previousTabListItem . querySelector ( `. ${ this . namespace } __tab` )
258
+ previousTab = previousTabListItem . querySelector ( '.nhsuk-tabs__tab' )
272
259
}
273
260
if ( previousTab ) {
274
261
this . hideTab ( currentTab )
@@ -297,19 +284,19 @@ class Tabs {
297
284
298
285
unhighlightTab ( $tab ) {
299
286
$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' )
301
288
$tab . setAttribute ( 'tabindex' , '-1' )
302
289
}
303
290
304
291
highlightTab ( $tab ) {
305
292
$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' )
307
294
$tab . setAttribute ( 'tabindex' , '0' )
308
295
}
309
296
310
297
getCurrentTab ( ) {
311
298
return this . $module . querySelector (
312
- `. ${ this . namespace } __list- item--selected .${ this . namespace } __tab`
299
+ '.nhsuk-tabs__list- item--selected .nhsuk-tabs__tab'
313
300
)
314
301
}
315
302
@@ -324,20 +311,11 @@ class Tabs {
324
311
}
325
312
326
313
/**
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
332
315
*/
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"]' )
340
318
tabs . forEach ( ( el ) => {
341
- new Tabs ( el , namespace , responsive , historyEnabled )
319
+ new Tabs ( el )
342
320
} )
343
321
}
0 commit comments