Skip to content

Commit

Permalink
feat(Tooltip): make the Top as the default position to show tooltip (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Dec 27, 2024
1 parent 6298251 commit cbbae9a
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/Masa.Blazor/Components/Tooltip/MTooltip.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ public MTooltip()
private static Block _block = new("m-tooltip");
private ModifierBuilder _modifierBuilder = _block.CreateModifierBuilder();
private ModifierBuilder _contentModifierBuilder = _block.Element("content").CreateModifierBuilder();

private Position ComputedPosition
{
get
{
if (Right) return Position.Right;
if (Bottom) return Position.Bottom;
if (Left) return Position.Left;
return Position.Top;
}
}

protected override IEnumerable<string> BuildComponentClass()
{
yield return _modifierBuilder
.Add(Top, Right, Bottom, Left)
.Add(ComputedPosition.ToString())
.Add("attached", Attach is not { AsT1: true })
.Build();
}
Expand All @@ -52,18 +63,18 @@ protected double CalculatedLeft
var activator = Dimensions.Activator;
var content = Dimensions.Content;
if (activator == null || content == null) return 0;

var unknown = !Bottom && !Left && !Top && !Right;

var activatorLeft = !IsDefaultAttach ? activator.OffsetLeft : activator.Left;
double left = 0;

if (Top || Bottom || unknown)
if (ComputedPosition is Position.Top or Position.Bottom)
{
left = activatorLeft + (activator.Width / 2) - (content.Width / 2);
}
else if (Left || Right)
else if (ComputedPosition is Position.Left or Position.Right)
{
left = activatorLeft + (Right ? activator.Width : -content.Width) + (Right ? 10 : -10);
var right = ComputedPosition == Position.Right;
left = activatorLeft + (right ? activator.Width : -content.Width) + (right ? 10 : -10);
}

if (NudgeLeft != null)
Expand Down Expand Up @@ -93,11 +104,12 @@ protected double CalculatedTop
var activatorTop = !IsDefaultAttach ? activator.OffsetTop : activator.Top;
double top = 0;

if (Top || Bottom)
if (ComputedPosition is Position.Top or Position.Bottom)
{
top = activatorTop + (Bottom ? activator.Height : -content.Height) + (Bottom ? 10 : -10);
var bottom = ComputedPosition == Position.Bottom;
top = activatorTop + (bottom ? activator.Height : -content.Height) + (bottom ? 10 : -10);
}
else if (Left || Right)
else if (ComputedPosition is Position.Left or Position.Right)
{
top = activatorTop + (activator.Height / 2) - (content.Height / 2);
}
Expand All @@ -122,5 +134,13 @@ protected double CalculatedTop
return CalcYOverflow(top);
}
}

private enum Position
{
Top,
Right,
Bottom,
Left
}
}
}

0 comments on commit cbbae9a

Please sign in to comment.