]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Use pull request #11167 from MoarCoding/develop for v6.5.0
authorStefan Lisper <Stefan.Lisper@knowit.se>
Sat, 16 Jun 2018 07:56:01 +0000 (09:56 +0200)
committerNicolas Coden <nicolas@ncoden.fr>
Sat, 16 Jun 2018 20:58:48 +0000 (22:58 +0200)
2bfd3e0c4 fixed bug where aria-controls is not updated with multiple ids
3e6e35cb6 Removed old code
6437acf2b fix: improve id detection/replacment for Toggler `aria-controls` attribute

Co-Authored-By: Nicolas Coden <nicolas@ncoden.fr>
Signed-off-by: Nicolas Coden <nicolas@ncoden.fr>
js/foundation.toggler.js

index 5373cecb4c1bd9f06edf2a4a31fd7edd7338831d..f1f67930c6d7d319c8ba1e6d7f9fe35f8b2d089d 100644 (file)
@@ -3,6 +3,7 @@
 import $ from 'jquery';
 import { Motion } from './foundation.util.motion';
 import { Plugin } from './foundation.core.plugin';
+import { RegExpEscape } from './foundation.core.utils';
 import { Triggers } from './foundation.util.triggers';
 
 /**
@@ -55,13 +56,20 @@ class Toggler extends Plugin {
       this.className = input[0] === '.' ? input.slice(1) : input;
     }
 
-    // Add ARIA attributes to triggers
-    var id = this.$element[0].id;
-    $(`[data-open="${id}"], [data-close="${id}"], [data-toggle="${id}"]`)
-      .attr({
-        'aria-controls': id,
-        'aria-expanded': this.$element.is(':hidden') ? false : true
-      });
+    // Add ARIA attributes to triggers:
+    var id = this.$element[0].id,
+      $triggers = $(`[data-open~="${id}"], [data-close~="${id}"], [data-toggle~="${id}"]`);
+
+    // - aria-expanded: according to the element visibility.
+    $triggers.attr('aria-expanded', !this.$element.is(':hidden'));
+    // - aria-controls: adding the element id to it if not already in it.
+    $triggers.each((index, trigger) => {
+      const $trigger = $(trigger);
+      const controls = $trigger.attr('aria-controls') || '';
+
+      const containsId = new RegExp(`\\b${RegExpEscape(id)}\\b`).test(controls);
+      if (!containsId) $trigger.attr('aria-controls', controls ? `${controls} ${id}` : id);
+    });
   }
 
   /**