Skip to content

Commit

Permalink
add useImperativeMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher J Baker committed Nov 20, 2023
1 parent 6a0cb93 commit 8dfe6d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(

return ReactWebComponent
}

export function useImperativeMethods<Methods extends string>(
container: HTMLElement | ShadowRoot,
methods: Record<Methods, () => unknown>,
): void {
const element = "host" in container ? container.host : container

for (const method in methods) {
//@ts-ignore
element[method] = methods[method]
}
}
2 changes: 2 additions & 0 deletions packages/react-to-web-component/src/react-to-web-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { createRoot } from "react-dom/client"

import r2wcCore from "@r2wc/core"

export { useImperativeMethods } from "@r2wc/core"

interface Context<Props extends R2WCBaseProps> {
root: Root
ReactComponent: React.ComponentType<Props>
Expand Down

0 comments on commit 8dfe6d0

Please sign in to comment.