From 0e221206a8f5ca3eaaac69f8c569495e3c2875f6 Mon Sep 17 00:00:00 2001 From: Niclas Date: Wed, 27 Nov 2024 14:16:40 +0100 Subject: [PATCH] feat: click away aria pressed --- source/js/ClickAway.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/source/js/ClickAway.ts b/source/js/ClickAway.ts index c50474dd4..c0222154a 100644 --- a/source/js/ClickAway.ts +++ b/source/js/ClickAway.ts @@ -1,5 +1,9 @@ export class ClickAway { - constructor(private element: Element, private classesToRemove: string[]) {} + constructor( + private element: Element, + private classesToRemove: string[], + private removePressed: NodeListOf + ) {} public handleClickAway(target: Element): void { if (this.element.contains(target)) return; @@ -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'); + }); } } @@ -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;