1+ // ------------------------------------------------------------------------
2+ // MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
3+ // ------------------------------------------------------------------------
4+
15using System . Diagnostics . CodeAnalysis ;
26using System . Globalization ;
37
48using Microsoft . AspNetCore . Components ;
59using Microsoft . FluentUI . AspNetCore . Components . Extensions ;
610using Microsoft . FluentUI . AspNetCore . Components . Utilities ;
11+ using Microsoft . JSInterop ;
712
813namespace Microsoft . FluentUI . AspNetCore . Components ;
914
10- public partial class FluentSlider < TValue > : FluentInputBase < TValue >
15+ public partial class FluentSlider < TValue > : FluentInputBase < TValue > , IAsyncDisposable
1116 where TValue : System . Numerics . INumber < TValue >
1217{
18+ private const string JAVASCRIPT_FILE = "./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Slider/FluentSlider.razor.js" ;
19+
20+ /// <summary />
21+ [ Inject ]
22+ private IJSRuntime JSRuntime { get ; set ; } = default ! ;
23+
24+ /// <summary />
25+ private IJSObjectReference ? _jsModule { get ; set ; }
26+
1327 /// <summary>
1428 /// Gets or sets the slider's minimal value.
1529 /// </summary>
@@ -29,7 +43,7 @@ public partial class FluentSlider<TValue> : FluentInputBase<TValue>
2943 public TValue ? Step { get ; set ; }
3044
3145 /// <summary>
32- /// Gets or sets the orentation of the slider. See <see cref="AspNetCore.Components.Orientation"/>
46+ /// Gets or sets the orientation of the slider. See <see cref="AspNetCore.Components.Orientation"/>
3347 /// </summary>
3448 [ Parameter ]
3549 public Orientation ? Orientation { get ; set ; }
@@ -46,6 +60,21 @@ public partial class FluentSlider<TValue> : FluentInputBase<TValue>
4660 [ Parameter ]
4761 public RenderFragment ? ChildContent { get ; set ; }
4862
63+ protected override async Task OnAfterRenderAsync ( bool firstRender )
64+ {
65+ if ( firstRender )
66+ {
67+ _jsModule ??= await JSRuntime . InvokeAsync < IJSObjectReference > ( "import" , JAVASCRIPT_FILE ) ;
68+ }
69+ else
70+ {
71+ if ( _jsModule is not null )
72+ {
73+ await _jsModule ! . InvokeVoidAsync ( "updateSlider" , Element ) ;
74+ }
75+ }
76+ }
77+
4978 protected override string ? ClassValue
5079 {
5180 get
@@ -120,4 +149,21 @@ private static string GetStepAttributeValue()
120149 throw new InvalidOperationException ( $ "The type '{ targetType } ' is not a supported numeric type.") ;
121150 }
122151 }
152+
153+ public async ValueTask DisposeAsync ( )
154+ {
155+ try
156+ {
157+ if ( _jsModule is not null )
158+ {
159+ await _jsModule . DisposeAsync ( ) ;
160+ }
161+ }
162+ catch ( Exception ex ) when ( ex is JSDisconnectedException ||
163+ ex is OperationCanceledException )
164+ {
165+ // The JSRuntime side may routinely be gone already if the reason we're disposing is that
166+ // the client disconnected. This is not an error.
167+ }
168+ }
123169}
0 commit comments