gather event listeners for slot default elements and render in a condition - fixes #1977

pull/1978/head
Chris Reeves 6 years ago
parent 8e9f37a7d4
commit 6c89975396

@ -234,29 +234,7 @@ export default class Block {
this.builders.mount.addLine(`${this.autofocus}.focus();`); this.builders.mount.addLine(`${this.autofocus}.focus();`);
} }
if (this.event_listeners.length > 0) { this.renderListeners();
this.addVariable('#dispose');
if (this.event_listeners.length === 1) {
this.builders.hydrate.addLine(
`#dispose = ${this.event_listeners[0]};`
);
this.builders.destroy.addLine(
`#dispose();`
)
} else {
this.builders.hydrate.addBlock(deindent`
#dispose = [
${this.event_listeners.join(',\n')}
];
`);
this.builders.destroy.addLine(
`@run_all(#dispose);`
);
}
}
const properties = new CodeBuilder(); const properties = new CodeBuilder();
@ -403,6 +381,32 @@ export default class Block {
}); });
} }
renderListeners(chunk: string = '') {
if (this.event_listeners.length > 0) {
this.addVariable(`#dispose${chunk}`);
if (this.event_listeners.length === 1) {
this.builders.hydrate.addLine(
`#dispose${chunk} = ${this.event_listeners[0]};`
);
this.builders.destroy.addLine(
`#dispose${chunk}();`
)
} else {
this.builders.hydrate.addBlock(deindent`
#dispose${chunk} = [
${this.event_listeners.join(',\n')}
];
`);
this.builders.destroy.addLine(
`@run_all(#dispose${chunk});`
);
}
}
}
toString() { toString() {
const localKey = this.key && this.getUniqueName('key'); const localKey = this.key && this.getUniqueName('key');

@ -72,7 +72,11 @@ export default class SlotWrapper extends Wrapper {
block.builders.update.pushCondition(`!${content_name}`); block.builders.update.pushCondition(`!${content_name}`);
block.builders.destroy.pushCondition(`!${content_name}`); block.builders.destroy.pushCondition(`!${content_name}`);
const listeners = block.event_listeners;
block.event_listeners = [];
this.fragment.render(block, parentNode, parentNodes); this.fragment.render(block, parentNode, parentNodes);
block.renderListeners(`_${content_name}`);
block.event_listeners = listeners;
block.builders.create.popCondition(); block.builders.create.popCondition();
block.builders.hydrate.popCondition(); block.builders.hydrate.popCondition();

@ -0,0 +1,7 @@
<script>
function click() {}
</script>
<p>
<slot><button on:click>Should not appear</button></slot>
</p>

@ -0,0 +1,3 @@
export default {
html: '<p>Hello</p>', show: true
};

@ -0,0 +1,7 @@
<script>
import Nested from './Nested.html';
</script>
<Nested>
Hello
</Nested>
Loading…
Cancel
Save