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

Custom element dispatch event #4142

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,4 @@ export class HtmlTag {
d() {
this.n.forEach(detach);
}
}
}
7 changes: 6 additions & 1 deletion src/runtime/internal/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export function createEventDispatcher() {
fn.call(component, event);
});
}

if (component.shadowRoot) {
const event = custom_event(type, detail);
component.dispatchEvent(event);
}
};
}

Expand All @@ -61,4 +66,4 @@ export function bubble(component, event) {
if (callbacks) {
callbacks.slice().forEach(fn => fn(event));
}
}
}
16 changes: 16 additions & 0 deletions test/custom-elements/samples/dispatch/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import { createEventDispatcher } from 'svelte';

const dispatch = createEventDispatcher();

const sayHello = () => {
dispatch('message', {
text: 'Hello!'
});
};
</script>

<svelte:options tag="custom-element" />
<button on:click={sayHello}>
Click to say hello
</button>
24 changes: 24 additions & 0 deletions test/custom-elements/samples/dispatch/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as assert from 'assert';
import './main.svelte';

export default async function(target) {
target.innerHTML = '<custom-element></custom-element>';
const el = target.querySelector('custom-element');
const button = el.shadowRoot.querySelector('button');

return new Promise((resolve, reject) => {
el.addEventListener('message', function changeHandler(evt) {
el.removeEventListener('message', changeHandler);

try {
assert.equal(evt.target, el);
assert.equal(evt.detail.text, 'Hello!');
resolve();
} catch (err) {
reject(err);
}
});

button.dispatchEvent(new MouseEvent('click'));
});
}