-
Notifications
You must be signed in to change notification settings - Fork 789
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(runtime): patch removeChild
for scoped
components
#5148
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -695,7 +695,7 @@ export const patch = (oldVNode: d.VNode, newVNode: d.VNode) => { | |
* | ||
* @param elm the element of interest | ||
*/ | ||
const updateFallbackSlotVisibility = (elm: d.RenderNode) => { | ||
export const updateFallbackSlotVisibility = (elm: d.RenderNode) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to export this to use in the patches in |
||
const childNodes: d.RenderNode[] = elm.childNodes as any; | ||
|
||
for (const childNode of childNodes) { | ||
|
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
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 { Component, h } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'remove-child-patch', | ||
scoped: true, | ||
}) | ||
export class RemoveChildPatch { | ||
render() { | ||
return ( | ||
<div> | ||
<p>I'm not in a slot</p> | ||
<div class="slot-container"> | ||
<slot>Slot fallback content</slot> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
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,27 @@ | ||
<!doctype html> | ||
<meta charset="utf8" /> | ||
<script src="./build/testapp.esm.js" type="module"></script> | ||
<script src="./build/testapp.js" nomodule></script> | ||
|
||
<remove-child-patch> | ||
<span>Slotted 1</span> | ||
<span>Slotted 2</span> | ||
</remove-child-patch> | ||
|
||
<button id="remove-child-button" type="button">Remove Last Child</button> | ||
<button id="remove-child-div-button" type="button">Remove Child Div</button> | ||
|
||
<script> | ||
document.querySelector('#remove-child-button').addEventListener('click', () => { | ||
const el = document.querySelector('remove-child-patch'); | ||
const slotContainer = el.querySelector('.slot-container'); | ||
const elementToRemove = slotContainer.children[slotContainer.children.length - 1]; | ||
el.removeChild(elementToRemove); | ||
}); | ||
|
||
document.querySelector('#remove-child-div-button').addEventListener('click', () => { | ||
const el = document.querySelector('remove-child-patch'); | ||
const elementToRemove = el.querySelector('div'); | ||
el.removeChild(elementToRemove); | ||
}); | ||
</script> |
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,64 @@ | ||
import { setupDomTests, waitForChanges } from '../util'; | ||
|
||
/** | ||
* Tests for the patched `removeChild()` method on `scoped` components. | ||
*/ | ||
describe('remove-child-patch', () => { | ||
const { setupDom, tearDownDom } = setupDomTests(document); | ||
|
||
let app: HTMLElement | undefined; | ||
let host: HTMLElement | undefined; | ||
|
||
beforeEach(async () => { | ||
app = await setupDom('/remove-child-patch/index.html'); | ||
host = app.querySelector('remove-child-patch'); | ||
}); | ||
|
||
afterEach(tearDownDom); | ||
|
||
it('should remove the last slotted node', async () => { | ||
expect(host).toBeDefined(); | ||
|
||
const slotContainer = host.querySelector('.slot-container'); | ||
expect(slotContainer).toBeDefined(); | ||
const slottedSpans = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpans.length).toBe(2); | ||
|
||
document.querySelector('button').click(); | ||
await waitForChanges(); | ||
|
||
const slottedSpansAfter = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpansAfter.length).toBe(1); | ||
}); | ||
|
||
it('should show slot-fb if the last slotted node is removed', async () => { | ||
expect(host).toBeDefined(); | ||
|
||
const slotContainer = host.querySelector('.slot-container'); | ||
expect(slotContainer).toBeDefined(); | ||
const slottedSpans = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpans.length).toBe(2); | ||
|
||
const button = document.querySelector<HTMLButtonElement>('#remove-child-button'); | ||
button.click(); | ||
await waitForChanges(); | ||
button.click(); | ||
await waitForChanges(); | ||
|
||
const slottedSpansAfter = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpansAfter.length).toBe(0); | ||
expect(slotContainer.textContent.trim()).toBe('Slot fallback content'); | ||
}); | ||
|
||
it('should still be able to remove nodes not slotted', async () => { | ||
expect(host).toBeDefined(); | ||
|
||
expect(host.querySelector('div')).toBeDefined(); | ||
|
||
const button = document.querySelector<HTMLButtonElement>('#remove-child-div-button'); | ||
button.click(); | ||
await waitForChanges(); | ||
|
||
expect(host.querySelector('div')).toBeNull(); | ||
}); | ||
}); |
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.
Just wanna call out that this will only be patched for
scoped
components. The associated issues had some examples using no encapsulation, but we're moving away from patching this behavior for that case.