Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling aria-expanded state #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@
<div class="text-center">
<h1 class="text-4xl tracking-tight font-extrabold text-gray-900 sm:text-5xl md:text-6xl">
<span class="block xl:inline">Stimulus</span>
<span class="block bg-gradient-to-r from-yellow-500 to-orange-500 xl:inline bg-clip-text text-transparent">Reveal Controller</span>
<span class="block bg-gradient-to-r from-yellow-500 to-orange-500 xl:inline bg-clip-text text-transparent"
>Reveal Controller</span
>
</h1>

<p class="mt-3 max-w-md mx-auto text-base text-gray-500 sm:text-lg md:mt-5 md:text-xl md:max-w-3xl">
Expand Down Expand Up @@ -136,45 +138,45 @@ <h1 class="text-4xl tracking-tight font-extrabold text-gray-900 sm:text-5xl md:t
<section class="mt-16">
<div data-controller="reveal">
<button
id="toggle-button"
data-action="click->reveal#toggle"
aria-expanded="false"
type="button"
class="bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border rounded shadow"
>
Toggle me!
</button>

<p id="toggle-target" data-reveal-target="item" class="hidden mt-4">
<p data-reveal-target="item" class="hidden mt-4">
Hey 👋
</p>
</div>

<div data-controller="reveal" class="mt-4">
<button
id="toggle-button"
data-action="click->reveal#hide"
aria-expanded="true"
type="button"
class="bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border rounded shadow"
>
Hide me!
</button>

<p id="toggle-target" data-reveal-target="item" class="mt-4">
<p data-reveal-target="item" class="mt-4">
Hey 🙈
</p>
</div>

<div data-controller="reveal" class="mt-4">
<button
id="toggle-button"
data-action="click->reveal#show"
aria-expanded="false"
type="button"
class="bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border rounded shadow"
>
Show me!
</button>

<p id="toggle-target" data-reveal-target="item" class="hidden mt-4">
<p data-reveal-target="item" class="hidden mt-4">
Hi 🐵
</p>
</div>
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ export default class extends Controller {
this.class = this.hasHiddenClass ? this.hiddenClass : 'hidden'
}

toggle (): void {
toggle (e): void {
e.currentTarget.setAttribute('aria-expanded', String(e.currentTarget.getAttribute('aria-expanded') !== 'true'))

this.itemTargets.forEach(item => {
item.classList.toggle(this.class)
})
}

show (): void {
show (e): void {
e.currentTarget.setAttribute('aria-expanded', 'true')

this.itemTargets.forEach(item => {
item.classList.remove(this.class)
})
}

hide (): void {
hide (e): void {
e.currentTarget.setAttribute('aria-expanded', 'false')

this.itemTargets.forEach(item => {
item.classList.add(this.class)
})
Expand Down
Loading