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

fix: inherit parent styles to extensions without is getter (#3710) (CP: 23.0) #3713

Merged
merged 1 commit into from
Apr 21, 2022
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
28 changes: 16 additions & 12 deletions packages/vaadin-themable-mixin/test/lit-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ import { ThemableMixin } from '../vaadin-themable-mixin.js';
* By default, the suite creates PolymerElement based custom elements, but in the
* -lit.test.js tests, we specifically want to create LitElement based custom elements instead.
*/
window.defineCustomElementFunction = (name, parentName, content, styles) => {
// eslint-disable-next-line max-params
window.defineCustomElementFunction = (name, parentName, content, styles, noIs) => {
const parentElement = parentName ? customElements.get(parentName) : LitElement;
class CustomElement extends ThemableMixin(parentElement) {
static get is() {
return name;
}

static get styles() {
return styles ? unsafeCSS(styles) : undefined;
}
}

render() {
if (content) {
const template = document.createElement('template');
template.innerHTML = content;
return template.content;
if (content) {
CustomElement.prototype.render = function () {
const template = document.createElement('template');
template.innerHTML = content;
return template.content;
};
}

if (!noIs) {
Object.defineProperty(CustomElement, 'is', {
get() {
return name;
}
return super.render();
}
});
}

customElements.define(name, CustomElement);
Expand Down
40 changes: 31 additions & 9 deletions packages/vaadin-themable-mixin/test/themable-mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ const createStyles =

const defineCustomElement =
window.defineCustomElementFunction ||
((name, parentName, content, styles) => {
// eslint-disable-next-line max-params
((name, parentName, content, styles, noIs) => {
const parentElement = parentName ? customElements.get(parentName) : PolymerElement;
class CustomElement extends ThemableMixin(parentElement) {
static get is() {
return name;
}
class CustomElement extends ThemableMixin(parentElement) {}

static get template() {
if (content) {
if (content) {
Object.defineProperty(CustomElement, 'template', {
get() {
if (styles) {
content = `<style>${styles}</style>${content}`;
}
Expand All @@ -28,8 +27,15 @@ const defineCustomElement =
template.innerHTML = content;
return template;
}
return super.template;
}
});
}

if (!noIs) {
Object.defineProperty(CustomElement, 'is', {
get() {
return name;
}
});
}

customElements.define(name, CustomElement);
Expand Down Expand Up @@ -196,8 +202,12 @@ defineCustomElement('test-qux', '', '<div part="text" id="text">text</div>');

defineCustomElement('test-own-template', 'test-foo', '<div part="text" id="text">text</div>');

defineCustomElement('test-own-template-no-is', 'test-foo', '<div part="text" id="text">text</div>', undefined, true);

defineCustomElement('test-no-template', '', '');

defineCustomElement('test-inherited-no-content-no-is', 'test-foo', '', undefined, true);

defineCustomElement('test-style-override', '', '<div part="text" id="text">text</div>');

defineCustomElement(
Expand Down Expand Up @@ -226,7 +236,9 @@ describe('ThemableMixin', () => {
<test-baz></test-baz>
<test-qux></test-qux>
<test-own-template></test-own-template>
<test-own-template-no-is></test-own-template-no-is>
<test-no-template></test-no-template>
<test-inherited-no-content-no-is></test-inherited-no-content-no-is>
<test-style-override></test-style-override>
<test-own-styles></test-own-styles>
</div>
Expand Down Expand Up @@ -280,6 +292,16 @@ describe('ThemableMixin', () => {
expect(getComputedStyle(getText(components['test-own-template'])).backgroundColor).to.equal('rgb(255, 0, 0)');
});

it('should inherit parent themes to own custom template with no is defined', async () => {
expect(getComputedStyle(getText(components['test-own-template-no-is'])).backgroundColor).to.equal('rgb(255, 0, 0)');
});

it('should inherit parent themes with no is nor content defined', async () => {
expect(getComputedStyle(getText(components['test-inherited-no-content-no-is'])).backgroundColor).to.equal(
'rgb(255, 0, 0)'
);
});

it('should override vaadin module styles', () => {
expect(getComputedStyle(getText(components['test-style-override'])).color).to.equal('rgb(0, 0, 0)');
});
Expand Down
12 changes: 10 additions & 2 deletions packages/vaadin-themable-mixin/vaadin-themable-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,15 @@ function getThemes(tagName) {
* @returns {boolean}
*/
function hasThemes(tagName) {
const elementClass = customElements.get(tagName);
return classHasThemes(customElements.get(tagName));
}

/**
* Check if the custom element type has themes applied.
* @param {Function} elementClass
* @returns {boolean}
*/
function classHasThemes(elementClass) {
return elementClass && Object.prototype.hasOwnProperty.call(elementClass, '__themes');
}

Expand All @@ -197,7 +205,7 @@ export const ThemableMixin = (superClass) =>
super.finalize();

const template = this.prototype._template;
if (!template || hasThemes(this.is)) {
if (!template || classHasThemes(this)) {
return;
}

Expand Down