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

[Blazor] Enable custom text in the new (.Net 9.0) reconnect dialog #57453

Open
1 task done
dgoldm opened this issue Aug 22, 2024 · 9 comments
Open
1 task done

[Blazor] Enable custom text in the new (.Net 9.0) reconnect dialog #57453

dgoldm opened this issue Aug 22, 2024 · 9 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@dgoldm
Copy link

dgoldm commented Aug 22, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

The new reconnect dialog, displayed by Blazor when the browser loses connection with the server is a wonderful improvement, and I'm tempted to upgrade my project, which is used in production, to .Net 9.0 preview for this reason alone.
The problem is that I need to display the text in languages other than English.
As far as I can see the English text is hard-coded, e.g.:

this.status.innerHTML = 'Rejoining the server...';

Describe the solution you'd like

Could you provide a way to customize the text?

Additional context

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Aug 22, 2024
@javiercn
Copy link
Member

javiercn commented Aug 22, 2024

@dgoldm thanks for contacting us.

We don't offer customization of the built-in UI bits other than you providing your own UI.

With that said, you should be able to detect when the element is made visible on to the page using a MutationObserver and change the contents programmatically by navigating to the element.

https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

@javiercn javiercn added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Aug 22, 2024
@dgoldm
Copy link
Author

dgoldm commented Aug 22, 2024

Thanks @javiercn.
Judging by the documentation for customization of the old reconnect dialog, (which BTW is still the same in the 9.0 docs -
https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/signalr?view=aspnetcore-9.0#reflect-the-server-side-connection-state-in-the-ui), which gives a straightforward, scriptless method to replace the built in UI with a functionally equivalent custom one, I was expecting a similar option for the new dialog.
I expect developers to be disappointed when they follow the docs only to discover that their custom UI looks so miserable compared to the built-in one.

Understanding that it now requires javascript, it would be great if MS could provide information on how to create my own UI and hook into the circuit events,
Or if indeed the only method is via MutationObserver, have the docs list the relevant element ids and classes, and provide some sample code.

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Aug 22, 2024
@javiercn
Copy link
Member

javiercn commented Aug 22, 2024

@dgoldm You can still use the option that you mention, but you won't get the styles, etc. of the new reconnect dialog.

The built-in reconnect UI has never been extensible by other means than you providing your own markup, at which point we simply set some classes and some text on the markup you provide.

In the future we likely want to move this UI into the template itself, there's no need for this UI to be hardcoded inside blazor and only creates problems like this.

If you want to replicate the UI within your app, you can grab the styles and HTML from our default one from here:

@javiercn javiercn removed the Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. label Aug 22, 2024
@javiercn javiercn added this to the .NET 10 Planning milestone Aug 22, 2024
@dgoldm
Copy link
Author

dgoldm commented Aug 23, 2024

@javiercn I'm not interested in the styles, only the functionality, namely to display different messages corresponding to the current state of the reconnection mechanism. But for that I would have to be able to implement the various callbacks of ReconnectDisplay. That doesn't seem to be in the scope of the snippets you marked.

Anyway in the meantime I've managed to implement the MutationObserver workaround that you suggested.
I'm putting it here, in case anyone comes here with similar needs, or has any suggestion for improvement (I'm no expert at javascript, Blazor has relieved me from pursuing that path 😄)

Add this to App.razor:

<script>
	const rejoiningText = "{your custom text}";
	const retryingText = "{your custom text}";
	const rejoinFailedText = "{your custom text}";
	const buttonCaption = "{your custom text}";

	const observer = new MutationObserver(() => {
		let dialog = document.querySelector('.components-reconnect-dialog');
		if (dialog) {
			let paragraph = dialog.querySelector('p');
			let text = paragraph.innerText;
			if (text.startsWith('Rejoining')) {
				paragraph.innerHTML = rejoiningText;
			} else if (text.startsWith('Rejoin failed')) {
				paragraph.innerHTML = text.replace(/.+ (\d+) seconds?/, retryingText);
			} else if (text.startsWith('Failed to rejoin')) {
                                paragraph.innerHTML = rejoinFailedText;
			}

			let button = dialog.querySelector('button');
			if (button) {
				button.textContent = buttonCaption;
			}
		};
	});

	observer.observe(document.body, { childList: true, subtree: true });
</script>

@pthivierge-sayona
Copy link

pthivierge-sayona commented Nov 13, 2024

Hi there, not sure this goes here, however I had an issue with text color. I suspect my theming (using mudblazor) was is inverting color of the paragraph style globally. So perhaps the new .net9 reconnect dialog should force black color in it's default style?

#components-reconnect-modal {
color: black !important;
}

For me, that made the text visible again when user's chose dark mode,

@raphadesa
Copy link

Hello @dgoldm I've tried your solution in a new Server app with .NET 9 but it doesn't work, the UI doesn't update...

Maybe @javiercn can help us ?

Thanks !

@dgoldm
Copy link
Author

dgoldm commented Nov 14, 2024

Hi @raphadesa,
Thanks for pointing this out.
Although It did work on the 9.0 preview version that was out at that time, now on 9.0.100 indeed it's not working.
I'll take a look.
Anyway, please upvote my OP. Maybe with some more upvotes we could get the feature request's priority boosted.

@boukenka
Copy link

boukenka commented Nov 18, 2024

Hi @dgoldm
I have tried as well. With no success, for the text color. 😿
I am using the MudBlazor library.

#components-reconnect-modal p {
    color: black !important;
}

@boukenka
Copy link

Based on your solution @dgoldm and with some modifications, I was finally able to display a black color. Here's my code if it helps anyone.

const observer = new MutationObserver(() => {
    let dialog = document.getElementById('components-reconnect-modal');
    if (dialog) {
        var shadowRoot = dialog.shadowRoot;
        if (shadowRoot) {
            var paragraph = shadowRoot.querySelector('p');
            if (paragraph) {
                paragraph.style.color = 'black';
                paragraph.innerHTML = '{your custom text}';
            }
        }
    };
});

observer.observe(document.body, { childList: true, subtree: true });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

5 participants