Skip to content

Commit d8649ec

Browse files
committed
feat: Svelte Context between reactified Svelte components
1 parent b3aa424 commit d8649ec

16 files changed

+140
-45
lines changed

docs/caveats.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ React vDOM output:
6868

6969
```jsx
7070
<Bridge>
71-
<SvelteToReactContext.Provider>
71+
<SvelteFirstContext.Provider>
7272
<p>
7373
<Bridge>
74-
<SvelteToReactContext.Provider>
74+
<SvelteFirstContext.Provider>
7575
<Child /> <- When merging the render trees the <h1>Content</h1> is injected here
76-
</SvelteToReactContext.Provider>
76+
</SvelteFirstContext.Provider>
7777
</Bridge>
7878
</p>
79-
</SvelteToReactContext.Provider>
79+
</SvelteFirstContext.Provider>
8080
</Bridge>
8181
```
8282

docs/reactify.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ type Props = {
1616
const Dialog: React.FC<Props> = ({ onClose }) => (
1717
<div className="dialog">
1818
<h2>Thanks for subscribing!</h2>
19-
<svelte.Button type="primary" onClick={() => onClose()}>
19+
<svelte.Button type="primary" onclick={() => onClose()}>
2020
Close
2121
</svelte.Button>
2222
</div>
2323
);
2424
```
2525

26-
React only has props, we we assume that the props starting with "on" followed by a capital letter are event handlers.
26+
## When React starts the rendering, rendering the children is delayed
27+
28+
This is because we want to extract the context from the Svelte component and provide that to the Svelte child components
2729

2830
## Svelte components missing CSS?
2931

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/bfanger/svelte-preprocess-react.git"
1212
},
13-
"version": "2.0.6",
13+
"version": "2.1.0",
1414
"license": "MIT",
1515
"type": "module",
1616
"scripts": {

playwright/tests/react-first.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test.describe("react-first", () => {
99
await page.goto("/react-spa.html");
1010
await testDog(page);
1111
await testChildren(page);
12+
await testContext(page);
1213
});
1314

1415
test("Hydrate reactified Svelte component inside React server rendered page", async ({
@@ -17,6 +18,7 @@ test.describe("react-first", () => {
1718
await page.goto("/react-ssr?hydrate=1");
1819
await testDog(page);
1920
await testChildren(page);
21+
await testContext(page);
2022
});
2123
});
2224

@@ -46,3 +48,8 @@ async function testChildren(page: Page) {
4648
}),
4749
).toHaveScreenshot();
4850
}
51+
async function testContext(page: Page) {
52+
await expect(
53+
page.locator("pre", { hasText: '"Svelte context value"' }),
54+
).toHaveScreenshot();
55+
}
2.93 KB
Loading
2.93 KB
Loading

src/demo/components/DebugContext.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<script lang="ts">
22
import { getContext } from "svelte";
33
4-
export let id: any;
4+
type Props = {
5+
id: any;
6+
};
7+
let { id }: Props = $props();
58
69
const context = getContext(id);
710
const text = JSON.stringify(context);

src/lib/hooks.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function hooks<T>(
3535
});
3636

3737
onDestroy(() => {
38-
const index = parent.hooks.findIndex((h) => h === hook);
38+
const index = parent.hooks.findIndex((h) => h.key === hook.key);
3939
if (index !== -1) {
4040
parent.hooks.splice(index, 1);
4141
}

src/lib/internal/Bridge.svelte.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useRef, useState, useEffect, createElement } from "react";
22
import type { ReactDependencies, TreeNode } from "./types.js";
33
import Child from "./Child.js";
4-
import SvelteToReactContext from "./SvelteToReactContext.js";
4+
import SvelteFirstContext from "./SvelteFirstContext.js";
55
import portalTag from "svelte-preprocess-react/internal/portalTag.js";
66

77
type BridgeProps = {
@@ -71,7 +71,7 @@ function renderBridge(
7171
}
7272

7373
const vdom = createElement(
74-
SvelteToReactContext.Provider,
74+
SvelteFirstContext.Provider,
7575
{ value: node },
7676
children === undefined
7777
? createElement(node.reactComponent, props)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
import { getAllContexts } from "svelte";
3+
4+
type Props = {
5+
setContexts: (value: Map<any, any>) => void;
6+
};
7+
const { setContexts }: Props = $props();
8+
9+
setContexts(getAllContexts());
10+
</script>

0 commit comments

Comments
 (0)