-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Hash routing to named element #8393
Comments
see also #6175 |
This also happens when using the basic functionality of a tabbed panel.
Clicking any of the panels will try to navigate you to a page instead of the named tab element. |
I want to note that this is still an issue with Preview 8 (only testet in Server Side Blazor). Consider the following example
Clicking either of the two links takes the browser to the url |
Here is a temporary workaround that seems to work for internal clicks, but not deep linking
|
@SQL-MisterMagoo the problem with that workaround is that it also stops any other I do not know what the implications are, but it looks like a fix for this bug could be adding a check around this line in NavigationManager.ts#L47, that checks if the anchor's href starts with a |
@egil Yes, it does, but you can easily use @onMouseUp or another event if you need Blazor code to fire as well as the internal browser scroll. See this example https://blazorfiddle.com/s/jtufkw80 this is a workaround - of course the real fix is needed. |
I haven't tested this but I believe it should work: |
This is still a problem in the release version |
I was just reading the source and realised if you simply include This line of code in NavigationManager returns directly out of the click handler before preventDefault can be called - if there is a Example:
|
I just tried that in my project and it's still redirecting to the root url, not scrolling to the element. |
Can you share the link? |
It's And the element it refers to on that page is
It's as straight forward as it can be |
@TheGrandUser is this server side Blazor or web assembly? |
This is server side, using .net core 3.1 preview 3 in the VS preview |
@TheGrandUser if the page is not the root of your site you will need to include the page route in the anchor href because Blazor uses root relative link resolution. Have you tried that? |
Ok, that works |
Updated work-around:
|
This worked for me...thank you so much |
Be careful with this.... if you use a component that uses anchors with this approach, and that component is used in different routes, then you'll have a problem since the URL is not static anymore. |
Any news regarding hash routing? I have many existing (from my previous project) html files containing anchor which I want to use in blazor ServerApp but as this doesn't work... have to stop my project... |
How has this still not been addressed? |
It's not just the scrolling to the html element, handling of the routing, but also synchronisation with the navigation. |
@MarcelDevG I take the point but every JS framework has figured this out. That's who Blazor is competing with. |
Why do I have to pull in (yet another JS framework) to fix this... |
I'm surprised the team still didn't address this... |
The proposed workaround has a shortcoming that can be hopefully be addressed--when the brower's DOM is mutating due to renders, the position of the target element may be stale when the JS is invoked to scroll. The thinking is to somehow wait for everything in the browser's DOM to be "stable", and then scroll to the element. (How do you define what is "stable" per the brower's DOM?) You can try to design their pages to know the size of the content in advance so that the height/width is constant, but this isn't very realistic. We ended up just adding an arbitrary delay before scrolling to ensure the DOM is "stable" enough. But even then, if a end-user has a very slow connection (blazor server)/slow device, the JS can still scroll before the DOM is "stable". |
It would be great if this works out of the box for the |
Describe the bug
I cannot use an html anchor to navigate to a specific html element of a page.
To Reproduce
Steps to reproduce the behavior:
Given there is a Blazor page that looks like the following:
When I click the link to Bar, i get redirected to the route
http://localhost:5000/foo#bar
, but will be at the top of the page. The fragment of the route is not used for selection of a specific HTML element.Expected behavior
The browser should scroll down to the proper element, as specified by the Element Selector.
In cases where the fragment of the URL is used for other purposes, ie. for OAuth, it should be possible to overwrite the default Blazor router behavior. The behavior should be set on a per-page and per-app level.
Workaround
The proposed workaround does not work, since the browser crashes and is only for illustration.
The current workaround is to write explicit code that interprets the URL. In the example above, we could use a little bit of JavaScript and then call that from our Blazor code:
Additional context
blazor 0.8.0-preview-19104-04
The text was updated successfully, but these errors were encountered: