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

feat(lightning-element): expose hostElement property #4259

Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
isFunction,
isNull,
isObject,
isFalse,
isUndefined,
KEY__SYNTHETIC_MODE,
keys,
Expand Down Expand Up @@ -194,6 +195,7 @@ export interface LightningElement extends HTMLElementTheGoodParts, AccessibleEle
constructor: LightningElementConstructor;
template: ShadowRoot | null;
refs: RefNodes | undefined;
elementSelf: Element;
render(): Template;
connectedCallback?(): void;
disconnectedCallback?(): void;
Expand Down Expand Up @@ -542,6 +544,18 @@ function warnIfInvokedDuringConstruction(vm: VM, methodOrPropName: string) {
return vm.shadowRoot;
},

get elementSelf(): Element {
const vm = getAssociatedVM(this);

if (process.env.NODE_ENV !== 'production') {
if (isFalse(vm.elm instanceof Element)) {
logError(`this.elementSelf should be an Element, found: ${vm.elm}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do assert.false here instead.

Historically we do try to avoid throwing dev-only errors, but in this case I don't think it applies, because this is an invariant in the framework itself. If we are returning something other than an Element here, then something has gone horribly wrong and we need to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, let me have a look on it

}
}

return vm.elm;
},

get refs(): RefNodes | undefined {
const vm = getAssociatedVM(this);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createElement } from 'lwc';

import Wrapper from 'x/wrapper';

function createWrapper() {
const elm = createElement('x-wrapper', { is: Wrapper });
document.body.appendChild(elm);
return elm;
}

it('should provide the root element for light rendering', () => {
const elm = createWrapper();

const divElement = elm.getDivElement();
const lightElement = elm.getLightElement();
const elementSelf = lightElement.getHostElement();

expect(elementSelf).toBeTruthy();
expect(elementSelf.tagName).toEqual('X-LIGHT');
expect(elementSelf.dataset.name).toEqual('lightElement');

expect(elementSelf).toEqual(lightElement);
expect(elementSelf.parentElement).toEqual(divElement);
});

it('should provide the root element for shadow rendering', () => {
const elm = createWrapper();

const divElement = elm.getDivElement();
const shadowElement = elm.getShadowElement();
const elementSelf = shadowElement.getHostElement();

expect(elementSelf).toBeTruthy();
expect(elementSelf.tagName).toEqual('X-SHADOW');
expect(elementSelf.dataset.name).toEqual('shadowElement');

expect(elementSelf).toEqual(shadowElement);
expect(elementSelf.parentElement).toEqual(divElement);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<template lwc:render-mode="light"></template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { LightningElement, api } from 'lwc';

export default class Light extends LightningElement {
static renderMode = 'light';

@api
getHostElement() {
return this.elementSelf;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LightningElement, api } from 'lwc';

export default class Shadow extends LightningElement {
@api
getHostElement() {
return this.elementSelf;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<div data-name="divElement" lwc:ref="div">
<x-light data-name="lightElement" lwc:ref="light"></x-light>
<x-shadow data-name="shadowElement" lwc:ref="shadow"></x-shadow>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LightningElement, api } from 'lwc';

export default class Wrapper extends LightningElement {
@api
getDivElement() {
return this.refs.div;
}

@api
getLightElement() {
return this.refs.light;
}

@api
getShadowElement() {
return this.refs.shadow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const expectedEnumerableProps = [
'dir',
'dispatchEvent',
'draggable',
'elementSelf',
'firstChild',
'firstElementChild',
'getAttribute',
Expand Down
Loading