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

Issue while wrapping React components #1182

Closed
pritam-dey3 opened this issue Jun 10, 2023 · 1 comment
Closed

Issue while wrapping React components #1182

pritam-dey3 opened this issue Jun 10, 2023 · 1 comment
Labels
bug Something isn't working enhancement Anything you want improved needs investigation

Comments

@pritam-dey3
Copy link

Describe the bug
I am trying to use Mantine component in my application using the following code.

class Collapse(pc.Component):
    library = "@mantine/core"
    tag = "Collapse"

    # variables
    in_: pc.Var[bool]  # type: ignore
    animate_opacity: pc.Var[bool]  # type: ignore
    transition_duration: pc.Var[int]  # type: ignore

    def get_triggers(self) -> Set[str]:
        """Get the event triggers for the component.

        Returns:
            The event triggers.
        """
        return super().get_triggers() | set({"on_transition_end"})

But I am facing this error
image

I suspect this is due to the fact that Mantine wants me to wrap my App with MantineProvider like this
image

But the generated _app.js has only ChakraProvider wrapped around it
image

Note that, Collapse was working for me for some earlier versions (I did not document the exact versions of either pynecone or Mantine).

To Reproduce
Try to wrap Collapse Mantine component.

Expected behavior
Users should be able to wrap any react component from any library

Specifics (please complete the following information):

  • Python Version: 3.11
  • Pynecone Version: 0.1.33
  • OS: Windows WSL
  • Browser (Optional): Chrome
@Lendemor Lendemor added bug Something isn't working enhancement Anything you want improved needs investigation labels Jun 10, 2023
@masenf
Copy link
Collaborator

masenf commented Nov 27, 2023

This use case can actually be handled now, via the use of _get_app_wrap_components API.

import reflex as rx
from reflex.components.component import Component


class MantineProvider(rx.Component):
    library = "@mantine/core"
    tag = "MantineProvider"


mantine_provider = MantineProvider.create()
mantine_provider.special_props.update({rx.Var.create("withGlobalStyles"), rx.Var.create("withNormalizeCSS")})


class MantineComponent(rx.Component):
    library = "@mantine/core"

    @staticmethod
    def _get_app_wrap_components() -> dict[tuple[int, str], Component]:
        return {
            (10, "MantineProvider"): mantine_provider,
        }


class Collapse(MantineComponent):
    tag = "Collapse"

    # variables
    in_: rx.Var[bool]  # type: ignore
    animate_opacity: rx.Var[bool]  # type: ignore
    transition_duration: rx.Var[int]  # type: ignore

    def get_event_triggers(self):
        """Get the event triggers for the component.

        Returns:
            The event triggers.
        """
        return super().get_event_triggers() | {
            "on_transition_end": lambda: [],
        }


class Button(MantineComponent):
    tag = "Button"


class State(rx.State):
    is_opened: bool = False


def index():
    return rx.vstack(
        Button.create("Toggle", on_click=lambda: State.set_is_opened(~State.is_opened)),
        Collapse.create("foo", in_=State.is_opened),
    )


# Add state and page to the app.
app = rx.App()
app.add_page(index)
app.compile()

@masenf masenf closed this as completed Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement Anything you want improved needs investigation
Projects
None yet
Development

No branches or pull requests

3 participants