We wrote two chunks of code for Tabby. One for listening to a userโs click. Another for listening to Left and Right arrow keys.
We can simplify these two events.
The click event
Hereโs the code for the click event:
tabsList.addEventListener('click', event => { const tab = event.target const target = tab.dataset.target const tabContent = tabby.querySelector('#' + target)
// Selects a tab tabs.forEach(t => { t.classList.remove('is-selected') t.setAttribute('tabindex', '-1') }) tab.classList.add('is-selected') tab.removeAttribute('tabindex')
// Selects the corresponding tab content tabContents.forEach(c => c.classList.remove('is-selected')) tabContent.classList.add('is-selected')})Most of the code here is used to select a tab (and its tab-contents). We can group these code into a function called selectTab.
function selectTab() { // Do something}First, letโs copy-paste everything over.
function selectTab() { const tab = event.target const target = tab.dataset.target const tabContent = tabby.querySelector('#' + target)
// Selects a tab tabs.forEach(t => { t.classList.remove('is-selected') t.setAttribute('tabindex', '-1') }) tab.classList.add('is-selected') tab.removeAttribute('tabindex')
// Selects the corresponding tab content tabContents.forEach(c => c.classList.remove('is-selected')) tabContent.classList.add('is-selected')}If you look at the code, you know we need these variables:
tabtabstabContenttabContents
We can get them from these locations:
tab: fromevent.targettabs: from the global scopetabContent: fromtabtabContents: from the global scope
This means: We only need the tab variable in selectTab. We can pass tab directly into selectTab. We donโt have to introduce the entire event object.
function selectTab(tab) { const target = tab.dataset.target const tabContent = tabby.querySelector('#' + target) // ...}Using selectTab:
tabsList.addEventListener('click', event => { const tab = event.target selectTab(tab)})It became much easier to understand what the click event handler does!
The keydown event
Hereโs what we wrote for switching tabs with Left and Right arrow keys.
document.addEventListener('keydown', event => { const { key } = event if (key !== 'ArrowLeft' && key !== 'ArrowRight') return if (!event.target.matches('.tab')) return
const index = tabs.findIndex(t => t.classList.contains('is-selected'))
let targetTab if (key === 'ArrowLeft' && index !== 0) targetTab = tabs[index - 1] if (key === 'ArrowRight' && index !== tabs.length - 1) targetTab = tabs[index + 1] if (targetTab) targetTab.click()})In the first three lines, we checked whether we want to act on the event. These three lines cannot be moved anywhere else.
document.addEventListener('keydown', event => { const { key } = event if (key !== 'ArrowLeft' && key !== 'ArrowRight') return if (!event.target.matches('.tab')) return // ...})The next few lines are used to get the target tab.
document.addEventListener('keydown', event => { // ... const index = tabs.findIndex(t => t.classList.contains('is-selected'))
let targetTab if (key === 'ArrowLeft' && index !== 0) targetTab = tabs[index - 1] if (key === 'ArrowRight' && index !== tabs.length - 1) targetTab = tabs[index + 1]})Here, we tried to:
- Decide whether we should get previous or next tab
- Find the previous or next tab
- Trigger a click event
To simplify the code, we can create getPreviousTab and getNextTab functions.
function getPreviousTab() { // Do something}
function getNextTab() { // Do something}First, letโs copy-paste the code we used to get the previous and next tabs.
function getPreviousTab() { if (key === 'ArrowLeft' && index !== 0) targetTab = tabs[index - 1]}
function getNextTab() { if (key === 'ArrowRight' && index !== tabs.length - 1) targetTab = tabs[index + 1]}The purpose of getPreviousTab is to find the previous tab. Likewise for getNextTab. The key a user pressed should not matter, so letโs remove the key part.
function getPreviousTab() { if (index !== 0) targetTab = tabs[index - 1]}
function getNextTab() { if (index !== tabs.length - 1) targetTab = tabs[index + 1]}This makes more sense.
We need to return the previous tab in getPreviousTab. We also need to return the next tab in getNextTab.
function getPreviousTab() { if (index !== 0) { return tabs[index - 1] }}
function getNextTab() { if (index !== tabs.length - 1) { return tabs[index + 1] }}You can tell we need two variables for both getPreviousTab and getNextTab
tabsindex
We can get tabs from the global scope, so we only need to pass in index to getPreviousTab and getNextTab.
function getPreviousTab(index) { if (index !== 0) { return tabs[index - 1] }}
function getNextTab(index) { if (index !== tabs.length - 1) { return tabs[index + 1] }}Using getPreviousTab and getNextTab:
document.addEventListener('keydown', event => { // ... const index = tabs.findIndex(t => t.classList.contains('is-selected'))
let targetTab if (key === 'ArrowLeft') targetTab = getPreviousTab(index) if (key === 'ArrowRight') targetTab = getNextTab(index) // ...})This makes more sense now. It reads:
- If key is left, we grab the previous tab
- If key is right, we grab the next tab
Thereโs one final improvement we can make. When we found targetTab, we triggered a โclickโ with it.
document.addEventListener('keydown', event => { // ... if (targetTab) { targetTab.click() }})If you read this code, you understand we triggered a click event, which selects the appropriate tab with selectTab.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson