question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering Blazor Custom Element from Leaflet

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In my app, I’m combining the JS mapping framework Leaflet and Blazor Server. I have a Blazor component LifePointDetail.razor which is embedded into a Leaflet popup as a Blazor Custom Element:

let marker = L.marker([latitude, longitude]);
marker.bindPopup("<life-point-detail></life-point-detail>");
marker.addTo(leafletMap);

This works fine so far, but I’m stumbling across an issue which I don’t know how to solve: Leaflet popups have the ability to automatically pan the map when opening a popup (see docu). But in conjunction with my Blazor Custom Element, this doesn’t work. Please see the following screenshot to better understand the problem:
image

Expected Behavior

Instead, it should look like this after clicking the marker and opening the popup:
image

Please note that in a vanilla Leaflet application, you don’t have to pan manually: the popup automatically gets centered in the middle so that it is completely visible.

Steps To Reproduce

  1. Create a Blazor app.
  2. Create a very basic Blazor component MyBlazorComponent and make it a Blazor Custom Element.
  3. Add this Blazor component to a Leaflet popup:
let marker = L.marker([latitude, longitude]);
marker.bindPopup("<my-blazor-component></my-blazor-component>");
marker.addTo(leafletMap);

Exceptions (if any)

No response

.NET Version

6.0.202

Anything else?

The guys on the Leaflet side don’t consider this a bug on their side (see related issue). So I’m in a typical sandwich position 🤷🏻‍♂️

I assume it could be related to some sort of timing: when Leaflet renders marker.bindPopup(), the Blazor Custom Element <my-blazor-component> is not yet ready rendered. Therefore the components extent (width and height) are not yet known/accessible for Leaflet - maybe width and height are 0 at a certain point in time when Leaflet tries to render them?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveSandersonMScommented, May 10, 2022

Looks like this would be handled by calling Leaflet’s update method after the DOM content is rendered (e.g., in Blazor’s OnAfterRenderAsync) as suggested at https://github.com/Leaflet/Leaflet/issues/8217#issuecomment-1121436852.

0reactions
mu88commented, May 28, 2022

I could solve it 💪🏻 instead of calling marker.getPopup().update(), I had to do the following:

let popup = marker.getPopup();
popup._updateLayout();
popup._updatePosition();
popup._adjustPan();

This is exactly the same code as Leaflet’s original, except that popup._updateContent(); isn’t called. Doing so triggers an infinite loop between Leaflet trying to render the popup, Blazor creating its component and afterwards updating the popup, Leaflet trying to render the popup again, etc. This infinite loop will be interrupted sometime by the browser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor - Keep the rendering after routing
I have a Blazor ( web-assembly ) page that has a map( Leaflet ) which is heavily loaded items on it. When user...
Read more >
Using .NET 7's Blazor Custom Elements to render dynamic ...
I'm currently working a new version of the “courseware” that powers https://practicaldotnet.io. As part of that I want a way to write course ......
Read more >
Leaflet configuration and usage
This page explains how to customize Docking Layout with custom content.
Read more >
Slippy Maps in Blazor with Leaflet - Bernard Darnton
Add interactive slippy maps to your Blazor app with Leaflet.js. Learn how to use IJSObjectReference to hold it all together.
Read more >
Blazor: How to Create a Leaflet.js Map and Call ...
All the nugets for Blazor, maps and Leaflet.js, even paid ones, are in half-trash condition when it comes to full-blown dealing with maps....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found