.Net 5.0 Blazor Goodness

 


With the release of .Net 5.0 quickly approaching it is time to take a look and see what kind of goodies are include for Blazor.

.Net 5.0 is aimed at unifying the .Net platform by commuting all the different aspects of the platform into a single release set.  But they have including a lot of upgrades and new features for Blazor.  To see these new features you have to be using at least Visual Studio 2019 16.8 Preview 3 and have .Net 5.0 install on your machine.

Here are the new features that I have found exciting.  See the linked source code to see them in action.

  1. CSS isolation
  2. Input Radio Component
  3. UI Focus Support
  4. File Input Component
  5. On-toggle Event Support 


CSS Isolation


With this feature, you can now include separate style sheets that are scoped just for your components.  This will allow you to provide the styling you want for your component libraries or just your individual component.

This is implemented by convention.  You provide a CSS file named the same as your component razor file.

You put all your styles in this file and they only have the scope of your component.  You can control the styling of child components by using the ::deep CSS selector

example: 
MyComponet.razor would have a MyComponet.razor.CSS style file for it.  In the solution explorer, it even places the style sheet under the component file.


Makes things clean and simple.


Input Radio Component

They have added an InputRadio control and the InputRadio Group control.  These work like the InputText control.  You can data bind the control with the @bind-value and they support integrated validation.


    <InputRadioGroup @bind-Value="survey.OpinionAboutBlazor">
        @foreach (var opinion in opinions)
            {
            <div class="form-check">
                <InputRadio class="form-check-input" id="@opinion.id" Value="@opinion.id" />
                <label class="form-check-label" for="@opinion.id">@opinion.label</label>
            </div>
            }
    </InputRadioGroup>

This new control fits in with the other input controls on the EditForm component and adds consistency to the form.

 UI Focus Support


This feature might not sound like much but after having to use JSInterOp to just set the first control on a page to have focus, this is great to see. 

To use this, you have to set an element reference to the control you want to set focus on and then after render call a method to set the focus:

          ElementReference textText;  

          public async Task Focus(ElementReference textInput)
         {
              await textInput.FocusAsync();
          }

I was not able to get it to set focus on an InputText control, for that I would still have to use the JSInterOp.  Hopefully, they will resolve that use case as well.


File Input Component


I am glad to see this control added, but it was one of the first control I made and drew a lot of attention on this blog.  But it is always nice to have a good file input control built-in. 

It can handle single or multiple files uploaded.  It works with Server-Side or Web Assembly Clients.  

The use of it is what you would have expected:

      <InputFile OnChange="OnInputFileChange" multiple />

      async Task OnInputFileChange(InputFileChangeEventArgs e)
      {
         var imageFiles = e.GetMultipleFiles();
      }

This is a nice addition to the built-in controls.

On-toggle Event Support


The On toggle event will allow you to better use the HTML <details></details> tags.  To use it, you add a flag and toggle the flag on the details click:

        <details id="details-toggle" @ontoggle="OnToggleRun">
            <summary>Summary</summary>
            <p>Detailed content</p>
       </details>

        @if (detailsExpanded)
        {
            <p>Read the details carefully!</p>
            <img src="/blazorCode.png" width="300" height="300" />
        }

        @code {

               bool detailsExpanded;
               string message { get; set; }

               void OnToggleRun()
              {
                   detailsExpanded = !detailsExpanded;
              }
        }

This may seem simple, but it is a nice UI element to use.

Other Enhancements


There are a lot of good enhances across the board in .Net 5.0.  Please check out the ASP.Net Blog for all the details.  

Here is a quick summary:

  • Lots of Web Assembly Performance enhancements
  • Inputs support aria-invalid by default
  • Influencing HTML head attributes
  • Protected Local Storage
  • Component Virtualization
  • Web Assembly Pre-rendering
  • Browser Compatibility for Web Assembly
  • JavaScript isolation and object reference
  • Custom validation class attributes



Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component