mirror of https://github.com/sveltejs/svelte
feat: introduce `$host` rune, deprecate `createEventDispatcher` (#11059)
closes #11022pull/11093/head
parent
8578857332
commit
22494be9ec
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"svelte": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: introduce `$host` rune, deprecate `createEventDispatcher`
|
@ -0,0 +1,8 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
error: {
|
||||||
|
code: 'invalid-host-location',
|
||||||
|
message: '$host() can only be used inside custom element component instances'
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,3 @@
|
|||||||
|
<script>
|
||||||
|
$host();
|
||||||
|
</script>
|
@ -0,0 +1 @@
|
|||||||
|
$host();
|
@ -0,0 +1,24 @@
|
|||||||
|
import { test } from '../../assert';
|
||||||
|
const tick = () => Promise.resolve();
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
async test({ assert, target }) {
|
||||||
|
target.innerHTML = '<custom-element></custom-element>';
|
||||||
|
/** @type {any} */
|
||||||
|
const el = target.querySelector('custom-element');
|
||||||
|
|
||||||
|
/** @type {string[]} */
|
||||||
|
const events = [];
|
||||||
|
const handle_evt = (e) => events.push(e.type, e.detail);
|
||||||
|
el.addEventListener('greeting', handle_evt);
|
||||||
|
|
||||||
|
await tick();
|
||||||
|
|
||||||
|
el.shadowRoot.querySelector('button').click();
|
||||||
|
assert.deepEqual(events, ['greeting', 'hello']);
|
||||||
|
|
||||||
|
el.removeEventListener('greeting', handle_evt);
|
||||||
|
el.shadowRoot.querySelector('button').click();
|
||||||
|
assert.deepEqual(events, ['greeting', 'hello']);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,9 @@
|
|||||||
|
<svelte:options customElement="custom-element" />
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function greet(greeting) {
|
||||||
|
$host().dispatchEvent(new CustomEvent('greeting', { detail: greeting }))
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={() => greet('hello')}>say hello</button>
|
Loading…
Reference in new issue