Michael Stearn
Michael Stearn

Reputation: 623

How to replace React-Leaflet Popup with custom component?

I'm using React-Leaflet and (callemall)Material-UI in a project. I'm attempting to render the Material-UI Card component within the <Popup></Popup> component of React-Leaflet. I've tried pulling it into the Popup as a component but the popup doesn't let the component work as it should. Specifically, the card component has a button element that expands it but unfortunately, the popup won't let me click it. I'm sure there's some CSS-y thing that I need to override but my thought is that an easier option would be to just replace the popup component with my own component but I'm not sure how to go about doing so. Any insight is much appreciated :)

My code looks like this:

<Marker position={position}>
    <Popup>
        <MapPopup />
    </Popup>
</Marker>

And the imported component looks like: (I've removed styles and unimportant details to make it simple to read)

<Card>
    <CardHeader />
        <CardMedia
            expandable={true}
            overlay={
                <CardText expandable={true}>
                    <div>
                        <p>Text</p>
                        <Chip>text</Chip>
                    </div>
                    <div>
                        <p>Text</p>
                        <Chip>Text</Chip>
                    </div>
                </CardText>
            }>
        <img src="cardMediaImageURL" />
    </CardMedia>
</Card>

Upvotes: 4

Views: 3940

Answers (1)

Michael Stearn
Michael Stearn

Reputation: 623

Long story short, React-Leaflet and Leaflet renders static html for the popup so it is not possible to render dynamic content within it. Rendering a material-ui component is possible, however it won't be interactive. You can accomplish this by wrapping your component with <MuiThemeProvider></MuiThemeProvider>. You just have to make sure that you have the content set to be the complete view and not with anything like an expander.

Solution is as follows:

<Popup>
    <MuiThemeProvider muiTheme={getMuiTheme(yourThemeName)}>
        <YourCustomComponent />
    </MuiThemeProvider>
</Popup>

Upvotes: 5

Related Questions