@rendermode InteractiveServer @implements IDisposable
@code { [Parameter] public bool InlineCollapsed { get; set; } private string[] selectedKeys = ["/"]; [Inject] private NavigationManager NavigationManager { get; set; } = default!; protected override void OnParametersSet() { UpdateSelectedKeys(NavigationManager.Uri); } protected override void OnInitialized() { UpdateSelectedKeys(NavigationManager.Uri); NavigationManager.LocationChanged += HandleLocationChanged; } private void HandleLocationChanged(object? sender, LocationChangedEventArgs e) { UpdateSelectedKeys(e.Location); StateHasChanged(); } private void UpdateSelectedKeys(string uri) { var relative = NavigationManager.ToBaseRelativePath(uri); if (string.IsNullOrEmpty(relative)) { selectedKeys = ["/"]; } else { var key = relative.Split('/', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? string.Empty; selectedKeys = [key]; } } private void HandleMenuClick(MenuItem menuItem) { if (menuItem.Key is null) { return; } selectedKeys = [menuItem.Key]; var target = menuItem.Key == "/" ? "/" : $"/{menuItem.Key}"; NavigationManager.NavigateTo(target); } public void Dispose() { NavigationManager.LocationChanged -= HandleLocationChanged; } }