Created
August 19, 2025 18:57
-
-
Save megclaypool/0fafb2354737f8edaf8ec50bf0b4be17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| You need to start with: | |
| ```js | |
| ((Drupal) => { | |
| Drupal.behaviors.nameOfLibrary = { | |
| attach(context) { | |
| ``` | |
| Then it ends with: | |
| ```js | |
| }, | |
| }; | |
| })(Drupal); | |
| ``` | |
| And your custom code goes in the middle. Here's an example: | |
| ```js | |
| ((Drupal) => { | |
| Drupal.behaviors.railToggle = { | |
| attach(context) { | |
| context | |
| .querySelectorAll(".rail .component .rail-toggle") | |
| .forEach((element) => { | |
| let selector = element.getAttribute("aria-controls"); | |
| let parent = element.parentElement; | |
| let target = element.parentElement.querySelector(`.${selector}`); | |
| target.setAttribute("aria-hidden", "true"); | |
| parent.setAttribute("aria-expanded", "false"); | |
| element.addEventListener("click", (event) => { | |
| event.preventDefault(); | |
| target.setAttribute( | |
| "aria-hidden", | |
| target.getAttribute("aria-hidden") === "true" ? "false" : "true" | |
| ); | |
| parent.setAttribute( | |
| "aria-expanded", | |
| parent.getAttribute("aria-expanded") === "false" | |
| ? "true" | |
| : "false" | |
| ); | |
| }); | |
| }); | |
| }, | |
| }; | |
| })(Drupal); | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment