dotnet / dotnet/aspnetcore

Expose internal JSRuntime in ElementReference for static extensions

Open
#47,197 5 comments 4 reactions 0 assignees View on GitHub
area-blazor enhancement
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

What happened is that I wanted to get a reference to my input textbox and wanted to select all the text on focus or click via a JS function.

Looking through the development of ".FocusAsync()" of the input I could see that there is an internal JSRuntime that's part of ElementReference component.

With some magic I was able to get it via Runtime Properties

`
var js = (IJSRuntime)typeof(WebElementReferenceContext).GetRuntimeProperties().First(x=> x.Name == "JSRuntime").GetValue(context);
`

But it would be nice if I could create an extension like `_myInputReference.SelectInputAsync()`

Where the "SelectInputAsync()"

```
public static void SelectInputAsync(this ElementReference context) {

var js = context.JsRuntime;

js.InvokeVoidAsync(obj, 'MyFunction');

}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.