Skip to content

Commit

Permalink
Merge pull request #986 from helsingborg-stad/feat/click-away-aria-pr…
Browse files Browse the repository at this point in the history
…essed

feat: click away aria pressed
  • Loading branch information
NiclasNorin authored Nov 27, 2024
2 parents e726bcd + 0e22120 commit 38f9f88
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions source/js/ClickAway.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export class ClickAway {
constructor(private element: Element, private classesToRemove: string[]) {}
constructor(
private element: Element,
private classesToRemove: string[],
private removePressed: NodeListOf<Element>
) {}

public handleClickAway(target: Element): void {
if (this.element.contains(target)) return;
Expand All @@ -13,6 +17,14 @@ export class ClickAway {
this.element.classList.remove(className);
}
});

if (this.element.hasAttribute('aria-pressed')) {
this.element.setAttribute('aria-pressed', 'false');
}

this.removePressed.forEach(element => {
element.setAttribute('aria-pressed', 'false');
});
}

}
Expand All @@ -27,7 +39,9 @@ export function initializeClickAways() {

if (!classesToRemove || classesToRemove.length <= 0) return;

clickAwayInstances.push(new ClickAway(element, classesToRemove));
const removePressed = element.querySelectorAll('[aria-pressed][data-js-click-away-remove-pressed]');

clickAwayInstances.push(new ClickAway(element, classesToRemove, removePressed));
});

if (clickAwayInstances.length <= 0) return;
Expand Down

0 comments on commit 38f9f88

Please sign in to comment.