Skip to content

Commit

Permalink
🐛 fix(Button): ignore the exceptions thrown druing pre-rendering (#2116)
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Aug 26, 2024
1 parent 8c68596 commit 7a39443
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
8 changes: 2 additions & 6 deletions src/Masa.Blazor/Extensions/ElementReferenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ namespace Masa.Blazor.Extensions;

public static class ElementReferenceExtensions
{
public static string? GetSelector(this ElementReference? elementReference)
{
return elementReference?.GetSelector();
}

public static string GetSelector(this ElementReference elementReference)
{
if (elementReference.TryGetSelector(out var selector))
{
return selector;
}

throw new InvalidOperationException($"ElementReference {elementReference.Id} does not have a selector.");
throw new InvalidOperationException(
"ElementReference has not been configured correctly. This issue may occur during pre-rendering; it can be ignored, or you can disable pre-rendering.");
}

public static bool TryGetSelector(this ElementReference elementReference, [NotNullWhen(true)] out string? selector)
Expand Down
47 changes: 30 additions & 17 deletions src/Masa.Blazor/Extensions/JsRuntimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ public static async Task AddHtmlElementEventListener(this IJSRuntime jsRuntime,
Func<Task> callback,
OneOf<EventListenerOptions, bool> options, EventListenerExtras? extras = null)
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
el.GetSelector(),
type,
DotNetObjectReference.Create(new Invoker(callback)),
options.Value,
extras
);
if (el.TryGetSelector(out var selector))
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
selector,
type,
DotNetObjectReference.Create(new Invoker(callback)),
options.Value,
extras
);
}
}

public static async Task AddHtmlElementEventListener<TEventArgs>(this IJSRuntime jsRuntime, string selector, string type,
Expand All @@ -57,17 +60,21 @@ await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
);
}

public static async Task AddHtmlElementEventListener<TEventArgs>(this IJSRuntime jsRuntime, ElementReference el, string type,
public static async Task AddHtmlElementEventListener<TEventArgs>(this IJSRuntime jsRuntime, ElementReference el,
string type,
Func<TEventArgs, Task> callback,
OneOf<EventListenerOptions, bool> options, EventListenerExtras? extras = null)
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
el.GetSelector(),
type,
DotNetObjectReference.Create(new Invoker<TEventArgs>(callback)),
options.Value,
extras
);
if (el.TryGetSelector(out var selector))
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
selector,
type,
DotNetObjectReference.Create(new Invoker<TEventArgs>(callback)),
options.Value,
extras
);
}
}

public static async Task AddClickEventListener(this IJSRuntime jsRuntime, ElementReference elementReference, Func<MouseEventArgs, Task> callback, bool stopPropagation, bool preventDefault)
Expand All @@ -84,12 +91,18 @@ public static async Task RemoveHtmlElementEventListener(this IJSRuntime jsRuntim
public static async Task RemoveHtmlElementEventListener(this IJSRuntime jsRuntime, ElementReference el, string type,
string? key = null)
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.RemoveHtmlElementEventListener, el.GetSelector(), type, key);
if (el.TryGetSelector(out var selector))
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.RemoveHtmlElementEventListener, selector, type, key);
}
}

public static async Task RemoveClickEventListener(this IJSRuntime jSRuntime, ElementReference el, string? key = null)
{
await jSRuntime.RemoveHtmlElementEventListener(el.GetSelector(), "click", key);
if (el.TryGetSelector(out var selector))
{
await jSRuntime.RemoveHtmlElementEventListener(selector, "click", key);
}
}

public static async Task ScrollTo(this IJSRuntime jsRuntime, string selector, double? top, double? left = null,
Expand Down

0 comments on commit 7a39443

Please sign in to comment.