Skip to content

Commit 86ab5ba

Browse files
author
Christopher J Baker
committed
fix build
1 parent 976880b commit 86ab5ba

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

packages/core/vite.config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ export default defineConfig({
99
entry: "src/core.ts",
1010
formats: ["es", "cjs"],
1111
},
12-
rollupOptions: {
13-
external: [/node_modules/],
14-
},
1512
},
1613
plugins: [dts()],
1714
test: {

packages/legacy/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineConfig({
1010
formats: ["es", "cjs"],
1111
},
1212
rollupOptions: {
13-
external: [/node_modules/],
13+
external: ["@r2wc/core"],
1414
},
1515
},
1616
plugins: [dts()],

packages/react-to-web-component/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineConfig({
1010
formats: ["es", "cjs"],
1111
},
1212
rollupOptions: {
13-
external: [/node_modules/],
13+
external: ["react", "react-dom", "@r2wc/core"],
1414
},
1515
},
1616
plugins: [dts()],

src/root/react-to-web-component.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
11
import type { R2WCOptions } from "@r2wc/core"
22

3+
import React from "react"
34
import { createRoot } from "react-dom/client"
45

56
import r2wcCore from "@r2wc/core"
67

7-
function mount(container: HTMLElement, element: JSX.Element) {
8-
const root = createRoot(container)
8+
interface Context<Props extends object> {
9+
container: HTMLElement
10+
ReactComponent: React.ComponentType<Props>
11+
}
12+
13+
function mount<Props extends object>(
14+
container: HTMLElement,
15+
ReactComponent: React.ComponentType<Props>,
16+
props: Props,
17+
): Context<Props> {
18+
const element = React.createElement(ReactComponent, props)
19+
20+
ReactDOM.render(element, container)
21+
22+
return {
23+
container,
24+
ReactComponent,
25+
}
26+
}
27+
28+
function update<Props extends object>(
29+
{ container, ReactComponent }: Context<Props>,
30+
props: Props,
31+
): void {
32+
const element = React.createElement(ReactComponent, props)
933

10-
root.render(element)
34+
ReactDOM.render(element, container)
1135
}
1236

13-
function unmount(container: HTMLElement) {
14-
// root.unmount()
37+
function unmount<Props extends object>({ container }: Context<Props>): void {
38+
ReactDOM.unmountComponentAtNode(container)
1539
}
1640

17-
export default function r2wc(
18-
ReactComponent: React.FC<any> | React.ComponentClass<any>,
19-
config: R2WCOptions = {},
41+
export default function r2wc<Props extends object>(
42+
ReactComponent: React.ComponentType<Props>,
43+
options: R2WCOptions<Props> = {},
2044
): CustomElementConstructor {
21-
return r2wcCore(ReactComponent, config, { mount, unmount })
45+
return r2wcCore(ReactComponent, options, { mount, update, unmount })
2246
}

0 commit comments

Comments
 (0)