Skip to content

Commit

Permalink
[n/a] add more a11y handling to tabs controller/demos
Browse files Browse the repository at this point in the history
  • Loading branch information
gregkohn committed Feb 4, 2022
1 parent 89e3bce commit aa38986
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/components/code-demo.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ const headingSlug = heading.replace(/\s+/g, '-').toLowerCase()
<div class="flex absolute top-1 right-2 p-3 backdrop-blur-sm">
<ul
class="flex space-x-1 pr-1 mr-[6px] relative after:absolute after:inset-y-0 after:left-full after:w-[2px] after:bg-black after:bg-opacity-20 after:scale-y-75"
data-action="keyup->tabs#cycleTabs"
data-action="keyup->tabs#handleKeydown"
role="tablist"
>
{['Preview', 'Code'].map((text, i) => (
<li>
<button
role="tab"
data-tabs-target="tab"
data-action="tabs#selectTab"
class={cx(
Expand Down Expand Up @@ -59,6 +60,7 @@ const headingSlug = heading.replace(/\s+/g, '-').toLowerCase()
class="rounded-lg p-8 pt-20 bg-gradient-to-r from-indigo-500 to-violet-500 text-slate-300 h-full overflow-auto"
data-tabs-target="panel"
role="tabpanel"
tabindex="0"
>
<slot name="preview" />
</div>
Expand Down
21 changes: 19 additions & 2 deletions src/js/controllers/tabs_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class extends Controller {
prevTab.removeAttribute('aria-selected')
prevTab.setAttribute('tabindex', -1)
prevPanel.setAttribute('hidden', true)
prevPanel.removeAttribute('tabindex')

const tab = this.tabTargets[this.indexValue]
const panel = this.panelTargets[this.indexValue]
Expand All @@ -93,6 +94,7 @@ export default class extends Controller {
tab.setAttribute('aria-selected', 'true')
tab.removeAttribute('tabindex')
panel.removeAttribute('hidden')
panel.setAttribute('tabindex', 0)

this.currentTab = {
tab,
Expand All @@ -108,11 +110,26 @@ export default class extends Controller {
this.indexValue = this.tabTargets.indexOf(e.currentTarget)
}

cycleTabs = (e) => {
handleKeydown = (e) => {
const key = normalizeKeyCode(e)
console.log(key)
if (['ArrowRight', 'ArrowLeft'].includes(key)) {
this.cycleTabs(key)
return
}

if (key === 'Home') {
this.indexValue = 0
return
}

if (!['ArrowRight', 'ArrowLeft'].includes(key)) return
if (key === 'End') {
this.indexValue = this.panelTargets.length - 1
return
}
}

cycleTabs = (key) => {
this.indexValue = wrap(
key === 'ArrowRight' ? this.indexValue + 1 : this.indexValue - 1,
this.panelTargets.length
Expand Down
18 changes: 10 additions & 8 deletions src/js/util/key-codes.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
const keyCodes = {
13: 'Enter',
32: 'Space',
9: 'Tab',
13: 'Enter',
27: 'Escape',
40: 'ArrowDown',
32: 'Space',
35: 'End',
36: 'Home',
37: 'ArrowLeft',
39: 'ArrowRight',
38: 'ArrowUp',
39: 'ArrowRight',
40: 'ArrowDown',
}

/**
* Noramlize code & keyCode for keyboard events
* @param {object} event
* @return {?string}
*/
* Noramlize code & keyCode for keyboard events
* @param {object} event
* @return {?string}
*/
export const normalizeKeyCode = ({ code, keyCode }) => {
if (code !== undefined) return code

Expand Down
18 changes: 16 additions & 2 deletions src/pages/tabs.astro
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ import CodeDemo from '../components/code-demo.astro'
>
<ul
class="flex justify-center mb-4"
data-action="keyup->tabs#cycleTabs"
data-action="keyup->tabs#handleKeydown"
role="tablist"
aria-label="Example 2 Tabs"
>
{['Tab 1', 'Tab 2', 'Tab 3'].map((text, i) => (
<li>
<button
role="tab"
data-tabs-target="tab"
data-action="tabs#selectTab"
class={cx(
Expand All @@ -89,6 +91,7 @@ import CodeDemo from '../components/code-demo.astro'
)}
aria-selected={i === 0 ? 'true' : null}
tabindex={i !== 0 ? '-1' : null}
id={`example-1-tab-${i + 1}`}
>
{text}
</button>
Expand All @@ -100,13 +103,16 @@ import CodeDemo from '../components/code-demo.astro'
class="p-12 border-2 border-white border-opacity-20 rounded-md shadow-lg text-6xl text-center font-extrabold"
data-tabs-target="panel"
role="tabpanel"
tabindex="0"
aria-labelledby="example-1-tab-1"
>
Panel 1
</div>
<div
class="p-12 border-2 border-white border-opacity-20 rounded-md shadow-lg text-6xl text-center font-extrabold"
data-tabs-target="panel"
role="tabpanel"
aria-labelledby="example-1-tab-2"
hidden
>
Panel 2
Expand All @@ -115,6 +121,7 @@ import CodeDemo from '../components/code-demo.astro'
class="p-12 border-2 border-white border-opacity-20 rounded-md shadow-lg text-6xl text-center font-extrabold"
data-tabs-target="panel"
role="tabpanel"
aria-labelledby="example-1-tab-3"
hidden
>
Panel 3
Expand Down Expand Up @@ -184,12 +191,14 @@ import CodeDemo from '../components/code-demo.astro'
>
<ul
class="flex justify-center md:hidden"
data-action="keyup->tabs#cycleTabs"
data-action="keyup->tabs#handleKeydown"
role="tablist"
aria-label="Example 2 Tabs"
>
{['Tab 1', 'Tab 2', 'Tab 3'].map((text, i) => (
<li>
<button
role="tab"
data-tabs-target="tab"
data-action="tabs#selectTab"
class={cx(
Expand All @@ -199,6 +208,7 @@ import CodeDemo from '../components/code-demo.astro'
)}
aria-selected={i === 0 ? 'true' : null}
tabindex={i !== 0 ? '-1' : null}
id={`example-2-tab-${i + 1}`}
>
{text}
</button>
Expand All @@ -211,13 +221,16 @@ import CodeDemo from '../components/code-demo.astro'
class="p-12 border-2 border-white border-opacity-20 rounded-md shadow-lg text-3xl font-extrabold"
data-tabs-target="panel"
role="tabpanel"
tabindex="0"
aria-labelledby="example-2-tab-1"
>
Panel 1
</div>
<div
class="p-12 border-2 border-white border-opacity-20 rounded-md shadow-lg text-3xl font-extrabold"
data-tabs-target="panel"
role="tabpanel"
aria-labelledby="example-2-tab-2"
hidden
>
Panel 2
Expand All @@ -226,6 +239,7 @@ import CodeDemo from '../components/code-demo.astro'
class="p-12 border-2 border-white border-opacity-20 rounded-md shadow-lg text-3xl font-extrabold"
data-tabs-target="panel"
role="tabpanel"
aria-labelledby="example-2-tab-3"
hidden
>
Panel 3
Expand Down

0 comments on commit aa38986

Please sign in to comment.