Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Strict Mode Support #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ type PropNames<Props> = Array<PropName<Props>>

export interface R2WCOptions<Props> {
shadow?: "open" | "closed"
props?: PropNames<Props> | Partial<Record<PropName<Props>, R2WCType>>
props?: PropNames<Props> | Partial<Record<PropName<Props>, R2WCType>>,
strictMode?: boolean
}

export interface R2WCRenderer<Props, Context> {
mount: (
container: HTMLElement,
ReactComponent: React.ComponentType<Props>,
props: Props,
strictMode: boolean
) => Context
update: (context: Context, props: Props) => void
update: (context: Context, props: Props, strictMode: boolean) => void
unmount: (context: Context) => void
}

Expand Down Expand Up @@ -136,14 +138,17 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
[renderSymbol]() {
if (!this[connectedSymbol]) return

const strictMode = options.strictMode || false;

if (!this[contextSymbol]) {
this[contextSymbol] = renderer.mount(
this.container,
ReactComponent,
this[propsSymbol],
strictMode
)
} else {
renderer.update(this[contextSymbol], this[propsSymbol])
renderer.update(this[contextSymbol], this[propsSymbol], strictMode)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ describe("react-to-web-component 1", () => {
expect(div?.textContent).toBe("hello, Bavin")
})

it("works with props array in strict mode", async () => {
function TestComponent({ name }: { name: string }) {
return <div>hello, {name}</div>
}

const TestElement = r2wc(TestComponent, { props: ["name"], strictMode: true })

customElements.define("test-hello-strict", TestElement)

const body = document.body
body.innerHTML = "<test-hello-strict name='Bavin'></test-hello>"

await flushPromises()

const div = body.querySelector("div")
expect(div?.textContent).toBe("hello, Bavin")
})

it("works with proptypes", async () => {
function WithProptypes({ name }: { name: string }) {
return <div>hello, {name}</div>
Expand Down
12 changes: 10 additions & 2 deletions packages/react-to-web-component/src/react-to-web-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ function mount<Props extends object>(
container: HTMLElement,
ReactComponent: React.ComponentType<Props>,
props: Props,
strictMode: boolean
): Context<Props> {
const root = createRoot(container)

const element = React.createElement(ReactComponent, props)
let element: React.ReactElement = React.createElement(ReactComponent, props)
if (strictMode) {
element = React.createElement(React.StrictMode, null, element)
}
root.render(element)

return {
Expand All @@ -30,8 +34,12 @@ function mount<Props extends object>(
function update<Props extends object>(
{ root, ReactComponent }: Context<Props>,
props: Props,
strictMode: boolean
): void {
const element = React.createElement(ReactComponent, props)
let element: React.ReactElement = React.createElement(ReactComponent, props)
if (strictMode) {
element = React.createElement(React.StrictMode, null, element)
}
root.render(element)
}

Expand Down