-
Notifications
You must be signed in to change notification settings - Fork 405
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
nolanlawson
merged 7 commits into
salesforce:master
from
rochejul:feat/4219-access-real-html-element-in-consistently-way
Jun 14, 2024
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0283f6e
feat(lightning-element): expose elementSelf property
rochejul 855ebe4
refactor(nolan): apply feedback
rochejul 6b7ece5
refactor(nolan): apply feedback v2
rochejul 98d6d13
refactor(nolan): apply feedback v3
rochejul a180cec
chore: rename elementSelf to hostElement
nolanlawson 9ededab
chore: alphabetical ordering
nolanlawson 6cb47e5
fix: do not support on the server side
nolanlawson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/@lwc/integration-karma/test/component/LightningElement.elementSelf/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
1 change: 1 addition & 0 deletions
1
...ges/@lwc/integration-karma/test/component/LightningElement.elementSelf/x/light/light.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<template lwc:render-mode="light"></template> |
10 changes: 10 additions & 0 deletions
10
packages/@lwc/integration-karma/test/component/LightningElement.elementSelf/x/light/light.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ges/@lwc/integration-karma/test/component/LightningElement.elementSelf/x/shadow/shadow.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...@lwc/integration-karma/test/component/LightningElement.elementSelf/x/wrapper/wrapper.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
18 changes: 18 additions & 0 deletions
18
...s/@lwc/integration-karma/test/component/LightningElement.elementSelf/x/wrapper/wrapper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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