Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit c052cfe

Browse files
simonowilliamernest
authored andcommitted
fix(menu): Close menu when a list-item was clicked. (#1756)
BREAKING CHANGE: Removes the `eventTargetHasClass` from the adapter. Previously clicks on a list-item in the page would leave the menu open. Now we check if a item in this menu was clicked and leave the menu open. Fixes #1747
1 parent d83d5bd commit c052cfe

File tree

7 files changed

+6
-27
lines changed

7 files changed

+6
-27
lines changed

packages/mdc-menu/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ Method Signature | Description
172172
`hasClass(className: string) => boolean` | Returns a boolean indicating whether the root element has a given class.
173173
`hasNecessaryDom() => boolean` | Returns boolean indicating whether the necessary DOM is present (namely, the `mdc-menu__items` container).
174174
`getAttributeForEventTarget(target: EventTarget, attributeName: string) => string` | Returns the value of a given attribute on an event target.
175-
`eventTargetHasClass: (target: EventTarget, className: string) => boolean` | Returns true if the event target has a given class.
176175
`getInnerDimensions() => {width: number, height: number}` | Returns an object with the items container width and height.
177176
`hasAnchor: () => boolean` | Returns whether the menu has an anchor for positioning.
178177
`getAnchorDimensions() => {width: number, height: number, top: number, right: number, bottom: number, left: number}` | Returns an object with the dimensions and position of the anchor (same semantics as `DOMRect`).

packages/mdc-menu/adapter.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ class MDCMenuAdapter {
5959
*/
6060
getAttributeForEventTarget(target, attributeName) {}
6161

62-
/**
63-
* @param {EventTarget} target
64-
* @param {string} className
65-
* @return {boolean}
66-
*/
67-
eventTargetHasClass(target, className) {}
68-
6962
/** @return {{ width: number, height: number }} */
7063
getInnerDimensions() {}
7164

packages/mdc-menu/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const cssClasses = {
2121
OPEN: 'mdc-menu--open',
2222
ANIMATING_OPEN: 'mdc-menu--animating-open',
2323
ANIMATING_CLOSED: 'mdc-menu--animating-closed',
24-
LIST_ITEM: 'mdc-list-item',
2524
SELECTED_LIST_ITEM: 'mdc-list-item--selected',
2625
};
2726

packages/mdc-menu/foundation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class MDCMenuFoundation extends MDCFoundation {
7979
hasClass: () => false,
8080
hasNecessaryDom: () => false,
8181
getAttributeForEventTarget: () => {},
82-
eventTargetHasClass: () => {},
8382
getInnerDimensions: () => ({}),
8483
hasAnchor: () => false,
8584
getAnchorDimensions: () => ({}),
@@ -239,15 +238,15 @@ class MDCMenuFoundation extends MDCFoundation {
239238
}
240239

241240
/**
242-
* Handle clicks and cancel the menu if not a list item
241+
* Handle clicks and cancel the menu if not a child list-item
243242
* @param {!Event} evt
244243
* @private
245244
*/
246245
handleDocumentClick_(evt) {
247246
let el = evt.target;
248247

249248
while (el && el !== document.documentElement) {
250-
if (this.adapter_.eventTargetHasClass(el, cssClasses.LIST_ITEM)) {
249+
if (this.adapter_.getIndexForEventTarget(el) !== -1) {
251250
return;
252251
}
253252
el = el.parentNode;

packages/mdc-menu/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ class MDCMenu extends MDCComponent {
139139
hasClass: (className) => this.root_.classList.contains(className),
140140
hasNecessaryDom: () => Boolean(this.itemsContainer_),
141141
getAttributeForEventTarget: (target, attributeName) => target.getAttribute(attributeName),
142-
eventTargetHasClass: (target, className) => target.classList.contains(className),
143142
getInnerDimensions: () => {
144143
const {itemsContainer_: itemsContainer} = this;
145144
return {width: itemsContainer.offsetWidth, height: itemsContainer.offsetHeight};

test/unit/mdc-menu/mdc-simple-menu.test.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ function getFixture(open) {
3737

3838
function setupTest(open = false) {
3939
const root = getFixture(open);
40-
const listItem = root.querySelector('.mdc-list-item');
4140
const component = new MDCMenu(root);
42-
return {root, listItem, component};
41+
return {root, component};
4342
}
4443

4544
suite('MDCMenu');
@@ -151,13 +150,6 @@ test('adapter#getAttributeForEventTarget returns the value of an attribute for a
151150
assert.equal(component.getDefaultFoundation().adapter_.getAttributeForEventTarget(target, attrName), attrVal);
152151
});
153152

154-
test('adapter#eventTargetHasClass returns true if a target has a given classname', () => {
155-
const {listItem, component} = setupTest();
156-
listItem.classList.add('foo');
157-
158-
assert.isTrue(component.getDefaultFoundation().adapter_.eventTargetHasClass(listItem, 'foo'));
159-
});
160-
161153
test('adapter#hasNecessaryDom returns false if the DOM does not include the items container', () => {
162154
const {root, component} = setupTest();
163155
const items = root.querySelector(strings.ITEMS_SELECTOR);

test/unit/mdc-menu/simple.foundation.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test('exports numbers', () => {
8686

8787
test('defaultAdapter returns a complete adapter implementation', () => {
8888
verifyDefaultAdapter(MDCMenuFoundation, [
89-
'addClass', 'removeClass', 'hasClass', 'hasNecessaryDom', 'getAttributeForEventTarget', 'eventTargetHasClass',
89+
'addClass', 'removeClass', 'hasClass', 'hasNecessaryDom', 'getAttributeForEventTarget',
9090
'getInnerDimensions', 'hasAnchor', 'getAnchorDimensions', 'getWindowDimensions',
9191
'getNumberOfItems', 'registerInteractionHandler', 'deregisterInteractionHandler', 'registerBodyClickHandler',
9292
'deregisterBodyClickHandler', 'getIndexForEventTarget', 'notifySelected', 'notifyCancel', 'saveFocus',
@@ -1058,8 +1058,7 @@ test('on document click cancels and closes the menu', () => {
10581058
td.when(mockAdapter.registerBodyClickHandler(td.matchers.isA(Function))).thenDo((handler) => {
10591059
documentClickHandler = handler;
10601060
});
1061-
td.when(mockAdapter.eventTargetHasClass(td.matchers.anything(), cssClasses.LIST_ITEM))
1062-
.thenReturn(false);
1061+
td.when(mockAdapter.getIndexForEventTarget(td.matchers.anything())).thenReturn(-1);
10631062

10641063
td.when(mockAdapter.hasClass(MDCMenuFoundation.cssClasses.OPEN)).thenReturn(true);
10651064

@@ -1086,8 +1085,7 @@ test('on menu item click does not emit cancel', () => {
10861085
td.when(mockAdapter.registerBodyClickHandler(td.matchers.isA(Function))).thenDo((handler) => {
10871086
documentClickHandler = handler;
10881087
});
1089-
td.when(mockAdapter.eventTargetHasClass(td.matchers.anything(), cssClasses.LIST_ITEM))
1090-
.thenReturn(true);
1088+
td.when(mockAdapter.getIndexForEventTarget(td.matchers.anything())).thenReturn(0);
10911089

10921090
foundation.init();
10931091
foundation.open();

0 commit comments

Comments
 (0)