feat: allow use of createContext when instantiating components programmatically

always-create-root-context
Rich Harris 1 week ago
parent 16fec7207a
commit 4c8f151af8

@ -0,0 +1,5 @@
---
'svelte': minor
---
feat: allow use of createContext when instantiating components programmatically

@ -208,11 +208,9 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
pending: () => {}
},
(anchor_node) => {
if (context) {
push({});
var ctx = /** @type {ComponentContext} */ (component_context);
ctx.c = context;
}
push({});
var ctx = /** @type {ComponentContext} */ (component_context);
if (context) ctx.c = context;
if (events) {
// We can't spread the object or else we'd lose the state proxy stuff, if it is one
@ -241,9 +239,7 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
}
}
if (context) {
pop();
}
pop();
}
);

@ -611,18 +611,14 @@ export class Renderer {
renderer.push(BLOCK_OPEN);
if (options.context) {
push();
/** @type {SSRContext} */ (ssr_context).c = options.context;
/** @type {SSRContext} */ (ssr_context).r = renderer;
}
push();
if (options.context) /** @type {SSRContext} */ (ssr_context).c = options.context;
/** @type {SSRContext} */ (ssr_context).r = renderer;
// @ts-expect-error
component(renderer, options.props ?? {});
if (options.context) {
pop();
}
pop();
renderer.push(BLOCK_CLOSE);

@ -0,0 +1,7 @@
<script>
import { get } from './main.svelte';
const message = get();
</script>
<h1>{message}</h1>

@ -0,0 +1,8 @@
import { test } from '../../test';
export default test({
ssrHtml: `<div></div>`,
html: `<div><h1>hello</h1></div>`,
test() {}
});

@ -0,0 +1,20 @@
<script module>
import { createContext, mount } from 'svelte';
import Child from './Child.svelte';
/** @type {ReturnType<typeof createContext<string>>} */
const [get, set] = createContext();
export { get };
function Wrapper(Component) {
return (...args) => {
set('hello');
return Component(...args);
};
}
</script>
<div {@attach (target) => {
mount(Wrapper(Child), { target });
}}></div>
Loading…
Cancel
Save