Skip to content

Commit

Permalink
Merge pull request #45 from goodguyry/feature/menu-submenu-configuration
Browse files Browse the repository at this point in the history
Feature: Support non-UL MenuBar Submenu Popups
  • Loading branch information
goodguyry authored Jan 2, 2021
2 parents 849c5af + 0d3a00c commit 5897c92
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module.exports = {
node: true,
jest: true,
},
globals: {
siteClassNames: true,
},
settings: {
'import/resolver': {
'babel-module': {},
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ vendor
# Application files #
######################
*.sass-cache
*.jekyll-cache
*.sublime-*
.project
.settings
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).

## 0.3.1

**Changed**

- Loosens MenuBar Popups' markup requirements (#45)

## 0.3.0

**Changed**
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/assets.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global: { css: 'build/css/global.f0739c4bd2ecae532939.min.css', js: 'build/js/global.14686bc4c744d0a95265.bundle.min.js' }
global: { css: 'build/css/global.dd061c22a39f20900817.min.css', js: 'build/js/global.353c609fea64cc772d5e.bundle.min.js' }

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aria-components",
"version": "0.3.0",
"version": "0.3.1",
"description": "JavaScript classes to aid in accessible web development.",
"keywords": [
"a11y",
Expand Down
14 changes: 8 additions & 6 deletions src/MenuBar/MenuBar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ const menubarMarkup = `
<li><a class="second-item" href="example.com">Cake</a></li>
<li>
<a class="third-item" href="example.com">Vegetables</a>
<ul class="sublist2">
<li><a class="sublist2-first-item" href="example.com">Carrots</a></li>
<li><a class="sublist2-second-item" href="example.com">Broccoli</a></li>
<li><a class="sublist2-third-item" href="example.com">Brussel Sprouts</a></li>
<li><a class="sublist2-last-item" href="example.com">Asparagus</a></li>
</ul>
<div class="not-a-list">
<ul class="sublist2">
<li><a class="sublist2-first-item" href="example.com">Carrots</a></li>
<li><a class="sublist2-second-item" href="example.com">Broccoli</a></li>
<li><a class="sublist2-third-item" href="example.com">Brussel Sprouts</a></li>
<li><a class="sublist2-last-item" href="example.com">Asparagus</a></li>
</ul>
</div>
</li>
<li><a class="fourth-item" href="example.com">Pie</a></li>
<li><a class="last-item" href="example.com">Ice Cream</a></li>
Expand Down
7 changes: 7 additions & 0 deletions src/MenuBar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ MenuBar.menu
MenuBar.menuBarItems
```

## Additional Notes

If a menu item has a sibling, that sibling will be turned into a "submenu popup".
If that element is not a UL element, the script will search the popup target with
`Element.querySelector('ul')` and use that as the `list` passed to the `Menu`
component.

## Example

```html
Expand Down
57 changes: 38 additions & 19 deletions src/MenuBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,46 @@ export default class MenuBar extends AriaComponent {
});

// Initialize popups for nested lists.
this.popups = this.menuBarItems.reduce((acc, controller) => {
const { popups, subMenus } = this.menuBarItems.reduce((acc, controller) => {
const target = controller.nextElementSibling;

if (null !== target && 'UL' === target.nodeName) {
const popup = new Popup({
controller,
target,
onInit: this.onPopupInit,
type: 'menu',
});
// Bail if there's no target.
if (null === target) {
return acc;
}

const popup = new Popup({
controller,
target,
onInit: this.onPopupInit,
type: 'menu',
});

// Initialize submenu Menus.
const subList = new Menu({ list: target });
target.addEventListener('keydown', this.handleMenuItemKeydown);
acc.popups.push(popup);

// Save the list's previous sibling.
subList.previousSibling = controller;
// If target isn't a UL, find the UL in target and use it.
const list = ('UL' === target.nodeName)
? target
: target.querySelector('ul');

return [...acc, popup];
// Bail if there's no list.
if (null === list) {
return acc;
}

// Initialize submenu Menus.
const subMenu = new Menu({ list });
target.addEventListener('keydown', this.handleMenuItemKeydown);

// Save the list's previous sibling.
subMenu.previousSibling = controller;
acc.subMenus.push(subMenu);

return acc;
}, []);
}, { popups: [], subMenus: [] });

// Save components as instance properties.
Object.assign(this, { popups, subMenus });

/**
* Set initial state.
Expand Down Expand Up @@ -426,16 +443,18 @@ export default class MenuBar extends AriaComponent {
// Remove tabindex attribute.
tabIndexAllow(this.menuBarItems);

// Destroy nested components.
// Destroy popups.
this.popups.forEach((popup) => {
if (isInstanceOf(popup.target.menu, Menu)) {
popup.target.menu.destroy();
}
popup.target.removeEventListener('keydown', this.handleMenuItemKeydown);

popup.destroy();
});

// Destroy subMenus.
this.subMenus.forEach((submenu) => {
submenu.destroy();
});

// Run {destroyCallback}
this.onDestroy.call(this);
}
Expand Down

0 comments on commit 5897c92

Please sign in to comment.