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 yield block placement #563

Merged
merged 2 commits into from
May 4, 2017
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: 5 additions & 3 deletions src/generators/dom/visitors/YieldTag.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export default function visitYieldTag ( generator, block, state ) {
block.builders.mount.addLine(
`${block.component}._yield && ${block.component}._yield.mount( ${state.parentNode || block.target}, null );`
const parentNode = state.parentNode || block.target;

( state.parentNode ? block.builders.create : block.builders.mount ).addLine(
`if ( ${block.component}._yield ) ${block.component}._yield.mount( ${parentNode}, null );`
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for switching this (and the destroy block below) to a regular if from a short circuited &&? Just a style thing? It seems we're relying on short circuiting in generated code for conciseness elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

No particular reason, just saw it and thought there's no reason it shouldn't be an if — the intent is clearer, and it minifies the same way. I reckon we should generally prefer if to && for statements (as opposed to expressions). Don't feel all that strongly though

);

block.builders.destroy.addLine(
`${block.component}._yield && ${block.component}._yield.destroy( detach );`
`if ( ${block.component}._yield ) ${block.component}._yield.destroy( detach );`
);
}
6 changes: 6 additions & 0 deletions test/runtime/samples/component-yield-placement/Modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class='modal-background' on:click='destroy()'></div>

<div class='modal'>
{{yield}}
<button on:click='destroy()'>close modal</button>
</div>
25 changes: 25 additions & 0 deletions test/runtime/samples/component-yield-placement/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
data: {
showModal: true
},

html: `
<div class='modal-background'></div>

<div class='modal'>
<h2>Hello!</h2>
<button>close modal</button>
</div>
`,

test ( assert, component, target, window ) {
const button = target.querySelector( 'button' );
const click = new window.MouseEvent( 'click' );

button.dispatchEvent( click );

assert.htmlEqual( target.innerHTML, `
<button>show modal</button>
`);
}
};
15 changes: 15 additions & 0 deletions test/runtime/samples/component-yield-placement/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{#if showModal}}
<Modal on:destroy='set({ showModal: false })'>
<h2>Hello!</h2>
</Modal>
{{else}}
<button on:click='set({ showModal: true })'>show modal</button>
{{/if}}

<script>
import Modal from './Modal.html';

export default {
components: { Modal }
};
</script>