Skip to content

Commit 6c5257b

Browse files
tanhauhauConduitry
andauthored
allow passing in context in constructor (#6032)
Co-authored-by: Conduitry <[email protected]>
1 parent bcf2313 commit 6c5257b

File tree

8 files changed

+82
-5
lines changed

8 files changed

+82
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Svelte changelog
22

3+
## Unreleased
4+
5+
* Allow root-level context to be passed to the component constructor ([#6032](https://github.com/sveltejs/svelte/pull/6032))
6+
37
## 3.36.0
48

59
* Add `this: void` typing to store functions ([#6094](https://github.com/sveltejs/svelte/pull/6094))

site/content/docs/03-run-time.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ The following initialisation options can be provided:
904904
| `target` | **none** | An `HTMLElement` to render to. This option is required
905905
| `anchor` | `null` | A child of `target` to render the component immediately before
906906
| `props` | `{}` | An object of properties to supply to the component
907+
| `context` | `new Map()` | A `Map` of root-level context key-value pairs to supply to the component
907908
| `hydrate` | `false` | See below
908909
| `intro` | `false` | If `true`, will play transitions on initial render, rather than waiting for subsequent state changes
909910

@@ -1081,3 +1082,29 @@ const { head, html, css } = App.render({
10811082
answer: 42
10821083
});
10831084
```
1085+
1086+
---
1087+
1088+
The `.render()` method accepts the following parameters:
1089+
1090+
| parameter | default | description |
1091+
| --- | --- | --- |
1092+
| `props` | `{}` | An object of properties to supply to the component
1093+
| `options` | `{}` | An object of options
1094+
1095+
The `options` object takes in the following options:
1096+
1097+
| option | default | description |
1098+
| --- | --- | --- |
1099+
| `context` | `new Map()` | A `Map` of root-level context key-value pairs to supply to the component
1100+
1101+
```js
1102+
const { head, html, css } = App.render(
1103+
// props
1104+
{ answer: 42 },
1105+
// options
1106+
{
1107+
context: new Map([['context-key', 'context-value']])
1108+
}
1109+
);
1110+
```

src/runtime/internal/Component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function init(component, options, instance, create_fragment, not_equal, p
120120
on_disconnect: [],
121121
before_update: [],
122122
after_update: [],
123-
context: new Map(parent_component ? parent_component.$$.context : []),
123+
context: new Map(parent_component ? parent_component.$$.context : options.context || []),
124124

125125
// everything else
126126
callbacks: blank_object(),

src/runtime/internal/ssr.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ export function debug(file, line, column, values) {
7474
let on_destroy;
7575

7676
export function create_ssr_component(fn) {
77-
function $$render(result, props, bindings, slots) {
77+
function $$render(result, props, bindings, slots, context) {
7878
const parent_component = current_component;
7979

8080
const $$ = {
8181
on_destroy,
82-
context: new Map(parent_component ? parent_component.$$.context : []),
82+
context: new Map(parent_component ? parent_component.$$.context : context || []),
8383

8484
// these will be immediately discarded
8585
on_mount: [],
@@ -97,7 +97,7 @@ export function create_ssr_component(fn) {
9797
}
9898

9999
return {
100-
render: (props = {}, options = {}) => {
100+
render: (props = {}, { $$slots = {}, context = new Map() } = {}) => {
101101
on_destroy = [];
102102

103103
const result: {
@@ -109,7 +109,7 @@ export function create_ssr_component(fn) {
109109
}>;
110110
} = { title: '', head: '', css: new Set() };
111111

112-
const html = $$render(result, props, {}, options);
112+
const html = $$render(result, props, {}, $$slots, context);
113113

114114
run_all(on_destroy);
115115

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import { getContext } from 'svelte';
3+
4+
const value = getContext('key');
5+
const fn = getContext('fn');
6+
</script>
7+
8+
<div>{value}</div>
9+
<button on:click={() => fn('hello world')} />
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default {
2+
async test({ assert, target, window }) {
3+
const Component = require('./Component.svelte').default;
4+
5+
const called = [];
6+
const component = new Component({
7+
target,
8+
context: new Map([
9+
['key', 'svelte'],
10+
['fn', (value) => called.push(value)]
11+
])
12+
});
13+
assert.htmlEqual(target.innerHTML, '<div>svelte</div><button></button>');
14+
15+
const button = target.querySelector('button');
16+
await button.dispatchEvent(new window.MouseEvent('click'));
17+
18+
assert.deepEqual(called, ['hello world']);
19+
20+
component.$destroy();
21+
},
22+
test_ssr({ assert }) {
23+
const Component = require('./Component.svelte').default;
24+
25+
const called = [];
26+
const { html } = Component.render(undefined, { context: new Map([
27+
['key', 'svelte'],
28+
['fn', (value) => called.push(value)]
29+
]) });
30+
assert.htmlEqual(html, '<div>svelte</div><button></button>');
31+
}
32+
};

test/runtime/samples/constructor-pass-context/main.svelte

Whitespace-only changes.

test/server-side-rendering/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
shouldUpdateExpected,
1414
mkdirp
1515
} from '../helpers';
16+
import { set_current_component } from '../../internal';
1617

1718
function tryToReadFile(file) {
1819
try {
@@ -127,6 +128,8 @@ describe('ssr', () => {
127128
showOutput(dir, { generate: 'ssr', format: 'cjs' });
128129
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), dir)}/main.svelte`;
129130
throw err;
131+
} finally {
132+
set_current_component(null);
130133
}
131134
});
132135
});
@@ -223,6 +226,8 @@ describe('ssr', () => {
223226
showOutput(cwd, compileOptions);
224227
throw err;
225228
}
229+
} finally {
230+
set_current_component(null);
226231
}
227232
});
228233
});

0 commit comments

Comments
 (0)