Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate theme attribute #238

Merged
merged 5 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/vaadin-context-menu-overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
};
}

static get observers() {
return ['_themeChanged(theme)'];
}

ready() {
super.ready();

Expand All @@ -95,6 +99,10 @@
return this.content.querySelector(':not(style):not(slot)');
}

_themeChanged(theme) {
this.close();
}

getBoundaries() {
// Measure actual overlay and content sizes
const overlayRect = this.getBoundingClientRect();
Expand Down
2 changes: 2 additions & 0 deletions src/vaadin-context-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@
*
* Note: the `theme` attribute value set on `<vaadin-context-menu>` is
* propagated to the internal `<vaadin-context-menu-overlay>` component.
* In case of using nested menu items, the `theme` attribute is also propagated
* to internal `vaadin-context-menu-list-box` and `vaadin-context-menu-item`'s.
*
* @memberof Vaadin
* @mixes Vaadin.ElementMixin
Expand Down
16 changes: 16 additions & 0 deletions src/vaadin-contextmenu-items-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@
// Store the reference to align based on parent overlay coordinates
subMenu.$.overlay._setParentOverlay(parent);

// Set theme attribute from parent element
if (parent.theme) {
subMenu.setAttribute('theme', parent.theme);
} else {
subMenu.removeAttribute('theme');
}

let x;
content.style.minWidth = '';
if (document.documentElement.clientWidth - itemRect.right > itemRect.width) {
Expand Down Expand Up @@ -200,6 +207,7 @@
component.setAttribute('role', 'separator');
}
component.classList.add('vaadin-menu-item');
this.theme && component.setAttribute('theme', this.theme);

component._item = item;

Expand Down Expand Up @@ -241,6 +249,7 @@
`;
Polymer.flush();
const listBox = root.querySelector('vaadin-context-menu-list-box');
this.theme && listBox.setAttribute('theme', this.theme);
listBox.classList.add('vaadin-menu-list-box');
requestAnimationFrame(() => listBox.setAttribute('role', 'menu'));
const subMenu = root.querySelector('vaadin-context-menu');
Expand Down Expand Up @@ -323,6 +332,13 @@
menu.$.overlay.addEventListener(openEvent, openSubMenu);
menu.$.overlay.addEventListener('keydown', e =>
(e.keyCode === 39 || e.keyCode === 13 || e.keyCode === 32) && openSubMenu(e));
} else {
const listBox = root.querySelector('vaadin-context-menu-list-box');
if (this.theme) {
listBox.setAttribute('theme', this.theme);
} else {
listBox.removeAttribute('theme');
}
}
}

Expand Down
88 changes: 88 additions & 0 deletions test/items.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
</template>
</test-fixture>

<test-fixture id="theme">
<template>
<vaadin-context-menu theme="foo">
<button id="target"></button>
</vaadin-context-menu>
</template>
</test-fixture>

<script>
const MOBILE = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
const OPENEVENT = MOBILE ? 'touchstart' : 'mouseover';
Expand Down Expand Up @@ -482,6 +490,86 @@
});
}
});

describe('theme propagation', () => {
let subMenu2;

beforeEach(done => {
rootMenu = fixture('theme');
rootMenu.openOn = OPENEVENT;
target = rootMenu.firstElementChild;
rootMenu.items = [
{text: 'foo-0', children:
[
{text: 'foo-0-0'},
{text: 'foo-0-1', children: [
{text: 'foo-0-1-0'}
]},
]
},
{text: 'foo-1'}
];
open();
flush(() => {
open(menuComponents()[0]);
subMenu = getSubMenu();
flush(() => {
open(menuComponents(subMenu)[1]);
subMenu2 = getSubMenu();
flush(done);
});
});
});

it('should propagate theme attribute to the nested elements', () => {
[rootMenu, subMenu, subMenu2].forEach(subMenu => {
const overlay = subMenu.$.overlay;
const listBox = overlay.querySelector('vaadin-context-menu-list-box');
const items = Array.from(listBox.querySelectorAll('vaadin-context-menu-item'));

expect(overlay.getAttribute('theme')).to.equal('foo');
expect(listBox.getAttribute('theme')).to.equal('foo');

const itemsThemeEqualsFoo = items.filter(item => item.getAttribute('theme') !== 'foo').length === 0;
expect(itemsThemeEqualsFoo).to.be.true;
});
});

it('should close the menu and submenu on theme changed', () => {
rootMenu.setAttribute('theme', 'bar');
expect(rootMenu.opened).to.be.false;
expect(subMenu.opened).to.be.false;
expect(subMenu2.opened).to.be.false;
});

it('should remove theme attribute from the nested elements', async() => {
rootMenu.removeAttribute('theme');

// Should wait until submenus will be opened again.
await new Promise(resolve => {
open();
flush(() => {
open(menuComponents()[0]);
flush(() => {
open(menuComponents(subMenu)[1]);
flush(resolve);
});
});
});

[rootMenu, subMenu, subMenu2].forEach(subMenu => {
const overlay = subMenu.$.overlay;
const listBox = overlay.querySelector('vaadin-context-menu-list-box');
const items = Array.from(listBox.querySelectorAll('vaadin-context-menu-item'));

expect(overlay.hasAttribute('theme')).to.be.false;
expect(listBox.hasAttribute('theme')).to.be.false;

const itemsDoNotHaveTheme = items.filter(item => item.hasAttribute('theme')).length === 0;
expect(itemsDoNotHaveTheme).to.be.true;
});
});
});
});
</script>
</body>
Expand Down