Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save megclaypool/0fafb2354737f8edaf8ec50bf0b4be17 to your computer and use it in GitHub Desktop.
Save megclaypool/0fafb2354737f8edaf8ec50bf0b4be17 to your computer and use it in GitHub Desktop.
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