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

Custom root component #21

Open
adenta opened this issue Dec 17, 2024 · 1 comment
Open

Custom root component #21

adenta opened this issue Dec 17, 2024 · 1 comment

Comments

@adenta
Copy link

adenta commented Dec 17, 2024

If I am mounting a react component from turbo, is it possible to customize the “root” wrapper?

for example, I’m using chakra ui component library. To use chakra with turbo mount, I would need to wrap every component in the ChakraProvider. It would be nice to not need to do that.

@skryukov
Copy link
Owner

Hey @adenta, thanks for the request!

I believe we can use a custom plugin for this:

// Add this file as, for example, utils/chakraPlugin.ts
// Then replace `import { registerComponent } from "turbo-mount/react";`
// with `import { registerComponent } from "/utils/chakraPlugin";`

import { buildRegisterFunction, Plugin, TurboMount } from "turbo-mount";
import { ComponentType, createElement } from "react";
import { createRoot } from "react-dom/client";
import { ChakraProvider } from "@chakra-ui/react";

const plugin: Plugin<ComponentType> = {
  mountComponent: (mountProps) => {
    const { el, Component, props } = mountProps;
    const root = createRoot(el);

    root.render(
      createElement(ChakraProvider, null, createElement(Component, props))
    );

    return () => {
      root.unmount();
    };
  },
};

const registerComponent = buildRegisterFunction(plugin);

export { TurboMount, registerComponent };

export default plugin;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants