using Connected.Extensions; using Microsoft.JSInterop; namespace Connected { /// /// Inject with the AddMudBlazorScrollServices extension /// public interface IScrollManager { ValueTask ScrollToAsync(string id, int left, int top, ScrollBehavior scrollBehavior); ValueTask ScrollIntoViewAsync(string selector, ScrollBehavior behavior); //ValueTask ScrollToFragmentAsync(string id, ScrollBehavior behavior); ValueTask ScrollToTopAsync(string id, ScrollBehavior scrollBehavior = ScrollBehavior.Auto); ValueTask ScrollToYearAsync(string elementId); ValueTask ScrollToListItemAsync(string elementId); ValueTask LockScrollAsync(string selector = "body", string cssClass = "scroll-locked"); ValueTask UnlockScrollAsync(string selector = "body", string cssClass = "scroll-locked"); ValueTask ScrollToBottomAsync(string elementId, ScrollBehavior scrollBehavior = ScrollBehavior.Auto); } public class ScrollManager : IScrollManager { [Obsolete] public string Selector { get; set; } private readonly IJSRuntime _jSRuntime; public ScrollManager(IJSRuntime jSRuntime) { _jSRuntime = jSRuntime; } /// /// Scrolls to the coordinates of the element /// /// id of element /// x coordinate /// y coordinate /// smooth or auto /// public ValueTask ScrollToAsync(string id, int left, int top, ScrollBehavior behavior) => _jSRuntime.InvokeVoidAsync("scrollManager.scrollTo", id, left, top, behavior.ToDescriptionString()); /// /// Scrolls the first instance of the selector into view /// /// /// /// public ValueTask ScrollIntoViewAsync(string selector, ScrollBehavior behavior) => _jSRuntime.InvokeVoidAsync("scrollManager.scrollIntoView", selector, behavior.ToDescriptionString()); /// /// Scrolls to the top of the element /// /// id of element /// smooth or auto /// public ValueTask ScrollToTopAsync(string id, ScrollBehavior scrollBehavior = ScrollBehavior.Auto) => ScrollToAsync(id, 0, 0, scrollBehavior); public async Task ScrollToTop(ScrollBehavior scrollBehavior = ScrollBehavior.Auto) { #pragma warning disable CS0612 // Type or member is obsolete await ScrollToAsync(Selector, 0, 0, scrollBehavior); #pragma warning restore CS0612 // Type or member is obsolete } /// /// Scroll to the bottom of the element (or if not found to the bottom of the page) /// /// id of element of null to scroll to page bottom /// smooth or auto /// public ValueTask ScrollToBottomAsync(string id, ScrollBehavior behavior) => _jSRuntime.InvokeVoidAsync("scrollManager.scrollToBottom", id, behavior.ToDescriptionString()); public ValueTask ScrollToYearAsync(string elementId) => _jSRuntime.InvokeVoidAsync("scrollManager.scrollToYear", elementId); public ValueTask ScrollToListItemAsync(string elementId) => _jSRuntime.InvokeVoidAsync("scrollManager.scrollToListItem", elementId); public ValueTask LockScrollAsync(string selector = "body", string cssClass = "scroll-locked") => _jSRuntime.InvokeVoidAsync("scrollManager.lockScroll", selector, cssClass); public ValueTask UnlockScrollAsync(string selector = "body", string cssClass = "scroll-locked") => _jSRuntime.InvokeVoidAsyncIgnoreErrors("scrollManager.unlockScroll", selector, cssClass); } /// /// Smooth: scrolls in a smooth fashion; /// Auto: is immediate /// public enum ScrollBehavior { Smooth, Auto } }