Skip to content

Commit

Permalink
🐛 fix(NavigationDrawer): MobileBreakpoint parameter is not functionin…
Browse files Browse the repository at this point in the history
…g as expected (#2071)
  • Loading branch information
capdiem authored Aug 1, 2024
1 parent f8f18fd commit 0b0cb8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
40 changes: 18 additions & 22 deletions src/Masa.Blazor/Components/DataTable/MDataTable.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public DataTableResizeMode ResizeMode
set => SetValue(value);
}

private bool _prevIsMobile;

public IEnumerable<DataTableHeader<TItem>> ComputedHeaders
{
get
Expand Down Expand Up @@ -193,31 +195,19 @@ public IEnumerable<DataTableHeader<TItem>> ComputedHeaders

private bool HasEllipsis => Headers.Any(u => u.HasEllipsis);

private bool IsMobile { get; set; }

private void CalculateIsMobile()
private bool IsMobile
{
var (width, mobile, name, mobileBreakpoint) = MasaBlazor.Breakpoint;

if (width == 0)
get
{
IsMobile = false;
return;
}
var (width, mobile, name, mobileBreakpoint) = MasaBlazor.Breakpoint;

if (mobileBreakpoint.Equals(MobileBreakpoint))
{
IsMobile = mobile;
return;
}
if (Equals(mobileBreakpoint.Value, MobileBreakpoint.Value))
{
return mobile;
}

if (MobileBreakpoint.IsT1)
{
IsMobile = width < MobileBreakpoint.AsT1;
return;
return MobileBreakpoint.IsT1 ? width < MobileBreakpoint.AsT1 : name <= MobileBreakpoint.AsT0;
}

IsMobile = name == MobileBreakpoint.AsT0;
}

public Dictionary<string, object?> ColspanAttrs => new()
Expand Down Expand Up @@ -247,7 +237,7 @@ protected override void OnInitialized()
CustomFilter = CustomFilterWithColumns;
ItemValues = Headers.Select(header => new ItemValue<TItem>(header.Value));

CalculateIsMobile();
_prevIsMobile = IsMobile;
MasaBlazor.WindowSizeChanged += MasaBlazorWindowSizeChanged;
MasaBlazor.RTLChanged += MasaBlazorOnRTLChanged;
}
Expand Down Expand Up @@ -303,7 +293,13 @@ private string GetText(string value)

private void MasaBlazorWindowSizeChanged(object? sender, WindowSizeChangedEventArgs e)
{
CalculateIsMobile();
if (_prevIsMobile == IsMobile)
{
return;
}

_prevIsMobile = IsMobile;

InvokeAsync(StateHasChanged);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public StringNumber? Height
[Parameter]
public OneOf<Breakpoints, double>? MobileBreakpoint
{
get => GetValue(MasaBlazor.Breakpoint.MobileBreakpoint);
get => GetValue<OneOf<Breakpoints, double>?>();
set => SetValue(value);
}

Expand Down Expand Up @@ -150,6 +150,7 @@ public bool IsDark
private ModifierBuilder _modifierBuilder = _block.CreateModifierBuilder();

private bool _prevPermanent;
private bool _prevIsMobile;
private readonly List<IDependent> _dependents = new();

protected object? Overlay { get; set; }
Expand All @@ -170,7 +171,7 @@ protected bool IsActive
set => SetValue(value);
}

protected bool IsMobile => !Stateless && !Permanent && IsMobileBreakpoint; //TODO: fix mobile
protected bool IsMobile => !Stateless && !Permanent && IsMobileBreakpoint;

protected bool ReactsToClick => !Stateless && !Permanent && (IsMobile || Temporary);

Expand Down Expand Up @@ -299,17 +300,14 @@ protected bool IsMobileBreakpoint
{
get
{
var mobile = MasaBlazor.Breakpoint.Mobile;
var width = MasaBlazor.Breakpoint.Width;
var name = MasaBlazor.Breakpoint.Name;
var mobileBreakpoint = MasaBlazor.Breakpoint.MobileBreakpoint;
var (width, mobile, name, mobileBreakpoint) = MasaBlazor.Breakpoint;

if (Equals(mobileBreakpoint.Value, MobileBreakpoint?.Value))
if (MobileBreakpoint is null || Equals(mobileBreakpoint.Value, MobileBreakpoint.Value))
{
return mobile;
}

return mobileBreakpoint.IsT1 ? width < mobileBreakpoint.AsT1 : name == mobileBreakpoint.AsT0;
return MobileBreakpoint.Value.IsT1 ? width < MobileBreakpoint.Value.AsT1 : name <= MobileBreakpoint.Value.AsT0;
}
}

Expand All @@ -319,8 +317,6 @@ protected bool IsMobileBreakpoint

protected bool ReactsToRoute => !DisableRouteWatcher && !Stateless && (Temporary || IsMobile);

protected bool IsFullscreen => MasaBlazor != null && MasaBlazor.Breakpoint.SmAndDown;

public IEnumerable<string> DependentSelectors
{
get
Expand All @@ -342,7 +338,7 @@ protected override void OnInitialized()

_prevPermanent = Permanent;

MasaBlazor.BreakpointChanged += OnBreakpointOnUpdate;
MasaBlazor.WindowSizeChanged += MasaBlazorWindowSizeChanged;

if (Value == null && ValueChanged.HasDelegate)
{
Expand Down Expand Up @@ -444,13 +440,15 @@ private async void OnLocationChanged(object? sender,
}
}

private async void OnBreakpointOnUpdate(object? sender, BreakpointChangedEventArgs e)
private async void MasaBlazorWindowSizeChanged(object? sender, WindowSizeChangedEventArgs e)
{
if (!e.MobileChanged)
if (_prevIsMobile == IsMobile)
{
return;
}

_prevIsMobile = IsMobile;

if (!ReactsToResize || !ReactsToMobile)
{
return;
Expand Down Expand Up @@ -510,6 +508,7 @@ private void Init()
}
else if (!Temporary)
{
_prevIsMobile = IsMobile;
IsActive = !IsMobile;
}
}
Expand Down Expand Up @@ -592,7 +591,7 @@ public async Task HandleOnClickAsync(MouseEventArgs e)
protected override async ValueTask DisposeAsyncCore()
{
RemoveApplication();
MasaBlazor!.BreakpointChanged -= OnBreakpointOnUpdate;
MasaBlazor!.WindowSizeChanged -= MasaBlazorWindowSizeChanged;
MasaBlazor.Application.PropertyChanged -= ApplicationPropertyChanged;
NavigationManager!.LocationChanged -= OnLocationChanged;
await OutsideClickJsModule.UnbindAndDisposeAsync();
Expand Down

0 comments on commit 0b0cb8b

Please sign in to comment.