Skip to content

Commit 548eb50

Browse files
author
Bob Fanger
committed
refactor: Easer access to utlitites
- Removed default export: update to `import preprocessReact to "svelte-preprocess/preprocessReact"` - Direct export utilities - Documented utlitites - Removed useWritable and rename useReadable to useStore
1 parent 3662bba commit 548eb50

File tree

14 files changed

+141
-71
lines changed

14 files changed

+141
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Visit https://github.com/bfanger/svelte-preprocess-react/releases for the release notes.

Readme.md renamed to README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ npm install svelte-preprocess-react
6262

6363
```js
6464
// svelte.config.js
65-
import preprocessReact from "svelte-preprocess-react";
65+
import preprocessReact from "svelte-preprocess-react/preprocessReact";
6666

6767
export default {
6868
preprocess: preprocessReact(),
@@ -74,7 +74,7 @@ When using other processors like [svelte-preprocess]() use:
7474
```js
7575
// svelte.config.js
7676
import preprocess from "svelte-preprocess";
77-
import preprocessReact from "svelte-preprocess-react";
77+
import preprocessReact from "svelte-preprocess-react/preprocessReact";
7878

7979
export default {
8080
preprocess: preprocessReact({
@@ -93,7 +93,7 @@ The preprocessor that is passed as an option is applied before running the prepr
9393
Once you've converted a React component to Svelte, you'd want delete that React component, but some if other React components depended on that component you can use `reactify` to use the new Svelte component as a React component.
9494

9595
```jsx
96-
import reactify from "$lib/reactify";
96+
import { reactify } from "resvelte-preprocess-react";
9797
import ButtonSvelte from "../components/Button.svelte";
9898

9999
const Button = reactify(ButtonSvelte);

docs/architecture.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Architecture
2+
3+
This document describes design desicions and implementation details of the preprocessor.
4+
15
## Context
26

37
```jsx
@@ -10,7 +14,7 @@
1014
</App>
1115
```
1216

13-
Both svelte and react have component trees, for context to work in both Svelte needs to act as if the tree is:
17+
Both Svelte and React have component trees, for context to work in both, Svelte needs to act as if the tree is:
1418

1519
```jsx
1620
<App>
@@ -28,7 +32,7 @@ and React needs to act as if the tree is:
2832

2933
### Client mode
3034

31-
sveltifyReact create a single React Root and based on the Hierachy of the ReactWrapper components we/re abel to built the react tree:
35+
sveltifyReact creates a single React Root and based on the Hierachy of the ReactWrapper components we're able to built the React tree:
3236

3337
```jsx
3438
<Bridge>
@@ -42,6 +46,9 @@ sveltifyReact create a single React Root and based on the Hierachy of the ReactW
4246

4347
The `<Bridge>`s use React Portals to render the components into the DOM of the ReactWrapper Svelte component.
4448

49+
This is why the childeren prop passed to your React is an array, even when you manually pass a children prop.
50+
This array allows svelte-preprocess-react to inject the slotted content into the correct place in the React tree.
51+
4552
### Server mode
4653

4754
Based off on how the Svelte component is compiled we can detect SSR and utitilize the renderToString method th generate the html. (limited to leaf nodes a.t.m.)

docs/utilities.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Utilities
2+
3+
svelte-preprocess-react comes with a utilities to make using Svelte primitives inside React components easier.
4+
And to make using React primitives inside Svelte components easier.
5+
6+
## reactify
7+
8+
Convert a Svelte component into an React component.
9+
10+
### Usage:
11+
12+
```ts
13+
import ButtonSvelte from "$lib/components/Button.svelte";
14+
const Button = reactify(ButtonSvelte);
15+
16+
type Props = {
17+
onClose: () => void;
18+
};
19+
const Dialog: React.FC<Props> = ({ onClose }) => (
20+
<div className="dialog">
21+
<h2>Thanks for subscribing!</h2>
22+
<Button type="primary" onClick={() => onClose()}>
23+
Close
24+
</Button>
25+
</div>
26+
);
27+
```
28+
29+
React only has props, we we asume that the props starting with "on" followed by a capital letter are event handlers.
30+
31+
## useStore
32+
33+
useStore is a React hook that allows using a Svelte Store in a React component.
34+
35+
### Usage:
36+
37+
```ts
38+
import { useStore } from "svelte-preprocess-react";
39+
const userStore = writable({ name: "John Doe" });
40+
41+
const UserGreet: React.FC = () => {
42+
const $user = useStore(userStore);
43+
return <h1>Hello, {$user.name}</h1>;
44+
};
45+
export default UserGreet;
46+
```
47+
48+
When the Svelte Store is updated the component will rerender and receive the new value from the useStore hook.
49+
50+
### Writable stores
51+
52+
Inside a Svelte component `$user = { name:'Jane Doe' }` or `$user.name = 'Jane Doe'` will trigger an update.
53+
In React and other regular javascript files this does _not_ work.
54+
To update the value and trigger an update use the `set` or `update` methods:
55+
56+
```ts
57+
// Instead of `$user = { name:'Jane Doe' }`
58+
user.set({ name: "Jane Doe" });
59+
60+
// Instead of `$user.name = 'Jane Doe'`
61+
user.update((user) => ({ ...user, name: "Jane Doe" }));
62+
```
63+
64+
See https://svelte.dev/docs#run-time-svelte-store for more information.

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": "0.9.0",
13+
"version": "0.10.0",
1414
"license": "MIT",
1515
"type": "module",
1616
"scripts": {

src/lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import preprocessReact from "./preprocess.js";
2-
3-
export default preprocessReact;
1+
export { default as useStore } from "./useStore.js";
2+
export { default as reactify } from "./reactify.js";
3+
export { default as sveltify } from "./sveltify.js";

src/lib/internal/Bridge.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type React from "react";
22
import { createElement } from "react";
3-
import useReadable from "../useReadable.js";
3+
import useStore from "../useStore.js";
44
import SvelteToReactContext from "./SvelteToReactContext.js";
55
import Child from "./Child.js";
66
import type { TreeNode } from "./types";
@@ -14,9 +14,9 @@ export type BridgeProps = {
1414
node: TreeNode;
1515
};
1616
const Bridge: React.FC<BridgeProps> = ({ createPortal, node }) => {
17-
const target = useReadable(node.target);
18-
let props = useReadable(node.props);
19-
const slot = useReadable(node.slot);
17+
const target = useStore(node.target);
18+
let props = useStore(node.props);
19+
const slot = useStore(node.slot);
2020

2121
if (!target) {
2222
return null;
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { useEffect as useLayoutEffect, useState } from "react";
1+
import { useEffect, useState } from "react";
22
import { get, type Readable } from "svelte/store";
33

4-
export default function useReadable<T>(store: Readable<T>): T {
4+
/**
5+
* Hook for using Svelte stores in React.
6+
*
7+
* Usage:
8+
*
9+
* const User: React.FC = () => {
10+
* const $user = useStore(userStore);
11+
* return <h1>Hello, {$user.name}</h1>;
12+
* }
13+
*/
14+
export default function useStore<T>(store: Readable<T>): T {
515
const [value, setValue] = useState(() => get(store));
6-
useLayoutEffect(() => {
16+
useEffect(() => {
717
let first = true;
818
const cancel = store.subscribe((next) => {
919
if (first) {

src/lib/useWritable.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)