From da0ff6c30296bf80ea47d8184620672bf6f1f51e Mon Sep 17 00:00:00 2001 From: hontas Date: Mon, 9 Mar 2020 00:32:04 +0100 Subject: [PATCH] for custom elements run onMount in connectedCallback --- src/runtime/internal/Component.ts | 6 ++++++ test/custom-elements/samples/oncreate/main.svelte | 3 +++ test/custom-elements/samples/oncreate/test.js | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index 10588a0804..ea3e9dafea 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -56,6 +56,9 @@ export function mount_component(component, target, anchor) { const { fragment, on_mount, on_destroy, after_update } = component.$$; fragment && fragment.m(target, anchor); + + // custom element: call onMount in connectedCallback instead + if (component.shadowRoot) return; // onMount happens before the initial afterUpdate add_render_callback(() => { @@ -176,6 +179,9 @@ if (typeof HTMLElement === 'function') { // @ts-ignore todo: improve typings this.appendChild(this.$$.slotted[key]); } + + const new_on_destroy = this.$$.on_mount.map(run).filter(is_function); + this.$$.on_destroy.push(...new_on_destroy); } attributeChangedCallback(attr, _oldValue, newValue) { diff --git a/test/custom-elements/samples/oncreate/main.svelte b/test/custom-elements/samples/oncreate/main.svelte index ed3980a28e..9ddf1880ec 100644 --- a/test/custom-elements/samples/oncreate/main.svelte +++ b/test/custom-elements/samples/oncreate/main.svelte @@ -4,8 +4,11 @@ import { onMount } from 'svelte'; export let wasCreated; + export let propsSetBeforeMount; + export let attrs; onMount(() => { wasCreated = true; + propsSetBeforeMount = attrs === 'should be set'; }); diff --git a/test/custom-elements/samples/oncreate/test.js b/test/custom-elements/samples/oncreate/test.js index 11d76078f2..a85c09178d 100644 --- a/test/custom-elements/samples/oncreate/test.js +++ b/test/custom-elements/samples/oncreate/test.js @@ -2,7 +2,8 @@ import * as assert from 'assert'; import './main.svelte'; export default function (target) { - target.innerHTML = ''; + target.innerHTML = ''; const el = target.querySelector('my-app'); assert.ok(el.wasCreated); + assert.ok(el.propsSetBeforeMount); } \ No newline at end of file